Skip to content

Commit 50ee1ec

Browse files
committed
rustc: Remove CrateId and all related support
This commit removes all support in the compiler for the #[crate_id] attribute and all of its derivative infrastructure. A list of the functionality removed is: * The #[crate_id] attribute no longer exists * There is no longer the concept of a version of a crate * Version numbers are no longer appended to symbol names * The --crate-id command line option has been removed To migrate forward, rename #[crate_id] to #[crate_name] and only the name of the crate itself should be mentioned. The version/path of the old crate id should be removed. For a transitionary state, the #[crate_id] attribute is still accepted if the #[crate_name] is not present, but it is warned about if it is the only identifier present. RFC: 0035-remove-crate-id [breaking-change]
1 parent aaff4e0 commit 50ee1ec

File tree

24 files changed

+230
-269
lines changed

24 files changed

+230
-269
lines changed

src/librustc/back/link.rs

+61-48
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use lib::llvm::llvm;
1919
use lib::llvm::ModuleRef;
2020
use lib;
2121
use metadata::common::LinkMeta;
22-
use metadata::{encoder, cstore, filesearch, csearch, loader};
22+
use metadata::{encoder, cstore, filesearch, csearch, loader, creader};
2323
use middle::trans::context::CrateContext;
2424
use middle::trans::common::gensym_name;
2525
use middle::ty;
@@ -40,9 +40,8 @@ use syntax::abi;
4040
use syntax::ast;
4141
use syntax::ast_map::{PathElem, PathElems, PathName};
4242
use syntax::ast_map;
43-
use syntax::attr;
4443
use syntax::attr::AttrMetaMethods;
45-
use syntax::crateid::CrateId;
44+
use syntax::codemap::Span;
4645
use syntax::parse::token;
4746

