2899 Commits

Author SHA1 Message Date
Samuel Tardieu
323e23005a Fix ICE when validating transmuting ZST to inhabited enum
MIR validation attempts to determine the number of bytes needed to
represent the size of the source type to compute the discriminant for
the inhabited target enum. For a ZST source, there is no source data to
use as a discriminant so no proper runtime check can be generated.

Since that should never be possible, insert a delayed bug to ensure the
problem has been properly reported to the user by the type checker.
2025-08-23 19:25:58 +02:00
Jacob Pratt
ca6415d692
Rollup merge of #145738 - cjgillot:union-find-uplift, r=Zalathar
Uplift rustc_mir_transform::coverage::counters::union_find to rustc_data_structures.

I was wondering if we had some union-find implementation in the compiler. We do. So put it where we can find it.
2025-08-22 22:00:56 -04:00
Jacob Pratt
c80e77fa21
Rollup merge of #145695 - cjgillot:place-elem-map, r=oli-obk,lcnr
Introduce ProjectionElem::try_map.

Small utility function useful to manipulate MIR place projections.
2025-08-22 22:00:54 -04:00
Jacob Pratt
bc4a6431eb
Rollup merge of #142185 - saethlin:refprop-moves, r=cjgillot
Convert moves of references to copies in ReferencePropagation

This is a fix for https://github.com/rust-lang/rust/issues/141101.

The root cause of this miscompile is that the SsaLocals analysis that MIR transforms use is supposed to detect locals that are only written to once, in their single assignment. But that analysis is subtly wrong; it does not consider `Operand::Move` to be a write even though the meaning ascribed to `Operand::Move` (at least as a function parameter) by Miri is that the callee may have done arbitrary writes to the caller's Local that the Operand wraps (because `Move` is pass-by-pointer). So Miri conwiders `Operand::Move` to be a write but both the MIR visitor system considers it a read, and so does SsaLocals.

I have tried fixing this by changing the `PlaceContext` that is ascribed to an `Operand::Move` to a `MutatingUseContext` but that seems to have borrow checker implications, and changing SsaLocals seems to have wide-ranging regressions in MIR optimizations.

So instead of doing those, this PR adds a new kludge to ReferencePropagation, which follows the same line of thinking as the kludge in CopyProp that solves this same problem inside that pass: a5584a8fe1/compiler/rustc_mir_transform/src/copy_prop.rs (L65-L98)
2025-08-22 22:00:46 -04:00
Camille Gillot
689171d38e Uplift rustc_mir_transform::coverage::counters::union_find to rustc_data_structures. 2025-08-22 02:22:51 +00:00
Camille Gillot
e5bd01b533 Correct comments. 2025-08-21 23:59:20 +00:00
Camille GILLOT
37e7f52876 Introduce ProjectionElem::try_map. 2025-08-21 02:06:21 +00:00
bors
425a9c0a0e Auto merge of #145284 - nnethercote:type_name-print-regions, r=lcnr
Print regions in `type_name`.

Currently they are skipped, which is a bit weird, and it sometimes causes malformed output like `Foo<>` and `dyn Bar<, A = u32>`.

Most regions are erased by the time `type_name` does its work. So all regions are now printed as `'_` in non-optional places. Not perfect, but better than the status quo.

`c_name` is updated to trim lifetimes from MIR pass names, so that the `PASS_NAMES` sanity check still works. It is also renamed as `simplify_pass_type_name` and made non-const, because it doesn't need to be const and the non-const implementation is much shorter.

The commit also renames `should_print_region` as `should_print_optional_region`, which makes it clearer that it only applies to some regions.

Fixes rust-lang/rust#145168.

r? `@lcnr`
2025-08-17 10:24:20 +00:00
Stuart Cook
02efea51af
Rollup merge of #145392 - Zalathar:create-mappings, r=petrochenkov
coverage: Remove intermediate data structures from mapping creation

The data structures in `coverage::mappings` were historically very useful for isolating the details of mapping-extraction from the details of how coverage mappings are stored in MIR.

But because of various changes that have taken place over time, they now provide little value, and cause difficulty for the coordinated changes that will be needed for introducing expansion mapping support.

In the future, the pendulum might eventually swing back towards these being useful again, but we can always reintroduce suitable intermediate data structures if and when that happens. For now, the simplicity of not having this intermediate layer is a higher priority.

