Skip to content

Commit 4e79a44

Browse files
committed
Auto merge of rust-lang#132761 - nnethercote:resolve-tweaks, r=<try>
Resolve tweaks r? `@ghost`
2 parents 78bb5ee + 8e22596 commit 4e79a44

File tree

5 files changed

+46
-88
lines changed

5 files changed

+46
-88
lines changed

compiler/rustc_parse/src/parser/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ impl<'a> Parser<'a> {
643643

644644
if case == Case::Insensitive
645645
&& let Some((ident, IdentIsRaw::No)) = self.token.ident()
646-
&& ident.as_str().to_lowercase() == kw.as_str().to_lowercase()
646+
&& ident.as_str().eq_ignore_ascii_case(kw.as_str())
647647
{
648648
true
649649
} else {

compiler/rustc_resolve/src/diagnostics.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -535,14 +535,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
535535
filter_fn: &impl Fn(Res) -> bool,
536536
ctxt: Option<SyntaxContext>,
537537
) {
538-
for (key, resolution) in self.resolutions(module).borrow().iter() {
539-
if let Some(binding) = resolution.borrow().binding {
540-
let res = binding.res();
541-
if filter_fn(res) && ctxt.map_or(true, |ctxt| ctxt == key.ident.span.ctxt()) {
542-
names.push(TypoSuggestion::typo_from_ident(key.ident, res));
543-
}
538+
module.for_each_child(self, |_this, ident, _ns, binding| {
539+
let res = binding.res();
540+
if filter_fn(res) && ctxt.map_or(true, |ctxt| ctxt == ident.span.ctxt()) {
541+
names.push(TypoSuggestion::typo_from_ident(ident, res));
544542
}
545-
}
543+
});
546544
}
547545

548546
/// Combines an error with provided span and emits it.

compiler/rustc_resolve/src/ident.rs

+21-63
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
311311

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

332-
module = match ribs[i].kind {
331+
module = match rib.kind {
333332
RibKind::Module(module) => module,
334333
RibKind::MacroDefinition(def) if def == self.macro_def(ident.span.ctxt()) => {
335334
// If an invocation of this macro created `ident`, give up on `ident`
@@ -350,6 +349,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
350349
ident,
351350
ns,
352351
parent_scope,
352+
false,
353353
finalize.map(|finalize| Finalize { used: Used::Scope, ..finalize }),
354354
ignore_binding,
355355
None,
@@ -494,7 +494,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
494494
Scope::CrateRoot => {
495495
let root_ident = Ident::new(kw::PathRoot, ident.span);
496496
let root_module = this.resolve_crate_root(root_ident);
497-
let binding = this.resolve_ident_in_module_ext(
497+
let binding = this.resolve_ident_in_module(
498498
ModuleOrUniformRoot::Module(root_module),
499499
ident,
500500
ns,
@@ -516,7 +516,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
516516
}
517517
Scope::Module(module, derive_fallback_lint_id) => {
518518
let adjusted_parent_scope = &ParentScope { module, ..*parent_scope };
519-
let binding = this.resolve_ident_in_module_unadjusted_ext(
519+
let binding = this.resolve_ident_in_module_unadjusted(
520520
ModuleOrUniformRoot::Module(module),
521521
ident,
522522
ns,
@@ -590,6 +590,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
590590
ident,
591591
ns,
592592
parent_scope,
593+
false,
593594
None,
594595
ignore_binding,
595596
ignore_import,
@@ -748,35 +749,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
748749
parent_scope: &ParentScope<'ra>,
749750
ignore_import: Option<Import<'ra>>,
750751
) -> Result<NameBinding<'ra>, Determinacy> {
751-
self.resolve_ident_in_module_ext(module, ident, ns, parent_scope, None, None, ignore_import)
752+
self.resolve_ident_in_module(module, ident, ns, parent_scope, None, None, ignore_import)
752753
.map_err(|(determinacy, _)| determinacy)
753754
}
754755

755756
#[instrument(level = "debug", skip(self))]
756757
pub(crate) fn resolve_ident_in_module(
757-
&mut self,
758-
module: ModuleOrUniformRoot<'ra>,
759-
ident: Ident,
760-
ns: Namespace,
761-
parent_scope: &ParentScope<'ra>,
762-
finalize: Option<Finalize>,
763-
ignore_binding: Option<NameBinding<'ra>>,
764-
ignore_import: Option<Import<'ra>>,
765-
) -> Result<NameBinding<'ra>, Determinacy> {
766-
self.resolve_ident_in_module_ext(
767-
module,
768-
ident,
769-
ns,
770-
parent_scope,
771-
finalize,
772-
ignore_binding,
773-
ignore_import,
774-
)
775-
.map_err(|(determinacy, _)| determinacy)
776-
}
777-
778-
#[instrument(level = "debug", skip(self))]
779-
fn resolve_ident_in_module_ext(
780758
&mut self,
781759
module: ModuleOrUniformRoot<'ra>,
782760
mut ident: Ident,
@@ -803,7 +781,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
803781
// No adjustments
804782
}
805783
}
806-
self.resolve_ident_in_module_unadjusted_ext(
784+
self.resolve_ident_in_module_unadjusted(
807785
module,
808786
ident,
809787
ns,
@@ -815,34 +793,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
815793
)
816794
}
817795

818-
#[instrument(level = "debug", skip(self))]
819-
fn resolve_ident_in_module_unadjusted(
820-
&mut self,
821-
module: ModuleOrUniformRoot<'ra>,
822-
ident: Ident,
823-
ns: Namespace,
824-
parent_scope: &ParentScope<'ra>,
825-
finalize: Option<Finalize>,
826-
ignore_binding: Option<NameBinding<'ra>>,
827-
ignore_import: Option<Import<'ra>>,
828-
) -> Result<NameBinding<'ra>, Determinacy> {
829-
self.resolve_ident_in_module_unadjusted_ext(
830-
module,
831-
ident,
832-
ns,
833-
parent_scope,
834-
false,
835-
finalize,
836-
ignore_binding,
837-
ignore_import,
838-
)
839-
.map_err(|(determinacy, _)| determinacy)
840-
}
841-
842796
/// Attempts to resolve `ident` in namespaces `ns` of `module`.
843797
/// Invariant: if `finalize` is `Some`, expansion and import resolution must be complete.
844798
#[instrument(level = "debug", skip(self))]
845-
fn resolve_ident_in_module_unadjusted_ext(
799+
fn resolve_ident_in_module_unadjusted(
846800
&mut self,
847801
module: ModuleOrUniformRoot<'ra>,
848802
ident: Ident,
@@ -1047,13 +1001,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10471001
ignore_binding,
10481002
ignore_import,
10491003
) {
1050-
Err(Determined) => continue,
1004+
Err((Determined, _)) => continue,
10511005
Ok(binding)
10521006
if !self.is_accessible_from(binding.vis, single_import.parent_scope.module) =>
10531007
{
10541008
continue;
10551009
}
1056-
Ok(_) | Err(Undetermined) => return Err((Undetermined, Weak::No)),
1010+
Ok(_) | Err((Undetermined, _)) => return Err((Undetermined, Weak::No)),
10571011
}
10581012
}
10591013

@@ -1122,19 +1076,20 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11221076
ident,
11231077
ns,
11241078
adjusted_parent_scope,
1079+
false,
11251080
None,
11261081
ignore_binding,
11271082
ignore_import,
11281083
);
11291084

11301085
match result {
1131-
Err(Determined) => continue,
1086+
Err((Determined, _)) => continue,
11321087
Ok(binding)
11331088
if !self.is_accessible_from(binding.vis, glob_import.parent_scope.module) =>
11341089
{
11351090
continue;
11361091
}
1137-
Ok(_) | Err(Undetermined) => return Err((Undetermined, Weak::Yes)),
1092+
Ok(_) | Err((Undetermined, _)) => return Err((Undetermined, Weak::Yes)),
11381093
}
11391094
}
11401095

@@ -1200,7 +1155,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12001155
// Still doesn't deal with upvars
12011156
if let Some(span) = finalize {
12021157
let (span, resolution_error) = match item {
1203-
None if rib_ident.as_str() == "self" => (span, LowercaseSelf),
1158+
None if rib_ident.name == kw::SelfLower => {
1159+
(span, LowercaseSelf)
1160+
}
12041161
None => {
12051162
// If we have a `let name = expr;`, we have the span for
12061163
// `name` and use that to see if it is followed by a type
@@ -1563,6 +1520,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15631520
ignore_binding,
15641521
ignore_import,
15651522
)
1523+
.map_err(|(determinacy, _)| determinacy)
15661524
} else if let Some(ribs) = ribs
15671525
&& let Some(TypeNS | ValueNS) = opt_ns
15681526
{

compiler/rustc_resolve/src/late.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use rustc_session::lint::{self, BuiltinLintDiag};
3030
use rustc_session::parse::feature_err;
3131
use rustc_span::source_map::{Spanned, respan};
3232
use rustc_span::symbol::{Ident, Symbol, kw, sym};
33-
use rustc_span::{BytePos, Span, SyntaxContext};
33+
use rustc_span::{BytePos, Span};
3434
use smallvec::{SmallVec, smallvec};
3535
use tracing::{debug, instrument, trace};
3636

@@ -1688,9 +1688,9 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
16881688
}
16891689
}
16901690

1691+
let normalized_ident = ident.normalize_to_macros_2_0();
16911692
let mut outer_res = None;
16921693
for rib in lifetime_rib_iter {
1693-
let normalized_ident = ident.normalize_to_macros_2_0();
16941694
if let Some((&outer, _)) = rib.bindings.get_key_value(&normalized_ident) {
16951695
outer_res = Some(outer);
16961696
break;
@@ -4776,8 +4776,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
47764776
self.r.traits_in_scope(
47774777
self.current_trait_ref.as_ref().map(|(module, _)| *module),
47784778
&self.parent_scope,
4779-
ident.span.ctxt(),
4780-
Some((ident.name, ns)),
4779+
Some((ident, ns)),
47814780
)
47824781
}
47834782

@@ -4869,7 +4868,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
48694868
.entry(self.parent_scope.module.nearest_parent_mod().expect_local())
48704869
.or_insert_with(|| {
48714870
self.r
4872-
.traits_in_scope(None, &self.parent_scope, SyntaxContext::root(), None)
4871+
.traits_in_scope(None, &self.parent_scope, None)
48734872
.into_iter()
48744873
.filter_map(|tr| {
48754874
if !tr.def_id.is_local()

compiler/rustc_resolve/src/lib.rs

+15-12
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,8 @@ struct BindingKey {
534534
/// identifier.
535535
ident: Ident,
536536
ns: Namespace,
537-
/// 0 if ident is not `_`, otherwise a value that's unique to the specific
538-
/// `_` in the expanded AST that introduced this binding.
537+
/// 0 if ident is not `_` or ``, otherwise a value that's unique to the specific
538+
/// `_`/`` in the expanded AST that introduced this binding.
539539
disambiguator: u32,
540540
}
541541

@@ -1750,8 +1750,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17501750
&mut self,
17511751
current_trait: Option<Module<'ra>>,
17521752
parent_scope: &ParentScope<'ra>,
1753-
ctxt: SyntaxContext,
1754-
assoc_item: Option<(Symbol, Namespace)>,
1753+
assoc_item: Option<(Ident, Namespace)>,
17551754
) -> Vec<TraitCandidate> {
17561755
let mut found_traits = Vec::new();
17571756

@@ -1762,6 +1761,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17621761
}
17631762
}
17641763

1764+
let ctxt = if let Some((ident, _)) = assoc_item {
1765+
ident.span.ctxt()
1766+
} else {
1767+
SyntaxContext::root()
1768+
};
17651769
self.visit_scopes(ScopeSet::All(TypeNS), parent_scope, ctxt, |this, scope, _, _| {
17661770
match scope {
17671771
Scope::Module(module, _) => {
@@ -1784,7 +1788,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
17841788
fn traits_in_module(
17851789
&mut self,
17861790
module: Module<'ra>,
1787-
assoc_item: Option<(Symbol, Namespace)>,
1791+
assoc_item: Option<(Ident, Namespace)>,
17881792
found_traits: &mut Vec<TraitCandidate>,
17891793
) {
17901794
module.ensure_traits(self);
@@ -1806,15 +1810,14 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18061810
fn trait_may_have_item(
18071811
&mut self,
18081812
trait_module: Option<Module<'ra>>,
1809-
assoc_item: Option<(Symbol, Namespace)>,
1813+
assoc_item: Option<(Ident, Namespace)>,
18101814
) -> bool {
18111815
match (trait_module, assoc_item) {
1812-
(Some(trait_module), Some((name, ns))) => {
1813-
self.resolutions(trait_module).borrow().iter().any(|resolution| {
1814-
let (&BindingKey { ident: assoc_ident, ns: assoc_ns, .. }, _) = resolution;
1815-
assoc_ns == ns && assoc_ident.name == name
1816-
})
1817-
}
1816+
(Some(trait_module), Some((ident, ns))) => self
1817+
.resolutions(trait_module)
1818+
.borrow()
1819+
.iter()
1820+
.any(|(key, _name_resolution)| key.ns == ns && key.ident.name == ident.name),
18181821
_ => true,
18191822
}
18201823
}

0 commit comments

Comments
 (0)