Skip to content

Commit

Permalink
Validate crate name in CLI option --extern
Browse files Browse the repository at this point in the history
  • Loading branch information
fmease committed Sep 20, 2023
1 parent f072775 commit 04de89d
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions compiler/rustc_session/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ rustc_span = { path = "../rustc_span" }
rustc_fs_util = { path = "../rustc_fs_util" }
rustc_ast = { path = "../rustc_ast" }
rustc_lint_defs = { path = "../rustc_lint_defs" }
rustc_lexer = { path = "../rustc_lexer" }
smallvec = "1.8.1"
termize = "0.1.1"

Expand Down
13 changes: 13 additions & 0 deletions compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2451,6 +2451,19 @@ pub fn parse_externs(
Some((opts, name)) => (Some(opts), name.to_string()),
};

if !rustc_lexer::is_ident(&name) {
let mut error = handler.early_struct_error(format!(
"crate name `{name}` passed to `--extern` is not a valid identifier"
));
let adjusted_name = name.replace("-", "_");
if rustc_lexer::is_ident(&adjusted_name) {
error.help(format!(
"consider replacing the dashes with underscores: `{adjusted_name}`"
));
}
error.emit();
}

let path = path.map(|p| CanonicalizedPath::new(p));

let entry = externs.entry(name.to_owned());
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_session/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1710,6 +1710,15 @@ impl EarlyErrorHandler {
self.handler.struct_fatal(msg).emit()
}

#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub(crate) fn early_struct_error(
&self,
msg: impl Into<DiagnosticMessage>,
) -> DiagnosticBuilder<'_, !> {
self.handler.struct_fatal(msg)
}

#[allow(rustc::untranslatable_diagnostic)]
#[allow(rustc::diagnostic_outside_of_impl)]
pub fn early_warn(&self, msg: impl Into<DiagnosticMessage>) {
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/extern-flag/invalid-crate-name-dashed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// compile-flags: --extern=my-awesome-library=libawesome.rlib
// error-pattern: crate name `my-awesome-library` passed to `--extern` is not a valid identifier
// error-pattern: consider replacing the dashes with underscores: `my_awesome_library`

pub use my_awesome_library::*;

fn main() {}
4 changes: 4 additions & 0 deletions tests/ui/extern-flag/invalid-crate-name-dashed.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
error: crate name `my-awesome-library` passed to `--extern` is not a valid identifier
|
= help: consider replacing the dashes with underscores: `my_awesome_library`

4 changes: 4 additions & 0 deletions tests/ui/extern-flag/invalid-crate-name.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// compile-flags: --extern=?#1%$
// error-pattern: crate name `?#1%$` passed to `--extern` is not a valid identifier

fn main() {}
2 changes: 2 additions & 0 deletions tests/ui/extern-flag/invalid-crate-name.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
error: crate name `?#1%$` passed to `--extern` is not a valid identifier

2 changes: 1 addition & 1 deletion tests/ui/rust-2018/trait-import-suggestions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// edition:2018
// aux-build:trait-import-suggestions.rs
// compile-flags:--extern trait-import-suggestions
// compile-flags:--extern trait_import_suggestions

mod foo {
mod foobar {
Expand Down

0 comments on commit 04de89d

Please sign in to comment.