Rollup merge of #145372 - petrochenkov:noresmacpath, r=jackh726

resolve: Miscellaneous cleanups

See individual commits.

All noticed when reviewing recent PRs to name resolution.
This commit is contained in:
Guillaume Gomez 2025-08-14 11:39:41 +02:00 committed by GitHub
commit 762b7ecdb0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 72 additions and 101 deletions

View file

@ -1016,16 +1016,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
.emit()
}
/// Lookup typo candidate in scope for a macro or import.
fn early_lookup_typo_candidate(
pub(crate) fn add_scope_set_candidates(
&mut self,
suggestions: &mut Vec<TypoSuggestion>,
scope_set: ScopeSet<'ra>,
parent_scope: &ParentScope<'ra>,
ident: Ident,
ctxt: SyntaxContext,
filter_fn: &impl Fn(Res) -> bool,
) -> Option<TypoSuggestion> {
let mut suggestions = Vec::new();
let ctxt = ident.span.ctxt();
) {
self.cm().visit_scopes(scope_set, parent_scope, ctxt, |this, scope, use_prelude, _| {
match scope {
Scope::DeriveHelpers(expn_id) => {
@ -1041,28 +1039,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}
}
Scope::DeriveHelpersCompat => {
let res = Res::NonMacroAttr(NonMacroAttrKind::DeriveHelperCompat);
if filter_fn(res) {
for derive in parent_scope.derives {
let parent_scope = &ParentScope { derives: &[], ..*parent_scope };
let Ok((Some(ext), _)) = this.reborrow().resolve_macro_path(
derive,
Some(MacroKind::Derive),
parent_scope,
false,
false,
None,
None,
) else {
continue;
};
suggestions.extend(
ext.helper_attrs
.iter()
.map(|name| TypoSuggestion::typo_from_name(*name, res)),
);
}
}
// Never recommend deprecated helper attributes.
}
Scope::MacroRules(macro_rules_scope) => {
if let MacroRulesScope::Binding(macro_rules_binding) = macro_rules_scope.get() {
@ -1076,7 +1053,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}
}
Scope::Module(module, _) => {
this.add_module_candidates(module, &mut suggestions, filter_fn, None);
this.add_module_candidates(module, suggestions, filter_fn, None);
}
Scope::MacroUsePrelude => {
suggestions.extend(this.macro_use_prelude.iter().filter_map(
@ -1134,6 +1111,19 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
None::<()>
});
}
/// Lookup typo candidate in scope for a macro or import.
fn early_lookup_typo_candidate(
&mut self,
scope_set: ScopeSet<'ra>,
parent_scope: &ParentScope<'ra>,
ident: Ident,
filter_fn: &impl Fn(Res) -> bool,
) -> Option<TypoSuggestion> {
let mut suggestions = Vec::new();
let ctxt = ident.span.ctxt();
self.add_scope_set_candidates(&mut suggestions, scope_set, parent_scope, ctxt, filter_fn);
// Make sure error reporting is deterministic.
suggestions.sort_by(|a, b| a.candidate.as_str().cmp(b.candidate.as_str()));

View file

@ -459,17 +459,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}
}
Scope::DeriveHelpersCompat => {
// FIXME: Try running this logic earlier, to allocate name bindings for
// legacy derive helpers when creating an attribute invocation with
// following derives. Legacy derive helpers are not common, so it shouldn't
// affect performance. It should also allow to remove the `derives`
// component from `ParentScope`.
let mut result = Err(Determinacy::Determined);
for derive in parent_scope.derives {
let parent_scope = &ParentScope { derives: &[], ..*parent_scope };
match this.reborrow().resolve_macro_path(
derive,
Some(MacroKind::Derive),
MacroKind::Derive,
parent_scope,
true,
force,

View file

@ -4315,7 +4315,6 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
qself,
path,
ns,
path_span,
source.defer_to_typeck(),
finalize,
source,
@ -4438,7 +4437,6 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
qself: &Option<Box<QSelf>>,
path: &[Segment],
primary_ns: Namespace,
span: Span,
defer_to_typeck: bool,
finalize: Finalize,
source: PathSource<'_, 'ast, 'ra>,
@ -4463,21 +4461,11 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
}
assert!(primary_ns != MacroNS);
if qself.is_none() {
let path_seg = |seg: &Segment| PathSegment::from_ident(seg.ident);
let path = Path { segments: path.iter().map(path_seg).collect(), span, tokens: None };
if let Ok((_, res)) = self.r.cm().resolve_macro_path(
&path,
None,
&self.parent_scope,
false,
false,
None,
None,
) {
return Ok(Some(PartialRes::new(res)));
}
if qself.is_none()
&& let PathResult::NonModule(res) =
self.r.cm().maybe_resolve_path(path, Some(MacroNS), &self.parent_scope, None)
{
return Ok(Some(res));
}
Ok(fin_res)

View file

@ -38,8 +38,8 @@ use crate::late::{
};
use crate::ty::fast_reject::SimplifiedType;
use crate::{
Module, ModuleKind, ModuleOrUniformRoot, PathResult, PathSource, Resolver, Segment, errors,
path_names_to_string,
Module, ModuleKind, ModuleOrUniformRoot, PathResult, PathSource, Resolver, ScopeSet, Segment,
errors, path_names_to_string,
};
type Res = def::Res<ast::NodeId>;
@ -2458,44 +2458,29 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
}
}
if let RibKind::Module(module) = rib.kind
&& let ModuleKind::Block = module.kind
{
self.r.add_module_candidates(module, &mut names, &filter_fn, Some(ctxt));
} else if let RibKind::Module(module) = rib.kind {
// Encountered a module item, abandon ribs and look into that module and preludes.
self.r.add_scope_set_candidates(
&mut names,
ScopeSet::Late(ns, module, None),
&self.parent_scope,
ctxt,
filter_fn,
);
break;
}
if let RibKind::MacroDefinition(def) = rib.kind
&& def == self.r.macro_def(ctxt)
{
// If an invocation of this macro created `ident`, give up on `ident`
// and switch to `ident`'s source from the macro definition.
ctxt.remove_mark();
continue;
}
// Items in scope
if let RibKind::Module(module) = rib.kind {
// Items from this module
self.r.add_module_candidates(module, &mut names, &filter_fn, Some(ctxt));
if let ModuleKind::Block = module.kind {
// We can see through blocks
} else {
// Items from the prelude
if !module.no_implicit_prelude {
names.extend(self.r.extern_prelude.keys().flat_map(|ident| {
let res = Res::Def(DefKind::Mod, CRATE_DEF_ID.to_def_id());
filter_fn(res)
.then_some(TypoSuggestion::typo_from_ident(ident.0, res))
}));
if let Some(prelude) = self.r.prelude {
self.r.add_module_candidates(prelude, &mut names, &filter_fn, None);
}
}
break;
}
}
}
// Add primitive types to the mix
if filter_fn(Res::PrimTy(PrimTy::Bool)) {
names.extend(PrimTy::ALL.iter().map(|prim_ty| {
TypoSuggestion::typo_from_name(prim_ty.name(), Res::PrimTy(*prim_ty))
}))
}
} else {
// Search in module.

View file

@ -398,7 +398,7 @@ impl<'ra, 'tcx> ResolverExpand for Resolver<'ra, 'tcx> {
resolution.exts = Some(
match self.cm().resolve_macro_path(
&resolution.path,
Some(MacroKind::Derive),
MacroKind::Derive,
&parent_scope,
true,
force,
@ -563,7 +563,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
) -> Result<(Arc<SyntaxExtension>, Res), Indeterminate> {
let (ext, res) = match self.cm().resolve_macro_or_delegation_path(
path,
Some(kind),
kind,
parent_scope,
true,
force,
@ -710,7 +710,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
pub(crate) fn resolve_macro_path<'r>(
self: CmResolver<'r, 'ra, 'tcx>,
path: &ast::Path,
kind: Option<MacroKind>,
kind: MacroKind,
parent_scope: &ParentScope<'ra>,
trace: bool,
force: bool,
@ -733,7 +733,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
fn resolve_macro_or_delegation_path<'r>(
mut self: CmResolver<'r, 'ra, 'tcx>,
ast_path: &ast::Path,
kind: Option<MacroKind>,
kind: MacroKind,
parent_scope: &ParentScope<'ra>,
trace: bool,
force: bool,
@ -747,7 +747,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
// Possibly apply the macro helper hack
if deleg_impl.is_none()
&& kind == Some(MacroKind::Bang)
&& kind == MacroKind::Bang
&& let [segment] = path.as_slice()
&& segment.ident.span.ctxt().outer_expn_data().local_inner_macros
{
@ -775,7 +775,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
};
if trace {
let kind = kind.expect("macro kind must be specified if tracing is enabled");
// FIXME: Should be an output of Speculative Resolution.
self.multi_segment_macro_resolutions.borrow_mut().push((
path,
@ -790,10 +789,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
self.prohibit_imported_non_macro_attrs(None, res.ok(), path_span);
res
} else {
let scope_set = kind.map_or(ScopeSet::All(MacroNS), ScopeSet::Macro);
let binding = self.reborrow().early_resolve_ident_in_lexical_scope(
path[0].ident,
scope_set,
ScopeSet::Macro(kind),
parent_scope,
None,
force,
@ -805,7 +803,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}
if trace {
let kind = kind.expect("macro kind must be specified if tracing is enabled");
// FIXME: Should be an output of Speculative Resolution.
self.single_segment_macro_resolutions.borrow_mut().push((
path[0].ident,

View file

@ -1,6 +1,9 @@
error[E0412]: cannot find type `S` in this scope
--> $DIR/arguments.rs:14:8
|
LL | struct S;
| - you might have meant to refer to this struct
...
LL | m!(S, S);
| ^ not found in this scope

View file

@ -3,6 +3,11 @@ error[E0422]: cannot find struct, variant or union type `MyStruct` in this scope
|
LL | let x = MyStruct {};
| ^^^^^^^^ not found in this scope
|
::: $DIR/auxiliary/use_by_macro.rs:15:1
|
LL | x!(my_struct);
| ------------- you might have meant to refer to this struct
error: aborting due to 1 previous error

View file

@ -48,7 +48,10 @@ error[E0425]: cannot find function `f` in this scope
--> $DIR/globs.rs:61:12
|
LL | n!(f);
| ----- in this macro invocation
| -----
| | |
| | you might have meant to refer to this function
| in this macro invocation
...
LL | $j();
| -- due to this macro variable
@ -56,15 +59,16 @@ LL | $j();
LL | n!(f);
| ^ not found in this scope
|
= help: consider importing this function:
foo::f
= note: this error originates in the macro `n` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0425]: cannot find function `f` in this scope
--> $DIR/globs.rs:65:17
|
LL | n!(f);
| ----- in this macro invocation
| -----
| | |
| | you might have meant to refer to this function
| in this macro invocation
...
LL | $j();
| -- due to this macro variable
@ -72,8 +76,6 @@ LL | $j();
LL | f
| ^ not found in this scope
|
= help: consider importing this function:
foo::f
= note: this error originates in the macro `n` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 4 previous errors

View file

@ -2,7 +2,13 @@ error: cannot find attribute `C` in this scope
--> $DIR/proc-macro-attributes.rs:9:3
|
LL | #[C]
| ^ help: a derive helper attribute with a similar name exists: `B`
| ^
|
help: the derive macro `B` accepts the similarly named `B` attribute
|
LL - #[C]
LL + #[B]
|
error[E0659]: `B` is ambiguous
--> $DIR/proc-macro-attributes.rs:6:3