Skip to content

Commit 1994875

Browse files
committed
purge DEF_ID_DEBUG TLS variable, and just always print a path, since I
think it can no longer panic
1 parent c0de23d commit 1994875

File tree

2 files changed

+30
-44
lines changed

2 files changed

+30
-44
lines changed

src/librustc/middle/def_id.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use middle::ty;
1112
use syntax::ast::{CrateNum, NodeId};
12-
use std::cell::Cell;
1313
use std::fmt;
1414

1515
#[derive(Clone, Eq, Ord, PartialOrd, PartialEq, RustcEncodable,
@@ -19,19 +19,26 @@ pub struct DefId {
1919
pub node: NodeId,
2020
}
2121

22-
fn default_def_id_debug(_: DefId, _: &mut fmt::Formatter) -> fmt::Result { Ok(()) }
23-
24-
thread_local!(pub static DEF_ID_DEBUG: Cell<fn(DefId, &mut fmt::Formatter) -> fmt::Result> =
25-
Cell::new(default_def_id_debug));
26-
2722
impl fmt::Debug for DefId {
2823
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29-
try!(write!(f, "DefId {{ krate: {}, node: {} }}",
24+
try!(write!(f, "DefId {{ krate: {}, node: {}",
3025
self.krate, self.node));
31-
DEF_ID_DEBUG.with(|def_id_debug| def_id_debug.get()(*self, f))
26+
27+
// Unfortunately, there seems to be no way to attempt to print
28+
// a path for a def-id, so I'll just make a best effort for now
29+
// and otherwise fallback to just printing the crate/node pair
30+
try!(ty::tls::with_opt(|opt_tcx| {
31+
if let Some(tcx) = opt_tcx {
32+
try!(write!(f, " => {}", tcx.item_path_str(*self)));
33+
}
34+
Ok(())
35+
}));
36+
37+
write!(f, " }}")
3238
}
3339
}
3440

41+
3542
impl DefId {
3643
pub fn local(id: NodeId) -> DefId {
3744
DefId { krate: LOCAL_CRATE, node: id }

src/librustc/middle/ty.rs

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,8 +1093,6 @@ impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Binder<T> {
10931093
}
10941094

10951095
pub mod tls {
1096-
use ast_map;
1097-
use middle::def_id::{DefId, DEF_ID_DEBUG, LOCAL_CRATE};
10981096
use middle::ty;
10991097
use session::Session;
11001098

@@ -1108,28 +1106,6 @@ pub mod tls {
11081106

11091107
scoped_thread_local!(static TLS_TCX: ThreadLocalTyCx);
11101108

1111-
fn def_id_debug(def_id: DefId, f: &mut fmt::Formatter) -> fmt::Result {
1112-
// Unfortunately, there seems to be no way to attempt to print
1113-
// a path for a def-id, so I'll just make a best effort for now
1114-
// and otherwise fallback to just printing the crate/node pair
1115-
with(|tcx| {
1116-
if def_id.krate == LOCAL_CRATE {
1117-
match tcx.map.find(def_id.node) {
1118-
Some(ast_map::NodeItem(..)) |
1119-
Some(ast_map::NodeForeignItem(..)) |
1120-
Some(ast_map::NodeImplItem(..)) |
1121-
Some(ast_map::NodeTraitItem(..)) |
1122-
Some(ast_map::NodeVariant(..)) |
1123-
Some(ast_map::NodeStructCtor(..)) => {
1124-
return write!(f, "{}", tcx.item_path_str(def_id));
1125-
}
1126-
_ => {}
1127-
}
1128-
}
1129-
Ok(())
1130-
})
1131-
}
1132-
11331109
fn span_debug(span: codemap::Span, f: &mut fmt::Formatter) -> fmt::Result {
11341110
with(|tcx| {
11351111
write!(f, "{}", tcx.sess.codemap().span_to_string(span))
@@ -1138,25 +1114,28 @@ pub mod tls {
11381114

11391115
pub fn enter<'tcx, F: FnOnce(&ty::ctxt<'tcx>) -> R, R>(tcx: ty::ctxt<'tcx>, f: F)
11401116
-> (Session, R) {
1141-
let result = DEF_ID_DEBUG.with(|def_id_dbg| {
1142-
codemap::SPAN_DEBUG.with(|span_dbg| {
1143-
let original_def_id_debug = def_id_dbg.get();
1144-
def_id_dbg.set(def_id_debug);
1145-
let original_span_debug = span_dbg.get();
1146-
span_dbg.set(span_debug);
1147-
let tls_ptr = &tcx as *const _ as *const ThreadLocalTyCx;
1148-
let result = TLS_TCX.set(unsafe { &*tls_ptr }, || f(&tcx));
1149-
def_id_dbg.set(original_def_id_debug);
1150-
span_dbg.set(original_span_debug);
1151-
result
1152-
})
1117+
let result = codemap::SPAN_DEBUG.with(|span_dbg| {
1118+
let original_span_debug = span_dbg.get();
1119+
span_dbg.set(span_debug);
1120+
let tls_ptr = &tcx as *const _ as *const ThreadLocalTyCx;
1121+
let result = TLS_TCX.set(unsafe { &*tls_ptr }, || f(&tcx));
1122+
span_dbg.set(original_span_debug);
1123+
result
11531124
});
11541125
(tcx.sess, result)
11551126
}
11561127

11571128
pub fn with<F: FnOnce(&ty::ctxt) -> R, R>(f: F) -> R {
11581129
TLS_TCX.with(|tcx| f(unsafe { &*(tcx as *const _ as *const ty::ctxt) }))
11591130
}
1131+
1132+
pub fn with_opt<F: FnOnce(Option<&ty::ctxt>) -> R, R>(f: F) -> R {
1133+
if TLS_TCX.is_set() {
1134+
with(|v| f(Some(v)))
1135+
} else {
1136+
f(None)
1137+
}
1138+
}
11601139
}
11611140

11621141
// Flags that we track on types. These flags are propagated upwards

0 commit comments

Comments
 (0)