Skip to content

Commit b11da34

Browse files
committed
do not access associated_item map directly
1 parent f35ff22 commit b11da34

File tree

4 files changed

+40
-25
lines changed

4 files changed

+40
-25
lines changed

src/librustc/ty/mod.rs

+29-10
Original file line numberDiff line numberDiff line change
@@ -2139,6 +2139,26 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
21392139
})
21402140
}
21412141

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

23972417
match item {
@@ -2412,15 +2432,13 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
24122432
if def_id.krate != LOCAL_CRATE {
24132433
return self.sess.cstore.trait_of_item(def_id);
24142434
}
2415-
match self.maps.associated_item.borrow().get(&def_id) {
2416-
Some(associated_item) => {
2435+
self.opt_associated_item(def_id)
2436+
.and_then(|associated_item| {
24172437
match associated_item.container {
24182438
TraitContainer(def_id) => Some(def_id),
24192439
ImplContainer(_) => None
24202440
}
2421-
}
2422-
None => None
2423-
}
2441+
})
24242442
}
24252443

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

2591-
ref r => {
2592-
panic!("unexpected container of associated items: {:?}", r)
2593-
}
2609+
_ => { }
25942610
}
2595-
panic!("associated item not found for def_id: {:?}", def_id);
2611+
2612+
span_bug!(parent_item.span,
2613+
"unexpected parent of trait or impl item or item not found: {:?}",
2614+
parent_item.node)
25962615
}
25972616

25982617
/// Calculates the Sized-constraint.

src/librustc_lint/bad_style.rs

+9-13
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

+1-1
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ impl<'a, 'tcx> CrateMetadata {
827827
EntryKind::AssociatedType(container) => {
828828
(ty::AssociatedKind::Type, container, false)
829829
}
830-
_ => bug!()
830+
_ => bug!("cannot get associated-item of `{:?}`", def_key)
831831
};
832832

833833
ty::AssociatedItem {

0 commit comments

Comments
 (0)