Skip to content

Commit 762b7ec

Browse files
Rollup merge of #145372 - petrochenkov:noresmacpath, r=jackh726
resolve: Miscellaneous cleanups See individual commits. All noticed when reviewing recent PRs to name resolution.
2 parents 707e956 + 9b797b9 commit 762b7ec

File tree

9 files changed

+72
-101
lines changed

9 files changed

+72
-101
lines changed

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 19 additions & 29 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) => {
@@ -1041,28 +1039,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10411039
}
10421040
}
10431041
Scope::DeriveHelpersCompat => {
1044-
let res = Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat);
1045-
if filter_fn(res) {
1046-
for derive in parent_scope.derives {
1047-
let parent_scope = &ParentScope { derives: &[], ..*parent_scope };
1048-
let Ok((Some(ext), _)) = this.reborrow().resolve_macro_path(
1049-
derive,
1050-
Some(MacroKind::Derive),
1051-
parent_scope,
1052-
false,
1053-
false,
1054-
None,
1055-
None,
1056-
) else {
1057-
continue;
1058-
};
1059-
suggestions.extend(
1060-
ext.helper_attrs
1061-
.iter()
1062-
.map(|name| TypoSuggestion::typo_from_name(*name, res)),
1063-
);
1064-
}
1065-
}
1042+
// Never recommend deprecated helper attributes.
10661043
}
10671044
Scope::MacroRules(macro_rules_scope) => {
10681045
if let MacroRulesScope::Binding(macro_rules_binding) = macro_rules_scope.get() {
@@ -1076,7 +1053,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10761053
}
10771054
}
10781055
Scope::Module(module, _) => {
1079-
this.add_module_candidates(module, &mut suggestions, filter_fn, None);
1056+
this.add_module_candidates(module, suggestions, filter_fn, None);
10801057
}
10811058
Scope::MacroUsePrelude => {
10821059
suggestions.extend(this.macro_use_prelude.iter().filter_map(
@@ -1134,6 +1111,19 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11341111

11351112
None::<()>
11361113
});
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);
11371127

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

