Skip to content

Commit a87adb0

Browse files
committed
Refactor some of the crate name parsing code and add a unit test
1 parent d4f880f commit a87adb0

File tree

1 file changed

+5
-34
lines changed

1 file changed

+5
-34
lines changed

compiler/rustc_session/src/config.rs

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::str::{self, FromStr};
1414
use std::sync::LazyLock;
1515
use std::{cmp, fmt, fs, iter};
1616

17+
use externs::{ExternOpt, split_extern_opt};
1718
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
1819
use rustc_data_structures::stable_hasher::{StableOrd, ToStableHashKey};
1920
use rustc_errors::emitter::HumanReadableErrorType;
@@ -39,6 +40,7 @@ use crate::utils::CanonicalizedPath;
3940
use crate::{EarlyDiagCtxt, HashStableContext, Session, filesearch, lint};
4041

4142
mod cfg;
43+
mod externs;
4244
mod native_libs;
4345
pub mod sigpipe;
4446

@@ -2188,44 +2190,13 @@ pub fn parse_externs(
21882190
matches: &getopts::Matches,
21892191
unstable_opts: &UnstableOptions,
21902192
) -> Externs {
2191-
fn is_ascii_ident(string: &str) -> bool {
2192-
let mut chars = string.chars();
2193-
if let Some(start) = chars.next()
2194-
&& (start.is_ascii_alphabetic() || start == '_')
2195-
{
2196-
chars.all(|char| char.is_ascii_alphanumeric() || char == '_')
2197-
} else {
2198-
false
2199-
}
2200-
}
2201-
22022193
let is_unstable_enabled = unstable_opts.unstable_options;
22032194
let mut externs: BTreeMap<String, ExternEntry> = BTreeMap::new();
22042195
for arg in matches.opt_strs("extern") {
2205-
let (name, path) = match arg.split_once('=') {
2206-
None => (arg, None),
2207-
Some((name, path)) => (name.to_string(), Some(Path::new(path))),
2208-
};
2209-
let (options, name) = match name.split_once(':') {
2210-
None => (None, name),
2211-
Some((opts, name)) => (Some(opts), name.to_string()),
2212-
};
2213-
2214-
if !is_ascii_ident(&name) {
2215-
let mut error = early_dcx.early_struct_fatal(format!(
2216-
"crate name `{name}` passed to `--extern` is not a valid ASCII identifier"
2217-
));
2218-
let adjusted_name = name.replace('-', "_");
2219-
if is_ascii_ident(&adjusted_name) {
2220-
#[allow(rustc::diagnostic_outside_of_impl)] // FIXME
2221-
error.help(format!(
2222-
"consider replacing the dashes with underscores: `{adjusted_name}`"
2223-
));
2224-
}
2225-
error.emit();
2226-
}
2196+
let ExternOpt { crate_name: name, path, options } =
2197+
split_extern_opt(early_dcx, &arg).unwrap_or_else(|e| e.emit());
22272198

2228-
let path = path.map(|p| CanonicalizedPath::new(p));
2199+
let path = path.map(|p| CanonicalizedPath::new(p.as_path()));
22292200

22302201
let entry = externs.entry(name.to_owned());
22312202

0 commit comments

Comments
 (0)