rust/compiler/rustc_interface/src
Matthias Krüger 2f1ac412ec
Rollup merge of #120828 - nnethercote:fix-stash-steal, r=oli-obk
Fix `ErrorGuaranteed` unsoundness with stash/steal.

When you stash an error, the error count is incremented. You can then use the non-zero error count to get an `ErrorGuaranteed`. You can then steal the error, which decrements the error count. You can then cancel the error.

Example code:
```
fn unsound(dcx: &DiagCtxt) -> ErrorGuaranteed {
    let sp = rustc_span::DUMMY_SP;
    let k = rustc_errors::StashKey::Cycle;
    dcx.struct_err("bogus").stash(sp, k);           // increment error count on stash
    let guar = dcx.has_errors().unwrap();           // ErrorGuaranteed from error count > 0
    let err = dcx.steal_diagnostic(sp, k).unwrap(); // decrement error count on steal
    err.cancel();                                   // cancel error
    guar                                            // ErrorGuaranteed with no error emitted!
}
```

This commit fixes the problem in the simplest way: by not counting stashed errors in `DiagCtxt::{err_count,has_errors}`.

However, just doing this without any other changes leads to over 40 ui test failures. Mostly because of uninteresting extra errors (many saying "type annotations needed" when type inference fails), and in a few cases, due to delayed bugs causing ICEs when no normal errors are printed.

To fix these, this commit adds `DiagCtxt::stashed_err_count`, and uses it in three places alongside `DiagCtxt::{has_errors,err_count}`. It's dodgy to rely on it, because unlike `DiagCtxt::err_count` it can go up and down. But it's needed to preserve existing behaviour, and at least the three places that need it are now obvious.

r? oli-obk
2024-02-09 14:41:52 +01:00
..
callbacks.rs Rename TRACK_DIAGNOSTICS as TRACK_DIAGNOSTIC. 2024-01-11 07:55:03 +11:00
errors.rs Write to stdout if - is given as output file 2023-06-06 17:53:29 -04:00
interface.rs SilentEmitter::fatal_note doesn't need to be optional. 2024-02-03 09:02:50 +11:00
lib.rs Invert diagnostic lints. 2024-02-06 13:12:33 +11:00
passes.rs Fix ErrorGuaranteed unsoundness with stash/steal. 2024-02-09 13:50:03 +11:00
proc_macro_decls.rs Move expansion of query macros in rustc_middle to rustc_middle::query 2023-05-15 08:49:13 +02:00
queries.rs Rename {create,emit}_warning as {create,emit}_warn. 2024-01-10 07:33:06 +11:00
tests.rs Rollup merge of #119162 - heiher:direct-access-external-data, r=petrochenkov 2024-02-07 18:24:41 +01:00
util.rs Rollup merge of #119815 - nagisa:nagisa/polishes-libloading-use-somewhat, r=bjorn3 2024-01-19 19:27:00 +01:00