mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-29 02:03:53 +00:00
Validate crate name in CLI option --extern
This commit is contained in:
parent
f0727758d1
commit
8d81d5a909
@ -2451,6 +2451,19 @@ pub fn parse_externs(
|
|||||||
Some((opts, name)) => (Some(opts), name.to_string()),
|
Some((opts, name)) => (Some(opts), name.to_string()),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if !crate::utils::is_ascii_ident(&name) {
|
||||||
|
let mut error = handler.early_struct_error(format!(
|
||||||
|
"crate name `{name}` passed to `--extern` is not a valid ASCII identifier"
|
||||||
|
));
|
||||||
|
let adjusted_name = name.replace("-", "_");
|
||||||
|
if crate::utils::is_ascii_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 path = path.map(|p| CanonicalizedPath::new(p));
|
||||||
|
|
||||||
let entry = externs.entry(name.to_owned());
|
let entry = externs.entry(name.to_owned());
|
||||||
|
@ -1710,6 +1710,15 @@ impl EarlyErrorHandler {
|
|||||||
self.handler.struct_fatal(msg).emit()
|
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::untranslatable_diagnostic)]
|
||||||
#[allow(rustc::diagnostic_outside_of_impl)]
|
#[allow(rustc::diagnostic_outside_of_impl)]
|
||||||
pub fn early_warn(&self, msg: impl Into<DiagnosticMessage>) {
|
pub fn early_warn(&self, msg: impl Into<DiagnosticMessage>) {
|
||||||
|
@ -158,3 +158,12 @@ pub fn extra_compiler_flags() -> Option<(Vec<String>, bool)> {
|
|||||||
|
|
||||||
if !result.is_empty() { Some((result, excluded_cargo_defaults)) } else { None }
|
if !result.is_empty() { Some((result, excluded_cargo_defaults)) } else { None }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn is_ascii_ident(string: &str) -> bool {
|
||||||
|
let mut chars = string.chars();
|
||||||
|
if let Some(start) = chars.next() && (start.is_ascii_alphabetic() || start == '_') {
|
||||||
|
chars.all(|char| char.is_ascii_alphanumeric() || char == '_')
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -15,7 +15,7 @@ INCR=$(TMPDIR)/incr
|
|||||||
all:
|
all:
|
||||||
cp first_crate.rs second_crate.rs $(TMPDIR)
|
cp first_crate.rs second_crate.rs $(TMPDIR)
|
||||||
$(RUSTC) $(TMPDIR)/first_crate.rs -C incremental=$(INCR) --target $(TARGET) --crate-type lib
|
$(RUSTC) $(TMPDIR)/first_crate.rs -C incremental=$(INCR) --target $(TARGET) --crate-type lib
|
||||||
$(RUSTC) $(TMPDIR)/second_crate.rs -C incremental=$(INCR) --target $(TARGET) --extern first-crate=$(TMPDIR) --crate-type lib
|
$(RUSTC) $(TMPDIR)/second_crate.rs -C incremental=$(INCR) --target $(TARGET) --extern first_crate=$(TMPDIR)/libfirst_crate.rlib --crate-type lib
|
||||||
rm $(TMPDIR)/first_crate.rs
|
rm $(TMPDIR)/first_crate.rs
|
||||||
$(RUSTC) $(TMPDIR)/second_crate.rs -C incremental=$(INCR) --target $(TARGET) --cfg second_run --crate-type lib
|
$(RUSTC) $(TMPDIR)/second_crate.rs -C incremental=$(INCR) --target $(TARGET) --cfg second_run --crate-type lib
|
||||||
|
|
||||||
|
10
tests/ui/extern-flag/invalid-crate-name-dashed.rs
Normal file
10
tests/ui/extern-flag/invalid-crate-name-dashed.rs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
// compile-flags: --extern=my-awesome-library=libawesome.rlib
|
||||||
|
// error-pattern: crate name `my-awesome-library` passed to `--extern` is not a valid ASCII identifier
|
||||||
|
// error-pattern: consider replacing the dashes with underscores: `my_awesome_library`
|
||||||
|
|
||||||
|
// In a sense, this is a regression test for issue #113035. We no longer suggest
|
||||||
|
// `pub use my-awesome-library::*;` (sic!) as we outright ban this crate name.
|
||||||
|
|
||||||
|
pub use my_awesome_library::*;
|
||||||
|
|
||||||
|
fn main() {}
|
4
tests/ui/extern-flag/invalid-crate-name-dashed.stderr
Normal file
4
tests/ui/extern-flag/invalid-crate-name-dashed.stderr
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
error: crate name `my-awesome-library` passed to `--extern` is not a valid ASCII identifier
|
||||||
|
|
|
||||||
|
= help: consider replacing the dashes with underscores: `my_awesome_library`
|
||||||
|
|
4
tests/ui/extern-flag/invalid-crate-name-non-ascii.rs
Normal file
4
tests/ui/extern-flag/invalid-crate-name-non-ascii.rs
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
// compile-flags: --extern čɍαţē=libnon_ascii.rlib
|
||||||
|
// error-pattern: crate name `čɍαţē` passed to `--extern` is not a valid ASCII identifier
|
||||||
|
|
||||||
|
fn main() {}
|
2
tests/ui/extern-flag/invalid-crate-name-non-ascii.stderr
Normal file
2
tests/ui/extern-flag/invalid-crate-name-non-ascii.stderr
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
error: crate name `čɍαţē` passed to `--extern` is not a valid ASCII identifier
|
||||||
|
|
4
tests/ui/extern-flag/invalid-crate-name.rs
Normal file
4
tests/ui/extern-flag/invalid-crate-name.rs
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
// compile-flags: --extern=?#1%$
|
||||||
|
// error-pattern: crate name `?#1%$` passed to `--extern` is not a valid ASCII identifier
|
||||||
|
|
||||||
|
fn main() {}
|
2
tests/ui/extern-flag/invalid-crate-name.stderr
Normal file
2
tests/ui/extern-flag/invalid-crate-name.stderr
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
error: crate name `?#1%$` passed to `--extern` is not a valid ASCII identifier
|
||||||
|
|
@ -1,6 +0,0 @@
|
|||||||
// compile-flags:--extern му_сгате
|
|
||||||
// edition:2018
|
|
||||||
|
|
||||||
use му_сгате::baz; //~ ERROR cannot load a crate with a non-ascii name `му_сгате`
|
|
||||||
|
|
||||||
fn main() {}
|
|
@ -1,8 +0,0 @@
|
|||||||
error: cannot load a crate with a non-ascii name `му_сгате`
|
|
||||||
--> $DIR/crate_name_nonascii_forbidden-2.rs:4:5
|
|
||||||
|
|
|
||||||
LL | use му_сгате::baz;
|
|
||||||
| ^^^^^^^^
|
|
||||||
|
|
||||||
error: aborting due to previous error
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
|||||||
error: cannot load a crate with a non-ascii name `ьаг`
|
error: cannot load a crate with a non-ascii name `ьаг`
|
||||||
--> $DIR/crate_name_nonascii_forbidden-1.rs:1:1
|
--> $DIR/crate_name_nonascii_forbidden.rs:1:1
|
||||||
|
|
|
|
||||||
LL | extern crate ьаг;
|
LL | extern crate ьаг;
|
||||||
| ^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^
|
@ -1,6 +1,6 @@
|
|||||||
// edition:2018
|
// edition:2018
|
||||||
// aux-build:trait-import-suggestions.rs
|
// aux-build:trait-import-suggestions.rs
|
||||||
// compile-flags:--extern trait-import-suggestions
|
// compile-flags:--extern trait_import_suggestions
|
||||||
|
|
||||||
mod foo {
|
mod foo {
|
||||||
mod foobar {
|
mod foobar {
|
||||||
|
Loading…
Reference in New Issue
Block a user