compiler/rustc_resolve/src/ident.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -459,17 +459,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
459459
}
460460
}
461461
Scope::DeriveHelpersCompat => {
462-
// FIXME: Try running this logic earlier, to allocate name bindings for
463-
// legacy derive helpers when creating an attribute invocation with
464-
// following derives. Legacy derive helpers are not common, so it shouldn't
465-
// affect performance. It should also allow to remove the `derives`
466-
// component from `ParentScope`.
467462
let mut result = Err(Determinacy::Determined);
468463
for derive in parent_scope.derives {
469464
let parent_scope = &ParentScope { derives: &[], ..*parent_scope };
470465
match this.reborrow().resolve_macro_path(
471466
derive,
472-
Some(MacroKind::Derive),
467+
MacroKind::Derive,
473468
parent_scope,
474469
true,
475470
force,

compiler/rustc_resolve/src/late.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4315,7 +4315,6 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
43154315
qself,
43164316
path,
43174317
ns,
4318-
path_span,
43194318
source.defer_to_typeck(),
43204319
finalize,
43214320
source,
@@ -4438,7 +4437,6 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
44384437
qself: &Option<Box<QSelf>>,
44394438
path: &[Segment],
44404439
primary_ns: Namespace,
4441-
span: Span,
44424440
defer_to_typeck: bool,
44434441
finalize: Finalize,
44444442
source: PathSource<'_, 'ast, 'ra>,
@@ -4463,21 +4461,11 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
44634461
}
44644462

44654463
assert!(primary_ns != MacroNS);
4466-
4467-
if qself.is_none() {
4468-
let path_seg = |seg: &Segment| PathSegment::from_ident(seg.ident);
4469-
let path = Path { segments: path.iter().map(path_seg).collect(), span, tokens: None };
4470-
if let Ok((_, res)) = self.r.cm().resolve_macro_path(
4471-
&path,
4472-
None,
4473-
&self.parent_scope,
4474-
false,
4475-
false,
4476-
None,
4477-
None,
4478-
) {
4479-
return Ok(Some(PartialRes::new(res)));
4480-
}
4464+
if qself.is_none()
4465+
&& let PathResult::NonModule(res) =
4466+
self.r.cm().maybe_resolve_path(path, Some(MacroNS), &self.parent_scope, None)
4467+
{
4468+
return Ok(Some(res));
44814469
}
44824470

44834471
Ok(fin_res)

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];

compiler/rustc_resolve/src/macros.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
398398
resolution.exts = Some(
399399
match self.cm().resolve_macro_path(
400400
&resolution.path,
401-
Some(MacroKind::Derive),
401+
MacroKind::Derive,
402402
&parent_scope,
403403
true,
404404
force,
@@ -563,7 +563,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
563563
) -> Result<(Arc<SyntaxExtension>, Res), Indeterminate> {
564564
let (ext, res) = match self.cm().resolve_macro_or_delegation_path(
565565
path,
566-
Some(kind),
566+
kind,
567567
parent_scope,
568568
true,
569569
force,
@@ -710,7 +710,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
710710
pub(crate) fn resolve_macro_path<'r>(
711711
self: CmResolver<'r, 'ra, 'tcx>,
712712
path: &ast::Path,
713-
kind: Option<MacroKind>,
713+
kind: MacroKind,
714714
parent_scope: &ParentScope<'ra>,
715715
trace: bool,
716716
force: bool,
@@ -733,7 +733,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
733733
fn resolve_macro_or_delegation_path<'r>(
734734
mut self: CmResolver<'r, 'ra, 'tcx>,
735735
ast_path: &ast::Path,
736-
kind: Option<MacroKind>,
736+
kind: MacroKind,
737737
parent_scope: &ParentScope<'ra>,
738738
trace: bool,
739739
force: bool,
@@ -747,7 +747,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
747747

748748
// Possibly apply the macro helper hack
749749
if deleg_impl.is_none()
750-
&& kind == Some(MacroKind::Bang)
750+
&& kind == MacroKind::Bang
751751
&& let [segment] = path.as_slice()
752752
&& segment.ident.span.ctxt().outer_expn_data().local_inner_macros
753753
{
@@ -775,7 +775,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
775775
};
776776

777777
if trace {
778-
let kind = kind.expect("macro kind must be specified if tracing is enabled");
779778
// FIXME: Should be an output of Speculative Resolution.
780779
self.multi_segment_macro_resolutions.borrow_mut().push((
781780
path,
@@ -790,10 +789,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
790789
self.prohibit_imported_non_macro_attrs(None, res.ok(), path_span);
791790
res
792791
} else {
793-
let scope_set = kind.map_or(ScopeSet::All(MacroNS), ScopeSet::Macro);
794792
let binding = self.reborrow().early_resolve_ident_in_lexical_scope(
795793
path[0].ident,
796-
scope_set,
794+
ScopeSet::Macro(kind),
797795
parent_scope,
798796
None,
799797
force,
@@ -805,7 +803,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
805803
}
806804

807805
if trace {
808-
let kind = kind.expect("macro kind must be specified if tracing is enabled");
809806
// FIXME: Should be an output of Speculative Resolution.
810807
self.single_segment_macro_resolutions.borrow_mut().push((
811808
path[0].ident,

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

tests/ui/proc-macro/proc-macro-attributes.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ error: cannot find attribute `C` in this scope
22
--> $DIR/proc-macro-attributes.rs:9:3
33
|
44
LL | #[C]
5-
| ^ help: a derive helper attribute with a similar name exists: `B`
5+
| ^
6+
|
7+
help: the derive macro `B` accepts the similarly named `B` attribute
8+
|
9+
LL - #[C]
10+
LL + #[B]
11+
|
612

713
error[E0659]: `B` is ambiguous
814
--> $DIR/proc-macro-attributes.rs:6:3

0 commit comments

Comments
 (0)