Skip to content

Commit 5229e0e

Browse files
committed
Auto merge of #33602 - eddyb:no-trans--check, r=michaelwoerister
Save metadata even with -Z no-trans (e.g. for multi-crate cargo check). Removes the item symbol map in metadata, as we can now generate them in a deterministic manner. The `-Z no-trans` change lets the LLVM passes and linking run, but with just metadata and no code. It fails while trying to link a binary because there's no `main` function, which is correct but not good UX. There's also no way to easily throw away all of the artifacts to rebuild with actual code generation. We might want `cargo check` to do that using cargo-internal information and then it would just work. cc @alexcrichton @nikomatsakis @Aatch @michaelwoerister
2 parents 487de42 + a619901 commit 5229e0e

File tree

24 files changed

+371
-350
lines changed

24 files changed

+371
-350
lines changed

src/librustc/middle/cstore.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ use mir::mir_map::MirMap;
3434
use session::Session;
3535
use session::config::PanicStrategy;
3636
use session::search_paths::PathKind;
37-
use util::nodemap::{FnvHashMap, NodeMap, NodeSet, DefIdMap};
38-
use std::cell::RefCell;
37+
use util::nodemap::{FnvHashMap, NodeSet, DefIdMap};
3938
use std::rc::Rc;
4039
use std::path::PathBuf;
4140
use syntax::ast;
@@ -169,7 +168,6 @@ pub trait CrateStore<'tcx> {
169168
fn item_super_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
170169
-> ty::GenericPredicates<'tcx>;
171170
fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute>;
172-
fn item_symbol(&self, def: DefId) -> String;
173171
fn trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)-> ty::TraitDef<'tcx>;
174172
fn adt_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx>;
175173
fn method_arg_names(&self, did: DefId) -> Vec<String>;
@@ -205,6 +203,7 @@ pub trait CrateStore<'tcx> {
205203
fn is_impl(&self, did: DefId) -> bool;
206204
fn is_default_impl(&self, impl_did: DefId) -> bool;
207205
fn is_extern_item<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, did: DefId) -> bool;
206+
fn is_foreign_item(&self, did: DefId) -> bool;
208207
fn is_static_method(&self, did: DefId) -> bool;
209208
fn is_statically_included_foreign_item(&self, id: ast::NodeId) -> bool;
210209
fn is_typedef(&self, did: DefId) -> bool;
@@ -274,7 +273,6 @@ pub trait CrateStore<'tcx> {
274273
fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<ast::CrateNum>;
275274
fn encode_metadata<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
276275
reexports: &def::ExportMap,
277-
item_symbols: &RefCell<NodeMap<String>>,
278276
link_meta: &LinkMeta,
279277
reachable: &NodeSet,
280278
mir_map: &MirMap<'tcx>,
@@ -352,7 +350,6 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
352350
fn item_super_predicates<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)
353351
-> ty::GenericPredicates<'tcx> { bug!("item_super_predicates") }
354352
fn item_attrs(&self, def_id: DefId) -> Vec<ast::Attribute> { bug!("item_attrs") }
355-
fn item_symbol(&self, def: DefId) -> String { bug!("item_symbol") }
356353
fn trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId)-> ty::TraitDef<'tcx>
357354
{ bug!("trait_def") }
358355
fn adt_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> ty::AdtDefMaster<'tcx>
@@ -399,6 +396,7 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
399396
fn is_default_impl(&self, impl_did: DefId) -> bool { bug!("is_default_impl") }
400397
fn is_extern_item<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, did: DefId) -> bool
401398
{ bug!("is_extern_item") }
399+
fn is_foreign_item(&self, did: DefId) -> bool { bug!("is_foreign_item") }
402400
fn is_static_method(&self, did: DefId) -> bool { bug!("is_static_method") }
403401
fn is_statically_included_foreign_item(&self, id: ast::NodeId) -> bool { false }
404402
fn is_typedef(&self, did: DefId) -> bool { bug!("is_typedef") }
@@ -481,7 +479,6 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
481479
fn extern_mod_stmt_cnum(&self, emod_id: ast::NodeId) -> Option<ast::CrateNum> { None }
482480
fn encode_metadata<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
483481
reexports: &def::ExportMap,
484-
item_symbols: &RefCell<NodeMap<String>>,
485482
link_meta: &LinkMeta,
486483
reachable: &NodeSet,
487484
mir_map: &MirMap<'tcx>,

src/librustc/session/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
11041104
let no_analysis = debugging_opts.no_analysis;
11051105

