about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNiko Matsakis <niko@alum.mit.edu>2015-08-16 08:52:36 -0400
committerNiko Matsakis <niko@alum.mit.edu>2015-08-24 05:35:34 -0400
commit19948751bd5487b4f7e8ba6943915a288a9be785 (patch)
tree89d0b566e67b3c00fcccd292dcb9f26e723767c6
parentc0de23de814f8e29475c5d33c03e890512797b8e (diff)
downloadrust-19948751bd5487b4f7e8ba6943915a288a9be785.tar.gz
rust-19948751bd5487b4f7e8ba6943915a288a9be785.zip
purge DEF_ID_DEBUG TLS variable, and just always print a path, since I
think it can no longer panic
-rw-r--r--src/librustc/middle/def_id.rs23
-rw-r--r--src/librustc/middle/ty.rs51
2 files changed, 30 insertions, 44 deletions
diff --git a/src/librustc/middle/def_id.rs b/src/librustc/middle/def_id.rs
index b91ccc2f782..2966339f0a4 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<fn(DefId, &mut fmt::Formatter) -> 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 0a4b935f1a2..7418d81ed3d 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<T> {
 }
 
 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<F: FnOnce(&ty::ctxt) -> R, R>(f: F) -> R {
         TLS_TCX.with(|tcx| f(unsafe { &*(tcx as *const _ as *const ty::ctxt) }))
     }
+
+    pub fn with_opt<F: FnOnce(Option<&ty::ctxt>) -> 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