Skip to content

Commit 0c05bb5

Browse files
authored
Rollup merge of #149630 - wafarm:fix-149604, r=JonathanBrouwer
Check identifiers defined in macros when suggesting identifiers hidden by hygiene Fix #149604 r? `@JonathanBrouwer` (Since you reviewed the other one related to this)
2 parents c42ce16 + e513ce3 commit 0c05bb5

File tree

6 files changed

+88
-0
lines changed

6 files changed

+88
-0
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,6 +1155,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
11551155
let callsite_span = span.source_callsite();
11561156
for rib in self.ribs[ValueNS].iter().rev() {
11571157
for (binding_ident, _) in &rib.bindings {
1158+
// Case 1: the identifier is defined in the same scope as the macro is called
11581159
if binding_ident.name == ident.name
11591160
&& !binding_ident.span.eq_ctxt(span)
11601161
&& !binding_ident.span.from_expansion()
@@ -1166,6 +1167,19 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
11661167
);
11671168
return;
11681169
}
1170+
1171+
// Case 2: the identifier is defined in a macro call in the same scope
1172+
if binding_ident.name == ident.name
1173+
&& binding_ident.span.from_expansion()
1174+
&& binding_ident.span.source_callsite().eq_ctxt(callsite_span)
1175+
&& binding_ident.span.source_callsite().lo() < callsite_span.lo()
1176+
{
1177+
err.span_help(
1178+
binding_ident.span,
1179+
"an identifier with the same name is defined here, but is not accessible due to macro hygiene",
1180+
);
1181+
return;
1182+
}
11691183
}
11701184
}
11711185
}

tests/ui/hygiene/pattern-macro.stderr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ error[E0425]: cannot find value `x` in this scope
33
|
44
LL | x + 1;
55
| ^ not found in this scope
6+
|
7+
help: an identifier with the same name is defined here, but is not accessible due to macro hygiene
8+
--> $DIR/pattern-macro.rs:1:28
9+
|
10+
LL | macro_rules! foo { () => ( x ) }
11+
| ^
12+
...
13+
LL | let foo!() = 2;
14+
| ------ in this macro invocation
15+
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
616

717
error: aborting due to 1 previous error
818

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
macro_rules! let_it { {} => { let it = (); } }
2+
macro_rules! print_it { {} => { println!("{:?}", it); } }
3+
//~^ ERROR cannot find value `it` in this scope
4+
5+
fn main() {
6+
let_it!();
7+
let () = it; //~ ERROR cannot find value `it` in this scope
8+
print_it!();
9+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
error[E0425]: cannot find value `it` in this scope
2+
--> $DIR/macro-hygiene-help-issue-149604.rs:7:14
3+
|
4+
LL | let () = it;
5+
| ^^ not found in this scope
6+
|
7+
help: an identifier with the same name is defined here, but is not accessible due to macro hygiene
8+
--> $DIR/macro-hygiene-help-issue-149604.rs:1:35
9+
|
10+
LL | macro_rules! let_it { {} => { let it = (); } }
11+
| ^^
12+
...
13+
LL | let_it!();
14+
| --------- in this macro invocation
15+
= note: this error originates in the macro `let_it` (in Nightly builds, run with -Z macro-backtrace for more info)
16+
17+
error[E0425]: cannot find value `it` in this scope
18+
--> $DIR/macro-hygiene-help-issue-149604.rs:2:50
19+
|
20+
LL | macro_rules! print_it { {} => { println!("{:?}", it); } }
21+
| ^^ not found in this scope
22+
...
23+
LL | print_it!();
24+
| ----------- in this macro invocation
25+
|
26+
help: an identifier with the same name is defined here, but is not accessible due to macro hygiene
27+
--> $DIR/macro-hygiene-help-issue-149604.rs:1:35
28+
|
29+
LL | macro_rules! let_it { {} => { let it = (); } }
30+
| ^^
31+
...
32+
LL | let_it!();
33+
| --------- in this macro invocation
34+
= note: this error originates in the macro `print_it` which comes from the expansion of the macro `let_it` (in Nightly builds, run with -Z macro-backtrace for more info)
35+
36+
error: aborting due to 2 previous errors
37+
38+
For more information about this error, try `rustc --explain E0425`.

tests/ui/proc-macro/gen-macro-rules-hygiene.stderr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ error[E0425]: cannot find value `local_def` in this scope
3030
|
3131
LL | local_def;
3232
| ^^^^^^^^^ help: a local variable with a similar name exists: `local_use`
33+
|
34+
help: an identifier with the same name is defined here, but is not accessible due to macro hygiene
35+
--> $DIR/gen-macro-rules-hygiene.rs:13:1
36+
|
37+
LL | gen_macro_rules!();
38+
| ^^^^^^^^^^^^^^^^^^
39+
...
40+
LL | generated!();
41+
| ------------ in this macro invocation
42+
= note: this error originates in the macro `generated` (in Nightly builds, run with -Z macro-backtrace for more info)
3343

3444
error: aborting due to 3 previous errors
3545

tests/ui/proc-macro/mixed-site-span.stderr

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,13 @@ error[E0425]: cannot find value `local_def` in this scope
606606
|
607607
LL | local_def;
608608
| ^^^^^^^^^ help: a local variable with a similar name exists: `local_use`
609+
|
610+
help: an identifier with the same name is defined here, but is not accessible due to macro hygiene
611+
--> $DIR/mixed-site-span.rs:23:9
612+
|
613+
LL | proc_macro_rules!();
614+
| ^^^^^^^^^^^^^^^^^^^
615+
= note: this error originates in the macro `proc_macro_rules` (in Nightly builds, run with -Z macro-backtrace for more info)
609616

610617
error: aborting due to 52 previous errors
611618

0 commit comments

Comments
 (0)