Skip to content

Commit ee612c4

Browse files
committed
Auto merge of #132761 - nnethercote:resolve-tweaks, r=petrochenkov
Resolve tweaks A couple of small perf improvements, and some minor refactorings, all in `rustc_resolve`. r? `@petrochenkov`
2 parents 7899368 + 12747f1 commit ee612c4

File tree

5 files changed

+53
-93
lines changed

5 files changed

+53
-93
lines changed

compiler/rustc_parse/src/parser/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,10 @@ impl<'a> Parser<'a> {
641641
return true;
642642
}
643643

644+
// Do an ASCII case-insensitive match, because all keywords are ASCII.
644645
if case == Case::Insensitive
645646
&& let Some((ident, IdentIsRaw::No)) = self.token.ident()
646-
&& ident.as_str().to_lowercase() == kw.as_str().to_lowercase()
647+
&& ident.as_str().eq_ignore_ascii_case(kw.as_str())
647648
{
648649
true
649650
} 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

+40-72
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ impl From<UsePrelude> for bool {
3838
}
3939
}
4040

41+
#[derive(Debug, PartialEq)]
42+
enum Shadowing {
43+
Restricted,
44+
Unrestricted,
45+
}
46+
4147
impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
4248
/// A generic scope visitor.
4349
/// Visits scopes in order to resolve some identifier in them or perform other actions.
@@ -311,13 +317,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
311317

