Skip redundant storage-conflict updates during coroutine layout
## Summary
When computing coroutine storage conflicts, we currently record conflicts between every pair in the live set. This PR instead remembers the last live set whose conflicts were recorded; if the current set is a subset, we skip the matrix updates. For example, after recording `{a, b, c}`, observing `{a, b}` adds no new conflicts.
I used an LLM to profile this change, and saw a compiler CPU reduction in the following projects:
- Codex: 2.69%
- Ruff: 2.84%
- uv: 5.28%
- ty: 3.28%
It encodes the `emit` behaviour, one of:
- `BugAbort`: Abort as a bug.
- `FatalAbort`: Abort as a fatal error.
- `ErrorGuaranteed`: Return an `ErrorGuaranteed` (the default).
- `()`: return `()`.
This is useful but `Diag` is very widely used and it's arguably not
useful enough to be worth the generic parameter. This commit removes it.
Benefits:
- Many fewer generic parameters, including many functions where the
genericity is entirely uninteresting. This includes every
`Diagnostic::into_diag` and `Subdiagnostic::add_to_diag`. Removes the
need for the comment about `<G>` on `trait Diagnostic`.
- No `PhantomData` in `Diag`.
- Makes bootstrapping a bit faster.
The downside is there is no longer a single `emit` method.
- Getting a `!` return type requires calling `emit_bug` or `emit_fatal`.
This only affects a few call sites.
- Getting an `ErrorGuaranteed` return type requires calling `emit_err`.
This affects a lot of call sites. (It arguably makes call sites more
self-documenting.)
- Using the wrong `emit_*` on a diagnostic causes a runtime abort, which
previously couldn't occur. Seems unlikely, but it's not impossible.
- Note: If no particular return type is needed, `emit` can be used for
any diagnostic. (Bug/fatal diagnostics will still trigger abort.)
fix const_item_mutation lint to use needs_drop instead of has_dtor
Description:
The const_item_mutation lint was only checking if the outer type has a direct Drop impl (has_dtor). But if a field inside the type has a Drop impl, that drop logic can also observe the mutation — so the warning should be suppressed in that case too.
This fixes a false positive where the lint would warn on code like:
```O.inner.val = 42;```
even when Inner has a Drop impl that prints the value — meaning the mutation IS observable and the warning is wrong.
Fix: replace has_dtor check with needs_drop, which checks the whole type including all fields inside it.
Avoid creating overlapping assignments in MatchBranchSimplification
MatchBranchSimplification can turn a match into an assignment, but doesn't make sure the LHS and RHS do not overlap. For example, the enum version of the mir-opt test in this PR is currently compiled with `-Zmir-opt-level=0 -Zmir-enable-passes=+MatchBranchSimplification` to
```
fn g(_1: Foo) -> Foo {
let mut _0: Foo;
let mut _2: Foo;
let mut _3: *const Foo;
let mut _4: u8;
bb0: {
_2 = copy _1;
_3 = &raw const _2;
_4 = discriminant((*_3));
_2 = no_retag copy (*_3); // Since _3 = &raw const 2 above, this overlaps!
_0 = copy _2;
return;
}
}
```
While this is technically a miscompile, I haven't found any way to turn this into something exciting, in part because the only thing that can be made to overlap here is a by-value enum discriminant which is always register-sized so backends lower this in a way that is okay with overlapping.
move bug and span_bug macros to rustc_span
This allows them to be used without depending on rustc_middle.
This is done by using a rustc_interface callback so that, if the callback is set, a internal compiler error diagnostic is printed through that. After that the panic is initiated.
r? @jdonszelmann @bjorn3
We only checked the provenance for a part of the allocation that will be
interned. Fixed by adding a method under `InterpCx` to check the whole
allocation.
GVN only checked whether the portion of allocation referred by `MPlaceTy`
contains provenance, but we then intern the whole allocation. This causes
ICE if the unchecked part contains provenance. This commit extends the
check to the whole allocation.
Also, removes a check for unsized local in `try_as_evaluated_constant`
which seems to duplicate with the one in `op_to_prop_const`.
Simplify the `G` in `Diag<'a, G>`
The `EmissionGuarantee` trait isn't necessary, and just complicates things. Details in individual commits.
r? @oli-obk
Remove unused arguments of functions
That these arguments are unused was hidden by the `#[trace::instrument]` macro, which uses the arguments.
I don't see an easy way to avoid this from happening in the future.
Disconnect `rustc_codegen_ssa` from `rustc_mir_transform`
The direct dependency from `rustc_codegen_ssa` to `rustc_mir_transform` allowed codegen to reuse some MIR-transform code for identifying unnecessary landing pads (for https://github.com/rust-lang/rust/pull/143208).
However, we can achieve the same result by calling through a hook on `TyCtxt`, eliminating the crate dependency.
---
There should be no change to compiler output.
The direct dependency from `rustc_codegen_ssa` to `rustc_mir_transform` allowed
codegen to reuse some MIR-transform code for identifying unnecessary landing
pads.
However, we can achieve the same result by calling through a hook on `TyCtxt`,
eliminating the crate dependency.
The bitset entries tracked by this code don't have a naturally fixed domain, so
using GrowableBitSet makes more sense than repeatedly resizing a DenseBitSet.
It's a trait that is used to give `Diag::emit` different return types
depending on the `G` in `Diag<'_, G>`. It works, but it's overkill.
There are only four different `G` types in practice (`BugAbort`,
`FatalAbort`, `ErrorGuaranteed`, and `()`) and having inherent `emit`
methods for each of those four concrete `Diag<'_, G>` types is good
enough.
This commit makes that change. This removes `EmissionGuarantee`
trait bounds from many places, which is nice.
Compile-time-only functions don't generate code, so instrumenting them for
coverage is useless.
This also avoids an ICE when trying to get the function's symbol name for an
unused-function record, which can occur when instrumenting `core`.
Co-Authored-By: Rachel Barker <rachel.barker@ferrous-systems.com>
remove EnumSizeOpt
This was landed years ago but has never been enabled due to odd issues that came up when merging it. The issues never got investigated (or at least, not with any conclusions). The author of the pass [said](https://github.com/rust-lang/rust/issues/154413#issuecomment-4150682443) they don't have time to work on it and recommend removing it.
So... let's do that.
r? @saethlin
Fixesrust-lang/rust#154413
Rollup of 3 pull requests
Successful merges:
- rust-lang/rust#161517 (coverage: Switch over to hybrid HIR-aware coverage spans )
- rust-lang/rust#162030 (Prevent `--test` to be used in `rustdoc-html` testsuite)
- rust-lang/rust#162329 (Suggest extra borrows for nested reference arguments)
Avoid the `arena_cache` modifier for several queries
The `arena_cache` query modifier is convenient, but we can potentially avoid some overhead by allocating values into the central `tcx.arena` when possible.
There should be no change to compiler output.
Instead of trying to heuristically recover source-code spans from MIR soup, we
can now take advantage of the `CoverageKind::Point` statements injected for
specific HIR expressions.
This is a transitional implementation that lets us remove the old MIR-soup
code, but still relies heavily on the existing span-refinement heuristics, and
does not take advantage of the full possibilities of HIR-aware coverage
instrumentation.
In order to avoid annoying never-executed spans in code that contains
assertions, a heuristic has been added that ignores any expression within the
arguments of a macro-expanded function call that returns `!`.
Some coverage tests now require `//@ min-llvm-version: 23`, due to changes in
how LLVM sorts the coverage mapping entries.
This variant will be used for marker statements that are injected when a HIR
expression is about to be lowered to MIR, and at other points relevant to
coverage instrumentation.
Adding this variant in its own commit keeps the mundane plumbing steps separate
from later changes that will perform injection and analysis.
fix(reborrow): recursive implementation
If Reborrow finds '&'a mut T' fields where 'a is the Reborrowed type's first lifetime parameter (currently the only lifetime that is allowed to reborrow) then it inserts a Deref and borrow of the T, and likewise if it finds a 'T: Reborrow' field then the field type is recursed into.
This makes Reborrow always produce the correct borrow checking logic at the cost of most probably being inconsiderately expensive. The thinking is that performance will be a followup consideration.
r? @oli-obk
* Implement Reborrow as a recursive operation
If Reborrow finds '&'a mut T' fields then it inserts
a Deref and borrow of the T, and likewise if it finds a
'T: Reborrow' field then the field type is recursed into.
This makes Reborrow always produce the correct borrow checking logic at
the cost of most probably being inconsiderately expensive. The thinking
is that performance will be a followup consideration.
* PhantomDeref
* Simpler deref test
* Add more PhantomDeref unreachability assertions
* Write out lifetime omission
* Document ProjectionElem::PhantomDeref
* Comment half of reborrow tests
* fix PhantomDeref conflicting with AccessDepth::Shallow
* Recheck CoerceShared in borrowck TypeChecker to ensure its lifetimes make sense
* Fix rebase
* Changes... but where to?
* Typo fix
* Improve comment
* Use fully_perform_op to evaluate CoerceShared trait in borrowck
* More CoerceShared comment
* Comment rest of Reborrow tests
Co-authored-by: Oli Scherer <github35764891676564198441@oli-obk.de>
This check is called from a few different places when coverage is enabled, so
we should probably let the query system take care of memoizing results and
tracking dependencies.