From 56f71015515490b65b5fbb46ff0bbc7d7af82450 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sat, 5 Jul 2014 12:37:43 -0700 Subject: [PATCH] rustc: Default #[crate_name] on input, not output --- src/librustc/back/link.rs | 21 ++++++++++++------- src/librustc/driver/driver.rs | 5 ++--- src/librustc/driver/mod.rs | 3 +-- src/librustdoc/clean/mod.rs | 9 +------- src/librustdoc/core.rs | 7 ++----- src/test/compile-fail/bad-crate-id2.rs | 2 +- .../run-make/weird-output-filenames/Makefile | 15 +++++++------ 7 files changed, 29 insertions(+), 33 deletions(-) diff --git a/src/librustc/back/link.rs b/src/librustc/back/link.rs index c306a184ca18c..f1856850701b2 100644 --- a/src/librustc/back/link.rs +++ b/src/librustc/back/link.rs @@ -11,7 +11,7 @@ use back::archive::{Archive, METADATA_FILENAME}; use back::rpath; use back::svh::Svh; -use driver::driver::{CrateTranslation, OutputFilenames}; +use driver::driver::{CrateTranslation, OutputFilenames, Input, FileInput}; use driver::config::NoDebugInfo; use driver::session::Session; use driver::config; @@ -545,10 +545,9 @@ pub mod write { * system linkers understand. */ -// FIXME (#9639): This needs to handle non-utf8 `out_filestem` values pub fn find_crate_name(sess: Option<&Session>, attrs: &[ast::Attribute], - out_filestem: &str) -> String { + input: &Input) -> String { use syntax::crateid::CrateId; let validate = |s: String, span: Option| { @@ -591,11 +590,17 @@ pub fn find_crate_name(sess: Option<&Session>, } None => {} } - return validate(from_str(out_filestem).unwrap_or_else(|| { - let mut s = out_filestem.chars().filter(|c| c.is_XID_continue()); - from_str(s.collect::().as_slice()) - .or(from_str("rust-out")).unwrap() - }), None) + match *input { + FileInput(ref path) => { + match path.filestem_str() { + Some(s) => return validate(s.to_string(), None), + None => {} + } + } + _ => {} + } + + "rust-out".to_string() } pub fn build_link_meta(sess: &Session, krate: &ast::Crate, diff --git a/src/librustc/driver/driver.rs b/src/librustc/driver/driver.rs index 12efecb4a0b70..16605c060170f 100644 --- a/src/librustc/driver/driver.rs +++ b/src/librustc/driver/driver.rs @@ -77,7 +77,7 @@ pub fn compile_input(sess: Session, krate.attrs.as_slice(), &sess); let id = link::find_crate_name(Some(&sess), krate.attrs.as_slice(), - outputs.out_filestem.as_slice()); + input); let (expanded_crate, ast_map) = match phase_2_configure_and_expand(&sess, krate, id.as_slice()) { None => return, @@ -652,8 +652,7 @@ pub fn pretty_print_input(sess: Session, ppm: PpMode, ofile: Option) { let krate = phase_1_parse_input(&sess, cfg, input); - let id = link::find_crate_name(Some(&sess), krate.attrs.as_slice(), - input.filestem().as_slice()); + let id = link::find_crate_name(Some(&sess), krate.attrs.as_slice(), input); let (krate, ast_map, is_expanded) = match ppm { PpmExpanded | PpmExpandedIdentified | PpmTyped | PpmFlowGraph(_) => { diff --git a/src/librustc/driver/mod.rs b/src/librustc/driver/mod.rs index cbbc8ebd8c875..807c2590566d9 100644 --- a/src/librustc/driver/mod.rs +++ b/src/librustc/driver/mod.rs @@ -303,8 +303,7 @@ fn print_crate_info(sess: &Session, ofile, attrs.as_slice(), sess); - let id = link::find_crate_name(Some(sess), attrs.as_slice(), - t_outputs.out_filestem.as_slice()); + let id = link::find_crate_name(Some(sess), attrs.as_slice(), input); if crate_name { println!("{}", id); diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 3cf8d6b3827ef..1ddd97e2ed45f 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -119,14 +119,7 @@ impl<'a> Clean for visit_ast::RustdocVisitor<'a> { // Figure out the name of this crate let input = driver::FileInput(cx.src.clone()); - let t_outputs = driver::build_output_filenames(&input, - &None, - &None, - self.attrs.as_slice(), - cx.sess()); - let name = link::find_crate_name(None, - self.attrs.as_slice(), - t_outputs.out_filestem.as_slice()); + let name = link::find_crate_name(None, self.attrs.as_slice(), &input); // Clean the crate, translating the entire libsyntax AST to one that is // understood by rustdoc. diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 058face151c62..9ef671ef2fcbb 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -84,8 +84,7 @@ fn get_ast_and_resolve(cpath: &Path, libs: HashSet, cfgs: Vec) use rustc::driver::driver::{FileInput, phase_1_parse_input, phase_2_configure_and_expand, - phase_3_run_analysis_passes, - build_output_filenames}; + phase_3_run_analysis_passes}; use rustc::driver::config::build_configuration; let input = FileInput(cpath.clone()); @@ -118,10 +117,8 @@ fn get_ast_and_resolve(cpath: &Path, libs: HashSet, cfgs: Vec) let krate = phase_1_parse_input(&sess, cfg, &input); - let t_outputs = build_output_filenames(&input, &None, &None, - krate.attrs.as_slice(), &sess); let name = link::find_crate_name(Some(&sess), krate.attrs.as_slice(), - t_outputs.out_filestem.as_slice()); + &input); let (krate, ast_map) = phase_2_configure_and_expand(&sess, krate, name.as_slice()) diff --git a/src/test/compile-fail/bad-crate-id2.rs b/src/test/compile-fail/bad-crate-id2.rs index fcbdc6d1f2401..22e98b61c6113 100644 --- a/src/test/compile-fail/bad-crate-id2.rs +++ b/src/test/compile-fail/bad-crate-id2.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -extern crate bar = "#a"; //~ ERROR: invalid character in crate name: `#` +extern crate bar = "#a"; //~ ERROR: invalid character `#` in crate name: `#a` fn main() {} diff --git a/src/test/run-make/weird-output-filenames/Makefile b/src/test/run-make/weird-output-filenames/Makefile index 37dcf9a6c4a1b..5d6e629ffc1d1 100644 --- a/src/test/run-make/weird-output-filenames/Makefile +++ b/src/test/run-make/weird-output-filenames/Makefile @@ -1,9 +1,12 @@ -include ../tools.mk all: - $(RUSTC) foo.rs -o $(TMPDIR)/.foo 2>&1 \ - | grep "invalid character in crate name:" - $(RUSTC) foo.rs -o $(TMPDIR)/.foo.bar 2>&1 \ - | grep "invalid character in crate name:" - $(RUSTC) foo.rs -o $(TMPDIR)/+foo+bar 2>&1 \ - | grep "invalid character in crate name:" + cp foo.rs $(TMPDIR)/.foo.rs + $(RUSTC) $(TMPDIR)/.foo.rs 2>&1 \ + | grep "invalid character.*in crate name:" + cp foo.rs $(TMPDIR)/.foo.bar + $(RUSTC) $(TMPDIR)/.foo.bar 2>&1 \ + | grep "invalid character.*in crate name:" + cp foo.rs $(TMPDIR)/+foo+bar + $(RUSTC) $(TMPDIR)/+foo+bar 2>&1 \ + | grep "invalid character.*in crate name:"