Skip to content

Commit fd8ca3e

Browse files
Rollup merge of rust-lang#42092 - cuviper:args_os, r=Mark-Simulacrum
Give a nicer error for non-Unicode arguments to rustc and rustdoc Previously, any non-Unicode argument would panic rustc: ``` $ rustc $'foo\x80bar' error: internal compiler error: unexpected panic note: the compiler unexpectedly panicked. this is a bug. note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports thread 'rustc' panicked at 'called `Result::unwrap()` on an `Err` value: "foo�bar"', /checkout/src/libcore/result.rs:859 note: Run with `RUST_BACKTRACE=1` for a backtrace. ``` Now it gives a clean error: ``` $ rustc $'foo\x80bar' error: Argument 1 is not valid Unicode: "foo�bar" ``` Maybe fixes rust-lang#15890, although we still can't *compile* arbitrary file names.
2 parents 7812adf + e86588e commit fd8ca3e

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/librustc_driver/lib.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1148,9 +1148,18 @@ pub fn diagnostics_registry() -> errors::registry::Registry {
11481148
Registry::new(&all_errors)
11491149
}
11501150

1151+
fn get_args() -> Vec<String> {
1152+
env::args_os().enumerate()
1153+
.map(|(i, arg)| arg.into_string().unwrap_or_else(|arg| {
1154+
early_error(ErrorOutputType::default(),
1155+
&format!("Argument {} is not valid Unicode: {:?}", i, arg))
1156+
}))
1157+
.collect()
1158+
}
1159+
11511160
pub fn main() {
11521161
env_logger::init().unwrap();
1153-
let result = run(|| run_compiler(&env::args().collect::<Vec<_>>(),
1162+
let result = run(|| run_compiler(&get_args(),
11541163
&mut RustcDefaultCalls,
11551164
None,
11561165
None));

src/librustdoc/lib.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,19 @@ pub fn main() {
107107
const STACK_SIZE: usize = 32_000_000; // 32MB
108108
env_logger::init().unwrap();
109109
let res = std::thread::Builder::new().stack_size(STACK_SIZE).spawn(move || {
110-
let s = env::args().collect::<Vec<_>>();
111-
main_args(&s)
110+
get_args().map(|args| main_args(&args)).unwrap_or(1)
112111
}).unwrap().join().unwrap_or(101);
113112
process::exit(res as i32);
114113
}
115114

115+
fn get_args() -> Option<Vec<String>> {
116+
env::args_os().enumerate()
117+
.map(|(i, arg)| arg.into_string().map_err(|arg| {
118+
print_error(format!("Argument {} is not valid Unicode: {:?}", i, arg));
119+
}).ok())
120+
.collect()
121+
}
122+
116123
fn stable(g: getopts::OptGroup) -> RustcOptGroup { RustcOptGroup::stable(g) }
117124
fn unstable(g: getopts::OptGroup) -> RustcOptGroup { RustcOptGroup::unstable(g) }
118125

0 commit comments

Comments
 (0)