312318
// Walk backwards up the ribs in scope.
313319
let mut module = self.graph_root;
314-
for i in (0..ribs.len()).rev() {
315-
debug!("walk rib\n{:?}", ribs[i].bindings);
320+
for (i, rib) in ribs.iter().enumerate().rev() {
321+
debug!("walk rib\n{:?}", rib.bindings);
316322
// Use the rib kind to determine whether we are resolving parameters
317323
// (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-
{
324+
let rib_ident = if rib.kind.contains_params() { normalized_ident } else { ident };
325+
if let Some((original_rib_ident_def, res)) = rib.bindings.get_key_value(&rib_ident) {
321326
// The ident resolves to a type parameter or local variable.
322327
return Some(LexicalScopeBinding::Res(self.validate_res_from_ribs(
323328
i,
@@ -329,7 +334,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
329334
)));
330335
}
331336

332-
module = match ribs[i].kind {
337+
module = match rib.kind {
333338
RibKind::Module(module) => module,
334339
RibKind::MacroDefinition(def) if def == self.macro_def(ident.span.ctxt()) => {
335340
// If an invocation of this macro created `ident`, give up on `ident`
@@ -350,6 +355,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
350355
ident,
351356
ns,
352357
parent_scope,
358+
Shadowing::Unrestricted,
353359
finalize.map(|finalize| Finalize { used: Used::Scope, ..finalize }),
354360
ignore_binding,
355361
None,
@@ -494,7 +500,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
494500
Scope::CrateRoot => {
495501
let root_ident = Ident::new(kw::PathRoot, ident.span);
496502
let root_module = this.resolve_crate_root(root_ident);
497-
let binding = this.resolve_ident_in_module_ext(
503+
let binding = this.resolve_ident_in_module(
498504
ModuleOrUniformRoot::Module(root_module),
499505
ident,
500506
ns,
@@ -516,12 +522,16 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
516522
}
517523
Scope::Module(module, derive_fallback_lint_id) => {
518524
let adjusted_parent_scope = &ParentScope { module, ..*parent_scope };
519-
let binding = this.resolve_ident_in_module_unadjusted_ext(
525+
let binding = this.resolve_ident_in_module_unadjusted(
520526
ModuleOrUniformRoot::Module(module),
521527
ident,
522528
ns,
523529
adjusted_parent_scope,
524-
!matches!(scope_set, ScopeSet::Late(..)),
530+
if matches!(scope_set, ScopeSet::Late(..)) {
531+
Shadowing::Unrestricted
532+
} else {
533+
Shadowing::Restricted
534+
},
525535
finalize.map(|finalize| Finalize { used: Used::Scope, ..finalize }),
526536
ignore_binding,
527537
ignore_import,
@@ -590,6 +600,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
590600
ident,
591601
ns,
592602
parent_scope,
603+
Shadowing::Unrestricted,
593604
None,
594605
ignore_binding,
595606
ignore_import,
@@ -748,35 +759,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
748759
parent_scope: &ParentScope<'ra>,
749760
ignore_import: Option<Import<'ra>>,
750761
) -> Result<NameBinding<'ra>, Determinacy> {
751-
self.resolve_ident_in_module_ext(module, ident, ns, parent_scope, None, None, ignore_import)
762+
self.resolve_ident_in_module(module, ident, ns, parent_scope, None, None, ignore_import)
752763
.map_err(|(determinacy, _)| determinacy)
753764
}
754765

755766
#[instrument(level = "debug", skip(self))]
756767
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(
780768
&mut self,
781769
module: ModuleOrUniformRoot<'ra>,
782770
mut ident: Ident,
@@ -803,52 +791,28 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
803791
// No adjustments
804792
}
805793
}
806-
self.resolve_ident_in_module_unadjusted_ext(
794+
self.resolve_ident_in_module_unadjusted(
807795
module,
808796
ident,
809797
ns,
810798
adjusted_parent_scope,
811-
false,
799+
Shadowing::Unrestricted,
812800
finalize,
813801
ignore_binding,
814802
ignore_import,
815803
)
816804
}
817805

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-
842806
/// Attempts to resolve `ident` in namespaces `ns` of `module`.
843807
/// Invariant: if `finalize` is `Some`, expansion and import resolution must be complete.
844808
#[instrument(level = "debug", skip(self))]
845-
fn resolve_ident_in_module_unadjusted_ext(
809+
fn resolve_ident_in_module_unadjusted(
846810
&mut self,
847811
module: ModuleOrUniformRoot<'ra>,
848812
ident: Ident,
849813
ns: Namespace,
850814
parent_scope: &ParentScope<'ra>,
851-
restricted_shadowing: bool,
815+
shadowing: Shadowing,
852816
finalize: Option<Finalize>,
853817
// This binding should be ignored during in-module resolution, so that we don't get
854818
// "self-confirming" import resolutions during import validation and checking.
@@ -858,7 +822,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
858822
let module = match module {
859823
ModuleOrUniformRoot::Module(module) => module,
860824
ModuleOrUniformRoot::CrateRootAndExternPrelude => {
861-
assert!(!restricted_shadowing);
825+
assert_eq!(shadowing, Shadowing::Unrestricted);
862826
let binding = self.early_resolve_ident_in_lexical_scope(
863827
ident,
864828
ScopeSet::AbsolutePath(ns),
@@ -871,7 +835,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
871835
return binding.map_err(|determinacy| (determinacy, Weak::No));
872836
}
873837
ModuleOrUniformRoot::ExternPrelude => {
874-
assert!(!restricted_shadowing);
838+
assert_eq!(shadowing, Shadowing::Unrestricted);
875839
return if ns != TypeNS {
876840
Err((Determined, Weak::No))
877841
} else if let Some(binding) = self.extern_prelude_get(ident, finalize.is_some()) {
@@ -884,7 +848,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
884848
};
885849
}
886850
ModuleOrUniformRoot::CurrentScope => {
887-
assert!(!restricted_shadowing);
851+
assert_eq!(shadowing, Shadowing::Unrestricted);
888852
if ns == TypeNS {
889853
if ident.name == kw::Crate || ident.name == kw::DollarCrate {
890854
let module = self.resolve_crate_root(ident);
@@ -943,7 +907,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
943907

944908
// Forbid expanded shadowing to avoid time travel.
945909
if let Some(shadowed_glob) = resolution.shadowed_glob
946-
&& restricted_shadowing
910+
&& shadowing == Shadowing::Restricted
947911
&& binding.expansion != LocalExpnId::ROOT
948912
&& binding.res() != shadowed_glob.res()
949913
{
@@ -958,7 +922,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
958922
});
959923
}
960924

961-
if !restricted_shadowing
925+
if shadowing == Shadowing::Unrestricted
962926
&& binding.expansion != LocalExpnId::ROOT
963927
&& let NameBindingKind::Import { import, .. } = binding.kind
964928
&& matches!(import.kind, ImportKind::MacroExport)
@@ -1047,13 +1011,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10471011
ignore_binding,
10481012
ignore_import,
10491013
) {
1050-
Err(Determined) => continue,
1014+
Err((Determined, _)) => continue,
10511015
Ok(binding)
10521016
if !self.is_accessible_from(binding.vis, single_import.parent_scope.module) =>
10531017
{
10541018
continue;
10551019
}
1056-
Ok(_) | Err(Undetermined) => return Err((Undetermined, Weak::No)),
1020+
Ok(_) | Err((Undetermined, _)) => return Err((Undetermined, Weak::No)),
10571021
}
10581022
}
10591023

@@ -1070,7 +1034,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10701034
// and prohibit access to macro-expanded `macro_export` macros instead (unless restricted
10711035
// shadowing is enabled, see `macro_expanded_macro_export_errors`).
10721036
if let Some(binding) = binding {
1073-
if binding.determined() || ns == MacroNS || restricted_shadowing {
1037+
if binding.determined() || ns == MacroNS || shadowing == Shadowing::Restricted {
10741038
return check_usable(self, binding);
10751039
} else {
10761040
return Err((Undetermined, Weak::No));
@@ -1122,19 +1086,20 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11221086
ident,
11231087
ns,
11241088
adjusted_parent_scope,
1089+
Shadowing::Unrestricted,
11251090
None,
11261091
ignore_binding,
11271092
ignore_import,
11281093
);
11291094

11301095
match result {
1131-
Err(Determined) => continue,
1096+
Err((Determined, _)) => continue,
11321097
Ok(binding)
11331098
if !self.is_accessible_from(binding.vis, glob_import.parent_scope.module) =>
11341099
{
11351100
continue;
11361101
}
1137-
Ok(_) | Err(Undetermined) => return Err((Undetermined, Weak::Yes)),
1102+
Ok(_) | Err((Undetermined, _)) => return Err((Undetermined, Weak::Yes)),
11381103
}
11391104
}
11401105

@@ -1200,7 +1165,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12001165
// Still doesn't deal with upvars
12011166
if let Some(span) = finalize {
12021167
let (span, resolution_error) = match item {
1203-
None if rib_ident.as_str() == "self" => (span, LowercaseSelf),
1168+
None if rib_ident.name == kw::SelfLower => {
1169+
(span, LowercaseSelf)
1170+
}
12041171
None => {
12051172
// If we have a `let name = expr;`, we have the span for
12061173
// `name` and use that to see if it is followed by a type
@@ -1563,6 +1530,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15631530
ignore_binding,
15641531
ignore_import,
15651532
)
1533+
.map_err(|(determinacy, _)| determinacy)
15661534
} else if let Some(ribs) = ribs
15671535
&& let Some(TypeNS | ValueNS) = opt_ns
15681536
{

compiler/rustc_resolve/src/late.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -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;

compiler/rustc_resolve/src/lib.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -1082,8 +1082,6 @@ pub struct Resolver<'ra, 'tcx> {
10821082
binding_parent_modules: FxHashMap<NameBinding<'ra>, Module<'ra>>,
10831083

10841084
underscore_disambiguator: u32,
1085-
/// Disambiguator for anonymous adts.
1086-
empty_disambiguator: u32,
10871085

10881086
/// Maps glob imports to the names of items actually imported.
10891087
glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,
@@ -1462,7 +1460,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
14621460
module_children: Default::default(),
14631461
trait_map: NodeMap::default(),
14641462
underscore_disambiguator: 0,
1465-
empty_disambiguator: 0,
14661463
empty_module,
14671464
module_map,
14681465
block_map: Default::default(),
@@ -1809,12 +1806,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18091806
assoc_item: Option<(Symbol, Namespace)>,
18101807
) -> bool {
18111808
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-
}
1809+
(Some(trait_module), Some((name, ns))) => self
1810+
.resolutions(trait_module)
1811+
.borrow()
1812+
.iter()
1813+
.any(|(key, _name_resolution)| key.ns == ns && key.ident.name == name),
18181814
_ => true,
18191815
}
18201816
}
@@ -1842,9 +1838,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18421838
let disambiguator = if ident.name == kw::Underscore {
18431839
self.underscore_disambiguator += 1;
18441840
self.underscore_disambiguator
1845-
} else if ident.name == kw::Empty {
1846-
self.empty_disambiguator += 1;
1847-
self.empty_disambiguator
18481841
} else {
18491842
0
18501843
};

0 commit comments

Comments
 (0)