Skip to content

Commit

Permalink
rustc: Default #[crate_name] on input, not output
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed Jul 5, 2014
1 parent 15b680a commit 56f7101
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 33 deletions.
21 changes: 13 additions & 8 deletions src/librustc/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Span>| {
Expand Down Expand Up @@ -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::<String>().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,
Expand Down
5 changes: 2 additions & 3 deletions src/librustc/driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -652,8 +652,7 @@ pub fn pretty_print_input(sess: Session,
ppm: PpMode,
ofile: Option<Path>) {
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(_) => {
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/driver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 1 addition & 8 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,7 @@ impl<'a> Clean<Crate> 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.
Expand Down
7 changes: 2 additions & 5 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ fn get_ast_and_resolve(cpath: &Path, libs: HashSet<Path>, cfgs: Vec<String>)
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());
Expand Down Expand Up @@ -118,10 +117,8 @@ fn get_ast_and_resolve(cpath: &Path, libs: HashSet<Path>, cfgs: Vec<String>)

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())
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/bad-crate-id2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}

15 changes: 9 additions & 6 deletions src/test/run-make/weird-output-filenames/Makefile
Original file line number Diff line number Diff line change
@@ -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:"

5 comments on commit 56f7101

@bors
Copy link
Contributor

@bors bors commented on 56f7101 Jul 5, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from brson
at alexcrichton@56f7101

@bors
Copy link
Contributor

@bors bors commented on 56f7101 Jul 5, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging alexcrichton/rust/no-crate-id = 56f7101 into auto

@bors
Copy link
Contributor

@bors bors commented on 56f7101 Jul 5, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alexcrichton/rust/no-crate-id = 56f7101 merged ok, testing candidate = c3ef04b

@bors
Copy link
Contributor

@bors bors commented on 56f7101 Jul 6, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = c3ef04b

Please sign in to comment.