Skip to content

Commit 6d947e6

Browse files
committed
Account for hygiene when suggesting typos.
1 parent 9b0a099 commit 6d947e6

File tree

7 files changed

+51
-23
lines changed

7 files changed

+51
-23
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_span::hygiene::MacroKind;
2424
use rustc_span::lev_distance::find_best_match_for_name;
2525
use rustc_span::source_map::SourceMap;
2626
use rustc_span::symbol::{kw, sym, Ident, Symbol};
27-
use rustc_span::{BytePos, Span};
27+
use rustc_span::{BytePos, Span, SyntaxContext};
2828

2929
use crate::imports::{Import, ImportKind, ImportResolver};
3030
use crate::late::{PatternSource, Rib};
@@ -47,13 +47,15 @@ pub(crate) type Suggestion = (Vec<(Span, String)>, String, Applicability);
4747
/// similarly named label and whether or not it is reachable.
4848
pub(crate) type LabelSuggestion = (Ident, bool);
4949

50+
#[derive(Debug)]
5051
pub(crate) enum SuggestionTarget {
5152
/// The target has a similar name as the name used by the programmer (probably a typo)
5253
SimilarlyNamed,
5354
/// The target is the only valid item that can be used in the corresponding context
5455
SingleItem,
5556
}
5657

