Refactor argument UTF-8 checking into rustc_driver::args::raw_args()
This commit is contained in:
parent
63091b105d
commit
fb87e606cc
3 changed files with 28 additions and 23 deletions
|
@ -1,7 +1,4 @@
|
||||||
use std::error;
|
use std::{env, error, fmt, fs, io};
|
||||||
use std::fmt;
|
|
||||||
use std::fs;
|
|
||||||
use std::io;
|
|
||||||
|
|
||||||
use rustc_session::EarlyDiagCtxt;
|
use rustc_session::EarlyDiagCtxt;
|
||||||
use rustc_span::ErrorGuaranteed;
|
use rustc_span::ErrorGuaranteed;
|
||||||
|
@ -116,6 +113,29 @@ pub fn arg_expand_all(
|
||||||
result.map(|()| expander.finish())
|
result.map(|()| expander.finish())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Gets the raw unprocessed command-line arguments as Unicode strings, without doing any further
|
||||||
|
/// processing (e.g., without `@file` expansion).
|
||||||
|
///
|
||||||
|
/// This function is identical to [`env::args()`] except that it emits an error when it encounters
|
||||||
|
/// non-Unicode arguments instead of panicking.
|
||||||
|
pub fn raw_args(early_dcx: &EarlyDiagCtxt) -> Result<Vec<String>, ErrorGuaranteed> {
|
||||||
|
let mut res = Ok(Vec::new());
|
||||||
|
for (i, arg) in env::args_os().enumerate() {
|
||||||
|
match arg.into_string() {
|
||||||
|
Ok(arg) => {
|
||||||
|
if let Ok(args) = &mut res {
|
||||||
|
args.push(arg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(arg) => {
|
||||||
|
res =
|
||||||
|
Err(early_dcx.early_err(format!("argument {i} is not valid Unicode: {arg:?}")))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum Error {
|
enum Error {
|
||||||
Utf8Error(String),
|
Utf8Error(String),
|
||||||
|
|
|
@ -1515,15 +1515,7 @@ pub fn main() -> ! {
|
||||||
let mut callbacks = TimePassesCallbacks::default();
|
let mut callbacks = TimePassesCallbacks::default();
|
||||||
let using_internal_features = install_ice_hook(DEFAULT_BUG_REPORT_URL, |_| ());
|
let using_internal_features = install_ice_hook(DEFAULT_BUG_REPORT_URL, |_| ());
|
||||||
let exit_code = catch_with_exit_code(|| {
|
let exit_code = catch_with_exit_code(|| {
|
||||||
let args = env::args_os()
|
RunCompiler::new(&args::raw_args(&early_dcx)?, &mut callbacks)
|
||||||
.enumerate()
|
|
||||||
.map(|(i, arg)| {
|
|
||||||
arg.into_string().unwrap_or_else(|arg| {
|
|
||||||
early_dcx.early_fatal(format!("argument {i} is not valid Unicode: {arg:?}"))
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
RunCompiler::new(&args, &mut callbacks)
|
|
||||||
.set_using_internal_features(using_internal_features)
|
.set_using_internal_features(using_internal_features)
|
||||||
.run()
|
.run()
|
||||||
});
|
});
|
||||||
|
|
|
@ -179,21 +179,14 @@ pub fn main() {
|
||||||
rustc_driver::init_logger(&early_dcx, rustc_log::LoggerConfig::from_env("RUSTDOC_LOG"));
|
rustc_driver::init_logger(&early_dcx, rustc_log::LoggerConfig::from_env("RUSTDOC_LOG"));
|
||||||
|
|
||||||
let exit_code = rustc_driver::catch_with_exit_code(|| {
|
let exit_code = rustc_driver::catch_with_exit_code(|| {
|
||||||
let args = env::args_os()
|
let at_args = rustc_driver::args::raw_args(&early_dcx)?;
|
||||||
.enumerate()
|
main_args(&mut early_dcx, &at_args, using_internal_features)
|
||||||
.map(|(i, arg)| {
|
|
||||||
arg.into_string().unwrap_or_else(|arg| {
|
|
||||||
early_dcx.early_fatal(format!("argument {i} is not valid Unicode: {arg:?}"))
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
main_args(&mut early_dcx, &args, using_internal_features)
|
|
||||||
});
|
});
|
||||||
process::exit(exit_code);
|
process::exit(exit_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init_logging(early_dcx: &EarlyDiagCtxt) {
|
fn init_logging(early_dcx: &EarlyDiagCtxt) {
|
||||||
let color_logs = match std::env::var("RUSTDOC_LOG_COLOR").as_deref() {
|
let color_logs = match env::var("RUSTDOC_LOG_COLOR").as_deref() {
|
||||||
Ok("always") => true,
|
Ok("always") => true,
|
||||||
Ok("never") => false,
|
Ok("never") => false,
|
||||||
Ok("auto") | Err(VarError::NotPresent) => io::stdout().is_terminal(),
|
Ok("auto") | Err(VarError::NotPresent) => io::stdout().is_terminal(),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue