Skip to content

Commit 99af1bc

Browse files
authored
Rollup merge of rust-lang#148612 - chenyukang:yukang-fix-148580-macro-hygiene-diagnostic, r=JonathanBrouwer
Add note for identifier with attempted hygiene violation Fixes rust-lang#148580 I changed the original test to make sure we are pointing to the right scope.
2 parents b0c4434 + 12cde30 commit 99af1bc

File tree

8 files changed

+103
-0
lines changed

8 files changed

+103
-0
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,8 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
11251125
}
11261126
}
11271127
}
1128+
1129+
self.suggest_ident_hidden_by_hygiene(err, path, span);
11281130
} else if err_code == E0412 {
11291131
if let Some(correct) = Self::likely_rust_type(path) {
11301132
err.span_suggestion(
@@ -1138,6 +1140,28 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
11381140
}
11391141
}
11401142

1143+
fn suggest_ident_hidden_by_hygiene(&self, err: &mut Diag<'_>, path: &[Segment], span: Span) {
1144+
let [segment] = path else { return };
1145+
1146+
let ident = segment.ident;
1147+
let callsite_span = span.source_callsite();
1148+
for rib in self.ribs[ValueNS].iter().rev() {
1149+
for (binding_ident, _) in &rib.bindings {
1150+
if binding_ident.name == ident.name
1151+
&& !binding_ident.span.eq_ctxt(span)
1152+
&& !binding_ident.span.from_expansion()
1153+
&& binding_ident.span.lo() < callsite_span.lo()
1154+
{
1155+
err.span_help(
1156+
binding_ident.span,
1157+
"an identifier with the same name exists, but is not accessible due to macro hygiene",
1158+
);
1159+
return;
1160+
}
1161+
}
1162+
}
1163+
}
1164+
11411165
/// Emit special messages for unresolved `Self` and `self`.
11421166
fn suggest_self_ty(
11431167
&self,
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
macro_rules! print_it { {} => { println!("{:?}", it); } }
2+
//~^ ERROR cannot find value `it` in this scope
3+
4+
fn main() {
5+
{
6+
let it = "hello";
7+
}
8+
{
9+
let it = "world";
10+
{
11+
let it = ();
12+
print_it!();
13+
}
14+
}
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0425]: cannot find value `it` in this scope
2+
--> $DIR/macro-hygiene-help-issue-148580.rs:1:50
3+
|
4+
LL | macro_rules! print_it { {} => { println!("{:?}", it); } }
5+
| ^^ not found in this scope
6+
...
7+
LL | print_it!();
8+
| ----------- in this macro invocation
9+
|
10+
help: an identifier with the same name exists, but is not accessible due to macro hygiene
11+
--> $DIR/macro-hygiene-help-issue-148580.rs:11:17
12+
|
13+
LL | let it = ();
14+
| ^^
15+
= note: this error originates in the macro `print_it` (in Nightly builds, run with -Z macro-backtrace for more info)
16+
17+
error: aborting due to 1 previous error
18+
19+
For more information about this error, try `rustc --explain E0425`.

tests/ui/macros/macro-hygiene-scope-15167.stderr

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ LL | macro_rules! f { () => (n) }
77
LL | println!("{}", f!());
88
| ---- in this macro invocation
99
|
10+
help: an identifier with the same name exists, but is not accessible due to macro hygiene
11+
--> $DIR/macro-hygiene-scope-15167.rs:12:9
12+
|
13+
LL | for n in 0..1 {
14+
| ^
1015
= note: this error originates in the macro `f` (in Nightly builds, run with -Z macro-backtrace for more info)
1116

1217
error[E0425]: cannot find value `n` in this scope
@@ -18,6 +23,11 @@ LL | macro_rules! f { () => (n) }
1823
LL | println!("{}", f!());
1924
| ---- in this macro invocation
2025
|
26+
help: an identifier with the same name exists, but is not accessible due to macro hygiene
27+
--> $DIR/macro-hygiene-scope-15167.rs:16:17
28+
|
29+
LL | if let Some(n) = None {
30+
| ^
2131
= note: this error originates in the macro `f` (in Nightly builds, run with -Z macro-backtrace for more info)
2232

2333
error[E0425]: cannot find value `n` in this scope
@@ -29,6 +39,11 @@ LL | macro_rules! f { () => (n) }
2939
LL | println!("{}", f!());
3040
| ---- in this macro invocation
3141
|
42+
help: an identifier with the same name exists, but is not accessible due to macro hygiene
43+
--> $DIR/macro-hygiene-scope-15167.rs:21:24
44+
|
45+
LL | } else if let Some(n) = None {
46+
| ^
3247
= note: this error originates in the macro `f` (in Nightly builds, run with -Z macro-backtrace for more info)
3348

3449
error[E0425]: cannot find value `n` in this scope
@@ -40,6 +55,11 @@ LL | macro_rules! f { () => (n) }
4055
LL | println!("{}", f!());
4156
| ---- in this macro invocation
4257
|
58+
help: an identifier with the same name exists, but is not accessible due to macro hygiene
59+
--> $DIR/macro-hygiene-scope-15167.rs:25:20
60+
|
61+
LL | while let Some(n) = None {
62+
| ^
4363
= note: this error originates in the macro `f` (in Nightly builds, run with -Z macro-backtrace for more info)
4464

4565
error: aborting due to 4 previous errors

tests/ui/macros/metavar-expressions/concat-hygiene.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ LL | ${concat($lhs, $rhs)}
77
LL | let _another = join!(abc, def);
88
| --------------- in this macro invocation
99
|
10+
help: an identifier with the same name exists, but is not accessible due to macro hygiene
11+
--> $DIR/concat-hygiene.rs:11:9
12+
|
13+
LL | let abcdef = 1;
14+
| ^^^^^^
1015
= note: this error originates in the macro `join` (in Nightly builds, run with -Z macro-backtrace for more info)
1116

1217
error: aborting due to 1 previous error

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ LL | gen_macro_rules!();
1818
LL | generated!();
1919
| ------------ in this macro invocation
2020
|
21+
help: an identifier with the same name exists, but is not accessible due to macro hygiene
22+
--> $DIR/gen-macro-rules-hygiene.rs:19:13
23+
|
24+
LL | let local_use = 1;
25+
| ^^^^^^^^^
2126
= note: this error originates in the macro `generated` (in Nightly builds, run with -Z macro-backtrace for more info)
2227

2328
error[E0425]: cannot find value `local_def` in this scope

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,11 @@ error[E0425]: cannot find value `local_use` in this scope
594594
LL | proc_macro_rules!();
595595
| ^^^^^^^^^^^^^^^^^^^ help: a local variable with a similar name exists: `local_def`
596596
|
597+
help: an identifier with the same name exists, but is not accessible due to macro hygiene
598+
--> $DIR/mixed-site-span.rs:21:13
599+
|
600+
LL | let local_use = 1;
601+
| ^^^^^^^^^
597602
= note: this error originates in the macro `proc_macro_rules` (in Nightly builds, run with -Z macro-backtrace for more info)
598603

599604
error[E0425]: cannot find value `local_def` in this scope

tests/ui/proc-macro/weird-hygiene.stderr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ LL | Value = (stringify!($tokens + hidden_ident), 1).1
77
LL | other!(50);
88
| ---------- in this macro invocation
99
|
10+
help: an identifier with the same name exists, but is not accessible due to macro hygiene
11+
--> $DIR/weird-hygiene.rs:44:9
12+
|
13+
LL | let hidden_ident = "Hello1";
14+
| ^^^^^^^^^^^^
1015
= 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)
1116

1217
error[E0425]: cannot find value `hidden_ident` in this scope
@@ -18,6 +23,11 @@ LL | hidden_ident
1823
LL | invoke_it!(25);
1924
| -------------- in this macro invocation
2025
|
26+
help: an identifier with the same name exists, but is not accessible due to macro hygiene
27+
--> $DIR/weird-hygiene.rs:44:9
28+
|
29+
LL | let hidden_ident = "Hello1";
30+
| ^^^^^^^^^^^^
2131
= note: this error originates in the macro `invoke_it` (in Nightly builds, run with -Z macro-backtrace for more info)
2232

2333
error: aborting due to 2 previous errors

0 commit comments

Comments
 (0)