Skip to content

Commit ab4cc2d

Browse files
committed
resolve: Move some code around
1 parent fe2524c commit ab4cc2d

File tree

5 files changed

+73
-74
lines changed

5 files changed

+73
-74
lines changed

src/librustc_resolve/build_reduced_graph.rs

+27-27
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,7 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
868868
// This is only a guess, two equivalent idents may incorrectly get different gensyms here.
869869
let ident = ident.gensym_if_underscore();
870870
let expansion = ExpnId::root(); // FIXME(jseyfried) intercrate hygiene
871+
// Record primary definitions.
871872
match res {
872873
Res::Def(kind @ DefKind::Mod, def_id)
873874
| Res::Def(kind @ DefKind::Enum, def_id)
@@ -879,51 +880,50 @@ impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
879880
span);
880881
self.r.define(parent, ident, TypeNS, (module, vis, DUMMY_SP, expansion));
881882
}
882-
Res::Def(DefKind::Variant, _)
883+
Res::Def(DefKind::Struct, _)
884+
| Res::Def(DefKind::Union, _)
885+
| Res::Def(DefKind::Variant, _)
883886
| Res::Def(DefKind::TyAlias, _)
884887
| Res::Def(DefKind::ForeignTy, _)
885888
| Res::Def(DefKind::OpaqueTy, _)
886889
| Res::Def(DefKind::TraitAlias, _)
887890
| Res::Def(DefKind::AssocTy, _)
888891
| Res::Def(DefKind::AssocOpaqueTy, _)
889892
| Res::PrimTy(..)
890-
| Res::ToolMod => {
891-
self.r.define(parent, ident, TypeNS, (res, vis, DUMMY_SP, expansion));
892-
}
893+
| Res::ToolMod =>
894+
self.r.define(parent, ident, TypeNS, (res, vis, DUMMY_SP, expansion)),
893895
Res::Def(DefKind::Fn, _)
896+
| Res::Def(DefKind::Method, _)
894897
| Res::Def(DefKind::Static, _)
895898
| Res::Def(DefKind::Const, _)
896899
| Res::Def(DefKind::AssocConst, _)
897-
| Res::Def(DefKind::Ctor(CtorOf::Variant, ..), _) => {
898-
self.r.define(parent, ident, ValueNS, (res, vis, DUMMY_SP, expansion));
899-
}
900-
Res::Def(DefKind::Ctor(CtorOf::Struct, ..), def_id) => {
901-
self.r.define(parent, ident, ValueNS, (res, vis, DUMMY_SP, expansion));
902-
903-
if let Some(struct_def_id) =
904-
self.r.cstore.def_key(def_id).parent
905-
.map(|index| DefId { krate: def_id.krate, index: index }) {
906-
self.r.struct_constructors.insert(struct_def_id, (res, vis));
907-
}
900+
| Res::Def(DefKind::Ctor(..), _) =>
901+
self.r.define(parent, ident, ValueNS, (res, vis, DUMMY_SP, expansion)),
902+
Res::Def(DefKind::Macro(..), _)
903+
| Res::NonMacroAttr(..) =>
904+
self.r.define(parent, ident, MacroNS, (res, vis, DUMMY_SP, expansion)),
905+
Res::Def(DefKind::TyParam, _) | Res::Def(DefKind::ConstParam, _)
906+
| Res::Local(..) | Res::SelfTy(..) | Res::SelfCtor(..) | Res::Err =>
907+
bug!("unexpected resolution: {:?}", res)
908+
}
909+
// Record some extra data for better diagnostics.
910+
match res {
911+
Res::Def(DefKind::Struct, def_id) | Res::Def(DefKind::Union, def_id) => {
912+
let field_names = self.r.cstore.struct_field_names_untracked(def_id);
913+
self.insert_field_names(def_id, field_names);
908914
}
909915
Res::Def(DefKind::Method, def_id) => {
910-
self.r.define(parent, ident, ValueNS, (res, vis, DUMMY_SP, expansion));
911-
912916
if self.r.cstore.associated_item_cloned_untracked(def_id).method_has_self_argument {
913917
self.r.has_self.insert(def_id);
914918
}
915919
}
916-
Res::Def(DefKind::Struct, def_id) | Res::Def(DefKind::Union, def_id) => {
917-
self.r.define(parent, ident, TypeNS, (res, vis, DUMMY_SP, expansion));
918-
919-
// Record field names for error reporting.
920-
let field_names = self.r.cstore.struct_field_names_untracked(def_id);
921-
self.insert_field_names(def_id, field_names);
922-
}
923-
Res::Def(DefKind::Macro(..), _) | Res::NonMacroAttr(..) => {
924-
self.r.define(parent, ident, MacroNS, (res, vis, DUMMY_SP, expansion));
920+
Res::Def(DefKind::Ctor(CtorOf::Struct, ..), def_id) => {
921+
let parent = self.r.cstore.def_key(def_id).parent;
922+
if let Some(struct_def_id) = parent.map(|index| DefId { index, ..def_id }) {
923+
self.r.struct_constructors.insert(struct_def_id, (res, vis));
924+
}
925925
}
926-
_ => bug!("unexpected resolution: {:?}", res)
926+
_ => {}
927927
}
928928
}
929929

