2618 Commits

Author SHA1 Message Date
Osama Abdelkader
bf6f3035af Fix typos: duplicate words in comments
- Fix 'the the' → 'the' in rustc_const_eval
- Fix 'wether' → 'whether' in compiletest
- Fix 'is is' → 'is' in rustc_ast_pretty (2 instances)

Signed-off-by: Osama Abdelkader <osama.abdelkader@gmail.com>
2025-10-27 18:01:04 +03:00
bors
4b53279854 Auto merge of #148040 - saethlin:trivial-consts, r=oli-obk
Add a fast path for lowering trivial consts

The objective of this PR is to improve compilation performance for crates that define a lot of trivial consts. This is a flamegraph of a build of a library crate that is just 100,000 trivial consts, taken from a nightly compiler:
<img width="842" height="280" alt="2025-10-25-164005_842x280_scrot" src="https://github.com/user-attachments/assets/e5400aaf-03bd-4461-b905-054aa82ca60f" />
My objective is to target all of the cycles in `eval_to_const_value_raw` that are not part of `mir_built`, because if you look at the `mir_built` for a trivial const, we already have the value available.

In this PR, the definition of a trivial const is this:
```rust
const A: usize = 0;
```
Specifically, we look for if the `mir_built` body is a single basic block containing one assign statement and a return terminator, where the assign statement assigns an `Operand::Constant(Const::Val)`. The MIR dumps for these look like:
```
const A: usize = {
    let mut _0: usize;

    bb0: {
        _0 = const 0_usize;
        return;
    }
}
```

The implementation is built around a new query, `trivial_const(LocalDefId) -> Option<(ConstValue, Ty)>` which returns the contents of the `Const::Val` in the `mir_built` if the `LocalDefId` is a trivial const.

Then I added _debug_ assertions to the beginning of `mir_for_ctfe` and `mir_promoted` to prevent trying to get the body of a trivial const, because that would defeat the optimization here. But these are deliberately _debug_ assertions because the consequence of failing the assertion is that compilation is slow, not corrupt. If we made these hard assertions, I'm sure there are obscure scenarios people will run into where the compiler would ICE instead of continuing on compilation, just a bit slower. I'd like to know about those, but I do not think serving up an ICE is worth it.

With the assertions in place, I just added logic around all the places they were hit, to skip over trying to analyze the bodies of trivial consts.

In the future, I'd like to see this work extended by:
* Pushing detection of trivial consts before MIR building
* Including DefKind::Static and DefKind::InlineConst
* Including consts like `_1 = const 0_usize; _0 = &_1`, which would make a lot of promoteds into trivial consts
* Handling less-trivial consts like `const A: usize = B`, which have `Operand::Constant(Const::Unevaluated)`
2025-10-27 11:02:41 +00:00
Ben Kimock
775da711c6 Add a fast path for lowering trivial consts 2025-10-25 16:59:53 -04:00
Camille Gillot
5dfbf67f94 Replace NullOp::SizeOf and NullOp::AlignOf by lang items. 2025-10-23 00:38:28 +00:00
bors
4d94478977 Auto merge of #147826 - Muscraft:update-typos, r=Noratrieb
Update typos

