-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Automatically taint InferCtxt when errors are emitted #126996
Conversation
Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt Some changes occurred in need_type_info.rs cc @lcnr |
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
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.
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 | ||
} | ||
self.tainted_by_errors.get() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the core change that causes diagnostics changes
pub fn dcx(&self) -> DiagCtxtHandle<'tcx> { | ||
self.tcx.dcx() | ||
pub fn dcx(&self) -> DiagCtxtHandle<'_> { | ||
self.tcx.dcx().taintable_handle(&self.tainted_by_errors) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is what enables the auto-tainting (as long as one goes through this dcx
). I'm looking into writing a lint that detects when you invoke dcx
on something that does not taint, while a tainting thing is available
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (c8dd503): comparison URL. Overall result: ❌✅ regressions and improvements - ACTION NEEDEDBenchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf. Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @bors rollup=never Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)Results (secondary 4.1%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesResults (secondary -3.6%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 694.171s -> 694.347s (0.03%) |
mdpe: &'a MoveDataParamEnv<'tcx>, | ||
skip_unreachable_unwind: bool, | ||
} | ||
|
||
impl<'a, 'tcx> MaybeInitializedPlaces<'a, 'tcx> { | ||
pub fn new(tcx: TyCtxt<'tcx>, body: &'a Body<'tcx>, mdpe: &'a MoveDataParamEnv<'tcx>) -> Self { | ||
impl<'a, 'mir, 'tcx> MaybeInitializedPlaces<'a, 'mir, 'tcx> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will make a plea for better lifetime names than 'a
. In a type with multiple lifetimes, I don't like when some of the names are useful ('mir
) and some are not ('a
). Elaborator<'a, 'b, 'mir, 'tcx>
is a good (well, bad) example.
I know some of the 'a
names are pre-existing but we can do better for the new ones. Would 'mdpe
be a good name here instead of 'a
? Or 'env
? Anything even slightly descriptive would be better than 'a
. And I think that would propagate to a lot of the other changes in this commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is very common to use 'a
lifetime. We should either change this across rustc (I mildly disagree that we should) or not do it.
But I def don't want to do it in this PR
@@ -144,7 +144,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |||
} | |||
} | |||
|
|||
pub(crate) fn dcx(&self) -> DiagCtxtHandle<'tcx> { | |||
pub(crate) fn dcx(&self) -> DiagCtxtHandle<'a> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'root_ctxt
(or 'rcx
) would be a better lifetime name than 'a
for FnCtxt
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preexisting and very 'root_ctxt
is very verbose. I don't really think we should do it. 'rcx
is not really better than 'a
. We could use 'infcx
, but it's used everywhere and does not spark joy. If someone wants to do that change, I won't stop them, but I don't want to do it, and definitely not in this PR.
@@ -139,7 +139,7 @@ pub struct TypeErrCtxt<'a, 'tcx> { | |||
} | |||
|
|||
impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, hard to improve on 'a
for TypeErrCtxt
due to it being used in multiple fields.
@@ -417,6 +418,7 @@ pub struct DiagCtxt { | |||
#[derive(Copy, Clone)] | |||
pub struct DiagCtxtHandle<'a> { | |||
dcx: &'a DiagCtxt, | |||
tainted_with_errors: Option<&'a Cell<Option<ErrorGuaranteed>>>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comments, please! This needs some explanation.
Will this change mean |
To play devil's advocate: why is this approach needed for |
No, there are like 10 sites where we taint from a delayed bug, no clue what's up there, need to look at them. Also we sometimes taint when obtaining an ErrorGuaranteed from other queries. |
Oh I def plan on doing that for other sites, too. The alternative is to rely on everyone either tainting when they emit an error or producing an error type. It's not always desirable to emit an error type (e.g. there is no reason to make |
happy with the general approach 👍 I personally think the current approach of using I think my general approach is something like: if we expect to be able to elide the lifetime in (nearly) all usages of the type, it can be non-descriptive. |
ugh, whatever @bors r+ |
☀️ Test successful - checks-actions |
Finished benchmarking commit (7b21c18): comparison URL. Overall result: ❌✅ regressions and improvements - ACTION NEEDEDNext Steps: If you can justify the regressions found in this perf run, please indicate this with @rustbot label: +perf-regression Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)This benchmark run did not return any relevant results for this metric. CyclesResults (secondary -2.5%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 697.828s -> 699.484s (0.24%) |
Visiting for weekly rustc-perf triage
@rustbot label: +perf-regression-triaged |
r? @nnethercote
Basically
InferCtxt::dcx
now returns aDiagCtxt
that refers back to theCell<Option<ErrorGuaranteed>>
of theInferCtxt
and thus when invokingDiag::emit
, and the diagnostic is an error, we taint theInferCtxt
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 invokingInferCtxt::dcx
instead ofTyCtxt::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 #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.