11061106
let mut output_types = HashMap::new();
1107-
if !debugging_opts.parse_only && !no_trans {
1107+
if !debugging_opts.parse_only {
11081108
for list in matches.opt_strs("emit") {
11091109
for output_type in list.split(',') {
11101110
let mut parts = output_type.splitn(2, '=');

src/librustc/session/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
// except according to those terms.
1010

1111
use dep_graph::DepGraph;
12+
use hir::def_id::DefIndex;
13+
use hir::svh::Svh;
1214
use lint;
1315
use middle::cstore::CrateStore;
1416
use middle::dependency_format;
@@ -312,6 +314,14 @@ impl Session {
312314
pub fn nonzeroing_move_hints(&self) -> bool {
313315
self.opts.debugging_opts.enable_nonzeroing_move_hints
314316
}
317+
318+
/// Returns the symbol name for the registrar function,
319+
/// given the crate Svh and the function DefIndex.
320+
pub fn generate_plugin_registrar_symbol(&self, svh: &Svh, index: DefIndex)
321+
-> String {
322+
format!("__rustc_plugin_registrar__{}_{}", svh, index.as_usize())
323+
}
324+
315325
pub fn sysroot<'a>(&'a self) -> &'a Path {
316326
match self.opts.maybe_sysroot {
317327
Some (ref sysroot) => sysroot,

src/librustc/ty/item_path.rs

+33-3
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,38 @@ use hir::def_id::{DefId, CRATE_DEF_INDEX};
1414
use ty::{self, Ty, TyCtxt};
1515
use syntax::ast;
1616

17+
use std::cell::Cell;
18+
19+
thread_local! {
20+
static FORCE_ABSOLUTE: Cell<bool> = Cell::new(false)
21+
}
22+
23+
/// Enforces that item_path_str always returns an absolute path.
24+
/// This is useful when building symbols that contain types,
25+
/// where we want the crate name to be part of the symbol.
26+
pub fn with_forced_absolute_paths<F: FnOnce() -> R, R>(f: F) -> R {
27+
FORCE_ABSOLUTE.with(|force| {
28+
let old = force.get();
29+
force.set(true);
30+
let result = f();
31+
force.set(old);
32+
result
33+
})
34+
}
35+
1736
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
1837
/// Returns a string identifying this def-id. This string is
1938
/// suitable for user output. It is relative to the current crate
20-
/// root.
39+
/// root, unless with_forced_absolute_paths was used.
2140
pub fn item_path_str(self, def_id: DefId) -> String {
22-
let mut buffer = LocalPathBuffer::new(RootMode::Local);
41+
let mode = FORCE_ABSOLUTE.with(|force| {
42+
if force.get() {
43+
RootMode::Absolute
44+
} else {
45+
RootMode::Local
46+
}
47+
});
48+
let mut buffer = LocalPathBuffer::new(mode);
2349
self.push_item_path(&mut buffer, def_id);
2450
buffer.into_string()
2551
}
@@ -75,7 +101,11 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
75101
RootMode::Absolute => {
76102
// In absolute mode, just write the crate name
77103
// unconditionally.
78-
buffer.push(&self.crate_name(cnum));
104+
if cnum == LOCAL_CRATE {
105+
buffer.push(&self.crate_name(cnum));
106+
} else {
107+
buffer.push(&self.sess.cstore.original_crate_name(cnum));
108+
}
79109
}
80110
}
81111
}

src/librustc_driver/lib.rs

-4
Original file line numberDiff line numberDiff line change
@@ -511,10 +511,6 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
511511
control.after_write_deps.stop = Compilation::Stop;
512512
}
513513

514-
if sess.opts.no_trans {
515-
control.after_analysis.stop = Compilation::Stop;
516-
}
517-
518514
if !sess.opts.output_types.keys().any(|&i| i == OutputType::Exe) {
519515
control.after_llvm.stop = Compilation::Stop;
520516
}

src/librustc_metadata/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub const tag_items_data_item_family: usize = 0x24;
3333

3434
pub const tag_items_data_item_type: usize = 0x25;
3535

36-
pub const tag_items_data_item_symbol: usize = 0x26;
36+
// GAP 0x26
3737

3838
pub const tag_items_data_item_variant: usize = 0x27;
3939

src/librustc_metadata/creader.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use cstore::{self, CStore, CrateSource, MetadataBlob};
1717
use decoder;
1818
use loader::{self, CratePaths};
1919

20+
use rustc::hir::def_id::DefIndex;
2021
use rustc::hir::svh::Svh;
2122
use rustc::dep_graph::{DepGraph, DepNode};
2223
use rustc::session::{config, Session};
@@ -610,9 +611,10 @@ impl<'a> CrateReader<'a> {
610611
macros
611612
}
612613

613-
/// Look for a plugin registrar. Returns library path and symbol name.
614+
/// Look for a plugin registrar. Returns library path, crate
615+
/// SVH and DefIndex of the registrar function.
614616
pub fn find_plugin_registrar(&mut self, span: Span, name: &str)
615-
-> Option<(PathBuf, String)> {
617+
-> Option<(PathBuf, Svh, DefIndex)> {
616618
let ekrate = self.read_extension_crate(span, &CrateInfo {
617619
name: name.to_string(),
618620
ident: name.to_string(),
@@ -630,12 +632,14 @@ impl<'a> CrateReader<'a> {
630632
span_fatal!(self.sess, span, E0456, "{}", &message[..]);
631633
}
632634

635+
let svh = decoder::get_crate_hash(ekrate.metadata.as_slice());
633636
let registrar =
634-
decoder::get_plugin_registrar_fn(ekrate.metadata.as_slice())
635-
.map(|id| decoder::get_symbol_from_buf(ekrate.metadata.as_slice(), id));
637+
decoder::get_plugin_registrar_fn(ekrate.metadata.as_slice());
636638

637639
match (ekrate.dylib.as_ref(), registrar) {
638-
(Some(dylib), Some(reg)) => Some((dylib.to_path_buf(), reg)),
640+
(Some(dylib), Some(reg)) => {
641+
Some((dylib.to_path_buf(), svh, reg))
642+
}
639643
(None, Some(_)) => {
640644
span_err!(self.sess, span, E0457,
641645
"plugin `{}` only found in rlib format, but must be available \

src/librustc_metadata/csearch.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc::hir::map as hir_map;
2525
use rustc::hir::map::DefKey;
2626
use rustc::mir::repr::Mir;
2727
use rustc::mir::mir_map::MirMap;
28-
use rustc::util::nodemap::{FnvHashMap, NodeMap, NodeSet, DefIdMap};
28+
use rustc::util::nodemap::{FnvHashMap, NodeSet, DefIdMap};
2929
use rustc::session::config::PanicStrategy;
3030

3131
use std::cell::RefCell;
@@ -115,13 +115,6 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
115115
decoder::get_item_attrs(&cdata, def_id.index)
116116
}
117117

118-
fn item_symbol(&self, def: DefId) -> String
119-
{
120-
self.dep_graph.read(DepNode::MetaData(def));
121-
let cdata = self.get_crate_data(def.krate);
122-
decoder::get_symbol(&cdata, def.index)
123-
}
124-
125118
fn trait_def<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, def: DefId) -> ty::TraitDef<'tcx>
126119
{
127120
self.dep_graph.read(DepNode::MetaData(def));
@@ -284,6 +277,11 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
284277
decoder::is_extern_item(&cdata, did.index, tcx)
285278
}
286279

280+
fn is_foreign_item(&self, did: DefId) -> bool {
281+
let cdata = self.get_crate_data(did.krate);
282+
decoder::is_foreign_item(&cdata, did.index)
283+
}
284+
287285
fn is_static_method(&self, def: DefId) -> bool
288286
{
289287
self.dep_graph.read(DepNode::MetaData(def));
@@ -564,7 +562,6 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
564562

565563
fn encode_metadata<'a>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>,
566564
reexports: &def::ExportMap,
567-
item_symbols: &RefCell<NodeMap<String>>,
568565
link_meta: &LinkMeta,
569566
reachable: &NodeSet,
570567
mir_map: &MirMap<'tcx>,
@@ -574,7 +571,6 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
574571
diag: tcx.sess.diagnostic(),
575572
tcx: tcx,
576573
reexports: reexports,
577-
item_symbols: item_symbols,
578574
link_meta: link_meta,
579575
cstore: self,
580576
reachable: reachable,

src/librustc_metadata/decoder.rs

+10-16
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,6 @@ fn item_sort(item: rbml::Doc) -> Option<char> {
213213
})
214214
}
215215

216-
fn item_symbol(item: rbml::Doc) -> String {
217-
reader::get_doc(item, tag_items_data_item_symbol).as_str().to_string()
218-
}
219-
220216
fn untranslated_def_id(d: rbml::Doc) -> DefId {
221217
let id = reader::doc_as_u64(d);
222218
let index = DefIndex::new((id & 0xFFFF_FFFF) as usize);
@@ -640,18 +636,6 @@ pub fn get_impl_trait<'a, 'tcx>(cdata: Cmd,
640636
}
641637
}
642638

643-
pub fn get_symbol(cdata: Cmd, id: DefIndex) -> String {
644-
return item_symbol(cdata.lookup_item(id));
645-
}
646-
647-
/// If you have a crate_metadata, call get_symbol instead
648-
pub fn get_symbol_from_buf(data: &[u8], id: DefIndex) -> String {
649-
let index = load_index(data);
650-
let pos = index.lookup_item(data, id).unwrap();
651-
let doc = reader::doc_at(data, pos as usize).unwrap().doc;
652-
item_symbol(doc)
653-
}
654-
655639
/// Iterates over the language items in the given crate.
656640
pub fn each_lang_item<F>(cdata: Cmd, mut f: F) -> bool where
657641
F: FnMut(DefIndex, usize) -> bool,
@@ -1642,6 +1626,16 @@ pub fn is_extern_item<'a, 'tcx>(cdata: Cmd,
16421626
}
16431627
}
16441628

1629+
pub fn is_foreign_item(cdata: Cmd, id: DefIndex) -> bool {
1630+
let item_doc = cdata.lookup_item(id);
1631+
let parent_item_id = match item_parent_item(cdata, item_doc) {
1632+
None => return false,
1633+
Some(item_id) => item_id,
1634+
};
1635+
let parent_item_doc = cdata.lookup_item(parent_item_id.index);
1636+
item_family(parent_item_doc) == ForeignMod
1637+
}
1638+
16451639
pub fn is_impl(cdata: Cmd, id: DefIndex) -> bool {
16461640
let item_doc = cdata.lookup_item(id);
16471641
match item_family(item_doc) {

0 commit comments

Comments
 (0)