I saw that `typos` was a few versions out of date and figured it would be a good idea to update it. Upgrading to `1.38.1` adds the [July](https://github.com/crate-ci/typos/issues/1331), [August](https://github.com/crate-ci/typos/issues/1345), and [September](https://github.com/crate-ci/typos/issues/1370) dictionary updates. As part of this change, I also sorted the configuration file.
2025-10-22 13:11:47 +00:00
Oli Scherer
375899c940 Allow unsizing pattern types with pointer base 2025-10-21 11:22:51 +00:00
Oli Scherer
ad4bd083f3 Add not-null pointer patterns to pattern types 2025-10-21 11:22:51 +00:00
Scott Schafer
12f6b9697f chore: Update typos to 1.38.1 2025-10-20 12:20:15 -06:00
Matthias Krüger
756d3a0fb7 Rollup merge of #147764 - beepster4096:oopsies_sorry, r=saethlin
Undo CopyForDeref assertion in const qualif

Fixes rust-lang/rust#147733 caused by rust-lang/rust#145513

This code in fact does not run only on runtime MIR.
2025-10-18 08:08:38 +02:00
Cameron Steffen
b323f567d9 Remove Option from impl_trait_header 2025-10-17 08:36:34 -05:00
beepster
f1b180a788 undo CopyForDeref assertion in const qualif 2025-10-15 14:28:03 -07:00
bors
3be68033b6 Auto merge of #145513 - beepster4096:erasedereftemps, r=saethlin,cjgillot
Validate CopyForDeref and DerefTemps better and remove them from runtime MIR

(split from my WIP rust-lang/rust#145344)

This PR:
- Removes `Rvalue::CopyForDeref` and `LocalInfo::DerefTemp` from runtime MIR
    - Using a new mir pass `EraseDerefTemps`
    - `CopyForDeref(x)` is turned into `Use(Copy(x))`
    - `DerefTemp` is turned into `Boring`
        - Not sure if this part is actually necessary, it made more sense in rust-lang/rust#145344 with `DerefTemp` storing actual data that I wanted to keep from having to be kept in sync with the rest of the body in runtime MIR
- Checks in validation that `CopyForDeref` and `DerefTemp` are only used together
- Removes special handling for `CopyForDeref` from many places
- Removes `CopyForDeref` from `custom_mir` reverting rust-lang/rust#111587
    - In runtime MIR simple copies can be used instead
    - In post cleanup analysis MIR it was already wrong to use due to the lack of support for creating `DerefTemp` locals
    - Possibly this should be its own PR?
 - Adds an argument to `deref_finder` to avoid creating new `DerefTemp`s and `CopyForDeref` in runtime MIR.
     - Ideally we would just avoid making intermediate derefs instead of fixing it at the end of a pass / during shim building
 - Removes some usages of `deref_finder` that I found out don't actually do anything

r? oli-obk
2025-10-12 02:34:20 +00:00
Camille Gillot
b7c2b3dc80 Remove StatementKind::Deinit. 2025-10-10 12:57:24 +00:00
Stuart Cook
9ace0de26b Rollup merge of #147489 - chenyukang:yukang-prefer-repeat-n, r=Kivooeo,oli-obk
Prefer to use repeat_n over repeat().take()

More from https://github.com/rust-lang/rust/pull/147464, but batch processed with `ast-grep` to find and replace.

second commit add notes for library: affaf532f9

r? ``@RalfJung``
2025-10-09 18:43:26 +11:00
Stuart Cook
fd6546d514 Rollup merge of #146568 - sayantn:simd-shuffle, r=RalfJung
Port the implemention of SIMD intrinsics from Miri to const-eval

Ported the implementation of most SIMD intrinsics from Miri to rustc_const_eval. Remaining are

 - Math functions (as per `@RalfJung's` suggestions)
 - FMA (non-deterministic)
 - Funnel Shifts (not implemented in Miri yet)
 - Unordered reduction intrinsics (not implemented in Miri yet)
2025-10-09 18:43:20 +11:00
yukang
1654cce210 prefer to use repeat_n over repeat and take 2025-10-09 01:24:55 +08:00
sayantn
45ca537746 Port the Miri implementations of SIMD intrinsics to rustc_const_eval 2025-10-08 21:05:19 +05:30
Matthias Krüger
795bbaa37b Rollup merge of #147464 - RalfJung:repeat, r=saethlin
prefer repeat_n() over repeat().take()
2025-10-08 15:39:27 +02:00
Jana Dönszelmann
cf63f1578c Rollup merge of #147451 - RalfJung:extra-const-ice, r=chenyukang
fix panic with extra-const-ub-checks

Fixes https://github.com/rust-lang/rust/issues/147306
2025-10-08 10:06:58 +02:00
Ralf Jung
70a357a4c6 prefer repeat_n() over repeat().take() 2025-10-08 09:04:22 +02:00
Boxy Uwu
8e9b0c4ca9 rename select_where_possible and select_all_or_error 2025-10-07 23:02:23 +01:00
Ralf Jung
8171174715 fix panic with extra-const-ub-checks 2025-10-07 19:16:58 +02:00
beepster4096
fc959e5464 remove DerefTemp and CopyFromDeref from runtime mir 2025-10-06 10:57:27 -07:00
Kivooeo
67bc030833 change flt back to ftl 2025-10-04 18:18:58 +00:00
bors
42b384ec0d Auto merge of #147055 - beepster4096:subtype_is_not_a_projection, r=lcnr
Turn ProjectionElem::Subtype into CastKind::Subtype

I noticed that drop elaboration can't, in general, handle `ProjectionElem::SubType`. It creates a disjoint move path that overlaps with other move paths. (`Subslice` does too, and I'm working on a different PR to make that special case less fragile.) If its skipped and treated as the same move path as its parent then `MovePath.place` has multiple possible projections. (It would probably make sense to remove all `Subtype` projections for the canonical place but it doesn't make sense to have this special case for a problem that doesn't actually occur in real MIR.)

The only reason this doesn't break is that `Subtype` is always the sole projection of the local its applied to. For the same reason, it works fine as a `CastKind` so I figured that makes more sense than documenting and validating this hidden invariant.

cc rust-lang/rust#112651, rust-lang/rust#133258

r? Icnr (bc you've been the main person dealing with `Subtype` it looks like)
2025-10-02 01:54:48 +00:00
Jubilee Young
0c9d0dfe04 remove explicit deref of AbiAlign for most methods
Much of the compiler calls functions on Align projected from AbiAlign.
AbiAlign impls Deref to its inner Align, so we can simplify these away.
Also, it will minimize disruption when AbiAlign is removed.

For now, preserve usages that might resolve to PartialOrd or PartialEq,
as those have odd inference.
2025-09-28 15:02:14 -07:00
Mark Rousskov
201f299ef6 Apply cfg(bootstrap) replacement 2025-09-26 19:09:23 -04:00
beepster4096
aa5a21450a ProjectionElem::Subtype -> CastKind::Subtype 2025-09-26 01:25:26 -07:00
Matthias Krüger
b184550f0e Rollup merge of #146991 - cachebag:span-caller-location, r=RalfJung
const_caller_location to use real Span instead of `DUMMY_SP`

Clarifying usage of DUMMY_SP
2025-09-26 06:36:32 +02:00
Akrm Al-Hakimi
c98b3b2da4 const_caller_location: edit FIXME to explain use of DUMMY_SP
Co-authored-by: Ralf Jung <post@ralfj.de>
2025-09-25 16:09:27 -04:00
Stuart Cook
8e62f95376 Rollup merge of #146735 - Qelxiros:const_mul_add, r=tgross35,RalfJung
unstably constify float mul_add methods

Tracking issue: rust-lang/rust#146724
r? `@tgross35`
2025-09-25 20:31:54 +10:00
Jeremy Smart
a00f24116e unstably constify float mul_add methods
Co-authored-by: Ralf Jung <post@ralfj.de>
2025-09-24 15:21:31 -04:00
Ralf Jung
8328c3dada const validation: better error for maybe-null references 2025-09-24 13:35:29 +02:00
Ralf Jung
0a41add629 const-eval: improve and actually test the errors when pointers might be outside the range of a scalar 2025-09-24 13:34:33 +02:00
Matthias Krüger
b7ab58eb4d Rollup merge of #146597 - modhanami:add-struct-tail-recursion-limit-span, r=oli-obk
Add span for struct tail recursion limit error

Fixes rust-lang/rust#135629

Changes
1. Add span to RecursionLimitReached
2. Add ObligationCause parameter to struct_tail_raw
4. Update call sites to pass nearby ObligationCause or create one
5. Update affected .stderr
2025-09-18 17:20:57 +02:00
Stuart Cook
540fd20ba6 Rollup merge of #146664 - fmease:clean-up-dyn, r=jdonszelmann
Clean up `ty::Dynamic`

1. As a follow-up to PR rust-lang/rust#143036, remove `DynKind` entirely.
2. Inside HIR ty lowering, consolidate modules `dyn_compatibility` and `lint` into `dyn_trait`
   * `dyn_compatibility` wasn't about dyn compatibility itself, it's about lowering trait object types
   * `lint` contained dyn-Trait-specific diagnostics+lints only
2025-09-18 11:48:51 +10:00
bors
5d1b897a07 Auto merge of #146331 - RalfJung:copy-prov-repeat, r=oli-obk
interpret: copy_provenance: avoid large intermediate buffer for large repeat counts

Copying provenance worked in this odd way where the "preparation" phase (which is supposed to just extract the necessary information from the source range) already did all the work of repeating the result N times for the target range. This was needed to use the existing `insert_presorted` function on `SortedMap`.

This PR generalizes `insert_presorted` so that we can avoid this odd structure on copy-provenance, and maybe even improve performance.
2025-09-17 13:56:54 +00:00
León Orell Valerian Liehr
26f3337d4e Remove DynKind 2025-09-17 04:46:46 +02:00
Camille Gillot
53b91ea87f Remove Rvalue::Len. 2025-09-16 22:23:19 +00:00
Tawan Muadmuenwai
6912631d3e Add span for struct tail recursion limit error 2025-09-16 23:04:59 +07:00
Stuart Cook
f162d11351 Rollup merge of #146402 - RalfJung:aggregate-init, r=saethlin
interpret: fix overlapping aggregate initialization

This fixes the problem pointed out by ````@saethlin```` in https://github.com/rust-lang/rust/issues/146383#issuecomment-3273224645.

Also clarify when exactly current de-facto MIR semantics allow overlap of the LHS and RHS in an assignment.
2025-09-16 10:25:40 +10:00
Ralf Jung
8ecda4b7f8 interpret: fix overlapping aggregate initialization 2025-09-10 15:59:11 +02:00
Matthias Krüger
422c76adae Rollup merge of #146178 - folkertdev:static-align, r=jdonszelmann,ralfjung,traviscross
Implement `#[rustc_align_static(N)]` on `static`s

Tracking issue: https://github.com/rust-lang/rust/issues/146177

```rust
#![feature(static_align)]

#[rustc_align_static(64)]
static SO_ALIGNED: u64 = 0;
```

We need a different attribute than `rustc_align` because unstable attributes are tied to their feature (we can't have two unstable features use the same unstable attribute). Otherwise this uses all of the same infrastructure as `#[rustc_align]`.

r? `@traviscross`
2025-09-10 14:17:38 +02:00
Ralf Jung
64ea775d27 interpret: copy_provenance: avoid large intermediate buffer for large repeat counts 2025-09-10 08:40:12 +02:00
Folkert de Vries
cbacd00f10 allow #[rustc_align_static(N)] on statics
We need a different attribute than `rustc_align` because unstable attributes are
tied to their feature (we can't have two unstable features use the same
unstable attribute). Otherwise this uses all of the same infrastructure
as `#[rustc_align]`.
2025-09-09 21:54:54 +02:00
bors
364da5d88d Auto merge of #145717 - BoxyUwU:erase_regions_rename, r=lcnr
rename erase_regions to erase_and_anonymize_regions

I find it consistently confusing that `erase_regions` does more than replacing regions with `'erased`. it also makes some code look real goofy to be writing manual folders to erase regions with a comment saying "we cant use erase regions" :> or code that re-calls erase_regions on types with regions already erased just to anonymize all the bound regions.

r? lcnr

idk how i feel about the name being almost twice as long now
2025-09-09 15:04:44 +00:00
Boxy
e379c77586 erase_regions to erase_and_anonymize_regions 2025-09-09 14:49:16 +02:00
Stuart Cook
915f9ff160 Rollup merge of #146324 - RalfJung:no-ptr-fragment, r=oli-obk
const-eval: disable pointer fragment support

This fixes https://github.com/rust-lang/rust/issues/146291 by disabling pointer fragment support for const-eval. I want to properly fix this eventually, but won't get to it in the next few weeks, so this is an emergency patch to prevent the buggy implementation from landing on stable. The beta cutoff is on Sep 12th so if this PR lands after that, we'll need a backport.
2025-09-09 14:35:05 +10:00
Jana Dönszelmann
6087d89004 fixup limit handling code 2025-09-08 15:07:12 -07:00
Ralf Jung
aed0ed4c93 const-eval: disable pointer fragment support 2025-09-08 13:22:52 +02:00