Skip to content

Commit 1cded84

Browse files
committed
Remove trait_item_map, clean up resolver.with_type_parameter_rib().
1 parent 2a1d2ed commit 1cded84

File tree

2 files changed

+73
-57
lines changed

2 files changed

+73
-57
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,10 @@ impl<'a> Resolver<'a> {
478478
self.define(module, ident, ns, (child.def, ty::Visibility::Public,
479479
DUMMY_SP, Mark::root()));
480480

481-
let has_self = self.session.cstore.associated_item_cloned(child.def.def_id())
482-
.method_has_self_argument;
483-
self.trait_item_map.insert((def_id, child.name, ns), (child.def, has_self));
481+
if self.session.cstore.associated_item_cloned(child.def.def_id())
482+
.method_has_self_argument {
483+
self.has_self.insert(child.def.def_id());
484+
}
484485
}
485486
module.populated.set(true);
486487
}
@@ -773,7 +774,6 @@ impl<'a, 'b> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b> {
773774

774775
fn visit_trait_item(&mut self, item: &'a TraitItem) {
775776
let parent = self.resolver.current_module;
776-
let def_id = parent.def_id().unwrap();
777777

778778
if let TraitItemKind::Macro(_) = item.node {
779779
self.visit_invoc(item.id);
@@ -782,16 +782,18 @@ impl<'a, 'b> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b> {
782782

783783
// Add the item to the trait info.
784784
let item_def_id = self.resolver.definitions.local_def_id(item.id);
785-
let (def, ns, has_self) = match item.node {
786-
TraitItemKind::Const(..) => (Def::AssociatedConst(item_def_id), ValueNS, false),
787-
TraitItemKind::Method(ref sig, _) =>
788-
(Def::Method(item_def_id), ValueNS, sig.decl.has_self()),
789-
TraitItemKind::Type(..) => (Def::AssociatedTy(item_def_id), TypeNS, false),
785+
let (def, ns) = match item.node {
786+
TraitItemKind::Const(..) => (Def::AssociatedConst(item_def_id), ValueNS),
787+
TraitItemKind::Method(ref sig, _) => {
788+
if sig.decl.has_self() {
789+
self.resolver.has_self.insert(item_def_id);
790+
}
791+
(Def::Method(item_def_id), ValueNS)
792+
}
793+
TraitItemKind::Type(..) => (Def::AssociatedTy(item_def_id), TypeNS),
790794
TraitItemKind::Macro(_) => bug!(), // handled above
791795
};
792796

793-
self.resolver.trait_item_map.insert((def_id, item.ident.name, ns), (def, has_self));
794-
795797
let vis = ty::Visibility::Public;
796798
self.resolver.define(parent, item.ident, ns, (def, vis, item.span, self.expansion));
797799

src/librustc_resolve/lib.rs

Lines changed: 60 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,8 @@ pub struct Resolver<'a> {
11111111

11121112
prelude: Option<Module<'a>>,
11131113

1114-
trait_item_map: FxHashMap<(DefId, Name, Namespace), (Def, bool /* has self */)>,
1114+
// n.b. This is used only for better diagnostics, not name resolution itself.
1115+
has_self: FxHashSet<DefId>,
11151116

11161117
// Names of fields of an item `DefId` accessible with dot syntax.
11171118
// Used for hints during error reporting.
@@ -1134,7 +1135,7 @@ pub struct Resolver<'a> {
11341135
label_ribs: Vec<Rib<'a>>,
11351136

11361137
// The trait that the current context can refer to.
1137-
current_trait_ref: Option<(DefId, TraitRef)>,
1138+
current_trait_ref: Option<(Module<'a>, TraitRef)>,
11381139

11391140
// The current self type if inside an impl (used for better errors).
11401141
current_self_type: Option<Ty>,
@@ -1337,7 +1338,7 @@ impl<'a> Resolver<'a> {
13371338
graph_root: graph_root,
13381339
prelude: None,
13391340

1340-
trait_item_map: FxHashMap(),
1341+
has_self: FxHashSet(),
13411342
field_names: FxHashMap(),
13421343

13431344
determined_imports: Vec::new(),
@@ -1750,22 +1751,21 @@ impl<'a> Resolver<'a> {
17501751
let mut function_type_rib = Rib::new(rib_kind);
17511752
let mut seen_bindings = FxHashMap();
17521753
for type_parameter in &generics.ty_params {
1753-
let name = type_parameter.ident.name;
1754+
let ident = type_parameter.ident.unhygienize();
17541755
debug!("with_type_parameter_rib: {}", type_parameter.id);
17551756

1756-
if seen_bindings.contains_key(&name) {
1757-
let span = seen_bindings.get(&name).unwrap();
1758-
resolve_error(self,
1759-
type_parameter.span,
1760-
ResolutionError::NameAlreadyUsedInTypeParameterList(name,
1761-
span));
1757+
if seen_bindings.contains_key(&ident) {
1758+
let span = seen_bindings.get(&ident).unwrap();
1759+
let err =
1760+
ResolutionError::NameAlreadyUsedInTypeParameterList(ident.name, span);
1761+
resolve_error(self, type_parameter.span, err);
17621762
}
1763-
seen_bindings.entry(name).or_insert(type_parameter.span);
1763+
seen_bindings.entry(ident).or_insert(type_parameter.span);
17641764

17651765
// plain insert (no renaming)
17661766
let def_id = self.definitions.local_def_id(type_parameter.id);
17671767
let def = Def::TyParam(def_id);
1768-
function_type_rib.bindings.insert(Ident::with_empty_ctxt(name), def);
1768+
function_type_rib.bindings.insert(ident, def);
17691769
self.record_def(type_parameter.id, PathResolution::new(def));
17701770
}
17711771
self.ribs[TypeNS].push(function_type_rib);
@@ -1825,11 +1825,20 @@ impl<'a> Resolver<'a> {
18251825
let mut new_val = None;
18261826
let mut new_id = None;
18271827
if let Some(trait_ref) = opt_trait_ref {
1828-
let def = self.smart_resolve_path(trait_ref.ref_id, None,
1829-
&trait_ref.path, PathSource::Trait).base_def();
1828+
let path: Vec<_> = trait_ref.path.segments.iter().map(|seg| seg.identifier).collect();
1829+
let def = self.smart_resolve_path_fragment(trait_ref.ref_id,
1830+
None,
1831+
&path,
1832+
trait_ref.path.span,
1833+
trait_ref.path.segments.last().unwrap().span,
1834+
PathSource::Trait)
1835+
.base_def();
18301836
if def != Def::Err {
1831-
new_val = Some((def.def_id(), trait_ref.clone()));
18321837
new_id = Some(def.def_id());
1838+
let span = trait_ref.path.span;
1839+
if let PathResult::Module(module) = self.resolve_path(&path, None, false, span) {
1840+
new_val = Some((module, trait_ref.clone()));
1841+
}
18331842
}
18341843
}
18351844
let original_trait_ref = replace(&mut self.current_trait_ref, new_val);
@@ -1880,7 +1889,7 @@ impl<'a> Resolver<'a> {
18801889
ImplItemKind::Const(..) => {
18811890
// If this is a trait impl, ensure the const
18821891
// exists in trait
1883-
this.check_trait_item(impl_item.ident.name,
1892+
this.check_trait_item(impl_item.ident,
18841893
ValueNS,
18851894
impl_item.span,
18861895
|n, s| ResolutionError::ConstNotMemberOfTrait(n, s));
@@ -1889,7 +1898,7 @@ impl<'a> Resolver<'a> {
18891898
ImplItemKind::Method(ref sig, _) => {
18901899
// If this is a trait impl, ensure the method
18911900
// exists in trait
1892-
this.check_trait_item(impl_item.ident.name,
1901+
this.check_trait_item(impl_item.ident,
18931902
ValueNS,
18941903
impl_item.span,
18951904
|n, s| ResolutionError::MethodNotMemberOfTrait(n, s));
@@ -1906,7 +1915,7 @@ impl<'a> Resolver<'a> {
19061915
ImplItemKind::Type(ref ty) => {
19071916
// If this is a trait impl, ensure the type
19081917
// exists in trait
1909-
this.check_trait_item(impl_item.ident.name,
1918+
this.check_trait_item(impl_item.ident,
19101919
TypeNS,
19111920
impl_item.span,
19121921
|n, s| ResolutionError::TypeNotMemberOfTrait(n, s));
@@ -1924,15 +1933,15 @@ impl<'a> Resolver<'a> {
19241933
});
19251934
}
19261935

1927-
fn check_trait_item<F>(&self, name: Name, ns: Namespace, span: Span, err: F)
1936+
fn check_trait_item<F>(&mut self, ident: Ident, ns: Namespace, span: Span, err: F)
19281937
where F: FnOnce(Name, &str) -> ResolutionError
19291938
{
19301939
// If there is a TraitRef in scope for an impl, then the method must be in the
19311940
// trait.
1932-
if let Some((did, ref trait_ref)) = self.current_trait_ref {
1933-
if !self.trait_item_map.contains_key(&(did, name, ns)) {
1934-
let path_str = path_names_to_string(&trait_ref.path);
1935-
resolve_error(self, span, err(name, &path_str));
1941+
if let Some((module, _)) = self.current_trait_ref {
1942+
if self.resolve_ident_in_module(module, ident, ns, false, false, span).is_err() {
1943+
let path = &self.current_trait_ref.as_ref().unwrap().1.path;
1944+
resolve_error(self, span, err(ident.name, &path_names_to_string(path)));
19361945
}
19371946
}
19381947
}
@@ -2302,15 +2311,16 @@ impl<'a> Resolver<'a> {
23022311
}
23032312

23042313
// Try to lookup the name in more relaxed fashion for better error reporting.
2305-
let name = path.last().unwrap().name;
2306-
let candidates = this.lookup_import_candidates(name, ns, is_expected);
2314+
let ident = *path.last().unwrap();
2315+
let candidates = this.lookup_import_candidates(ident.name, ns, is_expected);
23072316
if !candidates.is_empty() {
23082317
let mut module_span = this.current_module.span;
23092318
module_span.hi = module_span.lo;
23102319
// Report import candidates as help and proceed searching for labels.
23112320
show_candidates(&mut err, module_span, &candidates, def.is_some());
23122321
} else if is_expected(Def::Enum(DefId::local(CRATE_DEF_INDEX))) {
2313-
let enum_candidates = this.lookup_import_candidates(name, ns, is_enum_variant);
2322+
let enum_candidates =
2323+
this.lookup_import_candidates(ident.name, ns, is_enum_variant);
23142324
let mut enum_candidates = enum_candidates.iter()
23152325
.map(|suggestion| import_candidate_to_paths(&suggestion)).collect::<Vec<_>>();
23162326
enum_candidates.sort();
@@ -2326,7 +2336,7 @@ impl<'a> Resolver<'a> {
23262336
}
23272337
}
23282338
if path.len() == 1 && this.self_type_is_available(span) {
2329-
if let Some(candidate) = this.lookup_assoc_candidate(name, ns, is_expected) {
2339+
if let Some(candidate) = this.lookup_assoc_candidate(ident, ns, is_expected) {
23302340
let self_is_available = this.self_value_is_available(path[0].ctxt, span);
23312341
match candidate {
23322342
AssocSuggestion::Field => {
@@ -2440,7 +2450,7 @@ impl<'a> Resolver<'a> {
24402450
// or `<T>::A::B`. If `B` should be resolved in value namespace then
24412451
// it needs to be added to the trait map.
24422452
if ns == ValueNS {
2443-
let item_name = path.last().unwrap().name;
2453+
let item_name = *path.last().unwrap();
24442454
let traits = self.get_traits_containing_item(item_name, ns);
24452455
self.trait_map.insert(id, traits);
24462456
}
@@ -2819,7 +2829,7 @@ impl<'a> Resolver<'a> {
28192829
}
28202830

28212831
fn lookup_assoc_candidate<FilterFn>(&mut self,
2822-
name: Name,
2832+
ident: Ident,
28232833
ns: Namespace,
28242834
filter_fn: FilterFn)
28252835
-> Option<AssocSuggestion>
@@ -2845,7 +2855,7 @@ impl<'a> Resolver<'a> {
28452855
Def::Struct(did) | Def::Union(did)
28462856
if resolution.unresolved_segments() == 0 => {
28472857
if let Some(field_names) = self.field_names.get(&did) {
2848-
if field_names.iter().any(|&field_name| name == field_name) {
2858+
if field_names.iter().any(|&field_name| ident.name == field_name) {
28492859
return Some(AssocSuggestion::Field);
28502860
}
28512861
}
@@ -2857,10 +2867,12 @@ impl<'a> Resolver<'a> {
28572867
}
28582868

28592869
// Look for associated items in the current trait.
2860-
if let Some((trait_did, _)) = self.current_trait_ref {
2861-
if let Some(&(def, has_self)) = self.trait_item_map.get(&(trait_did, name, ns)) {
2870+
if let Some((module, _)) = self.current_trait_ref {
2871+
if let Ok(binding) =
2872+
self.resolve_ident_in_module(module, ident, ns, false, false, module.span) {
2873+
let def = binding.def();
28622874
if filter_fn(def) {
2863-
return Some(if has_self {
2875+
return Some(if self.has_self.contains(&def.def_id()) {
28642876
AssocSuggestion::MethodWithSelf
28652877
} else {
28662878
AssocSuggestion::AssocItem
@@ -3081,13 +3093,13 @@ impl<'a> Resolver<'a> {
30813093
// field, we need to add any trait methods we find that match
30823094
// the field name so that we can do some nice error reporting
30833095
// later on in typeck.
3084-
let traits = self.get_traits_containing_item(name.node.name, ValueNS);
3096+
let traits = self.get_traits_containing_item(name.node, ValueNS);
30853097
self.trait_map.insert(expr.id, traits);
30863098
}
30873099
ExprKind::MethodCall(name, ..) => {
30883100
debug!("(recording candidate traits for expr) recording traits for {}",
30893101
expr.id);
3090-
let traits = self.get_traits_containing_item(name.node.name, ValueNS);
3102+
let traits = self.get_traits_containing_item(name.node, ValueNS);
30913103
self.trait_map.insert(expr.id, traits);
30923104
}
30933105
_ => {
@@ -3096,20 +3108,21 @@ impl<'a> Resolver<'a> {
30963108
}
30973109
}
30983110

3099-
fn get_traits_containing_item(&mut self, name: Name, ns: Namespace) -> Vec<TraitCandidate> {
3100-
debug!("(getting traits containing item) looking for '{}'", name);
3111+
fn get_traits_containing_item(&mut self, ident: Ident, ns: Namespace) -> Vec<TraitCandidate> {
3112+
debug!("(getting traits containing item) looking for '{}'", ident.name);
31013113

31023114
let mut found_traits = Vec::new();
31033115
// Look for the current trait.
3104-
if let Some((trait_def_id, _)) = self.current_trait_ref {
3105-
if self.trait_item_map.contains_key(&(trait_def_id, name, ns)) {
3106-
found_traits.push(TraitCandidate { def_id: trait_def_id, import_id: None });
3116+
if let Some((module, _)) = self.current_trait_ref {
3117+
if self.resolve_ident_in_module(module, ident, ns, false, false, module.span).is_ok() {
3118+
let def_id = module.def_id().unwrap();
3119+
found_traits.push(TraitCandidate { def_id: def_id, import_id: None });
31073120
}
31083121
}
31093122

31103123
let mut search_module = self.current_module;
31113124
loop {
3112-
self.get_traits_in_module_containing_item(name, ns, search_module, &mut found_traits);
3125+
self.get_traits_in_module_containing_item(ident, ns, search_module, &mut found_traits);
31133126
match search_module.kind {
31143127
ModuleKind::Block(..) => search_module = search_module.parent.unwrap(),
31153128
_ => break,
@@ -3118,17 +3131,17 @@ impl<'a> Resolver<'a> {
31183131

31193132
if let Some(prelude) = self.prelude {
31203133
if !search_module.no_implicit_prelude {
3121-
self.get_traits_in_module_containing_item(name, ns, prelude, &mut found_traits);
3134+
self.get_traits_in_module_containing_item(ident, ns, prelude, &mut found_traits);
31223135
}
31233136
}
31243137

31253138
found_traits
31263139
}
31273140

31283141
fn get_traits_in_module_containing_item(&mut self,
3129-
name: Name,
3142+
ident: Ident,
31303143
ns: Namespace,
3131-
module: Module,
3144+
module: Module<'a>,
31323145
found_traits: &mut Vec<TraitCandidate>) {
31333146
let mut traits = module.traits.borrow_mut();
31343147
if traits.is_none() {
@@ -3143,8 +3156,8 @@ impl<'a> Resolver<'a> {
31433156
}
31443157

31453158
for &(trait_name, binding) in traits.as_ref().unwrap().iter() {
3146-
let trait_def_id = binding.def().def_id();
3147-
if self.trait_item_map.contains_key(&(trait_def_id, name, ns)) {
3159+
let module = binding.module().unwrap();
3160+
if self.resolve_ident_in_module(module, ident, ns, false, false, module.span).is_ok() {
31483161
let import_id = match binding.kind {
31493162
NameBindingKind::Import { directive, .. } => {
31503163
self.maybe_unused_trait_imports.insert(directive.id);
@@ -3153,6 +3166,7 @@ impl<'a> Resolver<'a> {
31533166
}
31543167
_ => None,
31553168
};
3169+
let trait_def_id = module.def_id().unwrap();
31563170
found_traits.push(TraitCandidate { def_id: trait_def_id, import_id: import_id });
31573171
}
31583172
}

0 commit comments

Comments
 (0)