diff options
| author | beetrees <b@beetr.ee> | 2023-05-15 18:35:14 +0000 |
|---|---|---|
| committer | beetrees <b@beetr.ee> | 2024-03-07 00:20:01 +0000 |
| commit | fb87e606cc6951cb41044b23f8f723abd9ad7fce (patch) | |
| tree | 4ecb922fa05e5560c7cc03ce78e87631eda88e72 /compiler/rustc_driver_impl/src/args.rs | |
| parent | 63091b105d08b7b0db19d699d3be3060acde04ad (diff) | |
| download | rust-fb87e606cc6951cb41044b23f8f723abd9ad7fce.tar.gz rust-fb87e606cc6951cb41044b23f8f723abd9ad7fce.zip | |
Refactor argument UTF-8 checking into `rustc_driver::args::raw_args()`
Diffstat (limited to 'compiler/rustc_driver_impl/src/args.rs')
| -rw-r--r-- | compiler/rustc_driver_impl/src/args.rs | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/compiler/rustc_driver_impl/src/args.rs b/compiler/rustc_driver_impl/src/args.rs index 8c03f54bb59..28574457389 100644 --- a/compiler/rustc_driver_impl/src/args.rs +++ b/compiler/rustc_driver_impl/src/args.rs @@ -1,7 +1,4 @@ -use std::error; -use std::fmt; -use std::fs; -use std::io; +use std::{env, error, fmt, fs, io}; use rustc_session::EarlyDiagCtxt; use rustc_span::ErrorGuaranteed; @@ -116,6 +113,29 @@ pub fn arg_expand_all( 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)] enum Error { Utf8Error(String), |
