Skip to content

Commit d949774

Browse files
committed
Move argfile expansion into run_compiler
This will make @path work with miri and other non-standard entrypoints. Also since this simplifies librustc_driver::args, move it into a simple source file. Also remove the tests since they're doing nothing more than checking `str::lines` has the right behaviour.
1 parent d2219c2 commit d949774

8 files changed

+70
-239
lines changed

src/librustc_driver/args.rs

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use std::error;
2+
use std::fmt;
3+
use std::fs;
4+
use std::io;
5+
use std::str;
6+
use std::sync::atomic::{AtomicBool, Ordering};
7+
8+
static USED_ARGSFILE_FEATURE: AtomicBool = AtomicBool::new(false);
9+
10+
pub fn used_unstable_argsfile() -> bool {
11+
USED_ARGSFILE_FEATURE.load(Ordering::Relaxed)
12+
}
13+
14+
pub fn arg_expand(arg: String) -> Result<Vec<String>, Error> {
15+
if arg.starts_with("@") {
16+
let path = &arg[1..];
17+
let file = match fs::read_to_string(path) {
18+
Ok(file) => {
19+
USED_ARGSFILE_FEATURE.store(true, Ordering::Relaxed);
20+
file
21+
}
22+
Err(ref err) if err.kind() == io::ErrorKind::InvalidData => {
23+
return Err(Error::Utf8Error(Some(path.to_string())));
24+
}
25+
Err(err) => return Err(Error::IOError(path.to_string(), err)),
26+
};
27+
Ok(file.lines().map(ToString::to_string).collect())
28+
} else {
29+
Ok(vec![arg])
30+
}
31+
}
32+
33+
#[derive(Debug)]
34+
pub enum Error {
35+
Utf8Error(Option<String>),
36+
IOError(String, io::Error),
37+
}
38+
39+
impl fmt::Display for Error {
40+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
41+
match self {
42+
Error::Utf8Error(None) => write!(fmt, "Utf8 error"),
43+
Error::Utf8Error(Some(path)) => write!(fmt, "Utf8 error in {}", path),
44+
Error::IOError(path, err) => write!(fmt, "IO Error: {}: {}", path, err),
45+
}
46+
}
47+
}
48+
49+
impl error::Error for Error {
50+
fn description(&self) -> &'static str {
51+
"argument error"
52+
}
53+
}

src/librustc_driver/args/mod.rs

-84
This file was deleted.

src/librustc_driver/args/tests.rs

-145
This file was deleted.

src/librustc_driver/lib.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,22 @@ impl Callbacks for TimePassesCallbacks {
140140
// See comments on CompilerCalls below for details about the callbacks argument.
141141
// The FileLoader provides a way to load files from sources other than the file system.
142142
pub fn run_compiler(
143-
args: &[String],
143+
at_args: &[String],
144144
callbacks: &mut (dyn Callbacks + Send),
145145
file_loader: Option<Box<dyn FileLoader + Send + Sync>>,
146146
emitter: Option<Box<dyn Write + Send>>
147147
) -> interface::Result<()> {
148+
let mut args = Vec::new();
149+
for arg in at_args {
150+
match args::arg_expand(arg.clone()) {
151+
Ok(arg) => args.extend(arg),
152+
Err(err) => early_error(ErrorOutputType::default(),
153+
&format!("Failed to load argument file: {}", err)),
154+
}
155+
}
148156
let diagnostic_output = emitter.map(|emitter| DiagnosticOutput::Raw(emitter))
149157
.unwrap_or(DiagnosticOutput::Default);
150-
let matches = match handle_options(args) {
158+
let matches = match handle_options(&args) {
151159
Some(matches) => matches,
152160
None => return Ok(()),
153161
};
@@ -1199,10 +1207,10 @@ pub fn main() {
11991207
init_rustc_env_logger();
12001208
let mut callbacks = TimePassesCallbacks::default();
12011209
let result = report_ices_to_stderr_if_any(|| {
1202-
let args = args::ArgsIter::new().enumerate()
1203-
.map(|(i, arg)| arg.unwrap_or_else(|err| {
1204-
early_error(ErrorOutputType::default(),
1205-
&format!("Argument {} is not valid: {}", i, err))
1210+
let args = env::args_os().enumerate()
1211+
.map(|(i, arg)| arg.into_string().unwrap_or_else(|arg| {
1212+
early_error(ErrorOutputType::default(),
1213+
&format!("Argument {} is not valid Unicode: {:?}", i, arg))
12061214
}))
12071215
.collect::<Vec<_>>();
12081216
run_compiler(&args, &mut callbacks, None, None)

src/test/ui/commandline-argfile-badutf8.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Check to see if we can get parameters from an @argsfile file
22
//
33
// build-fail
4-
// normalize-stderr-test: "Argument \d+" -> "Argument $$N"
54
// compile-flags: --cfg cmdline_set @{{src-base}}/commandline-argfile-badutf8.args
65

76
#[cfg(not(cmdline_set))]
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
error: Argument $N is not valid: Utf8 error in $DIR/commandline-argfile-badutf8.args
1+
error: Failed to load argument file: Utf8 error in $DIR/commandline-argfile-badutf8.args
22

src/test/ui/commandline-argfile-missing.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Check to see if we can get parameters from an @argsfile file
22
//
3+
// ignore-tidy-linelength
34
// build-fail
4-
// normalize-stderr-test: "Argument \d+" -> "Argument $$N"
55
// normalize-stderr-test: "os error \d+" -> "os error $$ERR"
66
// normalize-stderr-test: "commandline-argfile-missing.args:[^(]*" -> "commandline-argfile-missing.args: $$FILE_MISSING "
77
// compile-flags: --cfg cmdline_set @{{src-base}}/commandline-argfile-missing.args
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
error: Argument $N is not valid: IO Error: $DIR/commandline-argfile-missing.args: $FILE_MISSING (os error $ERR)
1+
error: Failed to load argument file: IO Error: $DIR/commandline-argfile-missing.args: $FILE_MISSING (os error $ERR)
22

0 commit comments

Comments
 (0)