Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that compile-flags arguments are the last in UI tests #103298

Merged
merged 5 commits into from
Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/test/ui/compiletest-self-test/compile-flags-last.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Check that the arguments provided through `// compile-flags` are added last to the command line
// in UI tests. To ensure that we invoke rustc with a flag that expects an argument withut actually
// providing it. If the compile-flags are not last, the test will fail as rustc will interpret the
// next flag as the argument of this flag.
//
// compile-flags: --cap-lints
// error-pattern: Argument to option 'cap-lints' missing
2 changes: 2 additions & 0 deletions src/test/ui/compiletest-self-test/compile-flags-last.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
error: Argument to option 'cap-lints' missing

110 changes: 71 additions & 39 deletions src/tools/compiletest/src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,13 @@ enum WillExecute {
Disabled,
}

/// Should `--emit metadata` be used?
/// What value should be passed to `--emit`?
#[derive(Copy, Clone)]
enum EmitMetadata {
Yes,
No,
enum Emit {
None,
Metadata,
LlvmIr,
Asm,
}

impl<'test> TestCx<'test> {
Expand Down Expand Up @@ -424,7 +426,7 @@ impl<'test> TestCx<'test> {
}

let should_run = self.run_if_enabled();
let mut proc_res = self.compile_test(should_run, EmitMetadata::No);
let mut proc_res = self.compile_test(should_run, Emit::None);

if !proc_res.status.success() {
self.fatal_proc_rec("compilation failed!", &proc_res);
Expand Down Expand Up @@ -676,7 +678,7 @@ impl<'test> TestCx<'test> {

// compile test file (it should have 'compile-flags:-g' in the header)
let should_run = self.run_if_enabled();
let compile_result = self.compile_test(should_run, EmitMetadata::No);
let compile_result = self.compile_test(should_run, Emit::None);
if !compile_result.status.success() {
self.fatal_proc_rec("compilation failed!", &compile_result);
}
Expand Down Expand Up @@ -796,7 +798,7 @@ impl<'test> TestCx<'test> {

// compile test file (it should have 'compile-flags:-g' in the header)
let should_run = self.run_if_enabled();
let compiler_run_result = self.compile_test(should_run, EmitMetadata::No);
let compiler_run_result = self.compile_test(should_run, Emit::None);
if !compiler_run_result.status.success() {
self.fatal_proc_rec("compilation failed!", &compiler_run_result);
}
Expand Down Expand Up @@ -1028,7 +1030,7 @@ impl<'test> TestCx<'test> {
fn run_debuginfo_lldb_test_no_opt(&self) {
// compile test file (it should have 'compile-flags:-g' in the header)
let should_run = self.run_if_enabled();
let compile_result = self.compile_test(should_run, EmitMetadata::No);
let compile_result = self.compile_test(should_run, Emit::None);
if !compile_result.status.success() {
self.fatal_proc_rec("compilation failed!", &compile_result);
}
Expand Down Expand Up @@ -1453,21 +1455,21 @@ impl<'test> TestCx<'test> {
}
}

fn should_emit_metadata(&self, pm: Option<PassMode>) -> EmitMetadata {
fn should_emit_metadata(&self, pm: Option<PassMode>) -> Emit {
match (pm, self.props.fail_mode, self.config.mode) {
(Some(PassMode::Check), ..) | (_, Some(FailMode::Check), Ui) => EmitMetadata::Yes,
_ => EmitMetadata::No,
(Some(PassMode::Check), ..) | (_, Some(FailMode::Check), Ui) => Emit::Metadata,
_ => Emit::None,
}
}

fn compile_test(&self, will_execute: WillExecute, emit_metadata: EmitMetadata) -> ProcRes {
self.compile_test_general(will_execute, emit_metadata, self.props.local_pass_mode())
fn compile_test(&self, will_execute: WillExecute, emit: Emit) -> ProcRes {
self.compile_test_general(will_execute, emit, self.props.local_pass_mode())
}

fn compile_test_general(
&self,
will_execute: WillExecute,
emit_metadata: EmitMetadata,
emit: Emit,
local_pm: Option<PassMode>,
) -> ProcRes {
// Only use `make_exe_name` when the test ends up being executed.
Expand Down Expand Up @@ -1499,10 +1501,13 @@ impl<'test> TestCx<'test> {
_ => AllowUnused::No,
};

let mut rustc =
self.make_compile_args(&self.testpaths.file, output_file, emit_metadata, allow_unused);

rustc.arg("-L").arg(&self.aux_output_dir_name());
let rustc = self.make_compile_args(
&self.testpaths.file,
output_file,
emit,
allow_unused,
LinkToAux::Yes,
);

self.compose_and_run_compiler(rustc, None)
}
Expand Down Expand Up @@ -1729,8 +1734,13 @@ impl<'test> TestCx<'test> {
// Create the directory for the stdout/stderr files.
create_dir_all(aux_cx.output_base_dir()).unwrap();
let input_file = &aux_testpaths.file;
let mut aux_rustc =
aux_cx.make_compile_args(input_file, aux_output, EmitMetadata::No, AllowUnused::No);
let mut aux_rustc = aux_cx.make_compile_args(
input_file,
aux_output,
Emit::None,
AllowUnused::No,
LinkToAux::No,
);

for key in &aux_props.unset_rustc_env {
aux_rustc.env_remove(key);
Expand Down Expand Up @@ -1867,8 +1877,9 @@ impl<'test> TestCx<'test> {
&self,
input_file: &Path,
output_file: TargetLocation,
emit_metadata: EmitMetadata,
emit: Emit,
allow_unused: AllowUnused,
link_to_aux: LinkToAux,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for my sanity: this new argument is necessary because there's no Command available until after this function returns it. We might want to refactor at some point to allow passing custom flags without needing all of them to be passed into this function.

) -> Command {
let is_aux = input_file.components().map(|c| c.as_os_str()).any(|c| c == "auxiliary");
let is_rustdoc = self.is_rustdoc() && !is_aux;
Expand Down Expand Up @@ -1983,8 +1994,18 @@ impl<'test> TestCx<'test> {
}
}

if let (false, EmitMetadata::Yes) = (is_rustdoc, emit_metadata) {
rustc.args(&["--emit", "metadata"]);
match emit {
Emit::None => {}
Emit::Metadata if is_rustdoc => {}
Emit::Metadata => {
rustc.args(&["--emit", "metadata"]);
}
Emit::LlvmIr => {
rustc.args(&["--emit", "llvm-ir"]);
}
Emit::Asm => {
rustc.args(&["--emit", "asm"]);
}
}

if !is_rustdoc {
Expand Down Expand Up @@ -2056,6 +2077,10 @@ impl<'test> TestCx<'test> {
rustc.arg("-Ctarget-feature=-crt-static");
}

if let LinkToAux::Yes = link_to_aux {
rustc.arg("-L").arg(self.aux_output_dir_name());
}

rustc.args(&self.props.compile_flags);

rustc
Expand Down Expand Up @@ -2247,13 +2272,15 @@ impl<'test> TestCx<'test> {
// codegen tests (using FileCheck)

fn compile_test_and_save_ir(&self) -> ProcRes {
let aux_dir = self.aux_output_dir_name();

let output_file = TargetLocation::ThisDirectory(self.output_base_dir());
let input_file = &self.testpaths.file;
let mut rustc =
self.make_compile_args(input_file, output_file, EmitMetadata::No, AllowUnused::No);
rustc.arg("-L").arg(aux_dir).arg("--emit=llvm-ir");
let rustc = self.make_compile_args(
input_file,
output_file,
Emit::LlvmIr,
AllowUnused::No,
LinkToAux::Yes,
);

self.compose_and_run_compiler(rustc, None)
}
Expand All @@ -2265,14 +2292,11 @@ impl<'test> TestCx<'test> {

let output_file = TargetLocation::ThisFile(output_path.clone());
let input_file = &self.testpaths.file;
let mut rustc =
self.make_compile_args(input_file, output_file, EmitMetadata::No, AllowUnused::No);

rustc.arg("-L").arg(self.aux_output_dir_name());

let mut emit = Emit::None;
match self.props.assembly_output.as_ref().map(AsRef::as_ref) {
Some("emit-asm") => {
rustc.arg("--emit=asm");
emit = Emit::Asm;
}

Some("ptx-linker") => {
Expand All @@ -2283,6 +2307,9 @@ impl<'test> TestCx<'test> {
None => self.fatal("missing 'assembly-output' header"),
}

let rustc =
self.make_compile_args(input_file, output_file, emit, AllowUnused::No, LinkToAux::Yes);

(self.compose_and_run_compiler(rustc, None), output_path)
}

Expand Down Expand Up @@ -2407,10 +2434,10 @@ impl<'test> TestCx<'test> {
let mut rustc = new_rustdoc.make_compile_args(
&new_rustdoc.testpaths.file,
output_file,
EmitMetadata::No,
Emit::None,
AllowUnused::Yes,
LinkToAux::Yes,
);
rustc.arg("-L").arg(&new_rustdoc.aux_output_dir_name());
new_rustdoc.build_all_auxiliary(&mut rustc);

let proc_res = new_rustdoc.document(&compare_dir);
Expand Down Expand Up @@ -2683,7 +2710,7 @@ impl<'test> TestCx<'test> {
fn run_codegen_units_test(&self) {
assert!(self.revision.is_none(), "revisions not relevant here");

let proc_res = self.compile_test(WillExecute::No, EmitMetadata::No);
let proc_res = self.compile_test(WillExecute::No, Emit::None);

if !proc_res.status.success() {
self.fatal_proc_rec("compilation failed!", &proc_res);
Expand Down Expand Up @@ -3196,7 +3223,7 @@ impl<'test> TestCx<'test> {
if let Some(FailMode::Build) = self.props.fail_mode {
// Make sure a build-fail test cannot fail due to failing analysis (e.g. typeck).
let pm = Some(PassMode::Check);
let proc_res = self.compile_test_general(WillExecute::No, EmitMetadata::Yes, pm);
let proc_res = self.compile_test_general(WillExecute::No, Emit::Metadata, pm);
self.check_if_test_should_compile(&proc_res, pm);
}

Expand Down Expand Up @@ -3354,13 +3381,13 @@ impl<'test> TestCx<'test> {
if self.props.run_rustfix && self.config.compare_mode.is_none() {
// And finally, compile the fixed code and make sure it both
// succeeds and has no diagnostics.
let mut rustc = self.make_compile_args(
let rustc = self.make_compile_args(
&self.testpaths.file.with_extension(UI_FIXED),
TargetLocation::ThisFile(self.make_exe_name()),
emit_metadata,
AllowUnused::No,
LinkToAux::Yes,
);
rustc.arg("-L").arg(&self.aux_output_dir_name());
let res = self.compose_and_run_compiler(rustc, None);
if !res.status.success() {
self.fatal_proc_rec("failed to compile fixed code", &res);
Expand Down Expand Up @@ -3948,3 +3975,8 @@ enum AllowUnused {
Yes,
No,
}

enum LinkToAux {
Yes,
No,
}