Repository: rust
Issue: Strictly proof not tainted by errors in `TypeckResults`
Labels: I-ICE,T-compiler,T-types

I get here from #125888.

For now there is a hack when calling `tcx.typeck()`:
https://github.com/rust-lang/rust/blob/76c73827dcd0b363e60b22c3cef64bde4171bf17/compiler/rustc_hir_typeck/src/writeback.rs#L125-L130

so that in returned `TypeckResults`, the `tainted_by_errors` may be `None` even though there is actually some errors. And this could cause many ICEs, because some functions trust that there is no type errors, and whenever it encounters an error, it won't throw an error report and just panic (#125888 is an example).

----
In this `tainted_by_errors` function, I try to change the condition to `if self.dcx().err_count_excluding_lint_errs() > 0`, then return the `ErrorGuaranteed`, and find that it could resolve 62 ICEs.

<details><summary><strong>Resolved ICE list</strong></summary>
<p>

```

    [crashes] tests/crashes/110630.rs
    [crashes] tests/crashes/111709.rs
    [crashes] tests/crashes/111709-2.rs
    [crashes] tests/crashes/112623.rs
    [crashes] tests/crashes/113379.rs
    [crashes] tests/crashes/114663.rs
    [crashes] tests/crashes/115808.rs
    [crashes] tests/crashes/117795.rs
    [crashes] tests/crashes/118545.rs
    [crashes] tests/crashes/119701.rs
    [crashes] tests/crashes/119729.rs
    [crashes] tests/crashes/119786.rs
    [crashes] tests/crashes/120793-2.rs
    [crashes] tests/crashes/120793.rs
    [crashes] tests/crashes/120792.rs
    [crashes] tests/crashes/120873.rs
    [crashes] tests/crashes/121097.rs
    [crashes] tests/crashes/121127.rs
    [crashes] tests/crashes/121063.rs
    [crashes] tests/crashes/121052.rs
    [crashes] tests/crashes/121411.rs
    [crashes] tests/crashes/121429.rs
    [crashes] tests/crashes/121613-2.rs
    [crashes] tests/crashes/121613.rs
    [crashes] tests/crashes/121623.rs
    [crashes] tests/crashes/121816.rs
    [crashes] tests/crashes/121957-1.rs
    [crashes] tests/crashes/121957-2.rs
    [crashes] tests/crashes/122044.rs
    [crashes] tests/crashes/122587-1.rs
    [crashes] tests/crashes/121858.rs
    [crashes] tests/crashes/122259.rs
    [crashes] tests/crashes/122630.rs
    [crashes] tests/crashes/122681.rs
    [crashes] tests/crashes/122904-2.rs
    [crashes] tests/crashes/122904.rs
    [crashes] tests/crashes/122909.rs
    [crashes] tests/crashes/123255.rs
    [crashes] tests/crashes/123276.rs
    [crashes] tests/crashes/123690.rs
    [crashes] tests/crashes/124004.rs
    [crashes] tests/crashes/124021.rs
    [crashes] tests/crashes/124083.rs
    [crashes] tests/crashes/124164.rs
    [crashes] tests/crashes/124262.rs
    [crashes] tests/crashes/124182.rs
    [crashes] tests/crashes/124340.rs
    [crashes] tests/crashes/124563.rs
    [crashes] tests/crashes/124583.rs
    [crashes] tests/crashes/124436.rs
    [crashes] tests/crashes/124894.rs
    [crashes] tests/crashes/125155.rs
    [crashes] tests/crashes/125185.rs
    [crashes] tests/crashes/125553.rs
    [crashes] tests/crashes/125758.rs
    [crashes] tests/crashes/125801.rs
    [crashes] tests/crashes/125768.rs
    [crashes] tests/crashes/125769.rs
    [crashes] tests/crashes/125874.rs
    [crashes] tests/crashes/125888.rs
    [crashes] tests/crashes/125914.rs
    [crashes] tests/crashes/125992.rs

```

</p>
</details>

https://github.com/rust-lang/rust/blob/f6b4b71ef10307201b52c17b0f9dcf9557cd90ba/compiler/rustc_infer/src/infer/mod.rs#L1152-L1172

---

But it will also break many ui tests, because as the hack said, it will affect diagnostic spans pretty negatively. So I think a proper way to resolve that is return a new defined enum rather than `Option`, such as

https://github.com/rust-lang/rust/blob/f6b4b71ef10307201b52c17b0f9dcf9557cd90ba/compiler/rustc_middle/src/ty/typeck_results.rs#L161-L163

```rust
enum TaintedByErrors {
    Normal(ErrorGuaranteed),
    Opaque(ErrorGuaranteed),
    StrictlyNoError,
}
```

So that we can choose to either early return or do more diagnostics.

@rustbot label +I-ICE +T-compiler