about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2014-07-15 09:13:40 -0700
committerAlex Crichton <alex@alexcrichton.com>2014-07-18 08:47:23 -0700
commit50868db351d5261346afe855f88235d044b80195 (patch)
tree529f43df6e1102554125bc5092c1698230a9ee7b
parent5ddc7b4a252fbebee5f2ac87ed755139816d6823 (diff)
downloadrust-50868db351d5261346afe855f88235d044b80195.tar.gz
rust-50868db351d5261346afe855f88235d044b80195.zip
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.
-rw-r--r--src/librustc/back/link.rs13
-rw-r--r--src/test/compile-fail/crate-name-mismatch.rs16
2 files changed, 28 insertions, 1 deletions
diff --git a/src/librustc/back/link.rs b/src/librustc/back/link.rs
index 88b42dba8e9..ab3e158b00a 100644
--- a/src/librustc/back/link.rs
+++ b/src/librustc/back/link.rs
@@ -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 => {}
             }
         }
diff --git a/src/test/compile-fail/crate-name-mismatch.rs b/src/test/compile-fail/crate-name-mismatch.rs
new file mode 100644
index 00000000000..387bda5f10f
--- /dev/null
+++ b/src/test/compile-fail/crate-name-mismatch.rs
@@ -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() {}