Skip to content
Open
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
24 changes: 24 additions & 0 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,8 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
}
}
}

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(
Expand All @@ -1138,6 +1140,28 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
}
}

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,
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/macros/macro-hygiene-help-issue-148580.rs
Original file line number Diff line number Diff line change
@@ -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!();
}
}
}
19 changes: 19 additions & 0 deletions tests/ui/macros/macro-hygiene-help-issue-148580.stderr
Original file line number Diff line number Diff line change
@@ -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`.
20 changes: 20 additions & 0 deletions tests/ui/macros/macro-hygiene-scope-15167.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/macros/metavar-expressions/concat-hygiene.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/proc-macro/gen-macro-rules-hygiene.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/proc-macro/mixed-site-span.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/proc-macro/weird-hygiene.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading