Skip to content

Commit ab03f85

Browse files
authored
Auto merge of #37463 - jseyfried:refactor_macro_reexports, r=nrc
macros: improve reexports This PR - avoids building multiple module graphs for a crate that is referenced by multiple `extern crate` items, - registers `#[no_link] extern crate`s to avoid loading the same crate metadata twice, - stability checks `#[no_link] extern crate`s, - [breaking-chage]: `#[no_link] #[macro_use] extern crate syntax;` is allowed on stable today - fixes `$crate` in `#[macro_reexport]`ed macros, - [breaking-change] for `#[feature(macro_reexport)]` (technically) - allows selective macro importing (i.e. `#[macro_use(foo, bar)]`) from custom derive crates, and - refactors the crate metadata to support re-exported macros in arbitrary modules (not yet needed). r? @nrc
2 parents c11e2bd + a0a9f8c commit ab03f85

File tree

24 files changed

+477
-419
lines changed

24 files changed

+477
-419
lines changed

src/librustc/hir/def.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ pub enum Def {
5252
ast::NodeId), // expr node that creates the closure
5353
Label(ast::NodeId),
5454

55+
// Macro namespace
56+
Macro(DefId),
57+
5558
// Both namespaces
5659
Err,
5760
}
@@ -133,7 +136,7 @@ impl Def {
133136
Def::Variant(id) | Def::VariantCtor(id, ..) | Def::Enum(id) | Def::TyAlias(id) |
134137
Def::AssociatedTy(id) | Def::TyParam(id) | Def::Struct(id) | Def::StructCtor(id, ..) |
135138
Def::Union(id) | Def::Trait(id) | Def::Method(id) | Def::Const(id) |
136-
Def::AssociatedConst(id) | Def::Local(id) | Def::Upvar(id, ..) => {
139+
Def::AssociatedConst(id) | Def::Local(id) | Def::Upvar(id, ..) | Def::Macro(id) => {
137140
id
138141
}
139142

@@ -173,6 +176,7 @@ impl Def {
173176
Def::Upvar(..) => "closure capture",
174177
Def::Label(..) => "label",
175178
Def::SelfTy(..) => "self type",
179+
Def::Macro(..) => "macro",
176180
Def::Err => "unresolved item",
177181
}
178182
}

src/librustc/hir/def_id.rs

+4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ impl Idx for CrateNum {
3434
/// LOCAL_CRATE in their DefId.
3535
pub const LOCAL_CRATE: CrateNum = CrateNum(0);
3636

37+
/// Virtual crate for builtin macros
38+
// FIXME(jseyfried): this is also used for custom derives until proc-macro crates get `CrateNum`s.
39+
pub const BUILTIN_MACROS_CRATE: CrateNum = CrateNum(!0);
40+
3741
impl CrateNum {
3842
pub fn new(x: usize) -> CrateNum {
3943
assert!(x < (u32::MAX as usize));

src/librustc/hir/map/collector.rs

+4
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,8 @@ impl<'ast> Visitor<'ast> for NodeCollector<'ast> {
226226
fn visit_lifetime(&mut self, lifetime: &'ast Lifetime) {
227227
self.insert(lifetime.id, NodeLifetime(lifetime));
228228
}
229+
230+
fn visit_macro_def(&mut self, macro_def: &'ast MacroDef) {
231+
self.insert_entry(macro_def.id, NotPresent);
232+
}
229233
}

src/librustc/middle/cstore.rs

+17-18
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,18 @@ pub struct LinkMeta {
6161
pub struct CrateSource {
6262
pub dylib: Option<(PathBuf, PathKind)>,
6363
pub rlib: Option<(PathBuf, PathKind)>,
64-
pub cnum: CrateNum,
64+
}
65+
66+
#[derive(RustcEncodable, RustcDecodable, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug)]
67+
pub enum DepKind {
68+
/// A dependency that is only used for its macros.
69+
MacrosOnly,
70+
/// A dependency that is always injected into the dependency list and so
71+
/// doesn't need to be linked to an rlib, e.g. the injected allocator.
72+
Implicit,
73+
/// A dependency that is required by an rlib version of this crate.
74+
/// Ordinary `extern crate`s result in `Explicit` dependencies.
75+
Explicit,
6576
}
6677

6778
#[derive(Copy, Debug, PartialEq, Clone, RustcEncodable, RustcDecodable)]
@@ -170,10 +181,10 @@ pub trait CrateStore<'tcx> {
170181
// crate metadata
171182
fn dylib_dependency_formats(&self, cnum: CrateNum)
172183
-> Vec<(CrateNum, LinkagePreference)>;
184+
fn dep_kind(&self, cnum: CrateNum) -> DepKind;
173185
fn lang_items(&self, cnum: CrateNum) -> Vec<(DefIndex, usize)>;
174186
fn missing_lang_items(&self, cnum: CrateNum) -> Vec<lang_items::LangItem>;
175187
fn is_staged_api(&self, cnum: CrateNum) -> bool;
176-
fn is_explicitly_linked(&self, cnum: CrateNum) -> bool;
177188
fn is_allocator(&self, cnum: CrateNum) -> bool;
178189
fn is_panic_runtime(&self, cnum: CrateNum) -> bool;
179190
fn is_compiler_builtins(&self, cnum: CrateNum) -> bool;
@@ -200,6 +211,7 @@ pub trait CrateStore<'tcx> {
200211
fn relative_def_path(&self, def: DefId) -> Option<hir_map::DefPath>;
201212
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>;
202213
fn item_children(&self, did: DefId) -> Vec<def::Export>;
214+
fn load_macro(&self, did: DefId, sess: &Session) -> ast::MacroDef;
203215

204216
// misc. metadata
205217
fn maybe_get_item_ast<'a>(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
@@ -342,7 +354,7 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
342354
fn missing_lang_items(&self, cnum: CrateNum) -> Vec<lang_items::LangItem>
343355
{ bug!("missing_lang_items") }
344356
fn is_staged_api(&self, cnum: CrateNum) -> bool { bug!("is_staged_api") }
345-
fn is_explicitly_linked(&self, cnum: CrateNum) -> bool { bug!("is_explicitly_linked") }
357+
fn dep_kind(&self, cnum: CrateNum) -> DepKind { bug!("is_explicitly_linked") }
346358
fn is_allocator(&self, cnum: CrateNum) -> bool { bug!("is_allocator") }
347359
fn is_panic_runtime(&self, cnum: CrateNum) -> bool { bug!("is_panic_runtime") }
348360
fn is_compiler_builtins(&self, cnum: CrateNum) -> bool { bug!("is_compiler_builtins") }
@@ -371,6 +383,7 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
371383
}
372384
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name> { bug!("struct_field_names") }
373385
fn item_children(&self, did: DefId) -> Vec<def::Export> { bug!("item_children") }
386+
fn load_macro(&self, did: DefId, sess: &Session) -> ast::MacroDef { bug!("load_macro") }
374387

375388
// misc. metadata
376389
fn maybe_get_item_ast<'a>(&'tcx self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
@@ -410,22 +423,8 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
410423
fn metadata_encoding_version(&self) -> &[u8] { bug!("metadata_encoding_version") }
411424
}
412425

413-
pub enum LoadedMacros {
414-
MacroRules(Vec<ast::MacroDef>),
415-
ProcMacros(Vec<(ast::Name, SyntaxExtension)>),
416-
}
417-
418-
impl LoadedMacros {
419-
pub fn is_proc_macros(&self) -> bool {
420-
match *self {
421-
LoadedMacros::ProcMacros(_) => true,
422-
_ => false,
423-
}
424-
}
425-
}
426-
427426
pub trait CrateLoader {
428427
fn process_item(&mut self, item: &ast::Item, defs: &Definitions, load_macros: bool)
429-
-> Option<LoadedMacros>;
428+
-> Vec<(ast::Name, SyntaxExtension)>;
430429
fn postprocess(&mut self, krate: &ast::Crate);
431430
}

src/librustc/middle/dependency_format.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ use hir::def_id::CrateNum;
6565

6666
use session;
6767
use session::config;
68+
use middle::cstore::DepKind;
6869
use middle::cstore::LinkagePreference::{self, RequireStatic, RequireDynamic};
6970
use util::nodemap::FxHashMap;
7071
use rustc_back::PanicStrategy;
@@ -123,6 +124,7 @@ fn calculate_type(sess: &session::Session,
123124
return v;
124125
}
125126
for cnum in sess.cstore.crates() {
127+
if sess.cstore.dep_kind(cnum) == DepKind::MacrosOnly { continue }
126128
let src = sess.cstore.used_crate_source(cnum);
127129
if src.rlib.is_some() { continue }
128130
sess.err(&format!("dependency `{}` not found in rlib format",
@@ -155,6 +157,7 @@ fn calculate_type(sess: &session::Session,
155157
// dependencies, ensuring there are no conflicts. The only valid case for a
156158
// dependency to be relied upon twice is for both cases to rely on a dylib.
157159
for cnum in sess.cstore.crates() {
160+
if sess.cstore.dep_kind(cnum) == DepKind::MacrosOnly { continue }
158161
let name = sess.cstore.crate_name(cnum);
159162
let src = sess.cstore.used_crate_source(cnum);
160163
if src.dylib.is_some() {
@@ -188,7 +191,7 @@ fn calculate_type(sess: &session::Session,
188191
let src = sess.cstore.used_crate_source(cnum);
189192
if src.dylib.is_none() &&
190193
!formats.contains_key(&cnum) &&
191-
sess.cstore.is_explicitly_linked(cnum) {
194+
sess.cstore.dep_kind(cnum) == DepKind::Explicit {
192195
assert!(src.rlib.is_some());
193196
info!("adding staticlib: {}", sess.cstore.crate_name(cnum));
194197
add_library(sess, cnum, RequireStatic, &mut formats);
@@ -272,7 +275,7 @@ fn attempt_static(sess: &session::Session) -> Option<DependencyList> {
272275
// everything in explicitly so long as it's actually required.
273276
let last_crate = sess.cstore.crates().len();
274277
let mut ret = (1..last_crate+1).map(|cnum| {
275-
if sess.cstore.is_explicitly_linked(CrateNum::new(cnum)) {
278+
if sess.cstore.dep_kind(CrateNum::new(cnum)) == DepKind::Explicit {
276279
Linkage::Static
277280
} else {
278281
Linkage::NotLinked

src/librustc_incremental/calculate_svh/svh_visitor.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,8 @@ impl<'a, 'hash, 'tcx> StrictVersionHashVisitor<'a, 'hash, 'tcx> {
834834
Def::Const(..) |
835835
Def::AssociatedConst(..) |
836836
Def::Local(..) |
837-
Def::Upvar(..) => {
837+
Def::Upvar(..) |
838+
Def::Macro(..) => {
838839
DefHash::SawDefId.hash(self.st);
839840
self.hash_def_id(def.def_id());
840841
}

src/librustc_llvm/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#![feature(staged_api)]
3030
#![feature(linked_from)]
3131
#![feature(concat_idents)]
32+
#![cfg_attr(not(stage0), feature(rustc_private))]
3233

3334
extern crate libc;
3435
#[macro_use]

0 commit comments

Comments
 (0)