There should be no changes to compiler output.
2025-08-15 16:16:43 +10:00
Zalathar
ecce90b3ef coverage: Remove intermediate data structures from mapping creation 2025-08-14 21:35:23 +10:00
Nicholas Nethercote
8296ad0456 Print regions in type_name.
Currently they are skipped, which is a bit weird, and it sometimes
causes malformed output like `Foo<>` and `dyn Bar<, A = u32>`.

Most regions are erased by the time `type_name` does its work. So all
regions are now printed as `'_` in non-optional places. Not perfect, but
better than the status quo.

`c_name` is updated to trim lifetimes from MIR pass names, so that the
`PASS_NAMES` sanity check still works. It is also renamed as
`simplify_pass_type_name` and made non-const, because it doesn't need
to be const and the non-const implementation is much shorter.

The commit also renames `should_print_region` as
`should_print_optional_region`, which makes it clearer that it only
applies to some regions.

Fixes #145168.
2025-08-14 21:13:06 +10:00
Cameron Steffen
d4eb0947f1 Cleanup assoc parent utils 2025-08-13 09:33:09 -05:00
Jana Dönszelmann
7aa8707639
make no_mangle explicit on foreign items 2025-08-12 12:07:14 +02:00
Ben Kimock
9aa8cfaf2f Convert moves of references to copies in RefProp 2025-08-11 23:10:56 -04:00
Stuart Cook
e63e769c9b
Rollup merge of #144883 - scottmcm:remove-unneeded-drop_in_place, r=nnethercote
Remove unneeded `drop_in_place` calls

Might as well pull this out from rust-lang/rust#144561 because this is still used in things like `Vec::truncate` where it'd be nice to allow it be removed if inlined enough to see that the type is `Copy`.

So long as perf says it's ok, at least 🤞
2025-08-09 13:58:44 +10:00
Zalathar
db9f0bb532 coverage: Remove obsolete comment about hashing HIR
This code does not hash HIR manually (and has not done so for some time); it
merely obtains a hash returned as part of `hir_owner_nodes`.
2025-08-08 20:04:15 +10:00
Zalathar
7671b5af71 coverage: Extract HIR-related helper code out of the main module 2025-08-08 20:04:13 +10:00
Stuart Cook
162e2e4e65
Rollup merge of #145030 - cjgillot:gvn-no-flatten-index, r=saethlin
GVN:  Do not flatten derefs with ProjectionElem::Index.

r? `@saethlin`

This should fix the bug you found with https://github.com/rust-lang/rust/pull/131650
2025-08-08 12:52:55 +10:00
Stuart Cook
562222b737
Rollup merge of #144999 - Zalathar:remove-mcdc, r=oli-obk
coverage: Remove all unstable support for MC/DC instrumentation

Preliminary support for a partial implementation of “Modified Condition/Decision Coverage” instrumentation was added behind the unstable flag `-Zcoverage-options=mcdc` in 2024. These are the most substantial PRs involved:

- rust-lang/rust#123409
- rust-lang/rust#126733

At the time, I accepted these PRs with relatively modest scrutiny, because I did not want to stand in the way of independent work on MC/DC instrumentation. My hope was that ongoing work by interested contributors would lead to the code becoming clearer and more maintainable over time.

---

However, that MC/DC code has proven itself to be a major burden on overall maintenance of coverage instrumentation, and a major obstacle to other planned improvements, such as internal changes needed for proper support of macro expansion regions.

I have also become reluctant to accept any further MC/DC-related changes that would increase this burden.

That tension has resulted in an unhappy impasse. On one hand, the present MC/DC implementation is not yet complete, and shows little sign of being complete at an acceptable level of code quality in the foreseeable future. On the other hand, the continued existence of this partial MC/DC implementation is imposing serious maintenance burdens on every other aspect of coverage instrumentation, and is preventing some of the very improvements that would make it easier to accept expanded MC/DC support in the future.

While I know this will be disappointing to some, I think the healthy way forward is accept that I made the wrong call in accepting the current implementation, and to remove it entirely from the compiler.
2025-08-08 12:52:54 +10:00
Camille Gillot
ebd60b9b8f Do not flatten derefs with ProjectionElem::Index. 2025-08-07 23:34:15 +00:00
Guillaume Gomez
e56c24129e
Rollup merge of #144996 - dianqk:simplifycfg-collapse_goto_chain-changed, r=cjgillot
simplifycfg: Mark as changed when start is modified in collapse goto chain

