Check identifiers defined in macros when suggesting identifiers hidden by hygiene

This commit is contained in:
Wafarm 2025-12-04 14:58:30 +08:00
parent 5325015e29
commit e513ce3fb4
No known key found for this signature in database
GPG key ID: AF2AA361024C8368
6 changed files with 88 additions and 0 deletions

View file

@ -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()
@ -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;
}
}
}
}

View file

@ -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

View file

@ -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!();
}

View file

@ -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`.

View file

@ -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

View file

@ -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