Skip to content

Commit 34d9ffe

Browse files
authored
Rollup merge of #48934 - Phlosioneer:42453-debug-hygene, r=petrochenkov
Fix hygene issue when deriving Debug The code for several of the core traits doesn't use hygenic macros. This isn't a problem, except for the Debug trait, which is the only one that uses a variable, named "builder". Variables can't share names with unit structs, so attempting to [derive(Debug)] on any type while a unit struct with the name "builder" was in scope would result in an error. This commit just changes the name of the variable to "__debug_trait_builder", because I couldn't figure out how to get a list of all unit structs in-scope from within the derive expansion function. If someone wants to have a unit struct with the exact name "__debug_trait_builder", they'll just have to do it without a [derive(Debug)]. I also checked the implementations of the other built-in derives to ensure they didn't declare any variables.
2 parents 14574db + c033c6e commit 34d9ffe

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/libsyntax_ext/deriving/debug.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn show_substructure(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<E
7070
// We want to make sure we have the ctxt set so that we can use unstable methods
7171
let span = span.with_ctxt(cx.backtrace());
7272
let name = cx.expr_lit(span, ast::LitKind::Str(ident.name, ast::StrStyle::Cooked));
73-
let builder = Ident::from_str("builder");
73+
let builder = Ident::from_str("__debug_trait_builder");
7474
let builder_expr = cx.expr_ident(span, builder.clone());
7575

7676
let fmt = substr.nonself_args[0].clone();

src/test/run-pass/issue-42453.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[derive(Debug)]
12+
struct builder;
13+
14+
fn main() {
15+
16+
}
17+

0 commit comments

Comments
 (0)