Skip to content

Commit 04de89d

Browse files
committed
Validate crate name in CLI option --extern
1 parent f072775 commit 04de89d

File tree

8 files changed

+41
-1
lines changed

8 files changed

+41
-1
lines changed

Diff for: compiler/rustc_session/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ rustc_span = { path = "../rustc_span" }
1919
rustc_fs_util = { path = "../rustc_fs_util" }
2020
rustc_ast = { path = "../rustc_ast" }
2121
rustc_lint_defs = { path = "../rustc_lint_defs" }
22+
rustc_lexer = { path = "../rustc_lexer" }
2223
smallvec = "1.8.1"
2324
termize = "0.1.1"
2425

Diff for: compiler/rustc_session/src/config.rs

+13
Original file line numberDiff line numberDiff line change
@@ -2451,6 +2451,19 @@ pub fn parse_externs(
24512451
Some((opts, name)) => (Some(opts), name.to_string()),
24522452
};
24532453

2454+
if !rustc_lexer::is_ident(&name) {
2455+
let mut error = handler.early_struct_error(format!(
2456+
"crate name `{name}` passed to `--extern` is not a valid identifier"
2457+
));
2458+
let adjusted_name = name.replace("-", "_");
2459+
if rustc_lexer::is_ident(&adjusted_name) {
2460+
error.help(format!(
2461+
"consider replacing the dashes with underscores: `{adjusted_name}`"
2462+
));
2463+
}
2464+
error.emit();
2465+
}
2466+
24542467
let path = path.map(|p| CanonicalizedPath::new(p));
24552468

24562469
let entry = externs.entry(name.to_owned());

Diff for: compiler/rustc_session/src/session.rs

+9
Original file line numberDiff line numberDiff line change
@@ -1710,6 +1710,15 @@ impl EarlyErrorHandler {
17101710
self.handler.struct_fatal(msg).emit()
17111711
}
17121712

1713+
#[allow(rustc::untranslatable_diagnostic)]
1714+
#[allow(rustc::diagnostic_outside_of_impl)]
1715+
pub(crate) fn early_struct_error(
1716+
&self,
1717+
msg: impl Into<DiagnosticMessage>,
1718+
) -> DiagnosticBuilder<'_, !> {
1719+
self.handler.struct_fatal(msg)
1720+
}
1721+
17131722
#[allow(rustc::untranslatable_diagnostic)]
17141723
#[allow(rustc::diagnostic_outside_of_impl)]
17151724
pub fn early_warn(&self, msg: impl Into<DiagnosticMessage>) {

Diff for: tests/ui/extern-flag/invalid-crate-name-dashed.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// compile-flags: --extern=my-awesome-library=libawesome.rlib
2+
// error-pattern: crate name `my-awesome-library` passed to `--extern` is not a valid identifier
3+
// error-pattern: consider replacing the dashes with underscores: `my_awesome_library`
4+
5+
pub use my_awesome_library::*;
6+
7+
fn main() {}
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
error: crate name `my-awesome-library` passed to `--extern` is not a valid identifier
2+
|
3+
= help: consider replacing the dashes with underscores: `my_awesome_library`
4+

Diff for: tests/ui/extern-flag/invalid-crate-name.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// compile-flags: --extern=?#1%$
2+
// error-pattern: crate name `?#1%$` passed to `--extern` is not a valid identifier
3+
4+
fn main() {}

Diff for: tests/ui/extern-flag/invalid-crate-name.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: crate name `?#1%$` passed to `--extern` is not a valid identifier
2+

Diff for: tests/ui/rust-2018/trait-import-suggestions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// edition:2018
22
// aux-build:trait-import-suggestions.rs
3-
// compile-flags:--extern trait-import-suggestions
3+
// compile-flags:--extern trait_import_suggestions
44

55
mod foo {
66
mod foobar {

0 commit comments

Comments
 (0)