Skip to content

Commit 9b797b9

Browse files
committed
resolve: Improve code reuse in typo candidate collection
1 parent d682943 commit 9b797b9

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(
@@ -1113,6 +1111,19 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11131111

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

11171128
// Make sure error reporting is deterministic.
11181129
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
@@ -38,8 +38,8 @@ use crate::late::{
3838
};
3939
use crate::ty::fast_reject::SimplifiedType;
4040
use crate::{
41-
Module, ModuleKind, ModuleOrUniformRoot, PathResult, PathSource, Resolver, Segment, errors,
42-
path_names_to_string,
41+
Module, ModuleKind, ModuleOrUniformRoot, PathResult, PathSource, Resolver, ScopeSet, Segment,
42+
errors, path_names_to_string,
4343
};
4444

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

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