Skip to content

Commit 40c9d46

Browse files
committed
resolve: Improve code reuse in typo candidate collection
1 parent 72beda5 commit 40c9d46

File tree

5 files changed

+52
-46
lines changed

5 files changed

+52
-46
lines changed

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,16 +1016,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10161016
.emit()
10171017
}
10181018

1019-
/// Lookup typo candidate in scope for a macro or import.
1020-
fn early_lookup_typo_candidate(
1019+
pub(crate) fn add_scope_set_candidates(
10211020
&mut self,
1021+
suggestions: &mut Vec<TypoSuggestion>,
10221022
scope_set: ScopeSet<'ra>,
10231023
parent_scope: &ParentScope<'ra>,
1024-
ident: Ident,
1024+
ctxt: SyntaxContext,
10251025
filter_fn: &impl Fn(Res) -> bool,
1026-
) -> Option<TypoSuggestion> {
1027-
let mut suggestions = Vec::new();
1028-
let ctxt = ident.span.ctxt();
1026+
) {
10291027
self.cm().visit_scopes(scope_set, parent_scope, ctxt, |this, scope, use_prelude, _| {
10301028
match scope {
10311029
Scope::DeriveHelpers(expn_id) => {
@@ -1055,7 +1053,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10551053
}
10561054
}
10571055
Scope::Module(module, _) => {
1058-
this.add_module_candidates(module, &mut suggestions, filter_fn, None);
1056+
this.add_module_candidates(module, suggestions, filter_fn, None);
10591057
}
10601058
Scope::MacroUsePrelude => {
10611059
suggestions.extend(this.macro_use_prelude.iter().filter_map(
@@ -1111,6 +1109,19 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11111109

11121110
None::<()>
11131111
});
1112+
}
1113+
1114+
/// Lookup typo candidate in scope for a macro or import.
1115+
fn early_lookup_typo_candidate(
1116+
&mut self,
1117+
scope_set: ScopeSet<'ra>,
1118+
parent_scope: &ParentScope<'ra>,
1119+
ident: Ident,
1120+
filter_fn: &impl Fn(Res) -> bool,
1121+
) -> Option<TypoSuggestion> {
1122+
let mut suggestions = Vec::new();
1123+
let ctxt = ident.span.ctxt();
1124+
self.add_scope_set_candidates(&mut suggestions, scope_set, parent_scope, ctxt, filter_fn);
11141125

11151126
// Make sure error reporting is deterministic.
11161127
suggestions.sort_by(|a, b| a.candidate.as_str().cmp(b.candidate.as_str()));

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ use crate::late::{
3939
};
4040
use crate::ty::fast_reject::SimplifiedType;
4141
use crate::{
42-
Module, ModuleKind, ModuleOrUniformRoot, PathResult, PathSource, Resolver, Segment, errors,
43-
path_names_to_string,
42+
Module, ModuleKind, ModuleOrUniformRoot, PathResult, PathSource, Resolver, ScopeSet, Segment,
43+
errors, path_names_to_string,
4444
};
4545

4646
type Res = def::Res<ast::NodeId>;
@@ -2459,45 +2459,30 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
24592459
}
24602460
}
24612461

2462+
if let RibKind::Module(module) = rib.kind
2463+
&& let ModuleKind::Block = module.kind
2464+
{
2465+
self.r.add_module_candidates(module, &mut names, &filter_fn, Some(ctxt));
2466+
} else if let RibKind::Module(module) = rib.kind {
2467+
// Encountered a module item, abandon ribs and look into that module and preludes.
2468+
self.r.add_scope_set_candidates(
2469+
&mut names,
2470+
ScopeSet::Late(ns, module, None),
2471+
&self.parent_scope,
2472+
ctxt,
2473+
filter_fn,
2474+
);
2475+
break;
2476+
}
2477+
24622478
if let RibKind::MacroDefinition(def) = rib.kind
24632479
&& def == self.r.macro_def(ctxt)
24642480
{
24652481
// If an invocation of this macro created `ident`, give up on `ident`
24662482
// and switch to `ident`'s source from the macro definition.
24672483
ctxt.remove_mark();
2468-
continue;
2469-
}
2470-
2471-
// Items in scope
2472-
if let RibKind::Module(module) = rib.kind {
2473-
// Items from this module
2474-
self.r.add_module_candidates(module, &mut names, &filter_fn, Some(ctxt));
2475-
2476-
if let ModuleKind::Block = module.kind {
2477-
// We can see through blocks
2478-
} else {
2479-
// Items from the prelude
2480-
if !module.no_implicit_prelude {
2481-
names.extend(self.r.extern_prelude.keys().flat_map(|ident| {
2482-
let res = Res::Def(DefKind::Mod, CRATE_DEF_ID.to_def_id());
2483-
filter_fn(res)
2484-
.then_some(TypoSuggestion::typo_from_ident(ident.0, res))
2485-
}));
2486-
2487-
if let Some(prelude) = self.r.prelude {
2488-
self.r.add_module_candidates(prelude, &mut names, &filter_fn, None);
2489-
}
2490-
}
2491-
break;
2492-
}
24932484
}
24942485
}
2495-
// Add primitive types to the mix
2496-
if filter_fn(Res::PrimTy(PrimTy::Bool)) {
2497-
names.extend(PrimTy::ALL.iter().map(|prim_ty| {
2498-
TypoSuggestion::typo_from_name(prim_ty.name(), Res::PrimTy(*prim_ty))
2499-
}))
2500-
}
25012486
} else {
25022487
// Search in module.
25032488
let mod_path = &path[..path.len() - 1];

tests/ui/hygiene/arguments.stderr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error[E0412]: cannot find type `S` in this scope
22
--> $DIR/arguments.rs:14:8
33
|
4+
LL | struct S;
5+
| - you might have meant to refer to this struct
6+
...
47
LL | m!(S, S);
58
| ^ not found in this scope
69

tests/ui/hygiene/cross-crate-name-hiding-2.stderr

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ error[E0422]: cannot find struct, variant or union type `MyStruct` in this scope
33
|
44
LL | let x = MyStruct {};
55
| ^^^^^^^^ not found in this scope
6+
|
7+
::: $DIR/auxiliary/use_by_macro.rs:15:1
8+
|
9+
LL | x!(my_struct);
10+
| ------------- you might have meant to refer to this struct
611

712
error: aborting due to 1 previous error
813

tests/ui/hygiene/globs.stderr

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,32 +48,34 @@ error[E0425]: cannot find function `f` in this scope
4848
--> $DIR/globs.rs:61:12
4949
|
5050
LL | n!(f);
51-
| ----- in this macro invocation
51+
| -----
52+
| | |
53+
| | you might have meant to refer to this function
54+
| in this macro invocation
5255
...
5356
LL | $j();
5457
| -- due to this macro variable
5558
...
5659
LL | n!(f);
5760
| ^ not found in this scope
5861
|
59-
= help: consider importing this function:
60-
foo::f
6162
= note: this error originates in the macro `n` (in Nightly builds, run with -Z macro-backtrace for more info)
6263

6364
error[E0425]: cannot find function `f` in this scope
6465
--> $DIR/globs.rs:65:17
6566
|
6667
LL | n!(f);
67-
| ----- in this macro invocation
68+
| -----
69+
| | |
70+
| | you might have meant to refer to this function
71+
| in this macro invocation
6872
...
6973
LL | $j();
7074
| -- due to this macro variable
7175
...
7276
LL | f
7377
| ^ not found in this scope
7478
|
75-
= help: consider importing this function:
76-
foo::f
7779
= note: this error originates in the macro `n` (in Nightly builds, run with -Z macro-backtrace for more info)
7880

7981
error: aborting due to 4 previous errors

0 commit comments

Comments
 (0)