Skip to content

Commit a7594f2

Browse files
committed
Disallow crate names with leading hyphens
Leading hyphens already don't work (#22661), so no code should break from this change. Closes #22661.
1 parent 2b01a37 commit a7594f2

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

src/librustc/metadata/creader.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,13 @@ pub fn validate_crate_name(sess: Option<&Session>, s: &str, sp: Option<Span>) {
8181
};
8282
if s.len() == 0 {
8383
err("crate name must not be empty");
84+
} else if s.char_at(0) == '-' {
85+
err(&format!("crate name cannot start with a hyphen: {}", s));
8486
}
8587
for c in s.chars() {
8688
if c.is_alphanumeric() { continue }
8789
if c == '_' || c == '-' { continue }
88-
err(&format!("invalid character `{}` in crate name: `{}`", c, s)[]);
90+
err(&format!("invalid character `{}` in crate name: `{}`", c, s));
8991
}
9092
match sess {
9193
Some(sess) => sess.abort_if_errors(),

src/test/run-make/weird-output-filenames/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ all:
1010
cp foo.rs $(TMPDIR)/+foo+bar
1111
$(RUSTC) $(TMPDIR)/+foo+bar 2>&1 \
1212
| grep "invalid character.*in crate name:"
13+
cp foo.rs $(TMPDIR)/-foo.rs
14+
$(RUSTC) $(TMPDIR)/-foo.rs 2>&1 \
15+
| grep "crate name cannot start with a hyphen:"

0 commit comments

Comments
 (0)