src/librustc_resolve/diagnostics.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -73,23 +73,23 @@ crate fn add_typo_suggestion(
7373
false
7474
}
7575

76-
crate fn add_module_candidates<'a>(
77-
resolver: &mut Resolver<'a>,
78-
module: Module<'a>,
79-
names: &mut Vec<TypoSuggestion>,
80-
filter_fn: &impl Fn(Res) -> bool,
81-
) {
82-
for (&(ident, _), resolution) in resolver.resolutions(module).borrow().iter() {
83-
if let Some(binding) = resolution.borrow().binding {
84-
let res = binding.res();
85-
if filter_fn(res) {
86-
names.push(TypoSuggestion::from_res(ident.name, res));
76+
impl<'a> Resolver<'a> {
77+
crate fn add_module_candidates(
78+
&mut self,
79+
module: Module<'a>,
80+
names: &mut Vec<TypoSuggestion>,
81+
filter_fn: &impl Fn(Res) -> bool,
82+
) {
83+
for (&(ident, _), resolution) in self.resolutions(module).borrow().iter() {
84+
if let Some(binding) = resolution.borrow().binding {
85+
let res = binding.res();
86+
if filter_fn(res) {
87+
names.push(TypoSuggestion::from_res(ident.name, res));
88+
}
8789
}
8890
}
8991
}
90-
}
9192

92-
impl<'a> Resolver<'a> {
9393
/// Combines an error with provided span and emits it.
9494
///
9595
/// This takes the error provided, combines it with the span and any additional spans inside the
@@ -405,10 +405,10 @@ impl<'a> Resolver<'a> {
405405
Scope::CrateRoot => {
406406
let root_ident = Ident::new(kw::PathRoot, ident.span);
407407
let root_module = this.resolve_crate_root(root_ident);
408-
add_module_candidates(this, root_module, &mut suggestions, filter_fn);
408+
this.add_module_candidates(root_module, &mut suggestions, filter_fn);
409409
}
410410
Scope::Module(module) => {
411-
add_module_candidates(this, module, &mut suggestions, filter_fn);
411+
this.add_module_candidates(module, &mut suggestions, filter_fn);
412412
}
413413
Scope::MacroUsePrelude => {
414414
suggestions.extend(this.macro_use_prelude.iter().filter_map(|(name, binding)| {
@@ -456,7 +456,7 @@ impl<'a> Resolver<'a> {
456456
Scope::StdLibPrelude => {
457457
if let Some(prelude) = this.prelude {
458458
let mut tmp_suggestions = Vec::new();
459-
add_module_candidates(this, prelude, &mut tmp_suggestions, filter_fn);
459+
this.add_module_candidates(prelude, &mut tmp_suggestions, filter_fn);
460460
suggestions.extend(tmp_suggestions.into_iter().filter(|s| {
461461
use_prelude || this.is_builtin_macro(s.res)
462462
}));

src/librustc_resolve/late/diagnostics.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use crate::{CrateLint, Module, ModuleKind, ModuleOrUniformRoot};
22
use crate::{PathResult, PathSource, Segment};
33
use crate::path_names_to_string;
4-
use crate::diagnostics::{add_typo_suggestion, add_module_candidates};
5-
use crate::diagnostics::{ImportSuggestion, TypoSuggestion};
4+
use crate::diagnostics::{add_typo_suggestion, ImportSuggestion, TypoSuggestion};
65
use crate::late::{LateResolutionVisitor, RibKind};
76

87
use errors::{Applicability, DiagnosticBuilder, DiagnosticId};
@@ -548,7 +547,7 @@ impl<'a> LateResolutionVisitor<'a, '_> {
548547
// Items in scope
549548
if let RibKind::ModuleRibKind(module) = rib.kind {
550549
// Items from this module
551-
add_module_candidates(self.r, module, &mut names, &filter_fn);
550+
self.r.add_module_candidates(module, &mut names, &filter_fn);
552551

553552
if let ModuleKind::Block(..) = module.kind {
554553
// We can see through blocks
@@ -577,7 +576,7 @@ impl<'a> LateResolutionVisitor<'a, '_> {
577576
}));
578577

579578
if let Some(prelude) = self.r.prelude {
580-
add_module_candidates(self.r, prelude, &mut names, &filter_fn);
579+
self.r.add_module_candidates(prelude, &mut names, &filter_fn);
581580
}
582581
}
583582
break;
@@ -599,7 +598,7 @@ impl<'a> LateResolutionVisitor<'a, '_> {
599598
mod_path, Some(TypeNS), false, span, CrateLint::No
600599
) {
601600
if let ModuleOrUniformRoot::Module(module) = module {
602-
add_module_candidates(self.r, module, &mut names, &filter_fn);
601+
self.r.add_module_candidates(module, &mut names, &filter_fn);
603602
}
604603
}
605604
}

src/librustc_resolve/lib.rs

+20
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ use std::collections::BTreeSet;
5959
use rustc_data_structures::ptr_key::PtrKey;
6060
use rustc_data_structures::sync::Lrc;
6161

62+
use build_reduced_graph::BuildReducedGraphVisitor;
6263
use diagnostics::{Suggestion, ImportSuggestion};
6364
use diagnostics::{find_span_of_binding_until_next_binding, extend_span_to_previous_binding};
6465
use late::{PathSource, Rib, RibKind::*};
@@ -1257,6 +1258,25 @@ impl<'a> Resolver<'a> {
12571258
self.arenas.alloc_module(module)
12581259
}
12591260

1261+
fn resolutions(&mut self, module: Module<'a>) -> &'a Resolutions<'a> {
1262+
if module.populate_on_access.get() {
1263+
module.populate_on_access.set(false);
1264+
let def_id = module.def_id().expect("unpopulated module without a def-id");
1265+
for child in self.cstore.item_children_untracked(def_id, self.session) {
1266+
let child = child.map_id(|_| panic!("unexpected id"));
1267+
BuildReducedGraphVisitor { parent_scope: self.dummy_parent_scope(), r: self }
1268+
.build_reduced_graph_for_external_crate_res(module, child);
1269+
}
1270+
}
1271+
&module.lazy_resolutions
1272+
}
1273+
1274+
fn resolution(&mut self, module: Module<'a>, ident: Ident, ns: Namespace)
1275+
-> &'a RefCell<NameResolution<'a>> {
1276+
*self.resolutions(module).borrow_mut().entry((ident.modern(), ns))
1277+
.or_insert_with(|| self.arenas.alloc_name_resolution())
1278+
}
1279+
12601280
fn record_use(&mut self, ident: Ident, ns: Namespace,
12611281
used_binding: &'a NameBinding<'a>, is_lexical_scope: bool) {
12621282
if let Some((b2, kind)) = used_binding.ambiguity {

src/librustc_resolve/resolve_imports.rs

+6-26
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ use crate::{CrateLint, Module, ModuleOrUniformRoot, PerNS, ScopeSet, ParentScope
77
use crate::Determinacy::{self, *};
88
use crate::Namespace::{self, TypeNS, MacroNS};
99
use crate::{NameBinding, NameBindingKind, ToNameBinding, PathResult, PrivacyError};
10-
use crate::{Resolutions, Resolver, ResolutionError, Segment};
10+
use crate::{Resolver, ResolutionError, Segment, ModuleKind};
1111
use crate::{names_to_string, module_to_string};
12-
use crate::ModuleKind;
13-
use crate::build_reduced_graph::BuildReducedGraphVisitor;
1412
use crate::diagnostics::Suggestion;
1513

1614
use errors::Applicability;
@@ -38,7 +36,7 @@ use syntax_pos::{MultiSpan, Span};
3836

3937
use log::*;
4038

41-
use std::cell::{Cell, RefCell};
39+
use std::cell::Cell;
4240
use std::{mem, ptr};
4341

4442
type Res = def::Res<NodeId>;
@@ -162,25 +160,6 @@ impl<'a> NameResolution<'a> {
162160
}
163161

164162
impl<'a> Resolver<'a> {
165-
crate fn resolutions(&mut self, module: Module<'a>) -> &'a Resolutions<'a> {
166-
if module.populate_on_access.get() {
167-
module.populate_on_access.set(false);
168-
let def_id = module.def_id().expect("unpopulated module without a def-id");
169-
for child in self.cstore.item_children_untracked(def_id, self.session) {
170-
let child = child.map_id(|_| panic!("unexpected id"));
171-
BuildReducedGraphVisitor { parent_scope: self.dummy_parent_scope(), r: self }
172-
.build_reduced_graph_for_external_crate_res(module, child);
173-
}
174-
}
175-
&module.lazy_resolutions
176-
}
177-
178-
crate fn resolution(&mut self, module: Module<'a>, ident: Ident, ns: Namespace)
179-
-> &'a RefCell<NameResolution<'a>> {
180-
*self.resolutions(module).borrow_mut().entry((ident.modern(), ns))
181-
.or_insert_with(|| self.arenas.alloc_name_resolution())
182-
}
183-
184163
crate fn resolve_ident_in_module_unadjusted(
185164
&mut self,
186165
module: ModuleOrUniformRoot<'a>,
@@ -1039,7 +1018,8 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
10391018

10401019
return if all_ns_failed {
10411020
let resolutions = match module {
1042-
ModuleOrUniformRoot::Module(module) => Some(self.r.resolutions(module).borrow()),
1021+
ModuleOrUniformRoot::Module(module) =>
1022+
Some(self.r.resolutions(module).borrow()),
10431023
_ => None,
10441024
};
10451025
let resolutions = resolutions.as_ref().into_iter().flat_map(|r| r.iter());
@@ -1292,8 +1272,8 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
12921272

12931273
// Ensure that `resolutions` isn't borrowed during `try_define`,
12941274
// since it might get updated via a glob cycle.
1295-
let bindings = self.r.resolutions(module).borrow().iter().filter_map(|(&ident, resolution)| {
1296-
resolution.borrow().binding().map(|binding| (ident, binding))
1275+
let bindings = self.r.resolutions(module).borrow().iter().filter_map(|(ident, resolution)| {
1276+
resolution.borrow().binding().map(|binding| (*ident, binding))
12971277
}).collect::<Vec<_>>();
12981278
for ((mut ident, ns), binding) in bindings {
12991279
let scope = match ident.span.reverse_glob_adjust(module.expansion, directive.span) {

0 commit comments

Comments
 (0)