Skip to content

Commit 5629efc

Browse files
committed
Make arg_expand_all not short-circuit on first error
1 parent 27fb598 commit 5629efc

26 files changed

+88
-33
lines changed

compiler/rustc_driver_impl/src/args.rs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,66 @@
1+
use rustc_session::config::ErrorOutputType;
2+
use rustc_span::ErrorGuaranteed;
13
use std::error;
24
use std::fmt;
35
use std::fs;
46
use std::io;
57

68
use rustc_session::EarlyErrorHandler;
79

8-
fn arg_expand(arg: String) -> Result<Vec<String>, Error> {
10+
fn arg_expand(arg: &str) -> Result<Vec<String>, Error> {
911
if let Some(path) = arg.strip_prefix('@') {
1012
let file = match fs::read_to_string(path) {
1113
Ok(file) => file,
1214
Err(ref err) if err.kind() == io::ErrorKind::InvalidData => {
13-
return Err(Error::Utf8Error(Some(path.to_string())));
15+
return Err(Error::Utf8Error(path.to_string()));
1416
}
1517
Err(err) => return Err(Error::IOError(path.to_string(), err)),
1618
};
1719
Ok(file.lines().map(ToString::to_string).collect())
1820
} else {
19-
Ok(vec![arg])
21+
Ok(vec![arg.to_string()])
2022
}
2123
}
2224

25+
/// Replaces any `@file` arguments with the contents of `file`, with each line of `file` as a
26+
/// separate argument.
27+
///
2328
/// **Note:** This function doesn't interpret argument 0 in any special way.
2429
/// If this function is intended to be used with command line arguments,
2530
/// `argv[0]` must be removed prior to calling it manually.
26-
pub fn arg_expand_all(handler: &EarlyErrorHandler, at_args: &[String]) -> Vec<String> {
27-
let mut args = Vec::new();
31+
pub fn arg_expand_all(
32+
handler: &EarlyErrorHandler,
33+
at_args: &[String],
34+
) -> Result<Vec<String>, ErrorGuaranteed> {
35+
let mut res = Ok(Vec::new());
2836
for arg in at_args {
29-
match arg_expand(arg.clone()) {
30-
Ok(arg) => args.extend(arg),
31-
Err(err) => handler.early_error(format!("Failed to load argument file: {err}")),
37+
match arg_expand(arg) {
38+
Ok(arg) => {
39+
if let Ok(args) = &mut res {
40+
args.extend(arg)
41+
}
42+
}
43+
Err(err) => {
44+
res =
45+
Err(handler
46+
.early_error_no_abort(format!("failed to load argument file: {err}")))
47+
}
3248
}
3349
}
34-
args
50+
res
3551
}
3652

3753
#[derive(Debug)]
38-
pub enum Error {
39-
Utf8Error(Option<String>),
54+
enum Error {
55+
Utf8Error(String),
4056
IOError(String, io::Error),
4157
}
4258

4359
impl fmt::Display for Error {
4460
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
4561
match self {
46-
Error::Utf8Error(None) => write!(fmt, "Utf8 error"),
47-
Error::Utf8Error(Some(path)) => write!(fmt, "Utf8 error in {path}"),
48-
Error::IOError(path, err) => write!(fmt, "IO Error: {path}: {err}"),
62+
Error::Utf8Error(path) => write!(fmt, "UTF-8 error in {path}"),
63+
Error::IOError(path, err) => write!(fmt, "IO error: {path}: {err}"),
4964
}
5065
}
5166
}

compiler/rustc_driver_impl/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ fn run_compiler(
280280
// the compiler with @empty_file as argv[0] and no more arguments.
281281
let at_args = at_args.get(1..).unwrap_or_default();
282282

283-
let args = args::arg_expand_all(&early_error_handler, at_args);
283+
let args = args::arg_expand_all(&early_error_handler, at_args)?;
284284

285285
let Some(matches) = handle_options(&early_error_handler, &args) else { return Ok(()) };
286286

src/librustdoc/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ fn main_args(handler: &mut EarlyErrorHandler, at_args: &[String]) -> MainResult
715715
// the compiler with @empty_file as argv[0] and no more arguments.
716716
let at_args = at_args.get(1..).unwrap_or_default();
717717

718-
let args = rustc_driver::args::arg_expand_all(handler, at_args);
718+
let args = rustc_driver::args::arg_expand_all(handler, at_args)?;
719719

720720
let mut options = getopts::Options::new();
721721
for option in opts() {

src/tools/tidy/src/ui_tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::path::{Path, PathBuf};
1111
const ENTRY_LIMIT: usize = 900;
1212
// FIXME: The following limits should be reduced eventually.
1313
const ISSUES_ENTRY_LIMIT: usize = 1891;
14-
const ROOT_ENTRY_LIMIT: usize = 866;
14+
const ROOT_ENTRY_LIMIT: usize = 860;
1515

1616
const EXPECTED_TEST_FILE_EXTENSIONS: &[&str] = &[
1717
"rs", // test source files
@@ -25,8 +25,8 @@ const EXPECTED_TEST_FILE_EXTENSIONS: &[&str] = &[
2525
const EXTENSION_EXCEPTION_PATHS: &[&str] = &[
2626
"tests/ui/asm/named-asm-labels.s", // loading an external asm file to test named labels lint
2727
"tests/ui/check-cfg/my-awesome-platform.json", // testing custom targets with cfgs
28-
"tests/ui/commandline-argfile-badutf8.args", // passing args via a file
29-
"tests/ui/commandline-argfile.args", // passing args via a file
28+
"tests/ui/argfile/commandline-argfile-badutf8.args", // passing args via a file
29+
"tests/ui/argfile/commandline-argfile.args", // passing args via a file
3030
"tests/ui/crate-loading/auxiliary/libfoo.rlib", // testing loading a manually created rlib
3131
"tests/ui/include-macros/data.bin", // testing including data with the include macros
3232
"tests/ui/include-macros/file.txt", // testing including data with the include macros

tests/rustdoc-ui/commandline-argfile-badutf8.rs renamed to tests/rustdoc-ui/argfile/commandline-argfile-badutf8.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Check to see if we can get parameters from an @argsfile file
22
//
3-
// compile-flags: --cfg cmdline_set @{{src-base}}/commandline-argfile-badutf8.args
3+
// compile-flags: --cfg cmdline_set @{{src-base}}/argfile/commandline-argfile-badutf8.args
44

55
#[cfg(not(cmdline_set))]
66
compile_error!("cmdline_set not set");
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: failed to load argument file: UTF-8 error in $DIR/commandline-argfile-badutf8.args
2+

tests/ui/commandline-argfile-missing.rs renamed to tests/rustdoc-ui/argfile/commandline-argfile-missing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// normalize-stderr-test: "os error \d+" -> "os error $$ERR"
44
// normalize-stderr-test: "commandline-argfile-missing.args:[^(]*" -> "commandline-argfile-missing.args: $$FILE_MISSING "
5-
// compile-flags: --cfg cmdline_set @{{src-base}}/commandline-argfile-missing.args
5+
// compile-flags: --cfg cmdline_set @{{src-base}}/argfile/commandline-argfile-missing.args
66

77
#[cfg(not(cmdline_set))]
88
compile_error!("cmdline_set not set");
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: failed to load argument file: IO error: $DIR/commandline-argfile-missing.args: $FILE_MISSING (os error $ERR)
2+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Check to see if we can get parameters from an @argsfile file
2+
//
3+
// normalize-stderr-test: "os error \d+" -> "os error $$ERR"
4+
// normalize-stderr-test: "commandline-argfile-missing.args:[^(]*" -> "commandline-argfile-missing.args: $$FILE_MISSING "
5+
// compile-flags: --cfg cmdline_set @{{src-base}}/argfile/commandline-argfile-missing.args @{{src-base}}/argfile/commandline-argfile-badutf8.args @{{src-base}}/argfile/commandline-argfile-missing2.args
6+
7+
#[cfg(not(cmdline_set))]
8+
compile_error!("cmdline_set not set");
9+
10+
#[cfg(not(unbroken))]
11+
compile_error!("unbroken not set");
12+
13+
fn main() {
14+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
error: failed to load argument file: IO error: $DIR/commandline-argfile-missing.args: $FILE_MISSING (os error $ERR)
2+
3+
error: failed to load argument file: UTF-8 error in $DIR/commandline-argfile-badutf8.args
4+
5+
error: failed to load argument file: IO error: $DIR/commandline-argfile-missing2.args: No such file or directory (os error $ERR)
6+

tests/rustdoc-ui/commandline-argfile.rs renamed to tests/rustdoc-ui/argfile/commandline-argfile.rs

Lines changed: 1 addition & 1 deletion
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
//
33
// check-pass
4-
// compile-flags: --cfg cmdline_set @{{src-base}}/commandline-argfile.args
4+
// compile-flags: --cfg cmdline_set @{{src-base}}/argfile/commandline-argfile.args
55

66
#[cfg(not(cmdline_set))]
77
compile_error!("cmdline_set not set");

tests/rustdoc-ui/commandline-argfile-badutf8.stderr

Lines changed: 0 additions & 2 deletions
This file was deleted.

tests/rustdoc-ui/commandline-argfile-missing.stderr

Lines changed: 0 additions & 2 deletions
This file was deleted.

tests/ui/commandline-argfile-badutf8.rs renamed to tests/ui/argfile/commandline-argfile-badutf8.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Check to see if we can get parameters from an @argsfile file
22
//
3-
// compile-flags: --cfg cmdline_set @{{src-base}}/commandline-argfile-badutf8.args
3+
// compile-flags: --cfg cmdline_set @{{src-base}}/argfile/commandline-argfile-badutf8.args
44

55
#[cfg(not(cmdline_set))]
66
compile_error!("cmdline_set not set");
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: failed to load argument file: UTF-8 error in $DIR/commandline-argfile-badutf8.args
2+

tests/rustdoc-ui/commandline-argfile-missing.rs renamed to tests/ui/argfile/commandline-argfile-missing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// normalize-stderr-test: "os error \d+" -> "os error $$ERR"
44
// normalize-stderr-test: "commandline-argfile-missing.args:[^(]*" -> "commandline-argfile-missing.args: $$FILE_MISSING "
5-
// compile-flags: --cfg cmdline_set @{{src-base}}/commandline-argfile-missing.args
5+
// compile-flags: --cfg cmdline_set @{{src-base}}/argfile/commandline-argfile-missing.args
66

77
#[cfg(not(cmdline_set))]
88
compile_error!("cmdline_set not set");
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: failed to load argument file: IO error: $DIR/commandline-argfile-missing.args: $FILE_MISSING (os error $ERR)
2+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Check to see if we can get parameters from an @argsfile file
2+
//
3+
// normalize-stderr-test: "os error \d+" -> "os error $$ERR"
4+
// normalize-stderr-test: "commandline-argfile-missing.args:[^(]*" -> "commandline-argfile-missing.args: $$FILE_MISSING "
5+
// compile-flags: --cfg cmdline_set @{{src-base}}/argfile/commandline-argfile-missing.args @{{src-base}}/argfile/commandline-argfile-badutf8.args @{{src-base}}/argfile/commandline-argfile-missing2.args
6+
7+
#[cfg(not(cmdline_set))]
8+
compile_error!("cmdline_set not set");
9+
10+
#[cfg(not(unbroken))]
11+
compile_error!("unbroken not set");
12+
13+
fn main() {
14+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
error: failed to load argument file: IO error: $DIR/commandline-argfile-missing.args: $FILE_MISSING (os error $ERR)
2+
3+
error: failed to load argument file: UTF-8 error in $DIR/commandline-argfile-badutf8.args
4+
5+
error: failed to load argument file: IO error: $DIR/commandline-argfile-missing2.args: No such file or directory (os error $ERR)
6+

tests/ui/commandline-argfile.rs renamed to tests/ui/argfile/commandline-argfile.rs

Lines changed: 1 addition & 1 deletion
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
//
33
// build-pass
4-
// compile-flags: --cfg cmdline_set @{{src-base}}/commandline-argfile.args
4+
// compile-flags: --cfg cmdline_set @{{src-base}}/argfile/commandline-argfile.args
55

66
#[cfg(not(cmdline_set))]
77
compile_error!("cmdline_set not set");

tests/ui/commandline-argfile-badutf8.stderr

Lines changed: 0 additions & 2 deletions
This file was deleted.

tests/ui/commandline-argfile-missing.stderr

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)