4847
#[deriving(Clone, PartialEq, PartialOrd, Ord, Eq)]
@@ -547,18 +546,49 @@ pub mod write {
547546
*/
548547

549548
// FIXME (#9639): This needs to handle non-utf8 `out_filestem` values
550-
pub fn find_crate_id(attrs: &[ast::Attribute], out_filestem: &str) -> CrateId {
551-
match attr::find_crateid(attrs) {
552-
None => from_str(out_filestem).unwrap_or_else(|| {
553-
let mut s = out_filestem.chars().filter(|c| c.is_XID_continue());
554-
from_str(s.collect::<String>().as_slice())
555-
.or(from_str("rust-out")).unwrap()
556-
}),
557-
Some(s) => s,
549+
pub fn find_crate_name(sess: Option<&Session>,
550+
attrs: &[ast::Attribute],
551+
out_filestem: &str) -> String {
552+
use syntax::crateid::CrateId;
553+
554+
let validate = |s: String, span: Option<Span>| {
555+
creader::validate_crate_name(sess, s.as_slice(), span);
556+
s
557+
};
558+
559+
let crate_name = attrs.iter().find(|at| at.check_name("crate_name"))
560+
.and_then(|at| at.value_str().map(|s| (at, s)));
561+
match crate_name {
562+
Some((attr, s)) => return validate(s.get().to_string(), Some(attr.span)),
563+
None => {}
564+
}
565+
let crate_id = attrs.iter().find(|at| at.check_name("crate_id"))
566+
.and_then(|at| at.value_str().map(|s| (at, s)))
567+
.and_then(|(at, s)| {
568+
from_str::<CrateId>(s.get()).map(|id| (at, id))
569+
});
570+
match crate_id {
571+
Some((attr, id)) => {
572+
match sess {
573+
Some(sess) => {
574+
sess.span_warn(attr.span, "the #[crate_id] attribute is \
575+
deprecated for the \
576+
#[crate_name] attribute");
577+
}
578+
None => {}
579+
}
580+
return validate(id.name, Some(attr.span))
581+
}
582+
None => {}
558583
}
584+
return validate(from_str(out_filestem).unwrap_or_else(|| {
585+
let mut s = out_filestem.chars().filter(|c| c.is_XID_continue());
586+
from_str(s.collect::<String>().as_slice())
587+
.or(from_str("rust-out")).unwrap()
588+
}), None)
559589
}
560590

561-
pub fn crate_id_hash(crate_id: &CrateId) -> String {
591+
pub fn crate_name_hash(sess: &Session, crate_name: &str) -> String {
562592
// This calculates CMH as defined above. Note that we don't use the path of
563593
// the crate id in the hash because lookups are only done by (name/vers),
564594
// not by path.
@@ -567,10 +597,9 @@ pub fn crate_id_hash(crate_id: &CrateId) -> String {
567597
truncated_hash_result(&mut s).as_slice().slice_to(8).to_string()
568598
}
569599

570-
// FIXME (#9639): This needs to handle non-utf8 `out_filestem` values
571-
pub fn build_link_meta(krate: &ast::Crate, out_filestem: &str) -> LinkMeta {
600+
pub fn build_link_meta(krate: &ast::Crate, name: String) -> LinkMeta {
572601
let r = LinkMeta {
573-
crateid: find_crate_id(krate.attrs.as_slice(), out_filestem),
602+
crate_name: name,
574603
crate_hash: Svh::calculate(krate),
575604
};
576605
info!("{}", r);
@@ -594,7 +623,7 @@ fn symbol_hash(tcx: &ty::ctxt,
594623
// to be independent of one another in the crate.
595624

596625
symbol_hasher.reset();
597-
symbol_hasher.input_str(link_meta.crateid.name.as_slice());
626+
symbol_hasher.input_str(link_meta.crate_name.as_slice());
598627
symbol_hasher.input_str("-");
599628
symbol_hasher.input_str(link_meta.crate_hash.as_str());
600629
symbol_hasher.input_str("-");
@@ -666,8 +695,7 @@ pub fn sanitize(s: &str) -> String {
666695
}
667696

668697
pub fn mangle<PI: Iterator<PathElem>>(mut path: PI,
669-
hash: Option<&str>,
670-
vers: Option<&str>) -> String {
698+
hash: Option<&str>) -> String {
671699
// Follow C++ namespace-mangling style, see
672700
// http://en.wikipedia.org/wiki/Name_mangling for more info.
673701
//
@@ -698,25 +726,13 @@ pub fn mangle<PI: Iterator<PathElem>>(mut path: PI,
698726
Some(s) => push(&mut n, s),
699727
None => {}
700728
}
701-
match vers {
702-
Some(s) => push(&mut n, s),
703-
None => {}
704-
}
705729

706730
n.push_char('E'); // End name-sequence.
707731
n
708732
}
709733

710-
pub fn exported_name(path: PathElems, hash: &str, vers: &str) -> String {
711-
// The version will get mangled to have a leading '_', but it makes more
712-
// sense to lead with a 'v' b/c this is a version...
713-
let vers = if vers.len() > 0 && !char::is_XID_start(vers.char_at(0)) {
714-
format!("v{}", vers)
715-
} else {
716-
vers.to_string()
717-
};
718-
719-
mangle(path, Some(hash), Some(vers.as_slice()))
734+
pub fn exported_name(path: PathElems, hash: &str) -> String {
735+
mangle(path, Some(hash))
720736
}
721737

722738
pub fn mangle_exported_name(ccx: &CrateContext, path: PathElems,
@@ -741,9 +757,7 @@ pub fn mangle_exported_name(ccx: &CrateContext, path: PathElems,
741757
hash.push_char(EXTRA_CHARS.as_bytes()[extra2] as char);
742758
hash.push_char(EXTRA_CHARS.as_bytes()[extra3] as char);
743759

744-
exported_name(path,
745-
hash.as_slice(),
746-
ccx.link_meta.crateid.version_or_default())
760+
exported_name(path, hash.as_slice())
747761
}
748762

749763
pub fn mangle_internal_name_by_type_and_seq(ccx: &CrateContext,
@@ -753,15 +767,11 @@ pub fn mangle_internal_name_by_type_and_seq(ccx: &CrateContext,
753767
let path = [PathName(token::intern(s.as_slice())),
754768
gensym_name(name)];
755769
let hash = get_symbol_hash(ccx, t);
756-
mangle(ast_map::Values(path.iter()), Some(hash.as_slice()), None)
770+
mangle(ast_map::Values(path.iter()), Some(hash.as_slice()))
757771
}
758772

759773
pub fn mangle_internal_name_by_path_and_seq(path: PathElems, flav: &str) -> String {
760-
mangle(path.chain(Some(gensym_name(flav)).move_iter()), None, None)
761-
}
762-
763-
pub fn output_lib_filename(id: &CrateId) -> String {
764-
format!("{}-{}-{}", id.name, crate_id_hash(id), id.version_or_default())
774+
mangle(path.chain(Some(gensym_name(flav)).move_iter()), None)
765775
}
766776

767777
pub fn get_cc_prog(sess: &Session) -> String {
@@ -803,14 +813,15 @@ fn remove(sess: &Session, path: &Path) {
803813
pub fn link_binary(sess: &Session,
804814
trans: &CrateTranslation,
805815
outputs: &OutputFilenames,
806-
id: &CrateId) -> Vec<Path> {
816+
crate_name: &str) -> Vec<Path> {
807817
let mut out_filenames = Vec::new();
808818
for &crate_type in sess.crate_types.borrow().iter() {
809819
if invalid_output_for_target(sess, crate_type) {
810820
sess.bug(format!("invalid output type `{}` for target os `{}`",
811821
crate_type, sess.targ_cfg.os).as_slice());
812822
}
813-
let out_file = link_binary_output(sess, trans, crate_type, outputs, id);
823+
let out_file = link_binary_output(sess, trans, crate_type, outputs,
824+
crate_name);
814825
out_filenames.push(out_file);
815826
}
816827

@@ -859,9 +870,11 @@ fn is_writeable(p: &Path) -> bool {
859870
}
860871
}
861872

862-
pub fn filename_for_input(sess: &Session, crate_type: config::CrateType,
863-
id: &CrateId, out_filename: &Path) -> Path {
864-
let libname = output_lib_filename(id);
873+
pub fn filename_for_input(sess: &Session,
874+
crate_type: config::CrateType,
875+
name: &str,
876+
out_filename: &Path) -> Path {
877+
let libname = format!("{}-{}", name, crate_name_hash(sess, name));
865878
match crate_type {
866879
config::CrateTypeRlib => {
867880
out_filename.with_filename(format!("lib{}.rlib", libname))
@@ -891,13 +904,13 @@ fn link_binary_output(sess: &Session,
891904
trans: &CrateTranslation,
892905
crate_type: config::CrateType,
893906
outputs: &OutputFilenames,
894-
id: &CrateId) -> Path {
907+
crate_name: &str) -> Path {
895908
let obj_filename = outputs.temp_path(OutputTypeObject);
896909
let out_filename = match outputs.single_output_file {
897910
Some(ref file) => file.clone(),
898911
None => {
899912
let out_filename = outputs.path(OutputTypeExe);
900-
filename_for_input(sess, crate_type, id, &out_filename)
913+
filename_for_input(sess, crate_type, crate_name, &out_filename)
901914
}
902915
};
903916

src/librustc/driver/config.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ pub struct Options {
9191
pub debugging_opts: u64,
9292
/// Whether to write dependency files. It's (enabled, optional filename).
9393
pub write_dependency_info: (bool, Option<Path>),
94-
/// Crate id-related things to maybe print. It's (crate_id, crate_name, crate_file_name).
95-
pub print_metas: (bool, bool, bool),
94+
/// Crate id-related things to maybe print. It's (crate_name, crate_file_name).
95+
pub print_metas: (bool, bool),
9696
pub cg: CodegenOptions,
9797
pub color: ColorConfig,
9898
}
@@ -117,7 +117,7 @@ pub fn basic_options() -> Options {
117117
no_analysis: false,
118118
debugging_opts: 0,
119119
write_dependency_info: (false, None),
120-
print_metas: (false, false, false),
120+
print_metas: (false, false),
121121
cg: basic_codegen_options(),
122122
color: Auto,
123123
}
@@ -505,7 +505,6 @@ pub fn optgroups() -> Vec<getopts::OptGroup> {
505505
"[bin|lib|rlib|dylib|staticlib]"),
506506
optmulti("", "emit", "Comma separated list of types of output for the compiler to emit",
507507
"[asm|bc|ir|obj|link]"),
508-
optflag("", "crate-id", "Output the crate id and exit"),
509508
optflag("", "crate-name", "Output the crate name and exit"),
510509
optflag("", "crate-file-name", "Output the file(s) that would be written if compilation \
511510
continued and exit"),
@@ -709,8 +708,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
709708
matches.opt_str("dep-info")
710709
.map(|p| Path::new(p)));
711710

712-
let print_metas = (matches.opt_present("crate-id"),
713-
matches.opt_present("crate-name"),
711+
let print_metas = (matches.opt_present("crate-name"),
714712
matches.opt_present("crate-file-name"));
715713
let cg = build_codegen_options(matches);
716714

0 commit comments

Comments
 (0)