Skip to content

Commit 81ff7e7

Browse files
committed
Auto merge of rust-lang#103298 - ferrocene:pa-compile-flags-last, r=jyn514
Ensure that compile-flags arguments are the last in UI tests Before this PR, compiletest would add `-L path/to/aux` at the end of the rustc flags, even after the custom ones set with the compile-flags header comment. This made it impossible to check how rustc would behave when a flag requiring an argument was passed without the argument, because the argument would become `-L`. This PR fixes that by adding the `-L path/to/aux` before the arguments defined in compile-flags, at least for UI tests. Other test suites might either be fixed as well by this change, or still present the old behavior (`-L` is now always passed before, but other tests suites might add additional flags after the custom ones).
2 parents 0950848 + 4c55b29 commit 81ff7e7

File tree

5 files changed

+80
-39
lines changed

5 files changed

+80
-39
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Check that the arguments provided through `// compile-flags` are added last to the command line
2+
// in UI tests. To ensure that we invoke rustc with a flag that expects an argument withut actually
3+
// providing it. If the compile-flags are not last, the test will fail as rustc will interpret the
4+
// next flag as the argument of this flag.
5+
//
6+
// compile-flags: --cap-lints
7+
// error-pattern: Argument to option 'cap-lints' missing
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: Argument to option 'cap-lints' missing
2+

src/tools/compiletest/src/runtest.rs

+71-39
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,13 @@ enum WillExecute {
208208
Disabled,
209209
}
210210

