Auto merge of #116001 - fmease:validate-crate-name-extern-cli-opt, r=est31
[breaking change] Validate crate name in `--extern` [MCP 650]
Reject non-ASCII-identifier crate names passed to the CLI option `--extern` (`rustc`, `rustdoc`).
Implements [MCP 650](https://github.com/rust-lang/compiler-team/issues/650) (except that we only allow ASCII identifiers not arbitrary Rust identifiers).
Fixes #113035.
[As mentioned on Zulip](376826988
), doing a crater run probably doesn't make sense since it wouldn't yield anything. Most users don't interact with `rustc` directly but only ever through Cargo which always passes a valid crate name to `--extern` when it invokes `rustc` and `rustdoc`. In any case, the user wouldn't be able to use such a crate name in the source code anyway.
Note that I'm not using [`rustc_session::output::validate_crate_name`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_session/output/fn.validate_crate_name.html) (used for `--crate-name` and `#![crate_name]`) since the latter doesn't reject non-ASCII crate names and ones that start with a digit.
As an aside, I've also thought about getting rid of `validate_crate_name` entirely in a separate PR (with another MCP) in favor of `is_ascii_ident` to reject more weird `--crate-name`s, `#![crate_name]`s and file names but I think that would lead to a lot of actual breakage, namely because of file names starting with a digit. In `tests/ui` 9 tests would be impacted for example.
CC `@estebank`
r? `@est31`
This commit is contained in:
commit
aadb5718dd
15 changed files with 60 additions and 17 deletions
|
@ -2475,6 +2475,19 @@ pub fn parse_externs(
|
|||
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 entry = externs.entry(name.to_owned());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue