Skip to content

Commit 0f7b14e

Browse files
committed
Refactor argument UTF-8 checking into rustc_driver::args::raw_args()
1 parent 6136997 commit 0f7b14e

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;
@@ -115,6 +112,29 @@ pub fn arg_expand_all(
115112
result.map(|()| expander.finish())
116113
}
117114

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

compiler/rustc_driver_impl/src/lib.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -1489,15 +1489,7 @@ pub fn main() -> ! {
14891489
let mut callbacks = TimePassesCallbacks::default();
14901490
let using_internal_features = install_ice_hook(DEFAULT_BUG_REPORT_URL, |_| ());
14911491
let exit_code = catch_with_exit_code(|| {
1492-
let args = env::args_os()
1493-
.enumerate()
1494-
.map(|(i, arg)| {
1495-
arg.into_string().unwrap_or_else(|arg| {
1496-
early_dcx.early_fatal(format!("argument {i} is not valid Unicode: {arg:?}"))
1497-
})
1498-
})
1499-
.collect::<Vec<_>>();
1500-
RunCompiler::new(&args, &mut callbacks)
1492+
RunCompiler::new(&args::raw_args(&early_dcx)?, &mut callbacks)
15011493
.set_using_internal_features(using_internal_features)
15021494
.run()
15031495
});

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)