Skip to content

Commit 3845a08

Browse files
committed
resolve: Move some common code into the scope visitor
1 parent 329c052 commit 3845a08

File tree

3 files changed

+114
-142
lines changed

3 files changed

+114
-142
lines changed

src/librustc_resolve/diagnostics.rs

+30-51
Original file line numberDiff line numberDiff line change
@@ -562,19 +562,10 @@ impl<'a> Resolver<'a> {
562562
&mut self,
563563
scope_set: ScopeSet,
564564
parent_scope: &ParentScope<'a>,
565-
orig_ident: Ident,
565+
ident: Ident,
566566
filter_fn: &impl Fn(Res) -> bool,
567567
) -> Option<TypoSuggestion> {
568-
let ident = orig_ident.modern();
569-
let rust_2015 = orig_ident.span.rust_2015();
570-
let is_absolute_path = match scope_set {
571-
ScopeSet::AbsolutePath(..) => true,
572-
_ => false,
573-
};
574-
575568
let mut suggestions = Vec::new();
576-
let mut use_prelude = !parent_scope.module.no_implicit_prelude;
577-
578569
self.visit_scopes(scope_set, parent_scope, ident, |this, scope, _| {
579570
match scope {
580571
Scope::DeriveHelpers => {
@@ -603,26 +594,22 @@ impl<'a> Resolver<'a> {
603594
}
604595
}
605596
Scope::CrateRoot => {
606-
let root_ident = Ident::new(kw::PathRoot, orig_ident.span);
597+
let root_ident = Ident::new(kw::PathRoot, ident.span);
607598
let root_module = this.resolve_crate_root(root_ident);
608599
add_module_candidates(root_module, &mut suggestions, filter_fn);
609600
}
610601
Scope::Module(module) => {
611-
use_prelude = !module.no_implicit_prelude;
612602
add_module_candidates(module, &mut suggestions, filter_fn);
613603
}
614604
Scope::MacroUsePrelude => {
615-
if use_prelude || rust_2015 {
616-
let macro_use_prelude = &this.macro_use_prelude;
617-
suggestions.extend(macro_use_prelude.iter().filter_map(|(name, binding)| {
618-
let res = binding.res();
619-
if filter_fn(res) {
620-
Some(TypoSuggestion::from_res(*name, res))
621-
} else {
622-
None
623-
}
624-
}));
625-
}
605+
suggestions.extend(this.macro_use_prelude.iter().filter_map(|(name, binding)| {
606+
let res = binding.res();
607+
if filter_fn(res) {
608+
Some(TypoSuggestion::from_res(*name, res))
609+
} else {
610+
None
611+
}
612+
}));
626613
}
627614
Scope::BuiltinMacros => {
628615
suggestions.extend(this.builtin_macros.iter().filter_map(|(name, binding)| {
@@ -643,41 +630,33 @@ impl<'a> Resolver<'a> {
643630
}
644631
}
645632
Scope::LegacyPluginHelpers => {
646-
if use_prelude || rust_2015 {
647-
let res = Res::NonMacroAttr(NonMacroAttrKind::LegacyPluginHelper);
648-
if filter_fn(res) {
649-
let plugin_attributes = this.session.plugin_attributes.borrow();
650-
suggestions.extend(plugin_attributes.iter().map(|(name, _)| {
651-
TypoSuggestion::from_res(*name, res)
652-
}));
653-
}
633+
let res = Res::NonMacroAttr(NonMacroAttrKind::LegacyPluginHelper);
634+
if filter_fn(res) {
635+
let plugin_attributes = this.session.plugin_attributes.borrow();
636+
suggestions.extend(plugin_attributes.iter().map(|(name, _)| {
637+
TypoSuggestion::from_res(*name, res)
638+
}));
654639
}
655640
}
656641
Scope::ExternPrelude => {
657-
if use_prelude || is_absolute_path {
658-
suggestions.extend(this.extern_prelude.iter().filter_map(|(ident, _)| {
659-
let res = Res::Def(DefKind::Mod, DefId::local(CRATE_DEF_INDEX));
660-
if filter_fn(res) {
661-
Some(TypoSuggestion::from_res(ident.name, res))
662-
} else {
663-
None
664-
}
665-
}));
666-
}
642+
suggestions.extend(this.extern_prelude.iter().filter_map(|(ident, _)| {
643+
let res = Res::Def(DefKind::Mod, DefId::local(CRATE_DEF_INDEX));
644+
if filter_fn(res) {
645+
Some(TypoSuggestion::from_res(ident.name, res))
646+
} else {
647+
None
648+
}
649+
}));
667650
}
668651
Scope::ToolPrelude => {
669-
if use_prelude {
670-
let res = Res::NonMacroAttr(NonMacroAttrKind::Tool);
671-
suggestions.extend(KNOWN_TOOLS.iter().map(|name| {
672-
TypoSuggestion::from_res(*name, res)
673-
}));
674-
}
652+
let res = Res::NonMacroAttr(NonMacroAttrKind::Tool);
653+
suggestions.extend(KNOWN_TOOLS.iter().map(|name| {
654+
TypoSuggestion::from_res(*name, res)
655+
}));
675656
}
676657
Scope::StdLibPrelude => {
677-
if use_prelude {
678-
if let Some(prelude) = this.prelude {
679-
add_module_candidates(prelude, &mut suggestions, filter_fn);
680-
}
658+
if let Some(prelude) = this.prelude {
659+
add_module_candidates(prelude, &mut suggestions, filter_fn);
681660
}
682661
}
683662
Scope::BuiltinTypes => {

src/librustc_resolve/lib.rs

+24-3
Original file line numberDiff line numberDiff line change
@@ -2145,7 +2145,7 @@ impl<'a> Resolver<'a> {
21452145
&mut self,
21462146
scope_set: ScopeSet,
21472147
parent_scope: &ParentScope<'a>,
2148-
mut ident: Ident,
2148+
ident: Ident,
21492149
mut visitor: impl FnMut(&mut Self, Scope<'a>, Ident) -> Option<T>,
21502150
) -> Option<T> {
21512151
// General principles:
@@ -2192,6 +2192,7 @@ impl<'a> Resolver<'a> {
21922192
// but introduced by legacy plugins using `register_attribute`. Priority is somewhere
21932193
// in prelude, not sure where exactly (creates ambiguities with any other prelude names).
21942194

2195+
let rust_2015 = ident.span.rust_2015();
21952196
let (ns, is_absolute_path) = match scope_set {
21962197
ScopeSet::Import(ns) => (ns, false),
21972198
ScopeSet::AbsolutePath(ns) => (ns, true),
@@ -2203,10 +2204,29 @@ impl<'a> Resolver<'a> {
22032204
TypeNS | ValueNS => Scope::Module(parent_scope.module),
22042205
MacroNS => Scope::DeriveHelpers,
22052206
};
2207+
let mut ident = ident.modern();
2208+
let mut use_prelude = !parent_scope.module.no_implicit_prelude;
22062209

22072210
loop {
2208-
if let break_result @ Some(..) = visitor(self, scope, ident) {
2209-
return break_result;
2211+
let visit = match scope {
2212+
Scope::DeriveHelpers => true,
2213+
Scope::MacroRules(..) => true,
2214+
Scope::CrateRoot => true,
2215+
Scope::Module(..) => true,
2216+
Scope::MacroUsePrelude => use_prelude || rust_2015,
2217+
Scope::BuiltinMacros => true,
2218+
Scope::BuiltinAttrs => true,
2219+
Scope::LegacyPluginHelpers => use_prelude || rust_2015,
2220+
Scope::ExternPrelude => use_prelude || is_absolute_path,
2221+
Scope::ToolPrelude => use_prelude,
2222+
Scope::StdLibPrelude => use_prelude,
2223+
Scope::BuiltinTypes => true,
2224+
};
2225+
2226+
if visit {
2227+
if let break_result @ Some(..) = visitor(self, scope, ident) {
2228+
return break_result;
2229+
}
22102230
}
22112231

22122232
scope = match scope {
@@ -2229,6 +2249,7 @@ impl<'a> Resolver<'a> {
22292249
ValueNS | MacroNS => break,
22302250
}
22312251
Scope::Module(module) => {
2252+
use_prelude = !module.no_implicit_prelude;
22322253
match self.hygienic_lexical_parent(module, &mut ident.span) {
22332254
Some(parent_module) => Scope::Module(parent_module),
22342255
None => {

src/librustc_resolve/macros.rs

+60-88
Original file line numberDiff line numberDiff line change
@@ -417,19 +417,17 @@ impl<'a> Resolver<'a> {
417417
}
418418

419419
assert!(force || !record_used); // `record_used` implies `force`
420-
let ident = orig_ident.modern();
421420

422421
// Make sure `self`, `super` etc produce an error when passed to here.
423-
if ident.is_path_segment_keyword() {
422+
if orig_ident.is_path_segment_keyword() {
424423
return Err(Determinacy::Determined);
425424
}
426425

427-
let rust_2015 = orig_ident.span.rust_2015();
428-
let (ns, macro_kind, is_import, is_absolute_path) = match scope_set {
429-
ScopeSet::Import(ns) => (ns, None, true, false),
430-
ScopeSet::AbsolutePath(ns) => (ns, None, false, true),
431-
ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind), false, false),
432-
ScopeSet::Module => (TypeNS, None, false, false),
426+
let (ns, macro_kind, is_import) = match scope_set {
427+
ScopeSet::Import(ns) => (ns, None, true),
428+
ScopeSet::AbsolutePath(ns) => (ns, None, false),
429+
ScopeSet::Macro(macro_kind) => (MacroNS, Some(macro_kind), false),
430+
ScopeSet::Module => (TypeNS, None, false),
433431
};
434432

435433
// This is *the* result, resolution from the scope closest to the resolved identifier.
@@ -444,11 +442,11 @@ impl<'a> Resolver<'a> {
444442
// So we have to save the innermost solution and continue searching in outer scopes
445443
// to detect potential ambiguities.
446444
let mut innermost_result: Option<(&NameBinding<'_>, Flags)> = None;
447-
let mut use_prelude = !parent_scope.module.no_implicit_prelude;
448445
let mut determinacy = Determinacy::Determined;
449446

450447
// Go through all the scopes and try to resolve the name.
451-
let break_result = self.visit_scopes(scope_set, parent_scope, ident, |this, scope, ident| {
448+
let break_result =
449+
self.visit_scopes(scope_set, parent_scope, orig_ident, |this, scope, ident| {
452450
let result = match scope {
453451
Scope::DeriveHelpers => {
454452
let mut result = Err(Determinacy::Determined);
@@ -478,11 +476,11 @@ impl<'a> Resolver<'a> {
478476
_ => Err(Determinacy::Determined),
479477
}
480478
Scope::CrateRoot => {
481-
let root_ident = Ident::new(kw::PathRoot, orig_ident.span);
479+
let root_ident = Ident::new(kw::PathRoot, ident.span);
482480
let root_module = this.resolve_crate_root(root_ident);
483481
let binding = this.resolve_ident_in_module_ext(
484482
ModuleOrUniformRoot::Module(root_module),
485-
orig_ident,
483+
ident,
486484
ns,
487485
None,
488486
record_used,
@@ -498,7 +496,6 @@ impl<'a> Resolver<'a> {
498496
}
499497
}
500498
Scope::Module(module) => {
501-
use_prelude = !module.no_implicit_prelude;
502499
let orig_current_module = mem::replace(&mut this.current_module, module);
503500
let binding = this.resolve_ident_in_module_unadjusted_ext(
504501
ModuleOrUniformRoot::Module(module),
@@ -528,94 +525,69 @@ impl<'a> Resolver<'a> {
528525
Err((Determinacy::Determined, _)) => Err(Determinacy::Determined),
529526
}
530527
}
531-
Scope::MacroUsePrelude => {
532-
if use_prelude || rust_2015 {
533-
match this.macro_use_prelude.get(&ident.name).cloned() {
534-
Some(binding) =>
535-
Ok((binding, Flags::PRELUDE | Flags::MISC_FROM_PRELUDE)),
536-
None => Err(Determinacy::determined(
537-
this.graph_root.unresolved_invocations.borrow().is_empty()
538-
))
539-
}
540-
} else {
541-
Err(Determinacy::Determined)
542-
}
528+
Scope::MacroUsePrelude => match this.macro_use_prelude.get(&ident.name).cloned() {
529+
Some(binding) => Ok((binding, Flags::PRELUDE | Flags::MISC_FROM_PRELUDE)),
530+
None => Err(Determinacy::determined(
531+
this.graph_root.unresolved_invocations.borrow().is_empty()
532+
))
543533
}
544-
Scope::BuiltinMacros => {
545-
match this.builtin_macros.get(&ident.name).cloned() {
546-
Some(binding) => Ok((binding, Flags::PRELUDE)),
547-
None => Err(Determinacy::Determined),
548-
}
534+
Scope::BuiltinMacros => match this.builtin_macros.get(&ident.name).cloned() {
535+
Some(binding) => Ok((binding, Flags::PRELUDE)),
536+
None => Err(Determinacy::Determined),
549537
}
550-
Scope::BuiltinAttrs => {
551-
if is_builtin_attr_name(ident.name) {
552-
let binding = (Res::NonMacroAttr(NonMacroAttrKind::Builtin),
553-
ty::Visibility::Public, DUMMY_SP, Mark::root())
554-
.to_name_binding(this.arenas);
555-
Ok((binding, Flags::PRELUDE))
556-
} else {
557-
Err(Determinacy::Determined)
558-
}
538+
Scope::BuiltinAttrs => if is_builtin_attr_name(ident.name) {
539+
let binding = (Res::NonMacroAttr(NonMacroAttrKind::Builtin),
540+
ty::Visibility::Public, DUMMY_SP, Mark::root())
541+
.to_name_binding(this.arenas);
542+
Ok((binding, Flags::PRELUDE))
543+
} else {
544+
Err(Determinacy::Determined)
559545
}
560-
Scope::LegacyPluginHelpers => {
561-
if (use_prelude || rust_2015) &&
562-
this.session.plugin_attributes.borrow().iter()
546+
Scope::LegacyPluginHelpers => if this.session.plugin_attributes.borrow().iter()
563547
.any(|(name, _)| ident.name == *name) {
564-
let binding = (Res::NonMacroAttr(NonMacroAttrKind::LegacyPluginHelper),
565-
ty::Visibility::Public, DUMMY_SP, Mark::root())
566-
.to_name_binding(this.arenas);
567-
Ok((binding, Flags::PRELUDE))
568-
} else {
569-
Err(Determinacy::Determined)
570-
}
548+
let binding = (Res::NonMacroAttr(NonMacroAttrKind::LegacyPluginHelper),
549+
ty::Visibility::Public, DUMMY_SP, Mark::root())
550+
.to_name_binding(this.arenas);
551+
Ok((binding, Flags::PRELUDE))
552+
} else {
553+
Err(Determinacy::Determined)
571554
}
572-
Scope::ExternPrelude => {
573-
if use_prelude || is_absolute_path {
574-
match this.extern_prelude_get(ident, !record_used) {
575-
Some(binding) => Ok((binding, Flags::PRELUDE)),
576-
None => Err(Determinacy::determined(
577-
this.graph_root.unresolved_invocations.borrow().is_empty()
578-
)),
579-
}
580-
} else {
581-
Err(Determinacy::Determined)
582-
}
555+
Scope::ExternPrelude => match this.extern_prelude_get(ident, !record_used) {
556+
Some(binding) => Ok((binding, Flags::PRELUDE)),
557+
None => Err(Determinacy::determined(
558+
this.graph_root.unresolved_invocations.borrow().is_empty()
559+
)),
583560
}
584-
Scope::ToolPrelude => {
585-
if use_prelude && KNOWN_TOOLS.contains(&ident.name) {
586-
let binding = (Res::ToolMod, ty::Visibility::Public,
587-
DUMMY_SP, Mark::root()).to_name_binding(this.arenas);
588-
Ok((binding, Flags::PRELUDE))
589-
} else {
590-
Err(Determinacy::Determined)
591-
}
561+
Scope::ToolPrelude => if KNOWN_TOOLS.contains(&ident.name) {
562+
let binding = (Res::ToolMod, ty::Visibility::Public, DUMMY_SP, Mark::root())
563+
.to_name_binding(this.arenas);
564+
Ok((binding, Flags::PRELUDE))
565+
} else {
566+
Err(Determinacy::Determined)
592567
}
593568
Scope::StdLibPrelude => {
594569
let mut result = Err(Determinacy::Determined);
595-
if use_prelude {
596-
if let Some(prelude) = this.prelude {
597-
if let Ok(binding) = this.resolve_ident_in_module_unadjusted(
598-
ModuleOrUniformRoot::Module(prelude),
599-
ident,
600-
ns,
601-
false,
602-
path_span,
603-
) {
604-
result = Ok((binding, Flags::PRELUDE | Flags::MISC_FROM_PRELUDE));
605-
}
570+
if let Some(prelude) = this.prelude {
571+
if let Ok(binding) = this.resolve_ident_in_module_unadjusted(
572+
ModuleOrUniformRoot::Module(prelude),
573+
ident,
574+
ns,
575+
false,
576+
path_span,
577+
) {
578+
result = Ok((binding, Flags::PRELUDE | Flags::MISC_FROM_PRELUDE));
606579
}
607580
}
608581
result
609582
}
610-
Scope::BuiltinTypes => {
611-
match this.primitive_type_table.primitive_types.get(&ident.name).cloned() {
612-
Some(prim_ty) => {
613-
let binding = (Res::PrimTy(prim_ty), ty::Visibility::Public,
614-
DUMMY_SP, Mark::root()).to_name_binding(this.arenas);
615-
Ok((binding, Flags::PRELUDE))
616-
}
617-
None => Err(Determinacy::Determined)
583+
Scope::BuiltinTypes => match this.primitive_type_table.primitive_types
584+
.get(&ident.name).cloned() {
585+
Some(prim_ty) => {
586+
let binding = (Res::PrimTy(prim_ty), ty::Visibility::Public,
587+
DUMMY_SP, Mark::root()).to_name_binding(this.arenas);
588+
Ok((binding, Flags::PRELUDE))
618589
}
590+
None => Err(Determinacy::Determined)
619591
}
620592
};
621593

@@ -712,7 +684,7 @@ impl<'a> Resolver<'a> {
712684
// the last segment, so we are certainly working with a single-segment attribute here.)
713685
assert!(ns == MacroNS);
714686
let binding = (Res::NonMacroAttr(NonMacroAttrKind::Custom),
715-
ty::Visibility::Public, ident.span, Mark::root())
687+
ty::Visibility::Public, orig_ident.span, Mark::root())
716688
.to_name_binding(self.arenas);
717689
Ok(binding)
718690
} else {

0 commit comments

Comments
 (0)