1
Fork 0

rustc: #[crate_name] and --crate-name must match

Part of the original discussions around the `--crate-name` flag brought up that
mass confusion can arise when the flag specifies a different name than is
contained in the crate.

The current primary use case of the `--crate-name` flag is through cargo and
not requiring a `#[crate_name]` attribute, but if the `#[crate_name]` attribute
is specified it will likely go awry when the two names deviate from one another.
This commit requires that if both are provided they both match to prevent this
confusion.
This commit is contained in:
Alex Crichton 2014-07-15 09:13:40 -07:00
parent 5ddc7b4a25
commit 50868db351
2 changed files with 28 additions and 1 deletions

View file

@ -579,7 +579,18 @@ pub fn find_crate_name(sess: Option<&Session>,
match sess {
Some(sess) => {
match sess.opts.crate_name {
Some(ref s) => return validate(s.clone(), None),
Some(ref s) => {
match attr_crate_name {
Some((attr, ref name)) if s.as_slice() != name.get() => {
let msg = format!("--crate-name and #[crate_name] \
are required to match, but `{}` \
!= `{}`", s, name);
sess.span_err(attr.span, msg.as_slice());
}
_ => {},
}
return validate(s.clone(), None);
}
None => {}
}
}

View file

@ -0,0 +1,16 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// compile-args: --crate-name foo
#![crate_name = "bar"]
//~^ ERROR: --crate-name and #[crate_name] are required to match, but `foo` != `bar`
fn main() {}