Skip to content

Commit cd9dae9

Browse files
committed
do not access associated_item map directly
1 parent e19e594 commit cd9dae9

File tree

4 files changed

+40
-25
lines changed

4 files changed

+40
-25
lines changed

src/librustc/ty/mod.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2138,6 +2138,26 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
21382138
})
21392139
}
21402140

2141+
pub fn opt_associated_item(self, def_id: DefId) -> Option<AssociatedItem> {
2142+
let is_associated_item = if let Some(node_id) = self.hir.as_local_node_id(def_id) {
2143+
match self.hir.get(node_id) {
2144+
hir_map::NodeTraitItem(_) | hir_map::NodeImplItem(_) => true,
2145+
_ => false,
2146+
}
2147+
} else {
2148+
match self.sess.cstore.describe_def(def_id).expect("no def for def-id") {
2149+
Def::AssociatedConst(_) | Def::Method(_) | Def::AssociatedTy(_) => true,
2150+
_ => false,
2151+
}
2152+
};
2153+
2154+
if is_associated_item {
2155+
Some(self.associated_item(def_id))
2156+
} else {
2157+
None
2158+
}
2159+
}
2160+
21412161
fn associated_item_from_trait_item_ref(self,
21422162
parent_def_id: DefId,
21432163
parent_vis: &hir::Visibility,
@@ -2390,7 +2410,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
23902410
None
23912411
}
23922412
} else {
2393-
self.maps.associated_item.borrow().get(&def_id).cloned()
2413+
self.opt_associated_item(def_id)
23942414
};
23952415

23962416
match item {
@@ -2411,15 +2431,13 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
24112431
if def_id.krate != LOCAL_CRATE {
24122432
return self.sess.cstore.trait_of_item(def_id);
24132433
}
2414-
match self.maps.associated_item.borrow().get(&def_id) {
2415-
Some(associated_item) => {
2434+
self.opt_associated_item(def_id)
2435+
.and_then(|associated_item| {
24162436
match associated_item.container {
24172437
TraitContainer(def_id) => Some(def_id),
24182438
ImplContainer(_) => None
24192439
}
2420-
}
2421-
None => None
2422-
}
2440+
})
24232441
}
24242442

24252443
/// Construct a parameter environment suitable for static contexts or other contexts where there
@@ -2587,11 +2605,12 @@ fn associated_item<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId)
25872605
}
25882606
}
25892607

2590-
ref r => {
2591-
panic!("unexpected container of associated items: {:?}", r)
2592-
}
2608+
_ => { }
25932609
}
2594-
panic!("associated item not found for def_id: {:?}", def_id);
2610+
2611+
span_bug!(parent_item.span,
2612+
"unexpected parent of trait or impl item or item not found: {:?}",
2613+
parent_item.node)
25952614
}
25962615

25972616
/// Calculates the Sized-constraint.

src/librustc_lint/bad_style.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,15 @@ pub enum MethodLateContext {
2727
PlainImpl,
2828
}
2929

30-
pub fn method_context(cx: &LateContext, id: ast::NodeId, span: Span) -> MethodLateContext {
30+
pub fn method_context(cx: &LateContext, id: ast::NodeId) -> MethodLateContext {
3131
let def_id = cx.tcx.hir.local_def_id(id);
32-
match cx.tcx.maps.associated_item.borrow().get(&def_id) {
33-
None => span_bug!(span, "missing method descriptor?!"),
34-
Some(item) => {
35-
match item.container {
36-
ty::TraitContainer(..) => MethodLateContext::TraitDefaultImpl,
37-
ty::ImplContainer(cid) => {
38-
match cx.tcx.impl_trait_ref(cid) {
39-
Some(_) => MethodLateContext::TraitImpl,
40-
None => MethodLateContext::PlainImpl,
41-
}
42-
}
32+
let item = cx.tcx.associated_item(def_id);
33+
match item.container {
34+
ty::TraitContainer(..) => MethodLateContext::TraitDefaultImpl,
35+
ty::ImplContainer(cid) => {
36+
match cx.tcx.impl_trait_ref(cid) {
37+
Some(_) => MethodLateContext::TraitImpl,
38+
None => MethodLateContext::PlainImpl,
4339
}
4440
}
4541
}
@@ -244,7 +240,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonSnakeCase {
244240
id: ast::NodeId) {
245241
match fk {
246242
FnKind::Method(name, ..) => {
247-
match method_context(cx, id, span) {
243+
match method_context(cx, id) {
248244
MethodLateContext::PlainImpl => {
249245
self.check_snake_case(cx, "method", &name.as_str(), Some(span))
250246
}

src/librustc_lint/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
432432

433433
fn check_impl_item(&mut self, cx: &LateContext, impl_item: &hir::ImplItem) {
434434
// If the method is an impl for a trait, don't doc.
435-
if method_context(cx, impl_item.id, impl_item.span) == MethodLateContext::TraitImpl {
435+
if method_context(cx, impl_item.id) == MethodLateContext::TraitImpl {
436436
return;
437437
}
438438

src/librustc_metadata/decoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ impl<'a, 'tcx> CrateMetadata {
823823
EntryKind::AssociatedType(container) => {
824824
(ty::AssociatedKind::Type, container, false)
825825
}
826-
_ => bug!()
826+
_ => bug!("cannot get associated-item of `{:?}`", def_key)
827827
};
828828

829829
ty::AssociatedItem {

0 commit comments

Comments
 (0)