From a7594f2d5b9f668b1001c9b0b6f1fdd95768c2c5 Mon Sep 17 00:00:00 2001 From: Chris Wong Date: Sun, 22 Feb 2015 20:05:05 +1300 Subject: [PATCH] Disallow crate names with leading hyphens Leading hyphens already don't work (#22661), so no code should break from this change. Closes #22661. --- src/librustc/metadata/creader.rs | 4 +++- src/test/run-make/weird-output-filenames/Makefile | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/librustc/metadata/creader.rs b/src/librustc/metadata/creader.rs index d48a404176ace..79bed8ce91639 100644 --- a/src/librustc/metadata/creader.rs +++ b/src/librustc/metadata/creader.rs @@ -81,11 +81,13 @@ pub fn validate_crate_name(sess: Option<&Session>, s: &str, sp: Option) { }; if s.len() == 0 { err("crate name must not be empty"); + } else if s.char_at(0) == '-' { + err(&format!("crate name cannot start with a hyphen: {}", s)); } for c in s.chars() { if c.is_alphanumeric() { continue } if c == '_' || c == '-' { continue } - err(&format!("invalid character `{}` in crate name: `{}`", c, s)[]); + err(&format!("invalid character `{}` in crate name: `{}`", c, s)); } match sess { Some(sess) => sess.abort_if_errors(), diff --git a/src/test/run-make/weird-output-filenames/Makefile b/src/test/run-make/weird-output-filenames/Makefile index 5d6e629ffc1d1..3d57a2263e1ef 100644 --- a/src/test/run-make/weird-output-filenames/Makefile +++ b/src/test/run-make/weird-output-filenames/Makefile @@ -10,3 +10,6 @@ all: cp foo.rs $(TMPDIR)/+foo+bar $(RUSTC) $(TMPDIR)/+foo+bar 2>&1 \ | grep "invalid character.*in crate name:" + cp foo.rs $(TMPDIR)/-foo.rs + $(RUSTC) $(TMPDIR)/-foo.rs 2>&1 \ + | grep "crate name cannot start with a hyphen:"