211-
/// Should `--emit metadata` be used?
211+
/// What value should be passed to `--emit`?
212212
#[derive(Copy, Clone)]
213-
enum EmitMetadata {
214-
Yes,
215-
No,
213+
enum Emit {
214+
None,
215+
Metadata,
216+
LlvmIr,
217+
Asm,
216218
}
217219

218220
impl<'test> TestCx<'test> {
@@ -412,7 +414,7 @@ impl<'test> TestCx<'test> {
412414
}
413415

414416
let should_run = self.run_if_enabled();
415-
let mut proc_res = self.compile_test(should_run, EmitMetadata::No);
417+
let mut proc_res = self.compile_test(should_run, Emit::None);
416418

417419
if !proc_res.status.success() {
418420
self.fatal_proc_rec("compilation failed!", &proc_res);
@@ -658,7 +660,7 @@ impl<'test> TestCx<'test> {
658660

659661
// compile test file (it should have 'compile-flags:-g' in the header)
660662
let should_run = self.run_if_enabled();
661-
let compile_result = self.compile_test(should_run, EmitMetadata::No);
663+
let compile_result = self.compile_test(should_run, Emit::None);
662664
if !compile_result.status.success() {
663665
self.fatal_proc_rec("compilation failed!", &compile_result);
664666
}
@@ -778,7 +780,7 @@ impl<'test> TestCx<'test> {
778780

779781
// compile test file (it should have 'compile-flags:-g' in the header)
780782
let should_run = self.run_if_enabled();
781-
let compiler_run_result = self.compile_test(should_run, EmitMetadata::No);
783+
let compiler_run_result = self.compile_test(should_run, Emit::None);
782784
if !compiler_run_result.status.success() {
783785
self.fatal_proc_rec("compilation failed!", &compiler_run_result);
784786
}
@@ -1010,7 +1012,7 @@ impl<'test> TestCx<'test> {
10101012
fn run_debuginfo_lldb_test_no_opt(&self) {
10111013
// compile test file (it should have 'compile-flags:-g' in the header)
10121014
let should_run = self.run_if_enabled();
1013-
let compile_result = self.compile_test(should_run, EmitMetadata::No);
1015+
let compile_result = self.compile_test(should_run, Emit::None);
10141016
if !compile_result.status.success() {
10151017
self.fatal_proc_rec("compilation failed!", &compile_result);
10161018
}
@@ -1426,21 +1428,21 @@ impl<'test> TestCx<'test> {
14261428
}
14271429
}
14281430

1429-
fn should_emit_metadata(&self, pm: Option<PassMode>) -> EmitMetadata {
1431+
fn should_emit_metadata(&self, pm: Option<PassMode>) -> Emit {
14301432
match (pm, self.props.fail_mode, self.config.mode) {
1431-
(Some(PassMode::Check), ..) | (_, Some(FailMode::Check), Ui) => EmitMetadata::Yes,
1432-
_ => EmitMetadata::No,
1433+
(Some(PassMode::Check), ..) | (_, Some(FailMode::Check), Ui) => Emit::Metadata,
1434+
_ => Emit::None,
14331435
}
14341436
}
14351437

1436-
fn compile_test(&self, will_execute: WillExecute, emit_metadata: EmitMetadata) -> ProcRes {
1437-
self.compile_test_general(will_execute, emit_metadata, self.props.local_pass_mode())
1438+
fn compile_test(&self, will_execute: WillExecute, emit: Emit) -> ProcRes {
1439+
self.compile_test_general(will_execute, emit, self.props.local_pass_mode())
14381440
}
14391441

14401442
fn compile_test_general(
14411443
&self,
14421444
will_execute: WillExecute,
1443-
emit_metadata: EmitMetadata,
1445+
emit: Emit,
14441446
local_pm: Option<PassMode>,
14451447
) -> ProcRes {
14461448
// Only use `make_exe_name` when the test ends up being executed.
@@ -1472,10 +1474,13 @@ impl<'test> TestCx<'test> {
14721474
_ => AllowUnused::No,
14731475
};
14741476

1475-
let mut rustc =
1476-
self.make_compile_args(&self.testpaths.file, output_file, emit_metadata, allow_unused);
1477-
1478-
rustc.arg("-L").arg(&self.aux_output_dir_name());
1477+
let rustc = self.make_compile_args(
1478+
&self.testpaths.file,
1479+
output_file,
1480+
emit,
1481+
allow_unused,
1482+
LinkToAux::Yes,
1483+
);
14791484

14801485
self.compose_and_run_compiler(rustc, None)
14811486
}
@@ -1702,8 +1707,13 @@ impl<'test> TestCx<'test> {
17021707
// Create the directory for the stdout/stderr files.
17031708
create_dir_all(aux_cx.output_base_dir()).unwrap();
17041709
let input_file = &aux_testpaths.file;
1705-
let mut aux_rustc =
1706-
aux_cx.make_compile_args(input_file, aux_output, EmitMetadata::No, AllowUnused::No);
1710+
let mut aux_rustc = aux_cx.make_compile_args(
1711+
input_file,
1712+
aux_output,
1713+
Emit::None,
1714+
AllowUnused::No,
1715+
LinkToAux::No,
1716+
);
17071717

17081718
for key in &aux_props.unset_rustc_env {
17091719
aux_rustc.env_remove(key);
@@ -1831,8 +1841,9 @@ impl<'test> TestCx<'test> {
18311841
&self,
18321842
input_file: &Path,
18331843
output_file: TargetLocation,
1834-
emit_metadata: EmitMetadata,
1844+
emit: Emit,
18351845
allow_unused: AllowUnused,
1846+
link_to_aux: LinkToAux,
18361847
) -> Command {
18371848
let is_aux = input_file.components().map(|c| c.as_os_str()).any(|c| c == "auxiliary");
18381849
let is_rustdoc = self.is_rustdoc() && !is_aux;
@@ -1947,8 +1958,18 @@ impl<'test> TestCx<'test> {
19471958
}
19481959
}
19491960

1950-
if let (false, EmitMetadata::Yes) = (is_rustdoc, emit_metadata) {
1951-
rustc.args(&["--emit", "metadata"]);
1961+
match emit {
1962+
Emit::None => {}
1963+
Emit::Metadata if is_rustdoc => {}
1964+
Emit::Metadata => {
1965+
rustc.args(&["--emit", "metadata"]);
1966+
}
1967+
Emit::LlvmIr => {
1968+
rustc.args(&["--emit", "llvm-ir"]);
1969+
}
1970+
Emit::Asm => {
1971+
rustc.args(&["--emit", "asm"]);
1972+
}
19521973
}
19531974

19541975
if !is_rustdoc {
@@ -2014,6 +2035,10 @@ impl<'test> TestCx<'test> {
20142035
rustc.arg("-Ctarget-feature=-crt-static");
20152036
}
20162037

2038+
if let LinkToAux::Yes = link_to_aux {
2039+
rustc.arg("-L").arg(self.aux_output_dir_name());
2040+
}
2041+
20172042
rustc.args(&self.props.compile_flags);
20182043

20192044
rustc
@@ -2205,13 +2230,15 @@ impl<'test> TestCx<'test> {
22052230
// codegen tests (using FileCheck)
22062231

22072232
fn compile_test_and_save_ir(&self) -> ProcRes {
2208-
let aux_dir = self.aux_output_dir_name();
2209-
22102233
let output_file = TargetLocation::ThisDirectory(self.output_base_dir());
22112234
let input_file = &self.testpaths.file;
2212-
let mut rustc =
2213-
self.make_compile_args(input_file, output_file, EmitMetadata::No, AllowUnused::No);
2214-
rustc.arg("-L").arg(aux_dir).arg("--emit=llvm-ir");
2235+
let rustc = self.make_compile_args(
2236+
input_file,
2237+
output_file,
2238+
Emit::LlvmIr,
2239+
AllowUnused::No,
2240+
LinkToAux::Yes,
2241+
);
22152242

22162243
self.compose_and_run_compiler(rustc, None)
22172244
}
@@ -2223,14 +2250,11 @@ impl<'test> TestCx<'test> {
22232250

22242251
let output_file = TargetLocation::ThisFile(output_path.clone());
22252252
let input_file = &self.testpaths.file;
2226-
let mut rustc =
2227-
self.make_compile_args(input_file, output_file, EmitMetadata::No, AllowUnused::No);
2228-
2229-
rustc.arg("-L").arg(self.aux_output_dir_name());
22302253

2254+
let mut emit = Emit::None;
22312255
match self.props.assembly_output.as_ref().map(AsRef::as_ref) {
22322256
Some("emit-asm") => {
2233-
rustc.arg("--emit=asm");
2257+
emit = Emit::Asm;
22342258
}
22352259

22362260
Some("ptx-linker") => {
@@ -2241,6 +2265,9 @@ impl<'test> TestCx<'test> {
22412265
None => self.fatal("missing 'assembly-output' header"),
22422266
}
22432267

2268+
let rustc =
2269+
self.make_compile_args(input_file, output_file, emit, AllowUnused::No, LinkToAux::Yes);
2270+
22442271
(self.compose_and_run_compiler(rustc, None), output_path)
22452272
}
22462273

@@ -2365,10 +2392,10 @@ impl<'test> TestCx<'test> {
23652392
let mut rustc = new_rustdoc.make_compile_args(
23662393
&new_rustdoc.testpaths.file,
23672394
output_file,
2368-
EmitMetadata::No,
2395+
Emit::None,
23692396
AllowUnused::Yes,
2397+
LinkToAux::Yes,
23702398
);
2371-
rustc.arg("-L").arg(&new_rustdoc.aux_output_dir_name());
23722399
new_rustdoc.build_all_auxiliary(&mut rustc);
23732400

23742401
let proc_res = new_rustdoc.document(&compare_dir);
@@ -2641,7 +2668,7 @@ impl<'test> TestCx<'test> {
26412668
fn run_codegen_units_test(&self) {
26422669
assert!(self.revision.is_none(), "revisions not relevant here");
26432670

2644-
let proc_res = self.compile_test(WillExecute::No, EmitMetadata::No);
2671+
let proc_res = self.compile_test(WillExecute::No, Emit::None);
26452672

26462673
if !proc_res.status.success() {
26472674
self.fatal_proc_rec("compilation failed!", &proc_res);
@@ -3154,7 +3181,7 @@ impl<'test> TestCx<'test> {
31543181
if let Some(FailMode::Build) = self.props.fail_mode {
31553182
// Make sure a build-fail test cannot fail due to failing analysis (e.g. typeck).
31563183
let pm = Some(PassMode::Check);
3157-
let proc_res = self.compile_test_general(WillExecute::No, EmitMetadata::Yes, pm);
3184+
let proc_res = self.compile_test_general(WillExecute::No, Emit::Metadata, pm);
31583185
self.check_if_test_should_compile(&proc_res, pm);
31593186
}
31603187

@@ -3312,13 +3339,13 @@ impl<'test> TestCx<'test> {
33123339
if self.props.run_rustfix && self.config.compare_mode.is_none() {
33133340
// And finally, compile the fixed code and make sure it both
33143341
// succeeds and has no diagnostics.
3315-
let mut rustc = self.make_compile_args(
3342+
let rustc = self.make_compile_args(
33163343
&self.testpaths.file.with_extension(UI_FIXED),
33173344
TargetLocation::ThisFile(self.make_exe_name()),
33183345
emit_metadata,
33193346
AllowUnused::No,
3347+
LinkToAux::Yes,
33203348
);
3321-
rustc.arg("-L").arg(&self.aux_output_dir_name());
33223349
let res = self.compose_and_run_compiler(rustc, None);
33233350
if !res.status.success() {
33243351
self.fatal_proc_rec("failed to compile fixed code", &res);
@@ -3852,3 +3879,8 @@ enum AllowUnused {
38523879
Yes,
38533880
No,
38543881
}
3882+
3883+
enum LinkToAux {
3884+
Yes,
3885+
No,
3886+
}

0 commit comments

Comments
 (0)