Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
let callsite_span = span.source_callsite();
for rib in self.ribs[ValueNS].iter().rev() {
for (binding_ident, _) in &rib.bindings {
// Case 1: the identifier is defined in the same scope as the macro is called
if binding_ident.name == ident.name
&& !binding_ident.span.eq_ctxt(span)
&& !binding_ident.span.from_expansion()
Expand All @@ -1166,6 +1167,19 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
);
return;
}

// Case 2: the identifier is defined in a macro call in the same scope
if binding_ident.name == ident.name
&& binding_ident.span.from_expansion()
&& binding_ident.span.source_callsite().eq_ctxt(callsite_span)
&& binding_ident.span.source_callsite().lo() < callsite_span.lo()
{
err.span_help(
binding_ident.span,
"an identifier with the same name is defined here, but is not accessible due to macro hygiene",
);
return;
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/hygiene/pattern-macro.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ error[E0425]: cannot find value `x` in this scope
|
LL | x + 1;
| ^ not found in this scope
|
help: an identifier with the same name is defined here, but is not accessible due to macro hygiene
--> $DIR/pattern-macro.rs:1:28
|
LL | macro_rules! foo { () => ( x ) }
| ^
...
LL | let foo!() = 2;
| ------ in this macro invocation
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 1 previous error

Expand Down
9 changes: 9 additions & 0 deletions tests/ui/macros/macro-hygiene-help-issue-149604.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
macro_rules! let_it { {} => { let it = (); } }
macro_rules! print_it { {} => { println!("{:?}", it); } }
//~^ ERROR cannot find value `it` in this scope

fn main() {
let_it!();
let () = it; //~ ERROR cannot find value `it` in this scope
print_it!();
}
38 changes: 38 additions & 0 deletions tests/ui/macros/macro-hygiene-help-issue-149604.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
error[E0425]: cannot find value `it` in this scope
--> $DIR/macro-hygiene-help-issue-149604.rs:7:14
|
LL | let () = it;
| ^^ not found in this scope
|
help: an identifier with the same name is defined here, but is not accessible due to macro hygiene
--> $DIR/macro-hygiene-help-issue-149604.rs:1:35
|
LL | macro_rules! let_it { {} => { let it = (); } }
| ^^
...
LL | let_it!();
| --------- in this macro invocation
= note: this error originates in the macro `let_it` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0425]: cannot find value `it` in this scope
--> $DIR/macro-hygiene-help-issue-149604.rs:2:50
|
LL | macro_rules! print_it { {} => { println!("{:?}", it); } }
| ^^ not found in this scope
...
LL | print_it!();
| ----------- in this macro invocation
|
help: an identifier with the same name is defined here, but is not accessible due to macro hygiene
--> $DIR/macro-hygiene-help-issue-149604.rs:1:35
|
LL | macro_rules! let_it { {} => { let it = (); } }
| ^^
...
LL | let_it!();
| --------- in this macro invocation
= 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)

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0425`.
10 changes: 10 additions & 0 deletions tests/ui/proc-macro/gen-macro-rules-hygiene.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ error[E0425]: cannot find value `local_def` in this scope
|
LL | local_def;
| ^^^^^^^^^ help: a local variable with a similar name exists: `local_use`
|
help: an identifier with the same name is defined here, but is not accessible due to macro hygiene
--> $DIR/gen-macro-rules-hygiene.rs:13:1
|
LL | gen_macro_rules!();
| ^^^^^^^^^^^^^^^^^^
...
LL | generated!();
| ------------ in this macro invocation
= note: this error originates in the macro `generated` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 3 previous errors

Expand Down
7 changes: 7 additions & 0 deletions tests/ui/proc-macro/mixed-site-span.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,13 @@ error[E0425]: cannot find value `local_def` in this scope
|
LL | local_def;
| ^^^^^^^^^ help: a local variable with a similar name exists: `local_use`
|
help: an identifier with the same name is defined here, but is not accessible due to macro hygiene
--> $DIR/mixed-site-span.rs:23:9
|
LL | proc_macro_rules!();
| ^^^^^^^^^^^^^^^^^^^
= note: this error originates in the macro `proc_macro_rules` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 52 previous errors

Expand Down
Loading