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

Error tainting in parallel compiler #126485

Closed
olafes opened this issue Jun 14, 2024 · 2 comments · Fixed by #126996
Closed

Error tainting in parallel compiler #126485

olafes opened this issue Jun 14, 2024 · 2 comments · Fixed by #126996
Labels
A-diagnostics Area: Messages for errors, warnings, and lints I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-compiler-parallel Working group: Parallelizing the compiler

Comments

@olafes
Copy link
Contributor

olafes commented Jun 14, 2024

Emitting diagnostics in one thread taints every infcx out there, but other threads don't expect their infcx to become tainted if they themselves aren't emitting anything. This leads to skipped diagnostics, early bailing out of queries that could've run to completion or painful to debug ICEs. For example, take #120601: snippet below run with -Zthreads=3 --edition=2021 works, but crashes if you uncomment the last function.

struct Tuple(i32);

async fn tuple() -> Tuple {
    Tuple(1i32)
}

async fn xyz() {
    match tuple() {
        Tuple(_) => {},
    }
}

/* uncomment this function to observe crashes
async fn fail() {
    Fail(())
}
*/

In parallel compiler, code that does different things depending on the result of InferCtxt::tainted_by_errors() can introduce spurious failures or even replace previously resolved Ty's with Ty::new_error():

let remapped_opaque_tys = regioncx.infer_opaque_types(infcx, opaque_type_values);

in regioncx.infer_opaque_types:
let ty =
infcx.infer_opaque_definition_from_instantiation(opaque_type_key, concrete_type);

fn infer_opaque_definition_from_instantiation(
&self,
opaque_type_key: OpaqueTypeKey<'tcx>,
instantiated_ty: OpaqueHiddenType<'tcx>,
) -> Ty<'tcx> {
if let Some(e) = self.tainted_by_errors() {
return Ty::new_error(self.tcx, e);
}

relevant part of InferCtxt:
/// Returns `true` if errors have been reported since this infcx was
/// created. This is sometimes used as a heuristic to skip
/// reporting errors that often occur as a result of earlier
/// errors, but where it's hard to be 100% sure (e.g., unresolved
/// inference variables, regionck errors).
#[must_use = "this method does not have any side effects"]
pub fn tainted_by_errors(&self) -> Option<ErrorGuaranteed> {
if let Some(guar) = self.tainted_by_errors.get() {
Some(guar)
} else if self.dcx().err_count_excluding_lint_errs() > self.err_count_on_creation {
// Errors reported since this infcx was made. Lint errors are
// excluded to avoid some being swallowed in the presence of
// non-lint errors. (It's arguable whether or not this exclusion is
// important.)
let guar = self.dcx().has_errors().unwrap();
self.set_tainted_by_errors(guar);
Some(guar)
} else {
None
}
}

rustc 1.81.0-nightly (f1586001a 2024-06-13)
binary: rustc
commit-hash: f1586001ace26df7cafeb6534eaf76fb2c5513e5
commit-date: 2024-06-13
host: x86_64-unknown-linux-gnu
release: 1.81.0-nightly
LLVM version: 18.1.7
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jun 14, 2024
@olafes
Copy link
Contributor Author

olafes commented Jun 14, 2024

@rustbot label T-compiler I-ICE A-diagnostics WG-compiler-parallel

@rustbot rustbot added A-diagnostics Area: Messages for errors, warnings, and lints I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-compiler-parallel Working group: Parallelizing the compiler labels Jun 14, 2024
@jieyouxu jieyouxu removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jun 14, 2024
@oli-obk
Copy link
Contributor

oli-obk commented Jun 15, 2024

yea, the reason this exists is that it wants to detect when queries invoked by the current InferCtxt cause errors. The way to fix this is to make more queries return Result<CurrentOutput, ErrorGuaranteed> or taint their output in case the output supports that.

Next steps include some data gathering: remove this check and look at the failing ui tests with -Ztreat-err-as-bug to see which queries are reporting errors, but not tainting their result.

bors added a commit to rust-lang-ci/rust that referenced this issue Jun 26, 2024
Automatically taint InferCtxt when errors are emitted

r? `@nnethercote`

Basically `InferCtxt::dcx` now returns a `DiagCtxt` that refers back to the `Cell<Option<ErrorGuaranteed>>` of the `InferCtxt` and thus when invoking `Diag::emit`, and the diagnostic is an error, we taint the `InferCtxt` directly.

That change on its own has no effect at all, because `InferCtxt` already tracks whether errors have been emitted by recording the global error count when it gets opened, and checking at the end whether the count changed. So I removed that error count check, which had a bit of fallout that I immediately fixed by invoking `InferCtxt::dcx` instead of `TyCtxt::dcx` in a bunch of places.

The remaining new errors are because an error was reported in another query, and never bubbled up. I think they are minor enough for this to be ok, and sometimes it actually improves diagnostics, by not silencing useful diagnostics anymore.

fixes rust-lang#126485 (cc `@olafes)`

There are more improvements we can do (like tainting in hir ty lowering), but I would rather do that in follow up PRs, because it requires some refactorings.
@bors bors closed this as completed in 7b21c18 Jul 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-compiler-parallel Working group: Parallelizing the compiler
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

4 participants