diff --git a/Cargo.lock b/Cargo.lock index aff2d7e65af..6c14056bdba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3091,6 +3091,7 @@ dependencies = [ "rustc_errors", "rustc_feature", "rustc_fs_util", + "rustc_hir", "rustc_index", "rustc_macros", "rustc_session", @@ -3563,6 +3564,42 @@ dependencies = [ name = "rustc_fs_util" version = "0.0.0" +[[package]] +name = "rustc_hir" +version = "0.0.0" +dependencies = [ + "arena", + "backtrace", + "bitflags", + "byteorder", + "chalk-engine", + "fmt_macros", + "graphviz", + "jobserver", + "log", + "measureme", + "num_cpus", + "parking_lot", + "polonius-engine", + "rustc-rayon", + "rustc-rayon-core", + "rustc_apfloat", + "rustc_data_structures", + "rustc_error_codes", + "rustc_errors", + "rustc_feature", + "rustc_fs_util", + "rustc_index", + "rustc_macros", + "rustc_session", + "rustc_span", + "rustc_target", + "scoped-tls", + "serialize", + "smallvec 1.0.0", + "syntax", +] + [[package]] name = "rustc_incremental" version = "0.0.0" @@ -3603,6 +3640,7 @@ dependencies = [ "rustc_data_structures", "rustc_errors", "rustc_expand", + "rustc_hir", "rustc_incremental", "rustc_lint", "rustc_metadata", diff --git a/src/librustc/Cargo.toml b/src/librustc/Cargo.toml index 973384bcf05..2e882cfdafd 100644 --- a/src/librustc/Cargo.toml +++ b/src/librustc/Cargo.toml @@ -23,6 +23,7 @@ rustc-rayon-core = "0.3.0" polonius-engine = "0.11.0" rustc_apfloat = { path = "../librustc_apfloat" } rustc_feature = { path = "../librustc_feature" } +rustc_hir = { path = "../librustc_hir" } rustc_target = { path = "../librustc_target" } rustc_macros = { path = "../librustc_macros" } rustc_data_structures = { path = "../librustc_data_structures" } diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs index ce1b8ca2f88..d7d9990bb88 100644 --- a/src/librustc/hir/mod.rs +++ b/src/librustc/hir/mod.rs @@ -34,7 +34,7 @@ use syntax::util::parser::ExprPrecedence; pub mod check_attr; pub mod def; -pub mod def_id; +pub use rustc_hir::def_id; pub mod intravisit; pub mod itemlikevisit; pub mod map; diff --git a/src/librustc_hir/Cargo.toml b/src/librustc_hir/Cargo.toml new file mode 100644 index 00000000000..24df1130368 --- /dev/null +++ b/src/librustc_hir/Cargo.toml @@ -0,0 +1,42 @@ +[package] +authors = ["The Rust Project Developers"] +name = "rustc_hir" +version = "0.0.0" +edition = "2018" + +[lib] +name = "rustc_hir" +path = "lib.rs" +doctest = false + +[dependencies] +arena = { path = "../libarena" } +bitflags = "1.2.1" +fmt_macros = { path = "../libfmt_macros" } +graphviz = { path = "../libgraphviz" } +jobserver = "0.1" +num_cpus = "1.0" +scoped-tls = "1.0" +log = { version = "0.4", features = ["release_max_level_info", "std"] } +rustc-rayon = "0.3.0" +rustc-rayon-core = "0.3.0" +polonius-engine = "0.11.0" +rustc_apfloat = { path = "../librustc_apfloat" } +rustc_feature = { path = "../librustc_feature" } +rustc_target = { path = "../librustc_target" } +rustc_macros = { path = "../librustc_macros" } +rustc_data_structures = { path = "../librustc_data_structures" } +rustc_index = { path = "../librustc_index" } +rustc_span = { path = "../librustc_span" } +errors = { path = "../librustc_errors", package = "rustc_errors" } +rustc_serialize = { path = "../libserialize", package = "serialize" } +syntax = { path = "../libsyntax" } +backtrace = "0.3.40" +parking_lot = "0.9" +byteorder = { version = "1.3" } +chalk-engine = { version = "0.9.0", default-features=false } +rustc_fs_util = { path = "../librustc_fs_util" } +smallvec = { version = "1.0", features = ["union", "may_dangle"] } +measureme = "0.5" +rustc_error_codes = { path = "../librustc_error_codes" } +rustc_session = { path = "../librustc_session" } diff --git a/src/librustc/hir/def_id.rs b/src/librustc_hir/def_id.rs similarity index 88% rename from src/librustc/hir/def_id.rs rename to src/librustc_hir/def_id.rs index 150fa5e8bf5..f8cacdc6238 100644 --- a/src/librustc/hir/def_id.rs +++ b/src/librustc_hir/def_id.rs @@ -1,4 +1,4 @@ -use crate::ty; +use rustc_data_structures::AtomicRef; use rustc_index::vec::Idx; use std::fmt; use std::u32; @@ -40,7 +40,7 @@ impl Idx for CrateNum { fn index(self) -> usize { match self { CrateNum::Index(idx) => Idx::index(idx), - _ => bug!("Tried to get crate index of {:?}", self), + _ => panic!("Tried to get crate index of {:?}", self), } } } @@ -61,14 +61,14 @@ impl CrateNum { pub fn as_usize(self) -> usize { match self { CrateNum::Index(id) => id.as_usize(), - _ => bug!("tried to get index of non-standard crate {:?}", self), + _ => panic!("tried to get index of non-standard crate {:?}", self), } } pub fn as_u32(self) -> u32 { match self { CrateNum::Index(id) => id.as_u32(), - _ => bug!("tried to get index of non-standard crate {:?}", self), + _ => panic!("tried to get index of non-standard crate {:?}", self), } } @@ -113,21 +113,6 @@ pub struct DefId { pub index: DefIndex, } -impl fmt::Debug for DefId { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "DefId({}:{}", self.krate, self.index.index())?; - - ty::tls::with_opt(|opt_tcx| { - if let Some(tcx) = opt_tcx { - write!(f, " ~ {}", tcx.def_path_debug_str(*self))?; - } - Ok(()) - })?; - - write!(f, ")") - } -} - impl DefId { /// Makes a local `DefId` from the given `DefIndex`. #[inline] @@ -153,6 +138,19 @@ impl DefId { impl rustc_serialize::UseSpecializedEncodable for DefId {} impl rustc_serialize::UseSpecializedDecodable for DefId {} +pub fn default_def_id_debug(def_id: DefId, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("DefId").field("krate", &def_id.krate).field("index", &def_id.index).finish() +} + +pub static DEF_ID_DEBUG: AtomicRef) -> fmt::Result> = + AtomicRef::new(&(default_def_id_debug as fn(_, &mut fmt::Formatter<'_>) -> _)); + +impl fmt::Debug for DefId { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + (*DEF_ID_DEBUG)(*self, f) + } +} + rustc_data_structures::define_id_collections!(DefIdMap, DefIdSet, DefId); /// A LocalDefId is equivalent to a DefId with `krate == LOCAL_CRATE`. Since diff --git a/src/librustc_hir/lib.rs b/src/librustc_hir/lib.rs new file mode 100644 index 00000000000..1ef1694c320 --- /dev/null +++ b/src/librustc_hir/lib.rs @@ -0,0 +1,3 @@ +#![feature(specialization)] + +pub mod def_id; diff --git a/src/librustc_interface/Cargo.toml b/src/librustc_interface/Cargo.toml index 9e2fae178d5..be60b75bc47 100644 --- a/src/librustc_interface/Cargo.toml +++ b/src/librustc_interface/Cargo.toml @@ -27,6 +27,7 @@ rustc_data_structures = { path = "../librustc_data_structures" } rustc_codegen_ssa = { path = "../librustc_codegen_ssa" } rustc_codegen_utils = { path = "../librustc_codegen_utils" } rustc_codegen_llvm = { path = "../librustc_codegen_llvm", optional = true } +rustc_hir = { path = "../librustc_hir" } rustc_metadata = { path = "../librustc_metadata" } rustc_mir = { path = "../librustc_mir" } rustc_passes = { path = "../librustc_passes" } diff --git a/src/librustc_interface/callbacks.rs b/src/librustc_interface/callbacks.rs index 1ed8d66863c..eb9c118bb01 100644 --- a/src/librustc_interface/callbacks.rs +++ b/src/librustc_interface/callbacks.rs @@ -40,9 +40,23 @@ fn track_diagnostic(diagnostic: &Diagnostic) { }) } +/// This is a callback from librustc_hir as it cannot access the implicit state +/// in librustc otherwise. +fn def_id_debug(def_id: rustc_hir::def_id::DefId, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "DefId({}:{}", def_id.krate, def_id.index.index())?; + tls::with_opt(|opt_tcx| { + if let Some(tcx) = opt_tcx { + write!(f, " ~ {}", tcx.def_path_debug_str(def_id))?; + } + Ok(()) + })?; + write!(f, ")") +} + /// Sets up the callbacks in prior crates which we want to refer to the /// TyCtxt in. pub fn setup_callbacks() { rustc_span::SPAN_DEBUG.swap(&(span_debug as fn(_, &mut fmt::Formatter<'_>) -> _)); + rustc_hir::def_id::DEF_ID_DEBUG.swap(&(def_id_debug as fn(_, &mut fmt::Formatter<'_>) -> _)); TRACK_DIAGNOSTICS.swap(&(track_diagnostic as fn(&_))); }