Add note for identifier with attempted hygiene violation

This commit is contained in:
yukang
2025-11-07 08:27:23 +08:00
parent c90bcb9571
commit 12cde3091a
8 changed files with 103 additions and 0 deletions
@@ -1125,6 +1125,8 @@ fn err_code_special_cases(
}
}
}
self.suggest_ident_hidden_by_hygiene(err, path, span);
} else if err_code == E0412 {
if let Some(correct) = Self::likely_rust_type(path) {
err.span_suggestion(
@@ -1138,6 +1140,28 @@ fn err_code_special_cases(
}
}
fn suggest_ident_hidden_by_hygiene(&self, err: &mut Diag<'_>, path: &[Segment], span: Span) {
let [segment] = path else { return };
let ident = segment.ident;
let callsite_span = span.source_callsite();
for rib in self.ribs[ValueNS].iter().rev() {
for (binding_ident, _) in &rib.bindings {
if binding_ident.name == ident.name
&& !binding_ident.span.eq_ctxt(span)
&& !binding_ident.span.from_expansion()
&& binding_ident.span.lo() < callsite_span.lo()
{
err.span_help(
binding_ident.span,
"an identifier with the same name exists, but is not accessible due to macro hygiene",
);
return;
}
}
}
}
/// Emit special messages for unresolved `Self` and `self`.
fn suggest_self_ty(
&self,
@@ -0,0 +1,15 @@
macro_rules! print_it { {} => { println!("{:?}", it); } }
//~^ ERROR cannot find value `it` in this scope
fn main() {
{
let it = "hello";
}
{
let it = "world";
{
let it = ();
print_it!();
}
}
}
@@ -0,0 +1,19 @@
error[E0425]: cannot find value `it` in this scope
--> $DIR/macro-hygiene-help-issue-148580.rs:1: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 exists, but is not accessible due to macro hygiene
--> $DIR/macro-hygiene-help-issue-148580.rs:11:17
|
LL | let it = ();
| ^^
= note: this error originates in the macro `print_it` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0425`.
@@ -7,6 +7,11 @@ LL | macro_rules! f { () => (n) }
LL | println!("{}", f!());
| ---- in this macro invocation
|
help: an identifier with the same name exists, but is not accessible due to macro hygiene
--> $DIR/macro-hygiene-scope-15167.rs:12:9
|
LL | for n in 0..1 {
| ^
= note: this error originates in the macro `f` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0425]: cannot find value `n` in this scope
@@ -18,6 +23,11 @@ LL | macro_rules! f { () => (n) }
LL | println!("{}", f!());
| ---- in this macro invocation
|
help: an identifier with the same name exists, but is not accessible due to macro hygiene
--> $DIR/macro-hygiene-scope-15167.rs:16:17
|
LL | if let Some(n) = None {
| ^
= note: this error originates in the macro `f` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0425]: cannot find value `n` in this scope
@@ -29,6 +39,11 @@ LL | macro_rules! f { () => (n) }
LL | println!("{}", f!());
| ---- in this macro invocation
|
help: an identifier with the same name exists, but is not accessible due to macro hygiene
--> $DIR/macro-hygiene-scope-15167.rs:21:24
|
LL | } else if let Some(n) = None {
| ^
= note: this error originates in the macro `f` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0425]: cannot find value `n` in this scope
@@ -40,6 +55,11 @@ LL | macro_rules! f { () => (n) }
LL | println!("{}", f!());
| ---- in this macro invocation
|
help: an identifier with the same name exists, but is not accessible due to macro hygiene
--> $DIR/macro-hygiene-scope-15167.rs:25:20
|
LL | while let Some(n) = None {
| ^
= note: this error originates in the macro `f` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 4 previous errors
@@ -7,6 +7,11 @@ LL | ${concat($lhs, $rhs)}
LL | let _another = join!(abc, def);
| --------------- in this macro invocation
|
help: an identifier with the same name exists, but is not accessible due to macro hygiene
--> $DIR/concat-hygiene.rs:11:9
|
LL | let abcdef = 1;
| ^^^^^^
= note: this error originates in the macro `join` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 1 previous error
@@ -18,6 +18,11 @@ LL | gen_macro_rules!();
LL | generated!();
| ------------ in this macro invocation
|
help: an identifier with the same name exists, but is not accessible due to macro hygiene
--> $DIR/gen-macro-rules-hygiene.rs:19:13
|
LL | let local_use = 1;
| ^^^^^^^^^
= note: this error originates in the macro `generated` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0425]: cannot find value `local_def` in this scope
@@ -594,6 +594,11 @@ error[E0425]: cannot find value `local_use` in this scope
LL | proc_macro_rules!();
| ^^^^^^^^^^^^^^^^^^^ help: a local variable with a similar name exists: `local_def`
|
help: an identifier with the same name exists, but is not accessible due to macro hygiene
--> $DIR/mixed-site-span.rs:21:13
|
LL | let local_use = 1;
| ^^^^^^^^^
= note: this error originates in the macro `proc_macro_rules` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0425]: cannot find value `local_def` in this scope
+10
View File
@@ -7,6 +7,11 @@ LL | Value = (stringify!($tokens + hidden_ident), 1).1
LL | other!(50);
| ---------- in this macro invocation
|
help: an identifier with the same name exists, but is not accessible due to macro hygiene
--> $DIR/weird-hygiene.rs:44:9
|
LL | let hidden_ident = "Hello1";
| ^^^^^^^^^^^^
= note: this error originates in the macro `inner` which comes from the expansion of the macro `other` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0425]: cannot find value `hidden_ident` in this scope
@@ -18,6 +23,11 @@ LL | hidden_ident
LL | invoke_it!(25);
| -------------- in this macro invocation
|
help: an identifier with the same name exists, but is not accessible due to macro hygiene
--> $DIR/weird-hygiene.rs:44:9
|
LL | let hidden_ident = "Hello1";
| ^^^^^^^^^^^^
= note: this error originates in the macro `invoke_it` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 2 previous errors