Skip to content

Commit

Permalink
Auto merge of #132761 - nnethercote:resolve-tweaks, r=<try>
Browse files Browse the repository at this point in the history
Resolve tweaks

r? `@ghost`
  • Loading branch information
bors committed Nov 8, 2024
2 parents 78bb5ee + 8e22596 commit 4e79a44
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 88 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ impl<'a> Parser<'a> {

if case == Case::Insensitive
&& let Some((ident, IdentIsRaw::No)) = self.token.ident()
&& ident.as_str().to_lowercase() == kw.as_str().to_lowercase()
&& ident.as_str().eq_ignore_ascii_case(kw.as_str())
{
true
} else {
Expand Down
12 changes: 5 additions & 7 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,14 +535,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
filter_fn: &impl Fn(Res) -> bool,
ctxt: Option<SyntaxContext>,
) {
for (key, resolution) in self.resolutions(module).borrow().iter() {
if let Some(binding) = resolution.borrow().binding {
let res = binding.res();
if filter_fn(res) && ctxt.map_or(true, |ctxt| ctxt == key.ident.span.ctxt()) {
names.push(TypoSuggestion::typo_from_ident(key.ident, res));
}
module.for_each_child(self, |_this, ident, _ns, binding| {
let res = binding.res();
if filter_fn(res) && ctxt.map_or(true, |ctxt| ctxt == ident.span.ctxt()) {
names.push(TypoSuggestion::typo_from_ident(ident, res));
}
}
});
}

/// Combines an error with provided span and emits it.
Expand Down
84 changes: 21 additions & 63 deletions compiler/rustc_resolve/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,13 +311,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {

// Walk backwards up the ribs in scope.
let mut module = self.graph_root;
for i in (0..ribs.len()).rev() {
debug!("walk rib\n{:?}", ribs[i].bindings);
for (i, rib) in ribs.iter().enumerate().rev() {
debug!("walk rib\n{:?}", rib.bindings);
// Use the rib kind to determine whether we are resolving parameters
// (macro 2.0 hygiene) or local variables (`macro_rules` hygiene).
let rib_ident = if ribs[i].kind.contains_params() { normalized_ident } else { ident };
if let Some((original_rib_ident_def, res)) = ribs[i].bindings.get_key_value(&rib_ident)
{
let rib_ident = if rib.kind.contains_params() { normalized_ident } else { ident };
if let Some((original_rib_ident_def, res)) = rib.bindings.get_key_value(&rib_ident) {
// The ident resolves to a type parameter or local variable.
return Some(LexicalScopeBinding::Res(self.validate_res_from_ribs(
i,
Expand All @@ -329,7 +328,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
)));
}

module = match ribs[i].kind {
module = match rib.kind {
RibKind::Module(module) => module,
RibKind::MacroDefinition(def) if def == self.macro_def(ident.span.ctxt()) => {
// If an invocation of this macro created `ident`, give up on `ident`
Expand All @@ -350,6 +349,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
ident,
ns,
parent_scope,
false,
finalize.map(|finalize| Finalize { used: Used::Scope, ..finalize }),
ignore_binding,
None,
Expand Down Expand Up @@ -494,7 +494,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
Scope::CrateRoot => {
let root_ident = Ident::new(kw::PathRoot, ident.span);
let root_module = this.resolve_crate_root(root_ident);
let binding = this.resolve_ident_in_module_ext(
let binding = this.resolve_ident_in_module(
ModuleOrUniformRoot::Module(root_module),
ident,
ns,
Expand All @@ -516,7 +516,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}
Scope::Module(module, derive_fallback_lint_id) => {
let adjusted_parent_scope = &ParentScope { module, ..*parent_scope };
let binding = this.resolve_ident_in_module_unadjusted_ext(
let binding = this.resolve_ident_in_module_unadjusted(
ModuleOrUniformRoot::Module(module),
ident,
ns,
Expand Down Expand Up @@ -590,6 +590,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
ident,
ns,
parent_scope,
false,
None,
ignore_binding,
ignore_import,
Expand Down Expand Up @@ -748,35 +749,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
parent_scope: &ParentScope<'ra>,
ignore_import: Option<Import<'ra>>,
) -> Result<NameBinding<'ra>, Determinacy> {
self.resolve_ident_in_module_ext(module, ident, ns, parent_scope, None, None, ignore_import)
self.resolve_ident_in_module(module, ident, ns, parent_scope, None, None, ignore_import)
.map_err(|(determinacy, _)| determinacy)
}

#[instrument(level = "debug", skip(self))]
pub(crate) fn resolve_ident_in_module(
&mut self,
module: ModuleOrUniformRoot<'ra>,
ident: Ident,
ns: Namespace,
parent_scope: &ParentScope<'ra>,
finalize: Option<Finalize>,
ignore_binding: Option<NameBinding<'ra>>,
ignore_import: Option<Import<'ra>>,
) -> Result<NameBinding<'ra>, Determinacy> {
self.resolve_ident_in_module_ext(
module,
ident,
ns,
parent_scope,
finalize,
ignore_binding,
ignore_import,
)
.map_err(|(determinacy, _)| determinacy)
}

#[instrument(level = "debug", skip(self))]
fn resolve_ident_in_module_ext(
&mut self,
module: ModuleOrUniformRoot<'ra>,
mut ident: Ident,
Expand All @@ -803,7 +781,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
// No adjustments
}
}
self.resolve_ident_in_module_unadjusted_ext(
self.resolve_ident_in_module_unadjusted(
module,
ident,
ns,
Expand All @@ -815,34 +793,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
)
}

#[instrument(level = "debug", skip(self))]
fn resolve_ident_in_module_unadjusted(
&mut self,
module: ModuleOrUniformRoot<'ra>,
ident: Ident,
ns: Namespace,
parent_scope: &ParentScope<'ra>,
finalize: Option<Finalize>,
ignore_binding: Option<NameBinding<'ra>>,
ignore_import: Option<Import<'ra>>,
) -> Result<NameBinding<'ra>, Determinacy> {
self.resolve_ident_in_module_unadjusted_ext(
module,
ident,
ns,
parent_scope,
false,
finalize,
ignore_binding,
ignore_import,
)
.map_err(|(determinacy, _)| determinacy)
}

/// Attempts to resolve `ident` in namespaces `ns` of `module`.
/// Invariant: if `finalize` is `Some`, expansion and import resolution must be complete.
#[instrument(level = "debug", skip(self))]
fn resolve_ident_in_module_unadjusted_ext(
fn resolve_ident_in_module_unadjusted(
&mut self,
module: ModuleOrUniformRoot<'ra>,
ident: Ident,
Expand Down Expand Up @@ -1047,13 +1001,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
ignore_binding,
ignore_import,
) {
Err(Determined) => continue,
Err((Determined, _)) => continue,
Ok(binding)
if !self.is_accessible_from(binding.vis, single_import.parent_scope.module) =>
{
continue;
}
Ok(_) | Err(Undetermined) => return Err((Undetermined, Weak::No)),
Ok(_) | Err((Undetermined, _)) => return Err((Undetermined, Weak::No)),
}
}

Expand Down Expand Up @@ -1122,19 +1076,20 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
ident,
ns,
adjusted_parent_scope,
false,
None,
ignore_binding,
ignore_import,
);

match result {
Err(Determined) => continue,
Err((Determined, _)) => continue,
Ok(binding)
if !self.is_accessible_from(binding.vis, glob_import.parent_scope.module) =>
{
continue;
}
Ok(_) | Err(Undetermined) => return Err((Undetermined, Weak::Yes)),
Ok(_) | Err((Undetermined, _)) => return Err((Undetermined, Weak::Yes)),
}
}

Expand Down Expand Up @@ -1200,7 +1155,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
// Still doesn't deal with upvars
if let Some(span) = finalize {
let (span, resolution_error) = match item {
None if rib_ident.as_str() == "self" => (span, LowercaseSelf),
None if rib_ident.name == kw::SelfLower => {
(span, LowercaseSelf)
}
None => {
// If we have a `let name = expr;`, we have the span for
// `name` and use that to see if it is followed by a type
Expand Down Expand Up @@ -1563,6 +1520,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
ignore_binding,
ignore_import,
)
.map_err(|(determinacy, _)| determinacy)
} else if let Some(ribs) = ribs
&& let Some(TypeNS | ValueNS) = opt_ns
{
Expand Down
9 changes: 4 additions & 5 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use rustc_session::lint::{self, BuiltinLintDiag};
use rustc_session::parse::feature_err;
use rustc_span::source_map::{Spanned, respan};
use rustc_span::symbol::{Ident, Symbol, kw, sym};
use rustc_span::{BytePos, Span, SyntaxContext};
use rustc_span::{BytePos, Span};
use smallvec::{SmallVec, smallvec};
use tracing::{debug, instrument, trace};

Expand Down Expand Up @@ -1688,9 +1688,9 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
}
}

let normalized_ident = ident.normalize_to_macros_2_0();
let mut outer_res = None;
for rib in lifetime_rib_iter {
let normalized_ident = ident.normalize_to_macros_2_0();
if let Some((&outer, _)) = rib.bindings.get_key_value(&normalized_ident) {
outer_res = Some(outer);
break;
Expand Down Expand Up @@ -4776,8 +4776,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
self.r.traits_in_scope(
self.current_trait_ref.as_ref().map(|(module, _)| *module),
&self.parent_scope,
ident.span.ctxt(),
Some((ident.name, ns)),
Some((ident, ns)),
)
}

Expand Down Expand Up @@ -4869,7 +4868,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
.entry(self.parent_scope.module.nearest_parent_mod().expect_local())
.or_insert_with(|| {
self.r
.traits_in_scope(None, &self.parent_scope, SyntaxContext::root(), None)
.traits_in_scope(None, &self.parent_scope, None)
.into_iter()
.filter_map(|tr| {
if !tr.def_id.is_local()
Expand Down
27 changes: 15 additions & 12 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,8 @@ struct BindingKey {
/// identifier.
ident: Ident,
ns: Namespace,
/// 0 if ident is not `_`, otherwise a value that's unique to the specific
/// `_` in the expanded AST that introduced this binding.
/// 0 if ident is not `_` or ``, otherwise a value that's unique to the specific
/// `_`/`` in the expanded AST that introduced this binding.
disambiguator: u32,
}

Expand Down Expand Up @@ -1750,8 +1750,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
&mut self,
current_trait: Option<Module<'ra>>,
parent_scope: &ParentScope<'ra>,
ctxt: SyntaxContext,
assoc_item: Option<(Symbol, Namespace)>,
assoc_item: Option<(Ident, Namespace)>,
) -> Vec<TraitCandidate> {
let mut found_traits = Vec::new();

Expand All @@ -1762,6 +1761,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}
}

let ctxt = if let Some((ident, _)) = assoc_item {
ident.span.ctxt()
} else {
SyntaxContext::root()
};
self.visit_scopes(ScopeSet::All(TypeNS), parent_scope, ctxt, |this, scope, _, _| {
match scope {
Scope::Module(module, _) => {
Expand All @@ -1784,7 +1788,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
fn traits_in_module(
&mut self,
module: Module<'ra>,
assoc_item: Option<(Symbol, Namespace)>,
assoc_item: Option<(Ident, Namespace)>,
found_traits: &mut Vec<TraitCandidate>,
) {
module.ensure_traits(self);
Expand All @@ -1806,15 +1810,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
fn trait_may_have_item(
&mut self,
trait_module: Option<Module<'ra>>,
assoc_item: Option<(Symbol, Namespace)>,
assoc_item: Option<(Ident, Namespace)>,
) -> bool {
match (trait_module, assoc_item) {
(Some(trait_module), Some((name, ns))) => {
self.resolutions(trait_module).borrow().iter().any(|resolution| {
let (&BindingKey { ident: assoc_ident, ns: assoc_ns, .. }, _) = resolution;
assoc_ns == ns && assoc_ident.name == name
})
}
(Some(trait_module), Some((ident, ns))) => self
.resolutions(trait_module)
.borrow()
.iter()
.any(|(key, _name_resolution)| key.ns == ns && key.ident.name == ident.name),
_ => true,
}
}
Expand Down

0 comments on commit 4e79a44

Please sign in to comment.