diff --git a/src/librustc/middle/def_id.rs b/src/librustc/middle/def_id.rs index b91ccc2f7822..2966339f0a4f 100644 --- a/src/librustc/middle/def_id.rs +++ b/src/librustc/middle/def_id.rs @@ -8,8 +8,8 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +use middle::ty; use syntax::ast::{CrateNum, NodeId}; -use std::cell::Cell; use std::fmt; #[derive(Clone, Eq, Ord, PartialOrd, PartialEq, RustcEncodable, @@ -19,19 +19,26 @@ pub struct DefId { pub node: NodeId, } -fn default_def_id_debug(_: DefId, _: &mut fmt::Formatter) -> fmt::Result { Ok(()) } - -thread_local!(pub static DEF_ID_DEBUG: Cell fmt::Result> = - Cell::new(default_def_id_debug)); - impl fmt::Debug for DefId { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - try!(write!(f, "DefId {{ krate: {}, node: {} }}", + try!(write!(f, "DefId {{ krate: {}, node: {}", self.krate, self.node)); - DEF_ID_DEBUG.with(|def_id_debug| def_id_debug.get()(*self, f)) + + // Unfortunately, there seems to be no way to attempt to print + // a path for a def-id, so I'll just make a best effort for now + // and otherwise fallback to just printing the crate/node pair + try!(ty::tls::with_opt(|opt_tcx| { + if let Some(tcx) = opt_tcx { + try!(write!(f, " => {}", tcx.item_path_str(*self))); + } + Ok(()) + })); + + write!(f, " }}") } } + impl DefId { pub fn local(id: NodeId) -> DefId { DefId { krate: LOCAL_CRATE, node: id } diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index 0a4b935f1a24..7418d81ed3dc 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -1093,8 +1093,6 @@ impl<'tcx, T: Lift<'tcx>> Lift<'tcx> for Binder { } pub mod tls { - use ast_map; - use middle::def_id::{DefId, DEF_ID_DEBUG, LOCAL_CRATE}; use middle::ty; use session::Session; @@ -1108,28 +1106,6 @@ pub mod tls { scoped_thread_local!(static TLS_TCX: ThreadLocalTyCx); - fn def_id_debug(def_id: DefId, f: &mut fmt::Formatter) -> fmt::Result { - // Unfortunately, there seems to be no way to attempt to print - // a path for a def-id, so I'll just make a best effort for now - // and otherwise fallback to just printing the crate/node pair - with(|tcx| { - if def_id.krate == LOCAL_CRATE { - match tcx.map.find(def_id.node) { - Some(ast_map::NodeItem(..)) | - Some(ast_map::NodeForeignItem(..)) | - Some(ast_map::NodeImplItem(..)) | - Some(ast_map::NodeTraitItem(..)) | - Some(ast_map::NodeVariant(..)) | - Some(ast_map::NodeStructCtor(..)) => { - return write!(f, "{}", tcx.item_path_str(def_id)); - } - _ => {} - } - } - Ok(()) - }) - } - fn span_debug(span: codemap::Span, f: &mut fmt::Formatter) -> fmt::Result { with(|tcx| { write!(f, "{}", tcx.sess.codemap().span_to_string(span)) @@ -1138,18 +1114,13 @@ pub mod tls { pub fn enter<'tcx, F: FnOnce(&ty::ctxt<'tcx>) -> R, R>(tcx: ty::ctxt<'tcx>, f: F) -> (Session, R) { - let result = DEF_ID_DEBUG.with(|def_id_dbg| { - codemap::SPAN_DEBUG.with(|span_dbg| { - let original_def_id_debug = def_id_dbg.get(); - def_id_dbg.set(def_id_debug); - let original_span_debug = span_dbg.get(); - span_dbg.set(span_debug); - let tls_ptr = &tcx as *const _ as *const ThreadLocalTyCx; - let result = TLS_TCX.set(unsafe { &*tls_ptr }, || f(&tcx)); - def_id_dbg.set(original_def_id_debug); - span_dbg.set(original_span_debug); - result - }) + let result = codemap::SPAN_DEBUG.with(|span_dbg| { + let original_span_debug = span_dbg.get(); + span_dbg.set(span_debug); + let tls_ptr = &tcx as *const _ as *const ThreadLocalTyCx; + let result = TLS_TCX.set(unsafe { &*tls_ptr }, || f(&tcx)); + span_dbg.set(original_span_debug); + result }); (tcx.sess, result) } @@ -1157,6 +1128,14 @@ pub mod tls { pub fn with R, R>(f: F) -> R { TLS_TCX.with(|tcx| f(unsafe { &*(tcx as *const _ as *const ty::ctxt) })) } + + pub fn with_opt) -> R, R>(f: F) -> R { + if TLS_TCX.is_set() { + with(|v| f(Some(v))) + } else { + f(None) + } + } } // Flags that we track on types. These flags are propagated upwards