Skip to content

Commit 7a2e583

Browse files
committed
Refactor argument UTF-8 checking into rustc_driver::args::raw_args()
1 parent 5629efc commit 7a2e583

File tree

3 files changed

+30
-37
lines changed

3 files changed

+30
-37
lines changed

compiler/rustc_driver_impl/src/args.rs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
use rustc_session::config::ErrorOutputType;
21
use rustc_span::ErrorGuaranteed;
3-
use std::error;
4-
use std::fmt;
5-
use std::fs;
6-
use std::io;
2+
use std::{env, error, fmt, fs, io};
73

84
use rustc_session::EarlyErrorHandler;
95

@@ -50,6 +46,29 @@ pub fn arg_expand_all(
5046
res
5147
}
5248

49+
/// Gets the raw unprocessed command-line arguments as Unicode strings, without doing any further
50+
/// processing (e.g., without `@file` expansion).
51+
///
52+
/// This function is identical to [`env::args()`] except that it emits an error when it encounters
53+
/// non-Unicode arguments instead of panicking.
54+
pub fn raw_args(handler: &EarlyErrorHandler) -> Result<Vec<String>, ErrorGuaranteed> {
55+
let mut res = Ok(Vec::new());
56+
for (i, arg) in env::args_os().enumerate() {
57+
match arg.into_string() {
58+
Ok(arg) => {
59+
if let Ok(args) = &mut res {
60+
args.push(arg);
61+
}
62+
}
63+
Err(arg) => {
64+
res = Err(handler
65+
.early_error_no_abort(format!("argument {i} is not valid Unicode: {arg:?}")))
66+
}
67+
}
68+
}
69+
res
70+
}
71+
5372
#[derive(Debug)]
5473
enum Error {
5574
Utf8Error(String),

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,17 +1565,8 @@ pub fn main() -> ! {
15651565
signal_handler::install();
15661566
let mut callbacks = TimePassesCallbacks::default();
15671567
install_ice_hook(DEFAULT_BUG_REPORT_URL, |_| ());
1568-
let exit_code = catch_with_exit_code(|| {
1569-
let args = env::args_os()
1570-
.enumerate()
1571-
.map(|(i, arg)| {
1572-
arg.into_string().unwrap_or_else(|arg| {
1573-
handler.early_error(format!("argument {i} is not valid Unicode: {arg:?}"))
1574-
})
1575-
})
1576-
.collect::<Vec<_>>();
1577-
RunCompiler::new(&args, &mut callbacks).run()
1578-
});
1568+
let exit_code =
1569+
catch_with_exit_code(|| RunCompiler::new(&args::raw_args(&handler)?, &mut callbacks).run());
15791570

15801571
if let Some(format) = callbacks.time_passes {
15811572
let end_rss = get_resident_set_size();

src/librustdoc/lib.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -175,19 +175,15 @@ pub fn main() {
175175
init_logging(&handler);
176176
rustc_driver::init_env_logger(&handler, "RUSTDOC_LOG");
177177

178-
let exit_code = rustc_driver::catch_with_exit_code(|| match get_args(&handler) {
179-
Some(args) => main_args(&mut handler, &args),
180-
_ =>
181-
{
182-
#[allow(deprecated)]
183-
Err(ErrorGuaranteed::unchecked_claim_error_was_emitted())
184-
}
178+
let exit_code = rustc_driver::catch_with_exit_code(|| {
179+
let at_args = rustc_driver::args::raw_args(&handler)?;
180+
main_args(&mut handler, &at_args)
185181
});
186182
process::exit(exit_code);
187183
}
188184

189185
fn init_logging(handler: &EarlyErrorHandler) {
190-
let color_logs = match std::env::var("RUSTDOC_LOG_COLOR").as_deref() {
186+
let color_logs = match env::var("RUSTDOC_LOG_COLOR").as_deref() {
191187
Ok("always") => true,
192188
Ok("never") => false,
193189
Ok("auto") | Err(VarError::NotPresent) => io::stdout().is_terminal(),
@@ -217,19 +213,6 @@ fn init_logging(handler: &EarlyErrorHandler) {
217213
tracing::subscriber::set_global_default(subscriber).unwrap();
218214
}
219215

220-
fn get_args(handler: &EarlyErrorHandler) -> Option<Vec<String>> {
221-
env::args_os()
222-
.enumerate()
223-
.map(|(i, arg)| {
224-
arg.into_string()
225-
.map_err(|arg| {
226-
handler.early_warn(format!("Argument {i} is not valid Unicode: {arg:?}"));
227-
})
228-
.ok()
229-
})
230-
.collect()
231-
}
232-
233216
fn opts() -> Vec<RustcOptGroup> {
234217
let stable: fn(_, fn(&mut getopts::Options) -> &mut _) -> _ = RustcOptGroup::stable;
235218
let unstable: fn(_, fn(&mut getopts::Options) -> &mut _) -> _ = RustcOptGroup::unstable;

0 commit comments

Comments
 (0)