Skip to content

Commit fb87e60

Browse files
committed
Refactor argument UTF-8 checking into rustc_driver::args::raw_args()
1 parent 63091b1 commit fb87e60

File tree

3 files changed

+28
-23
lines changed

3 files changed

+28
-23
lines changed

compiler/rustc_driver_impl/src/args.rs

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use std::error;
2-
use std::fmt;
3-
use std::fs;
4-
use std::io;
1+
use std::{env, error, fmt, fs, io};
52

63
use rustc_session::EarlyDiagCtxt;
74
use rustc_span::ErrorGuaranteed;
@@ -116,6 +113,29 @@ pub fn arg_expand_all(
116113
result.map(|()| expander.finish())
117114
}
118115

116+
/// Gets the raw unprocessed command-line arguments as Unicode strings, without doing any further
117+
/// processing (e.g., without `@file` expansion).
118+
///
119+
/// This function is identical to [`env::args()`] except that it emits an error when it encounters
120+
/// non-Unicode arguments instead of panicking.
121+
pub fn raw_args(early_dcx: &EarlyDiagCtxt) -> Result<Vec<String>, ErrorGuaranteed> {
122+
let mut res = Ok(Vec::new());
123+
for (i, arg) in env::args_os().enumerate() {
124+
match arg.into_string() {
125+
Ok(arg) => {
126+
if let Ok(args) = &mut res {
127+
args.push(arg);
128+
}
129+
}
130+
Err(arg) => {
131+
res =
132+
Err(early_dcx.early_err(format!("argument {i} is not valid Unicode: {arg:?}")))
133+
}
134+
}
135+
}
136+
res
137+
}
138+
119139
#[derive(Debug)]
120140
enum Error {
121141
Utf8Error(String),

compiler/rustc_driver_impl/src/lib.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -1515,15 +1515,7 @@ pub fn main() -> ! {
15151515
let mut callbacks = TimePassesCallbacks::default();
15161516
let using_internal_features = install_ice_hook(DEFAULT_BUG_REPORT_URL, |_| ());
15171517
let exit_code = catch_with_exit_code(|| {
1518-
let args = env::args_os()
1519-
.enumerate()
1520-
.map(|(i, arg)| {
1521-
arg.into_string().unwrap_or_else(|arg| {
1522-
early_dcx.early_fatal(format!("argument {i} is not valid Unicode: {arg:?}"))
1523-
})
1524-
})
1525-
.collect::<Vec<_>>();
1526-
RunCompiler::new(&args, &mut callbacks)
1518+
RunCompiler::new(&args::raw_args(&early_dcx)?, &mut callbacks)
15271519
.set_using_internal_features(using_internal_features)
15281520
.run()
15291521
});

src/librustdoc/lib.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -179,21 +179,14 @@ pub fn main() {
179179
rustc_driver::init_logger(&early_dcx, rustc_log::LoggerConfig::from_env("RUSTDOC_LOG"));
180180

181181
let exit_code = rustc_driver::catch_with_exit_code(|| {
182-
let args = env::args_os()
183-
.enumerate()
184-
.map(|(i, arg)| {
185-
arg.into_string().unwrap_or_else(|arg| {
186-
early_dcx.early_fatal(format!("argument {i} is not valid Unicode: {arg:?}"))
187-
})
188-
})
189-
.collect::<Vec<_>>();
190-
main_args(&mut early_dcx, &args, using_internal_features)
182+
let at_args = rustc_driver::args::raw_args(&early_dcx)?;
183+
main_args(&mut early_dcx, &at_args, using_internal_features)
191184
});
192185
process::exit(exit_code);
193186
}
194187

195188
fn init_logging(early_dcx: &EarlyDiagCtxt) {
196-
let color_logs = match std::env::var("RUSTDOC_LOG_COLOR").as_deref() {
189+
let color_logs = match env::var("RUSTDOC_LOG_COLOR").as_deref() {
197190
Ok("always") => true,
198191
Ok("never") => false,
199192
Ok("auto") | Err(VarError::NotPresent) => io::stdout().is_terminal(),

0 commit comments

Comments
 (0)