Split from rust-lang/rust#142771.
r? cjgillot
2025-08-06 21:29:33 +02:00
Zalathar
81ed042c8c coverage: Remove all unstable support for MC/DC instrumentation 2025-08-06 22:38:52 +10:00
dianqk
2909de557c
simplifycfg: Mark as changed when start is modified in collapse goto chain 2025-08-06 20:26:10 +08:00
Scott McMurray
4e81ecaf3a Let RemoveUnneededDrops also remove drop_in_place 2025-08-04 23:54:39 -07:00
Zalathar
fb39d3ed88 coverage: Push async special case down into extract_refined_covspans 2025-08-04 13:48:41 +10:00
Zalathar
51e62a09a3 coverage: Remove -Zcoverage-options=no-mir-spans
This flag turned out to be less useful than anticipated, and interferes with
work towards expansion support.
2025-08-04 13:48:40 +10:00
Samuel Tardieu
179b4b6691
Rollup merge of #144756 - WaffleLapkin:inf-rec-etc-ctfe, r=lqd
detect infinite recursion with tail calls in ctfe

fixes rust-lang/rust#144753
2025-08-02 11:24:26 +02:00
Samuel Tardieu
fc4b3fa3f9
Rollup merge of #132748 - lolbinarycat:rustdoc-intra-doc-link-warn-more-54191, r=GuillaumeGomez
get rid of some false negatives in rustdoc::broken_intra_doc_links

rustdoc will not try to do intra-doc linking if the "path" of a link looks too much like a "real url".

however, only inline links (`[text](url)`) can actually contain a url, other types of links (reference links, shortcut links) contain a *reference* which is later resolved to an actual url.

the "path" in this case cannot be a url, and therefore it should not be skipped due to looking like a url.

fixes https://github.com/rust-lang/rust/issues/54191

to minimize the number of false positives that will be introduced, the following heuristic is used:

If there's no backticks, be lenient revert to old behavior.
This is to prevent churn by linting on stuff that isn't meant to be a link.
only shortcut links have simple enough syntax that they
are likely to be written accidentlly, collapsed and reference links
need 4 metachars, and reference links will not usually use
backticks in the reference name.
therefore, only shortcut syntax gets the lenient behavior.
here's a truth table for how link kinds that cannot be urls are handled:

|              |  is shortcut link  | not shortcut link |
|--------------|--------------------|-------------------|
| has backtick |    never ignore    |    never ignore   |
| no backtick  | ignore if url-like |    never ignore   |
2025-08-02 11:24:20 +02:00
Waffle Lapkin
5aec4379e3
detect infinite recursion with tail calls in ctfe 2025-07-31 23:59:55 +02:00
Jana Dönszelmann
e1d3ad89c7
remove rustc_attr_data_structures 2025-07-31 14:19:27 +02:00
bors
64ca23b623 Auto merge of #144723 - Zalathar:rollup-f9e0rfo, r=Zalathar
Rollup of 3 pull requests

Successful merges:

 - rust-lang/rust#144657 (fix: Only "close the window" when its the last annotated file)
 - rust-lang/rust#144665 (Re-block SRoA on SIMD types)
 - rust-lang/rust#144713 (`rustc_middle::ty` cleanups)

r? `@ghost`
`@rustbot` modify labels: rollup
2025-07-31 08:54:41 +00:00
Stuart Cook
5c123c25e3
Rollup merge of #144663 - Zalathar:empty-span, r=petrochenkov
coverage: Re-land "Enlarge empty spans during MIR instrumentation"

This allows us to assume that coverage spans will only be discarded during codegen in very unusual situations.

---

This seemingly-simple change has a rather messy history:
- rust-lang/rust#140847
- rust-lang/rust#141650
- rust-lang/rust#144298
- rust-lang/rust#144480

Since then, a number of related changes have landed that should make it reasonable to try again:
- rust-lang/rust#144530
- rust-lang/rust#144560
- rust-lang/rust#144616

In particular, we have multiple fixes/mitigations, and a confirmed regression test for the original bug that is not triggered by re-landing the changes in this PR.
2025-07-31 15:42:00 +10:00
Stuart Cook
f478bec907
Rollup merge of #143672 - beepster4096:box_drop_flags_again, r=oli-obk
Fix Box allocator drop elaboration

New version of rust-lang/rust#131146.

Clearing Box's drop flag after running its destructor can cause it to skip dropping its allocator, so just don't. Its cleared by the drop ladder code afterwards already.

Unlike the last PR this also handles other types with destructors properly, in the event that we can have open drops on them in the future (by partial initialization or DerefMove or something).

Finally, I also added tests for the interaction with async drop here but I discovered rust-lang/rust#143658, so one of the tests has a `knownbug` annotation. Not sure if it should be in this PR at all though.

Fixes rust-lang/rust#131082