58+
#[derive(Debug)]
5759
pub(crate) struct TypoSuggestion {
5860
pub candidate: Symbol,
5961
pub res: Res,
@@ -482,11 +484,12 @@ impl<'a> Resolver<'a> {
482484
module: Module<'a>,
483485
names: &mut Vec<TypoSuggestion>,
484486
filter_fn: &impl Fn(Res) -> bool,
487+
ctxt: Option<SyntaxContext>,
485488
) {
486489
for (key, resolution) in self.resolutions(module).borrow().iter() {
487490
if let Some(binding) = resolution.borrow().binding {
488491
let res = binding.res();
489-
if filter_fn(res) {
492+
if filter_fn(res) && ctxt.map_or(true, |ctxt| ctxt == key.ident.span.ctxt()) {
490493
names.push(TypoSuggestion::typo_from_res(key.ident.name, res));
491494
}
492495
}
@@ -1181,10 +1184,10 @@ impl<'a> Resolver<'a> {
11811184
Scope::CrateRoot => {
11821185
let root_ident = Ident::new(kw::PathRoot, ident.span);
11831186
let root_module = this.resolve_crate_root(root_ident);
1184-
this.add_module_candidates(root_module, &mut suggestions, filter_fn);
1187+
this.add_module_candidates(root_module, &mut suggestions, filter_fn, None);
11851188
}
11861189
Scope::Module(module, _) => {
1187-
this.add_module_candidates(module, &mut suggestions, filter_fn);
1190+
this.add_module_candidates(module, &mut suggestions, filter_fn, None);
11881191
}
11891192
Scope::MacroUsePrelude => {
11901193
suggestions.extend(this.macro_use_prelude.iter().filter_map(
@@ -1221,7 +1224,7 @@ impl<'a> Resolver<'a> {
12211224
Scope::StdLibPrelude => {
12221225
if let Some(prelude) = this.prelude {
12231226
let mut tmp_suggestions = Vec::new();
1224-
this.add_module_candidates(prelude, &mut tmp_suggestions, filter_fn);
1227+
this.add_module_candidates(prelude, &mut tmp_suggestions, filter_fn, None);
12251228
suggestions.extend(
12261229
tmp_suggestions
12271230
.into_iter()

compiler/rustc_resolve/src/late/diagnostics.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ pub(super) enum LifetimeElisionCandidate {
131131
}
132132

133133
/// Only used for diagnostics.
134+
#[derive(Debug)]
134135
struct BaseError {
135136
msg: String,
136137
fallback_label: String,
@@ -1584,19 +1585,35 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
15841585
) -> Option<TypoSuggestion> {
15851586
let mut names = Vec::new();
15861587
if path.len() == 1 {
1588+
let mut ctxt = path.last().unwrap().ident.span.ctxt();
1589+
15871590
// Search in lexical scope.
15881591
// Walk backwards up the ribs in scope and collect candidates.
15891592
for rib in self.ribs[ns].iter().rev() {
1593+
let rib_ctxt = if rib.kind.contains_params() {
1594+
ctxt.normalize_to_macros_2_0()
1595+
} else {
1596+
ctxt.normalize_to_macro_rules()
1597+
};
1598+
15901599
// Locals and type parameters
15911600
for (ident, &res) in &rib.bindings {
1592-
if filter_fn(res) {
1601+
if filter_fn(res) && ident.span.ctxt() == rib_ctxt {
15931602
names.push(TypoSuggestion::typo_from_res(ident.name, res));
15941603
}
15951604
}
1605+
1606+
if let RibKind::MacroDefinition(def) = rib.kind && def == self.r.macro_def(ctxt) {
1607+
// If an invocation of this macro created `ident`, give up on `ident`
1608+
// and switch to `ident`'s source from the macro definition.
1609+
ctxt.remove_mark();
1610+
continue;
1611+
}
1612+
15961613
// Items in scope
15971614
if let RibKind::ModuleRibKind(module) = rib.kind {
15981615
// Items from this module
1599-
self.r.add_module_candidates(module, &mut names, &filter_fn);
1616+
self.r.add_module_candidates(module, &mut names, &filter_fn, Some(ctxt));
16001617

16011618
if let ModuleKind::Block = module.kind {
16021619
// We can see through blocks
@@ -1622,7 +1639,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
16221639
}));
16231640

16241641
if let Some(prelude) = self.r.prelude {
1625-
self.r.add_module_candidates(prelude, &mut names, &filter_fn);
1642+
self.r.add_module_candidates(prelude, &mut names, &filter_fn, None);
16261643
}
16271644
}
16281645
break;
@@ -1641,7 +1658,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
16411658
if let PathResult::Module(ModuleOrUniformRoot::Module(module)) =
16421659
self.resolve_path(mod_path, Some(TypeNS), None)
16431660
{
1644-
self.r.add_module_candidates(module, &mut names, &filter_fn);
1661+
self.r.add_module_candidates(module, &mut names, &filter_fn, None);
16451662
}
16461663
}
16471664

src/test/ui/hygiene/globs.stderr

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
error[E0425]: cannot find function `f` in this scope
22
--> $DIR/globs.rs:22:9
33
|
4+
LL | pub fn g() {}
5+
| ---------- similarly named function `g` defined here
6+
...
47
LL | f();
5-
| ^ not found in this scope
8+
| ^
9+
|
10+
help: a function with a similar name exists
611
|
12+
LL | g();
13+
| ~
714
help: consider importing this function
815
|
916
LL | use foo::f;
@@ -12,8 +19,11 @@ LL | use foo::f;
1219
error[E0425]: cannot find function `g` in this scope
1320
--> $DIR/globs.rs:15:5
1421
|
22+
LL | pub fn f() {}
23+
| ---------- similarly named function `f` defined here
24+
...
1525
LL | g();
16-
| ^ not found in this scope
26+
| ^
1727
...
1828
LL | / m! {
1929
LL | | use bar::*;
@@ -23,6 +33,10 @@ LL | | }
2333
| |_____- in this macro invocation
2434
|
2535
= note: this error originates in the macro `m` (in Nightly builds, run with -Z macro-backtrace for more info)
36+
help: a function with a similar name exists
37+
|
38+
LL | f();
39+
| ~
2640
help: consider importing this function
2741
|
2842
LL | use bar::g;

src/test/ui/hygiene/rustc-macro-transparency.stderr

+1-7
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,8 @@ LL | semitransparent;
1919
error[E0423]: expected value, found macro `opaque`
2020
--> $DIR/rustc-macro-transparency.rs:30:5
2121
|
22-
LL | struct Opaque;
23-
| -------------- similarly named unit struct `Opaque` defined here
24-
...
2522
LL | opaque;
26-
| ^^^^^^
27-
| |
28-
| not a value
29-
| help: a unit struct with a similar name exists (notice the capitalization): `Opaque`
23+
| ^^^^^^ not a value
3024

3125
error: aborting due to 3 previous errors
3226

src/test/ui/macros/macro-context.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ error[E0425]: cannot find value `i` in this scope
5757
--> $DIR/macro-context.rs:3:13
5858
|
5959
LL | () => ( i ; typeof );
60-
| ^ help: a local variable with a similar name exists: `a`
60+
| ^ not found in this scope
6161
...
6262
LL | let i = m!();
6363
| ---- in this macro invocation

src/test/ui/proc-macro/gen-macro-rules-hygiene.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ error[E0425]: cannot find value `local_use` in this scope
1313
--> $DIR/gen-macro-rules-hygiene.rs:12:1
1414
|
1515
LL | gen_macro_rules!();
16-
| ^^^^^^^^^^^^^^^^^^ not found in this scope
16+
| ^^^^^^^^^^^^^^^^^^ help: a local variable with a similar name exists: `local_def`
1717
...
1818
LL | generated!();
1919
| ------------ in this macro invocation
@@ -24,7 +24,7 @@ error[E0425]: cannot find value `local_def` in this scope
2424
--> $DIR/gen-macro-rules-hygiene.rs:21:9
2525
|
2626
LL | local_def;
27-
| ^^^^^^^^^ not found in this scope
27+
| ^^^^^^^^^ help: a local variable with a similar name exists: `local_use`
2828

2929
error: aborting due to 3 previous errors
3030

src/test/ui/proc-macro/mixed-site-span.stderr

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

1717
error[E0425]: cannot find value `local_def` in this scope
1818
--> $DIR/mixed-site-span.rs:17:9
1919
|
2020
LL | local_def;
21-
| ^^^^^^^^^ not found in this scope
21+
| ^^^^^^^^^ help: a local variable with a similar name exists: `local_use`
2222

2323
error[E0412]: cannot find type `ItemUse` in crate `$crate`
2424
--> $DIR/mixed-site-span.rs:24:1

0 commit comments

Comments
 (0)