Skip to content

[WIP] Resolver: introduce SmartResolver for speculative and finalize resolutions. #144912

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

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
8 changes: 4 additions & 4 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use crate::imports::{ImportData, ImportKind};
use crate::macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef};
use crate::{
BindingKey, ExternPreludeEntry, Finalize, MacroData, Module, ModuleKind, ModuleOrUniformRoot,
NameBinding, ParentScope, PathResult, ResolutionError, Resolver, Segment, Used,
NameBinding, ParentScope, PathResult, ResolutionError, Resolver, Segment, SmartResolver, Used,
VisResolutionError, errors,
};

Expand Down Expand Up @@ -223,7 +223,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {

pub(crate) fn build_reduced_graph_external(&self, module: Module<'ra>) {
for child in self.tcx.module_children(module.def_id()) {
let parent_scope = ParentScope::module(module, self);
let parent_scope = ParentScope::module(module, self.arenas);
self.build_reduced_graph_for_external_crate_res(child, parent_scope)
}
}
Expand Down Expand Up @@ -373,7 +373,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
res,
))
};
match self.r.resolve_path(
match SmartResolver::finalize(self.r, finalize).resolve_path(
&segments,
None,
parent_scope,
Expand Down Expand Up @@ -1128,7 +1128,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
});
} else {
for ident in single_imports.iter().cloned() {
let result = self.r.maybe_resolve_ident_in_module(
let result = self.r.smart().maybe_resolve_ident_in_module(
ModuleOrUniformRoot::Module(module),
ident,
MacroNS,
Expand Down
74 changes: 37 additions & 37 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ use crate::{
AmbiguityError, AmbiguityErrorMisc, AmbiguityKind, BindingError, BindingKey, Finalize,
ForwardGenericParamBanReason, HasGenericParams, LexicalScopeBinding, MacroRulesScope, Module,
ModuleKind, ModuleOrUniformRoot, NameBinding, NameBindingKind, ParentScope, PathResult,
PrivacyError, ResolutionError, Resolver, Scope, ScopeSet, Segment, UseError, Used,
VisResolutionError, errors as errs, path_names_to_string,
PrivacyError, ResolutionError, Resolver, Scope, ScopeSet, Segment, SmartResolver, UseError,
Used, VisResolutionError, errors as errs, path_names_to_string,
};

type Res = def::Res<ast::NodeId>;
Expand Down Expand Up @@ -467,13 +467,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {

pub(crate) fn lint_if_path_starts_with_module(
&mut self,
finalize: Option<Finalize>,
finalize: Finalize,
path: &[Segment],
second_binding: Option<NameBinding<'_>>,
) {
let Some(Finalize { node_id, root_span, .. }) = finalize else {
return;
};
let Finalize { node_id, root_span, .. } = finalize;

let first_name = match path.get(0) {
// In the 2018 edition this lint is a hard error, so nothing to do
Expand Down Expand Up @@ -1027,7 +1025,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
) -> Option<TypoSuggestion> {
let mut suggestions = Vec::new();
let ctxt = ident.span.ctxt();
self.visit_scopes(scope_set, parent_scope, ctxt, |this, scope, use_prelude, _| {
self.smart().visit_scopes(scope_set, parent_scope, ctxt, |this, scope, use_prelude, _| {
match scope {
Scope::DeriveHelpers(expn_id) => {
let res = Res::NonMacroAttr(NonMacroAttrKind::DeriveHelper);
Expand All @@ -1046,7 +1044,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
if filter_fn(res) {
for derive in parent_scope.derives {
let parent_scope = &ParentScope { derives: &[], ..*parent_scope };
let Ok((Some(ext), _)) = this.resolve_macro_path(
let Ok((Some(ext), _)) = this.reborrow().resolve_macro_path(
derive,
Some(MacroKind::Derive),
parent_scope,
Expand Down Expand Up @@ -1480,7 +1478,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
) {
// Bring imported but unused `derive` macros into `macro_map` so we ensure they can be used
// for suggestions.
self.visit_scopes(
self.smart().visit_scopes(
ScopeSet::Macro(MacroKind::Derive),
&parent_scope,
ident.span.ctxt(),
Expand Down Expand Up @@ -1589,7 +1587,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
});
}
for ns in [Namespace::MacroNS, Namespace::TypeNS, Namespace::ValueNS] {
let Ok(binding) = self.early_resolve_ident_in_lexical_scope(
let Ok(binding) = self.smart().early_resolve_ident_in_lexical_scope(
ident,
ScopeSet::All(ns),
parent_scope,
Expand Down Expand Up @@ -2269,21 +2267,22 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
if ns == TypeNS || ns == ValueNS {
let ns_to_try = if ns == TypeNS { ValueNS } else { TypeNS };
let binding = if let Some(module) = module {
self.resolve_ident_in_module(
module,
ident,
ns_to_try,
parent_scope,
None,
ignore_binding,
ignore_import,
)
.ok()
SmartResolver::Speculative(self)
.resolve_ident_in_module(
module,
ident,
ns_to_try,
parent_scope,
None,
ignore_binding,
ignore_import,
)
.ok()
} else if let Some(ribs) = ribs
&& let Some(TypeNS | ValueNS) = opt_ns
{
assert!(ignore_import.is_none());
match self.resolve_ident_in_lexical_scope(
match SmartResolver::Speculative(self).resolve_ident_in_lexical_scope(
ident,
ns_to_try,
parent_scope,
Expand All @@ -2296,16 +2295,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
_ => None,
}
} else {
self.early_resolve_ident_in_lexical_scope(
ident,
ScopeSet::All(ns_to_try),
parent_scope,
None,
false,
ignore_binding,
ignore_import,
)
.ok()
self.smart()
.early_resolve_ident_in_lexical_scope(
ident,
ScopeSet::All(ns_to_try),
parent_scope,
None,
false,
ignore_binding,
ignore_import,
)
.ok()
};
if let Some(binding) = binding {
msg = format!(
Expand All @@ -2332,7 +2332,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
// Check whether the name refers to an item in the value namespace.
let binding = if let Some(ribs) = ribs {
assert!(ignore_import.is_none());
self.resolve_ident_in_lexical_scope(
SmartResolver::Speculative(self).resolve_ident_in_lexical_scope(
ident,
ValueNS,
parent_scope,
Expand Down Expand Up @@ -2399,7 +2399,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
},
)
});
if let Ok(binding) = self.early_resolve_ident_in_lexical_scope(
if let Ok(binding) = self.smart().early_resolve_ident_in_lexical_scope(
ident,
ScopeSet::All(ValueNS),
parent_scope,
Expand Down Expand Up @@ -2529,7 +2529,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
) -> Option<(Vec<Segment>, Option<String>)> {
// Replace first ident with `self` and check if that is valid.
path[0].ident.name = kw::SelfLower;
let result = self.maybe_resolve_path(&path, None, parent_scope, None);
let result = self.smart().maybe_resolve_path(&path, None, parent_scope, None);
debug!(?path, ?result);
if let PathResult::Module(..) = result { Some((path, None)) } else { None }
}
Expand All @@ -2549,7 +2549,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
) -> Option<(Vec<Segment>, Option<String>)> {
// Replace first ident with `crate` and check if that is valid.
path[0].ident.name = kw::Crate;
let result = self.maybe_resolve_path(&path, None, parent_scope, None);
let result = self.smart().maybe_resolve_path(&path, None, parent_scope, None);
debug!(?path, ?result);
if let PathResult::Module(..) = result {
Some((
Expand Down Expand Up @@ -2581,7 +2581,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
) -> Option<(Vec<Segment>, Option<String>)> {
// Replace first ident with `crate` and check if that is valid.
path[0].ident.name = kw::Super;
let result = self.maybe_resolve_path(&path, None, parent_scope, None);
let result = self.smart().maybe_resolve_path(&path, None, parent_scope, None);
debug!(?path, ?result);
if let PathResult::Module(..) = result { Some((path, None)) } else { None }
}
Expand Down Expand Up @@ -2616,7 +2616,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
for name in extern_crate_names.into_iter() {
// Replace first ident with a crate name and check if that is valid.
path[0].ident.name = name;
let result = self.maybe_resolve_path(&path, None, parent_scope, None);
let result = self.smart().maybe_resolve_path(&path, None, parent_scope, None);
debug!(?path, ?name, ?result);
if let PathResult::Module(..) = result {
return Some((path, None));
Expand Down
Loading
Loading