r? wesleywiser - prev. reviewer
2025-07-31 15:41:59 +10:00
Scott McMurray
fe08ba0bae Re-block SRoA on SIMD types
Fixes 144621
2025-07-29 20:41:34 -07:00
Zalathar
2e6f4a5922 coverage: Re-land "Enlarge empty spans during MIR instrumentation"
This allows us to assume that coverage spans will only be discarded during
codegen in very unusual situations.
2025-07-30 13:17:05 +10:00
Stuart Cook
826c462c32
Rollup merge of #144566 - scottmcm:align-of-slice, r=oli-obk
Simplify `align_of_val::<[T]>(…)` → `align_of::<T>()`

I spotted this while working on the inliner (rust-lang/rust#144561).  In particular, if [`Layout::for_value`](https://doc.rust-lang.org/std/alloc/struct.Layout.html#method.for_value) inlines, then it can be pretty easy to end up with an `align_of_val::<[T]>` today (demo: <https://rust.godbolt.org/z/Tesnscj4a>) where we can save at least a block, if not more, by using the version that's an rvalue and not a call.
2025-07-29 23:50:35 +10:00
Zalathar
682f744f89 coverage: Treat #[automatically_derived] as #[coverage(off)] 2025-07-29 19:56:31 +10:00
Zalathar
b4d0c91635 coverage: Rename CoverageStatus to CoverageAttrKind
This patch also prepares the affected code in `coverage_attr_on` for some
subsequent changes.
2025-07-29 19:55:54 +10:00
Scott McMurray
8ef9233339 Simplify align_of_val::<[T]>(…)align_of::<T>() 2025-07-28 23:19:06 -07:00
bors
e3514bde96 Auto merge of #144377 - camsteffen:simplify-impl-of-method, r=fee1-dead
Rename impl_of_method and trait_of_item

This PR used to tweak the implementation of impl_of_method, but that introduced a perf regression.

Rename impl_of_method and trait_of_item to impl_of_assoc and trait_of_assoc respectively. This reflects how the two functions are closely related. And it reflects the behavior more accurately as the functions check whether the input is an associated item.
2025-07-28 16:38:15 +00:00
Cameron Steffen
b43164cef6 Rename impl_of_method -> impl_of_assoc 2025-07-28 09:54:53 -05:00
Cameron Steffen
172af038a7 Rename trait_of_item -> trait_of_assoc 2025-07-28 09:53:50 -05:00
bors
9ba00e0f9e Auto merge of #144543 - scottmcm:more-sroa, r=cjgillot
Allow more MIR SROA

This removes some guards on SROA that are no longer needed:
- With https://github.com/rust-lang/compiler-team/issues/838 it no longer needs to check for SIMD
- With https://github.com/rust-lang/compiler-team/issues/807 it no longer needs to check for niches
  - This means that `Wrapper(char)` and `Pin<&mut T>` can get SRoA'd now, where previously they weren't because the check was banning SRaA for anything with a niche -- not just things with `#[rustc_layout_scalar_valid_range_*]`.
  - Technically rust-lang/rust#133652 isn't complete yet, but `NonZero` and `NonNull` have already moved over, so this is fine.  At worst this will mean that LLVM gets less `!range` metadata on something that wasn't already fixed by rust-lang/rust#133651 or rust-lang/rust#135236, but that's still sound, and unblocking general SRoA is worth that tradeoff.
2025-07-28 11:53:15 +00:00
bors
d242a8bd5a Auto merge of #144469 - Kivooeo:chains-cleanup, r=SparrowLii
Some `let chains` clean-up

Not sure if this kind of clean-up is welcoming because of size, but I decided to try out one

r? compiler
2025-07-28 05:25:23 +00:00
Kivooeo
b8eb046e6e use let chains in mir, resolve, target 2025-07-28 06:10:36 +05:00
Kivooeo
bae38bad78 use let chains in hir, lint, mir 2025-07-28 06:10:14 +05:00
Scott McMurray
47bfa846f3 Allow more MIR SROA 2025-07-27 13:21:06 -07:00
Zalathar
2b17897092 Revert "coverage: Enlarge empty spans during MIR instrumentation, not codegen"
This reverts commit f877aa7d14916f71a2f88c6d4c009e7ded7684c4.
2025-07-26 11:14:40 +10:00
beepster4096
ba55f20f43 span_bug instead of handling currently impossible drop case 2025-07-25 13:19:43 -07:00
beepster4096
dad982633c fix box destructor generation 2025-07-25 13:19:43 -07:00