| 
 | 1 | +//! If `-o -` or `--emit KIND=-` is provided, output should be written to stdout  | 
 | 2 | +//! instead. Binary output (`obj`, `llvm-bc`, `link` and `metadata`)  | 
 | 3 | +//! being written this way will result in an error if stdout is a tty.  | 
 | 4 | +//! Multiple output types going to stdout will trigger an error too,  | 
 | 5 | +//! as they would all be mixed together.  | 
 | 6 | +//!  | 
 | 7 | +//! See <https://github.com/rust-lang/rust/pull/111626>.  | 
 | 8 | +
  | 
 | 9 | +use std::fs::File;  | 
 | 10 | + | 
 | 11 | +use run_make_support::{diff, run_in_tmpdir, rustc};  | 
 | 12 | + | 
 | 13 | +// Test emitting text outputs to stdout works correctly  | 
 | 14 | +fn run_diff(name: &str, file_args: &[&str]) {  | 
 | 15 | +    rustc().emit(format!("{name}={name}")).input("test.rs").args(file_args).run();  | 
 | 16 | +    let out = rustc().emit(format!("{name}=-")).input("test.rs").run().stdout_utf8();  | 
 | 17 | +    diff().expected_file(name).actual_text("stdout", &out).run();  | 
 | 18 | +}  | 
 | 19 | + | 
 | 20 | +// Test that emitting binary formats to a terminal gives the correct error  | 
 | 21 | +fn run_terminal_err_diff(name: &str) {  | 
 | 22 | +    #[cfg(not(windows))]  | 
 | 23 | +    let terminal = File::create("/dev/ptmx").unwrap();  | 
 | 24 | +    // FIXME: If this test fails and the compiler does print to the console,  | 
 | 25 | +    // then this will produce a lot of output.  | 
 | 26 | +    // We should spawn a new console instead to print stdout.  | 
 | 27 | +    #[cfg(windows)]  | 
 | 28 | +    let terminal = File::options().read(true).write(true).open(r"\\.\CONOUT$").unwrap();  | 
 | 29 | + | 
 | 30 | +    let err = File::create(name).unwrap();  | 
 | 31 | +    rustc().emit(format!("{name}=-")).input("test.rs").stdout(terminal).stderr(err).run_fail();  | 
 | 32 | +    diff().expected_file(format!("emit-{name}.stderr")).actual_file(name).run();  | 
 | 33 | +}  | 
 | 34 | + | 
 | 35 | +fn main() {  | 
 | 36 | +    run_in_tmpdir(|| {  | 
 | 37 | +        run_diff("asm", &[]);  | 
 | 38 | +        run_diff("llvm-ir", &[]);  | 
 | 39 | +        run_diff("dep-info", &["-Zdep-info-omit-d-target=yes"]);  | 
 | 40 | +        run_diff("mir", &[]);  | 
 | 41 | + | 
 | 42 | +        run_terminal_err_diff("llvm-bc");  | 
 | 43 | +        run_terminal_err_diff("obj");  | 
 | 44 | +        run_terminal_err_diff("metadata");  | 
 | 45 | +        run_terminal_err_diff("link");  | 
 | 46 | + | 
 | 47 | +        // Test error for emitting multiple types to stdout  | 
 | 48 | +        rustc()  | 
 | 49 | +            .input("test.rs")  | 
 | 50 | +            .emit("asm=-")  | 
 | 51 | +            .emit("llvm-ir=-")  | 
 | 52 | +            .emit("dep-info=-")  | 
 | 53 | +            .emit("mir=-")  | 
 | 54 | +            .stderr(File::create("multiple-types").unwrap())  | 
 | 55 | +            .run_fail();  | 
 | 56 | +        diff().expected_file("emit-multiple-types.stderr").actual_file("multiple-types").run();  | 
 | 57 | + | 
 | 58 | +        // Same as above, but using `-o`  | 
 | 59 | +        rustc()  | 
 | 60 | +            .input("test.rs")  | 
 | 61 | +            .output("-")  | 
 | 62 | +            .emit("asm,llvm-ir,dep-info,mir")  | 
 | 63 | +            .stderr(File::create("multiple-types-option-o").unwrap())  | 
 | 64 | +            .run_fail();  | 
 | 65 | +        diff()  | 
 | 66 | +            .expected_file("emit-multiple-types.stderr")  | 
 | 67 | +            .actual_file("multiple-types-option-o")  | 
 | 68 | +            .run();  | 
 | 69 | + | 
 | 70 | +        // Test that `-o -` redirected to a file works correctly (#26719)  | 
 | 71 | +        rustc().input("test.rs").output("-").stdout(File::create("out-stdout").unwrap()).run();  | 
 | 72 | +    });  | 
 | 73 | +}  | 
0 commit comments