Skip to content

Commit d692d37

Browse files
committed
Do not suggest binding from outside of a macro in macro
1 parent f219ab5 commit d692d37

File tree

7 files changed

+11
-25
lines changed

7 files changed

+11
-25
lines changed

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1437,14 +1437,17 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14371437
suggestion: Option<TypoSuggestion>,
14381438
span: Span,
14391439
) -> bool {
1440+
if !span.can_be_used_for_suggestions() {
1441+
return false;
1442+
}
14401443
let suggestion = match suggestion {
14411444
None => return false,
14421445
// We shouldn't suggest underscore.
14431446
Some(suggestion) if suggestion.candidate == kw::Underscore => return false,
14441447
Some(suggestion) => suggestion,
14451448
};
14461449
if let Some(def_span) = suggestion.res.opt_def_id().map(|def_id| self.def_span(def_id)) {
1447-
if span.overlaps(def_span) {
1450+
if span.overlaps(def_span) || !def_span.can_be_used_for_suggestions() {
14481451
// Don't suggest typo suggestion for itself like in the following:
14491452
// error[E0423]: expected function, tuple struct or tuple variant, found struct `X`
14501453
// --> $DIR/issue-64792-bad-unicode-ctor.rs:3:14

compiler/rustc_span/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -604,15 +604,13 @@ impl Span {
604604
// FIXME: If this span comes from a `derive` macro but it points at code the user wrote,
605605
// the callsite span and the span will be pointing at different places. It also means that
606606
// we can safely provide suggestions on this span.
607-
ExpnKind::Macro(MacroKind::Attr, _) | ExpnKind::Macro(MacroKind::Derive, _)
607+
ExpnKind::Macro(..)
608608
if self.parent_callsite().map(|p| (p.lo(), p.hi()))
609609
!= Some((self.lo(), self.hi())) =>
610610
{
611611
true
612612
}
613-
ExpnKind::Desugaring(DesugaringKind::Resize)
614-
| ExpnKind::Macro(MacroKind::Bang, _)
615-
| ExpnKind::Desugaring(_) => {
613+
ExpnKind::Desugaring(DesugaringKind::Resize) | ExpnKind::Desugaring(_) => {
616614
self.parent_callsite().unwrap().can_be_used_for_suggestions()
617615
}
618616
ExpnKind::Macro(..) => false,

tests/ui/proc-macro/generate-mod.stderr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ error[E0412]: cannot find type `FromOutside` in the expanded code of procedural
44
LL | generate_mod::check!();
55
| ^^^^^^^^^^^^^^^^^^^^^^ `FromOutside` not found in expanded code of this procedural macro
66
|
7-
= help: consider importing this struct:
8-
FromOutside
97
= note: this error originates in the macro `generate_mod::check` (in Nightly builds, run with -Z macro-backtrace for more info)
108

119
error[E0412]: cannot find type `Outer` in the expanded code of procedural macro `generate_mod::check`
@@ -14,8 +12,6 @@ error[E0412]: cannot find type `Outer` in the expanded code of procedural macro
1412
LL | generate_mod::check!();
1513
| ^^^^^^^^^^^^^^^^^^^^^^ `Outer` not found in expanded code of this procedural macro
1614
|
17-
= help: consider importing this struct:
18-
Outer
1915
= note: this error originates in the macro `generate_mod::check` (in Nightly builds, run with -Z macro-backtrace for more info)
2016

2117
error[E0412]: cannot find type `FromOutside` in the expanded code of procedural macro `generate_mod::check_attr`

tests/ui/proc-macro/lints_in_proc_macros.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,5 @@ fn main() {
88
let foobar = 42;
99
bang_proc_macro2!();
1010
//~^ ERROR cannot find value `foobar2`
11-
//~| HELP a local variable with a similar name exists
12-
//~| SUGGESTION foobar
1311
println!("{}", x);
1412
}

tests/ui/proc-macro/lints_in_proc_macros.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0425]: cannot find value `foobar2` in the expanded code of procedural mac
22
--> $DIR/lints_in_proc_macros.rs:9:5
33
|
44
LL | bang_proc_macro2!();
5-
| ^^^^^^^^^^^^^^^^^^^ help: a local variable with a similar name exists: `foobar`
5+
| ^^^^^^^^^^^^^^^^^^^ `foobar2` not found in expanded code of this procedural macro
66
|
77
= note: this error originates in the macro `bang_proc_macro2` (in Nightly builds, run with -Z macro-backtrace for more info)
88

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error[E0425]: cannot find value `local_use` in the expanded code of procedural m
1010
--> $DIR/mixed-site-span.rs:13:9
1111
|
1212
LL | proc_macro_rules!();
13-
| ^^^^^^^^^^^^^^^^^^^ help: a local variable with a similar name exists: `local_def`
13+
| ^^^^^^^^^^^^^^^^^^^ `local_use` not found in expanded code of this procedural macro
1414
|
1515
= note: this error originates in the macro `proc_macro_rules` (in Nightly builds, run with -Z macro-backtrace for more info)
1616

tests/ui/proc-macro/parent-source-spans.stderr

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -140,41 +140,32 @@ error[E0425]: cannot find value `ok` in the expanded code of procedural macro `p
140140
--> $DIR/parent-source-spans.rs:29:5
141141
|
142142
LL | parent_source_spans!($($tokens)*);
143-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: a tuple variant with a similar name exists: `Ok`
143+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ok` not found in expanded code of this procedural macro
144144
...
145145
LL | one!("hello", "world");
146146
| ---------------------- in this macro invocation
147-
--> $SRC_DIR/core/src/result.rs:LL:COL
148-
|
149-
= note: similarly named tuple variant `Ok` defined here
150147
|
151148
= note: this error originates in the macro `parent_source_spans` which comes from the expansion of the macro `one` (in Nightly builds, run with -Z macro-backtrace for more info)
152149

153150
error[E0425]: cannot find value `ok` in the expanded code of procedural macro `parent_source_spans`
154151
--> $DIR/parent-source-spans.rs:29:5
155152
|
156153
LL | parent_source_spans!($($tokens)*);
157-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: a tuple variant with a similar name exists: `Ok`
154+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ok` not found in expanded code of this procedural macro
158155
...
159156
LL | two!("yay", "rust");
160157
| ------------------- in this macro invocation
161-
--> $SRC_DIR/core/src/result.rs:LL:COL
162-
|
163-
= note: similarly named tuple variant `Ok` defined here
164158
|
165159
= note: this error originates in the macro `parent_source_spans` which comes from the expansion of the macro `two` (in Nightly builds, run with -Z macro-backtrace for more info)
166160

167161
error[E0425]: cannot find value `ok` in the expanded code of procedural macro `parent_source_spans`
168162
--> $DIR/parent-source-spans.rs:29:5
169163
|
170164
LL | parent_source_spans!($($tokens)*);
171-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: a tuple variant with a similar name exists: `Ok`
165+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `ok` not found in expanded code of this procedural macro
172166
...
173167
LL | three!("hip", "hop");
174168
| -------------------- in this macro invocation
175-
--> $SRC_DIR/core/src/result.rs:LL:COL
176-
|
177-
= note: similarly named tuple variant `Ok` defined here
178169
|
179170
= note: this error originates in the macro `parent_source_spans` which comes from the expansion of the macro `three` (in Nightly builds, run with -Z macro-backtrace for more info)
180171

0 commit comments

Comments
 (0)