Skip to content

resolve: Miscellaneous cleanups #145372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 19 additions & 29 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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() {
Expand All @@ -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(
Expand Down Expand Up @@ -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()));
Expand Down
7 changes: 1 addition & 6 deletions compiler/rustc_resolve/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 5 additions & 17 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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>,
Expand All @@ -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)
Expand Down
51 changes: 18 additions & 33 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;
Expand Down Expand Up @@ -2458,45 +2458,30 @@ 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.
let mod_path = &path[..path.len() - 1];
Expand Down
15 changes: 6 additions & 9 deletions compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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
{
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/hygiene/arguments.stderr
Original file line number Diff line number Diff line change
@@ -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

Expand Down
5 changes: 5 additions & 0 deletions tests/ui/hygiene/cross-crate-name-hiding-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 8 additions & 6 deletions tests/ui/hygiene/globs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,34 @@ 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
...
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
...
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
Expand Down
8 changes: 7 additions & 1 deletion tests/ui/proc-macro/proc-macro-attributes.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading