| 
 | 1 | +// This test checks if internal compilation error (ICE) log files work as expected.  | 
 | 2 | +// - Get the number of lines from the log files without any configuration options,  | 
 | 3 | +// then check that the line count doesn't change if the backtrace gets configured to be short  | 
 | 4 | +// or full.  | 
 | 5 | +// - Check that disabling ICE logging results in zero files created.  | 
 | 6 | +// - Check that the ICE files contain some of the expected strings.  | 
 | 7 | +// See https://github.com/rust-lang/rust/pull/108714  | 
 | 8 | + | 
 | 9 | +use run_make_support::{cwd, has_extension, has_prefix, rfs, rustc, shallow_find_files};  | 
 | 10 | + | 
 | 11 | +fn main() {  | 
 | 12 | +    rustc().input("lib.rs").arg("-Ztreat-err-as-bug=1").run_fail();  | 
 | 13 | +    let default = get_text_from_ice(".").lines().count();  | 
 | 14 | +    clear_ice_files();  | 
 | 15 | + | 
 | 16 | +    rustc().env("RUSTC_ICE", cwd()).input("lib.rs").arg("-Ztreat-err-as-bug=1").run_fail();  | 
 | 17 | +    let ice_text = get_text_from_ice(cwd());  | 
 | 18 | +    let default_set = ice_text.lines().count();  | 
 | 19 | +    let content = ice_text;  | 
 | 20 | +    let ice_files = shallow_find_files(cwd(), |path| {  | 
 | 21 | +        has_prefix(path, "rustc-ice") && has_extension(path, "txt")  | 
 | 22 | +    });  | 
 | 23 | +    assert_eq!(ice_files.len(), 1); // There should only be 1 ICE file.  | 
 | 24 | +    let ice_file_name =  | 
 | 25 | +        ice_files.first().and_then(|f| f.file_name()).and_then(|n| n.to_str()).unwrap();  | 
 | 26 | +    // Ensure that the ICE dump path doesn't contain `:`, because they cause problems on Windows.  | 
 | 27 | +    assert!(!ice_file_name.contains(":"), "{ice_file_name}");  | 
 | 28 | + | 
 | 29 | +    clear_ice_files();  | 
 | 30 | +    rustc()  | 
 | 31 | +        .env("RUSTC_ICE", cwd())  | 
 | 32 | +        .input("lib.rs")  | 
 | 33 | +        .env("RUST_BACKTRACE", "short")  | 
 | 34 | +        .arg("-Ztreat-err-as-bug=1")  | 
 | 35 | +        .run_fail();  | 
 | 36 | +    let short = get_text_from_ice(cwd()).lines().count();  | 
 | 37 | +    clear_ice_files();  | 
 | 38 | +    rustc()  | 
 | 39 | +        .env("RUSTC_ICE", cwd())  | 
 | 40 | +        .input("lib.rs")  | 
 | 41 | +        .env("RUST_BACKTRACE", "full")  | 
 | 42 | +        .arg("-Ztreat-err-as-bug=1")  | 
 | 43 | +        .run_fail();  | 
 | 44 | +    let full = get_text_from_ice(cwd()).lines().count();  | 
 | 45 | +    clear_ice_files();  | 
 | 46 | + | 
 | 47 | +    // The ICE dump is explicitly disabled. Therefore, this should produce no files.  | 
 | 48 | +    rustc().env("RUSTC_ICE", "0").input("lib.rs").arg("-Ztreat-err-as-bug=1").run_fail();  | 
 | 49 | +    let ice_files = shallow_find_files(cwd(), |path| {  | 
 | 50 | +        has_prefix(path, "rustc-ice") && has_extension(path, "txt")  | 
 | 51 | +    });  | 
 | 52 | +    assert!(ice_files.is_empty()); // There should be 0 ICE files.  | 
 | 53 | + | 
 | 54 | +    // The line count should not change.  | 
 | 55 | +    assert_eq!(short, default_set);  | 
 | 56 | +    assert_eq!(short, default);  | 
 | 57 | +    assert_eq!(full, default_set);  | 
 | 58 | +    assert!(default > 0);  | 
 | 59 | +    // Some of the expected strings in an ICE file should appear.  | 
 | 60 | +    assert!(content.contains("thread 'rustc' panicked at"));  | 
 | 61 | +    assert!(content.contains("stack backtrace:"));  | 
 | 62 | +}  | 
 | 63 | + | 
 | 64 | +fn clear_ice_files() {  | 
 | 65 | +    let ice_files = shallow_find_files(cwd(), |path| {  | 
 | 66 | +        has_prefix(path, "rustc-ice") && has_extension(path, "txt")  | 
 | 67 | +    });  | 
 | 68 | +    for file in ice_files {  | 
 | 69 | +        rfs::remove_file(file);  | 
 | 70 | +    }  | 
 | 71 | +}  | 
 | 72 | + | 
 | 73 | +#[track_caller]  | 
 | 74 | +fn get_text_from_ice(dir: impl AsRef<std::path::Path>) -> String {  | 
 | 75 | +    let ice_files =  | 
 | 76 | +        shallow_find_files(dir, |path| has_prefix(path, "rustc-ice") && has_extension(path, "txt"));  | 
 | 77 | +    assert_eq!(ice_files.len(), 1); // There should only be 1 ICE file.  | 
 | 78 | +    let ice_file = ice_files.get(0).unwrap();  | 
 | 79 | +    let output = rfs::read_to_string(ice_file);  | 
 | 80 | +    output  | 
 | 81 | +}  | 
0 commit comments