Skip to content

Commit 50868db

Browse files
committed
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.
1 parent 5ddc7b4 commit 50868db

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/librustc/back/link.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,18 @@ pub fn find_crate_name(sess: Option<&Session>,
579579
match sess {
580580
Some(sess) => {
581581
match sess.opts.crate_name {
582-
Some(ref s) => return validate(s.clone(), None),
582+
Some(ref s) => {
583+
match attr_crate_name {
584+
Some((attr, ref name)) if s.as_slice() != name.get() => {
585+
let msg = format!("--crate-name and #[crate_name] \
586+
are required to match, but `{}` \
587+
!= `{}`", s, name);
588+
sess.span_err(attr.span, msg.as_slice());
589+
}
590+
_ => {},
591+
}
592+
return validate(s.clone(), None);
593+
}
583594
None => {}
584595
}
585596
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-args: --crate-name foo
12+
13+
#![crate_name = "bar"]
14+
//~^ ERROR: --crate-name and #[crate_name] are required to match, but `foo` != `bar`
15+
16+
fn main() {}

0 commit comments

Comments
 (0)