Skip to content

Commit 6136997

Browse files
committed
Make arg_expand_all not short-circuit on first error
1 parent 2dceda4 commit 6136997

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+266
-53
lines changed

compiler/rustc_driver_impl/src/args.rs

+17-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::fs;
44
use std::io;
55

66
use rustc_session::EarlyDiagCtxt;
7+
use rustc_span::ErrorGuaranteed;
78

89
/// Expands argfiles in command line arguments.
910
#[derive(Default)]
@@ -86,41 +87,47 @@ impl Expander {
8687
fn read_file(path: &str) -> Result<String, Error> {
8788
fs::read_to_string(path).map_err(|e| {
8889
if e.kind() == io::ErrorKind::InvalidData {
89-
Error::Utf8Error(Some(path.to_string()))
90+
Error::Utf8Error(path.to_string())
9091
} else {
9192
Error::IOError(path.to_string(), e)
9293
}
9394
})
9495
}
9596
}
9697

98+
/// Replaces any `@file` arguments with the contents of `file`, with each line of `file` as a
99+
/// separate argument.
100+
///
97101
/// **Note:** This function doesn't interpret argument 0 in any special way.
98102
/// If this function is intended to be used with command line arguments,
99103
/// `argv[0]` must be removed prior to calling it manually.
100-
pub fn arg_expand_all(early_dcx: &EarlyDiagCtxt, at_args: &[String]) -> Vec<String> {
104+
pub fn arg_expand_all(
105+
early_dcx: &EarlyDiagCtxt,
106+
at_args: &[String],
107+
) -> Result<Vec<String>, ErrorGuaranteed> {
101108
let mut expander = Expander::default();
109+
let mut result = Ok(());
102110
for arg in at_args {
103111
if let Err(err) = expander.arg(arg) {
104-
early_dcx.early_fatal(format!("Failed to load argument file: {err}"));
112+
result = Err(early_dcx.early_err(format!("failed to load argument file: {err}")));
105113
}
106114
}
107-
expander.finish()
115+
result.map(|()| expander.finish())
108116
}
109117

110118
#[derive(Debug)]
111-
pub enum Error {
112-
Utf8Error(Option<String>),
119+
enum Error {
120+
Utf8Error(String),
113121
IOError(String, io::Error),
114122
ShellParseError(String),
115123
}
116124

117125
impl fmt::Display for Error {
118126
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
119127
match self {
120-
Error::Utf8Error(None) => write!(fmt, "Utf8 error"),
121-
Error::Utf8Error(Some(path)) => write!(fmt, "Utf8 error in {path}"),
122-
Error::IOError(path, err) => write!(fmt, "IO Error: {path}: {err}"),
123-
Error::ShellParseError(path) => write!(fmt, "Invalid shell-style arguments in {path}"),
128+
Error::Utf8Error(path) => write!(fmt, "UTF-8 error in {path}"),
129+
Error::IOError(path, err) => write!(fmt, "IO error: {path}: {err}"),
130+
Error::ShellParseError(path) => write!(fmt, "invalid shell-style arguments in {path}"),
124131
}
125132
}
126133
}

compiler/rustc_driver_impl/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ fn run_compiler(
291291
// the compiler with @empty_file as argv[0] and no more arguments.
292292
let at_args = at_args.get(1..).unwrap_or_default();
293293

294-
let args = args::arg_expand_all(&default_early_dcx, at_args);
294+
let args = args::arg_expand_all(&default_early_dcx, at_args)?;
295295

296296
let Some(matches) = handle_options(&default_early_dcx, &args) else { return Ok(()) };
297297

src/librustdoc/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ fn main_args(
705705
// the compiler with @empty_file as argv[0] and no more arguments.
706706
let at_args = at_args.get(1..).unwrap_or_default();
707707

708-
let args = rustc_driver::args::arg_expand_all(early_dcx, at_args);
708+
let args = rustc_driver::args::arg_expand_all(early_dcx, at_args)?;
709709

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

src/tools/tidy/src/ui_tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const ENTRY_LIMIT: usize = 900;
1818
// FIXME: The following limits should be reduced eventually.
1919

2020
const ISSUES_ENTRY_LIMIT: usize = 1781;
21-
const ROOT_ENTRY_LIMIT: usize = 872;
21+
const ROOT_ENTRY_LIMIT: usize = 866;
2222

2323
const EXPECTED_TEST_FILE_EXTENSIONS: &[&str] = &[
2424
"rs", // test source files
@@ -33,8 +33,8 @@ const EXTENSION_EXCEPTION_PATHS: &[&str] = &[
3333
"tests/ui/asm/named-asm-labels.s", // loading an external asm file to test named labels lint
3434
"tests/ui/codegen/mismatched-data-layout.json", // testing mismatched data layout w/ custom targets
3535
"tests/ui/check-cfg/my-awesome-platform.json", // testing custom targets with cfgs
36-
"tests/ui/commandline-argfile-badutf8.args", // passing args via a file
37-
"tests/ui/commandline-argfile.args", // passing args via a file
36+
"tests/ui/argfile/commandline-argfile-badutf8.args", // passing args via a file
37+
"tests/ui/argfile/commandline-argfile.args", // passing args via a file
3838
"tests/ui/crate-loading/auxiliary/libfoo.rlib", // testing loading a manually created rlib
3939
"tests/ui/include-macros/data.bin", // testing including data with the include macros
4040
"tests/ui/include-macros/file.txt", // testing including data with the include macros
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Check to see if we can get parameters from an @argsfile file
2+
//
3+
// Path replacement in .stderr files (i.e. `$DIR`) doesn't handle mixed path
4+
// separators. This test uses backslash as the path separator for the command
5+
// line arguments and is only run on windows.
6+
//
7+
//@ only-windows
8+
//@ compile-flags: --cfg cmdline_set @{{src-base}}\argfile\commandline-argfile-badutf8.args
9+
10+
#[cfg(not(cmdline_set))]
11+
compile_error!("cmdline_set not set");
12+
13+
#[cfg(not(unbroken))]
14+
compile_error!("unbroken not set");
15+
16+
fn main() {
17+
}
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Check to see if we can get parameters from an @argsfile file
2+
//
3+
// Path replacement in .stderr files (i.e. `$DIR`) doesn't handle mixed path
4+
// separators. We have a duplicated version of this test that uses backslash as
5+
// the path separator for the command line arguments that is only run on
6+
// windows.
7+
//
8+
//@ ignore-windows
9+
//@ compile-flags: --cfg cmdline_set @{{src-base}}/argfile/commandline-argfile-badutf8.args
10+
11+
#[cfg(not(cmdline_set))]
12+
compile_error!("cmdline_set not set");
13+
14+
#[cfg(not(unbroken))]
15+
compile_error!("unbroken not set");
16+
17+
fn main() {
18+
}
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-windows.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
// Check to see if we can get parameters from an @argsfile file
22
//
3+
// Path replacement in .stderr files (i.e. `$DIR`) doesn't handle mixed path
4+
// separators. This test uses backslash as the path separator for the command
5+
// line arguments and is only run on windows.
6+
//
7+
//@ only-windows
38
//@ normalize-stderr-test: "os error \d+" -> "os error $$ERR"
49
//@ 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
10+
//@ compile-flags: --cfg cmdline_set @{{src-base}}\argfile\commandline-argfile-missing.args
611

712
#[cfg(not(cmdline_set))]
813
compile_error!("cmdline_set not set");
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Check to see if we can get parameters from an @argsfile file
2+
//
3+
// Path replacement in .stderr files (i.e. `$DIR`) doesn't handle mixed path
4+
// separators. We have a duplicated version of this test that uses backslash as
5+
// the path separator for the command line arguments that is only run on
6+
// windows.
7+
//
8+
//@ ignore-windows
9+
//@ normalize-stderr-test: "os error \d+" -> "os error $$ERR"
10+
//@ normalize-stderr-test: "commandline-argfile-missing.args:[^(]*" -> "commandline-argfile-missing.args: $$FILE_MISSING "
11+
//@ compile-flags: --cfg cmdline_set @{{src-base}}/argfile/commandline-argfile-missing.args
12+
13+
#[cfg(not(cmdline_set))]
14+
compile_error!("cmdline_set not set");
15+
16+
#[cfg(not(unbroken))]
17+
compile_error!("unbroken not set");
18+
19+
fn main() {
20+
}
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Check to see if we can get parameters from an @argsfile file
2+
//
3+
// Path replacement in .stderr files (i.e. `$DIR`) doesn't handle mixed path
4+
// separators. This test uses backslash as the path separator for the command
5+
// line arguments and is only run on windows.
6+
//
7+
//@ only-windows
8+
//@ normalize-stderr-test: "os error \d+" -> "os error $$ERR"
9+
//@ normalize-stderr-test: "commandline-argfile-missing.args:[^(]*" -> "commandline-argfile-missing.args: $$FILE_MISSING "
10+
//@ 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
11+
12+
#[cfg(not(cmdline_set))]
13+
compile_error!("cmdline_set not set");
14+
15+
#[cfg(not(unbroken))]
16+
compile_error!("unbroken not set");
17+
18+
fn main() {
19+
}
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Check to see if we can get parameters from an @argsfile file
2+
//
3+
// Path replacement in .stderr files (i.e. `$DIR`) doesn't handle mixed path
4+
// separators. We have a duplicated version of this test that uses backslash as
5+
// the path separator for the command line arguments that is only run on
6+
// windows.
7+
//
8+
//@ ignore-windows
9+
//@ normalize-stderr-test: "os error \d+" -> "os error $$ERR"
10+
//@ normalize-stderr-test: "commandline-argfile-missing.args:[^(]*" -> "commandline-argfile-missing.args: $$FILE_MISSING "
11+
//@ 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
12+
13+
#[cfg(not(cmdline_set))]
14+
compile_error!("cmdline_set not set");
15+
16+
#[cfg(not(unbroken))]
17+
compile_error!("unbroken not set");
18+
19+
fn main() {
20+
}
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

+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
//
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.rs

-12
This file was deleted.

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

-2
This file was deleted.

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

-2
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Check to see if we can get parameters from an @argsfile file
2+
//
3+
// Path replacement in .stderr files (i.e. `$DIR`) doesn't handle mixed path
4+
// separators. This test uses backslash as the path separator for the command
5+
// line arguments and is only run on windows.
6+
//
7+
//@ only-windows
8+
//@ compile-flags: --cfg cmdline_set @{{src-base}}\argfile\commandline-argfile-badutf8.args
9+
10+
#[cfg(not(cmdline_set))]
11+
compile_error!("cmdline_set not set");
12+
13+
#[cfg(not(unbroken))]
14+
compile_error!("unbroken not set");
15+
16+
fn main() {
17+
}
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Check to see if we can get parameters from an @argsfile file
2+
//
3+
// Path replacement in .stderr files (i.e. `$DIR`) doesn't handle mixed path
4+
// separators. We have a duplicated version of this test that uses backslash as
5+
// the path separator for the command line arguments that is only run on
6+
// windows.
7+
//
8+
//@ ignore-windows
9+
//@ compile-flags: --cfg cmdline_set @{{src-base}}/argfile/commandline-argfile-badutf8.args
10+
11+
#[cfg(not(cmdline_set))]
12+
compile_error!("cmdline_set not set");
13+
14+
#[cfg(not(unbroken))]
15+
compile_error!("unbroken not set");
16+
17+
fn main() {
18+
}
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-windows.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
// Check to see if we can get parameters from an @argsfile file
22
//
3+
// Path replacement in .stderr files (i.e. `$DIR`) doesn't handle mixed path
4+
// separators. This test uses backslash as the path separator for the command
5+
// line arguments and is only run on windows.
6+
//
7+
//@ only-windows
38
//@ normalize-stderr-test: "os error \d+" -> "os error $$ERR"
49
//@ 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
10+
//@ compile-flags: --cfg cmdline_set @{{src-base}}\argfile\commandline-argfile-missing.args
611

712
#[cfg(not(cmdline_set))]
813
compile_error!("cmdline_set not set");
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Check to see if we can get parameters from an @argsfile file
2+
//
3+
// Path replacement in .stderr files (i.e. `$DIR`) doesn't handle mixed path
4+
// separators. We have a duplicated version of this test that uses backslash as
5+
// the path separator for the command line arguments that is only run on
6+
// windows.
7+
//
8+
//@ ignore-windows
9+
//@ normalize-stderr-test: "os error \d+" -> "os error $$ERR"
10+
//@ normalize-stderr-test: "commandline-argfile-missing.args:[^(]*" -> "commandline-argfile-missing.args: $$FILE_MISSING "
11+
//@ compile-flags: --cfg cmdline_set @{{src-base}}/argfile/commandline-argfile-missing.args
12+
13+
#[cfg(not(cmdline_set))]
14+
compile_error!("cmdline_set not set");
15+
16+
#[cfg(not(unbroken))]
17+
compile_error!("unbroken not set");
18+
19+
fn main() {
20+
}
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Check to see if we can get parameters from an @argsfile file
2+
//
3+
// Path replacement in .stderr files (i.e. `$DIR`) doesn't handle mixed path
4+
// separators. This test uses backslash as the path separator for the command
5+
// line arguments and is only run on windows.
6+
//
7+
//@ only-windows
8+
//@ normalize-stderr-test: "os error \d+" -> "os error $$ERR"
9+
//@ normalize-stderr-test: "commandline-argfile-missing.args:[^(]*" -> "commandline-argfile-missing.args: $$FILE_MISSING "
10+
//@ 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
11+
12+
#[cfg(not(cmdline_set))]
13+
compile_error!("cmdline_set not set");
14+
15+
#[cfg(not(unbroken))]
16+
compile_error!("unbroken not set");
17+
18+
fn main() {
19+
}
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+

0 commit comments

Comments
 (0)