Commit Graph

2762 Commits

Author SHA1 Message Date
Oli Scherer
fd3da4bebd Replace some Option<Span> with Span and use DUMMY_SP instead of None 2025-06-05 14:14:59 +00:00
Matthias Krüger
9ec41bcf72
Rollup merge of #141932 - azhogin:azhogin/async-drop-inside-asyncgen-fix, r=oli-obk
Fix for async drop inside async gen fn

Return value (for yield) is corrected for async drop inside async gen function.
In CFG, when internal async drop future is polled and returned `Poll<()>::Pending`, then async gen resume function returns `Poll<(OptRet)>::Pending`.

Fixes rust-lang/rust#140530
2025-06-04 19:50:21 +02:00
Andrew Zhogin
d5a9a00518 Fix for async drop inside async gen fn 2025-06-04 18:45:34 +07:00
Matthias Krüger
aed1171c66
Rollup merge of #141677 - azhogin:azhogin/async-drop-unexpected-type-instead-of-drop-fn-fix, r=oli-obk
Async drop - type instead of async drop fn, fixes #140484

Fixes: rust-lang/rust#140484
Fixes: rust-lang/rust#140500

Fixes ICE, when type is provided in AsyncDrop trait instead of `async fn drop()`.
Fixes ICE, when async drop fn has wrong signature.
2025-06-03 07:03:43 +02:00
Andrew Zhogin
f023a69f32 Async drop - type instead of async drop fn and incorrect drop signature don't ICE now 2025-06-01 15:22:29 +07:00
bors
f0999ffdc4 Auto merge of #139118 - scottmcm:slice-get-unchecked-intrinsic, r=workingjubilee
`slice.get(i)` should use a slice projection in MIR, like `slice[i]` does

`slice[i]` is built-in magic, so ends up being quite different from `slice.get(i)` in MIR, even though they're both doing nearly identical operations -- checking the length of the slice then getting a ref/ptr to the element if it's in-bounds.

This PR adds a `slice_get_unchecked` intrinsic for `impl SliceIndex for usize` to use to fix that, so it no longer needs to do a bunch of lines of pointer math and instead just gets the obvious single statement.  (This is *not* used for the range versions, since `slice[i..]` and `slice[..k]` can't use the mir Slice projection as they're using fenceposts, not indices.)

I originally tried to do this with some kind of GVN pattern, but realized that I'm pretty sure it's not legal to optimize `BinOp::Offset` to `PlaceElem::Index` without an extremely complicated condition.  Basically, the problem is that the `Index` projection on a dereferenced slice pointer *cares about the metadata*, since it's UB to `PlaceElem::Index` outside the range described by the metadata.  But then you cast the fat pointer to a thin pointer then offset it, that *ignores* the slice length metadata, so it's possible to write things that are legal with `Offset` but would be UB if translated in the obvious way to `Index`.  Checking (or even determining) the necessary conditions for that would be complicated and error-prone, whereas this intrinsic-based approach is quite straight-forward.

Zero backend changes, because it just lowers to MIR, so it's already supported naturally by CTFE/Miri/cg_llvm/cg_clif.
2025-05-31 21:38:21 +00:00
Jubilee
3846f2f08f
Rollup merge of #141494 - dianqk:match-br-non-int, r=wesleywiser
mir-opt: Do not transform non-int type in match_branches

Fixes #141378.

r? mir-opt
2025-05-30 13:52:26 -07:00
Scott McMurray
4668124cc7 slice.get(i) should use a slice projection in MIR, like slice[i] does 2025-05-30 12:04:41 -07:00
Trevor Gross
e0278ed5af
Rollup merge of #141551 - compiler-errors:hir-lints, r=BoxyUwU
Make two transmute-related MIR lints into HIR lint

Make `PTR_TO_INTEGER_TRANSMUTE_IN_CONSTS` (rust-lang/rust#130540) and `UNNECESSARY_TRANSMUTES` (rust-lang/rust#136083) into "normal" HIR-based lints.

Funny enough this came up in the review of the latter (https://github.com/rust-lang/rust/pull/136083#issuecomment-2614301413), but I guess it just was overlooked.

But anywyas, there's no reason for these to be MIR lints; in fact, it makes the suggestions for them a bit more complicated than necessary.

Note that there's probably a few more simplifications and improvements to be done here. Follow-ups can be done in a separate PR, especially if they're about the messaging and suggestions themselves, which I didn't write.
2025-05-27 20:28:32 -04:00
Trevor Gross
ee4efa1f86
Rollup merge of #141252 - dianqk:gvn-repeat-index, r=saethlin
gvn: bail out unavoidable non-ssa locals in repeat

Fixes #141251.

We cannot transform `*elem` to `array[idx1]` in the following code, as `idx1` has already been modified.

```rust
    mir! {
        let array;
        let elem;
        {
            array = [*val; 5];
            elem = &array[idx1];
            idx1 = idx2;
            RET = *elem;
            Return()
        }
    }
```

Perhaps I could transform it to `array[0]`, but I prefer the conservative approach.

r? mir-opt
2025-05-27 20:28:31 -04:00
Zalathar
3f526eeec4 coverage: Revert "unused local file IDs" due to empty function names
This reverts commit 3b22c21dd8, reversing
changes made to 5f292eea6d.
2025-05-27 23:33:29 +10:00
许杰友 Jieyou Xu (Joe)
afb57cadda
Rollup merge of #141513 - nia-e:allocbytes-extend, r=RalfJung
interpret: add allocation parameters to `AllocBytes`

Necessary for a better implementation of [rust-lang/miri#4343](https://github.com/rust-lang/miri/pull/4343). Also included here is the code from that PR, adapted to this new interface for the sake of example and so that CI can run on them; the Miri changes can be reverted and merged separately, though.

r? `@RalfJung`
2025-05-27 01:29:20 +08:00
许杰友 Jieyou Xu (Joe)
0e710d0883
Rollup merge of #141431 - compiler-errors:open-drop, r=oli-obk
Emit dummy open drop for unsafe binder

Fixes rust-lang/rust#141394

We can't taint the body in wfcheck when we have a `T: Copy` bound failure, so we end up binding MIR here. Emit a dummy open drop so that drop elaboration doesn't fail.

r? oli-obk
2025-05-27 01:29:18 +08:00
dianqk
457f8ba447
mir-opt: Do not transform non-int type in match_branches 2025-05-26 18:15:54 +08:00
Nia Espera
e388a3e405
extend allocbytes with associated type 2025-05-26 00:15:16 +02:00
Michael Goulet
295a8d56f5 Make UNNECESSARY_TRANSMUTES into a HIR lint 2025-05-25 15:57:48 +00:00
Michael Goulet
5370c5753f Make PTR_TO_INTEGER_TRANSMUTE_IN_CONSTS into a HIR lint 2025-05-25 15:57:48 +00:00
Matthias Krüger
f7a11798e8
Rollup merge of #140967 - azhogin:azhogin/async-drop-poll-shim-for-error-dropee-fix, r=oli-obk
Async drop poll shim for error dropee generates noop body

Fixes https://github.com/rust-lang/rust/issues/140930.

When dropee type for async drop poll shim is `ty::Error(_)`, the generated poll function will be noop body. To avoid ICE in `elaborate_drop`.
2025-05-23 13:34:17 +02:00
Michael Goulet
11392f4978 Emit dummy open drop for unsafe binder 2025-05-23 10:43:21 +00:00
Andrew Zhogin
cb8fdb4d80 Async drop poll shim for error dropee generates noop body (fixes #140930) 2025-05-23 03:40:27 +07:00
bendn
8373bb17d6
use uX::from instead of _ as uX in non - const contexts 2025-05-22 17:08:32 +07:00
Matthias Krüger
cc87ae85dd
Rollup merge of #140431 - bend-n:dont_handle_bool_transmute, r=Nadrieril
dont handle bool transmute

removes `transmute(u8) -> bool` suggestion due to ambiguity, leave it for clippy

elaboration on ambiguity in question:
`transmute::<u8, bool>(x)` will codegen to an `assume(u8 < 2)`;
`_ == 1` or `_ != 0` or `_ % 2 == 0` would remove that assumption
`match _ { x @ (0 | 1) => x == 1, _ => std::hint::unreachable_unchecked() }` is very verbose

`@rustbot` label L-unnecessary_transmutes
2025-05-22 07:19:01 +02:00
Matthias Krüger
1461ca3f39
Rollup merge of #141328 - azhogin:azhogin/async-drop-ice-for-empty-impl-fix, r=oli-obk
When AsyncDrop impl is empty, sync drop generated in elaborator

Fixes #140974.
2025-05-21 15:38:10 +02:00
Andrew Zhogin
7c38b6fd28 Async drop fix for 'broken mir in AsyncDropGlue, place has deref as a later projection' (#140975) 2025-05-21 18:29:13 +07:00
Andrew Zhogin
dc794f18a4 When AsyncDrop impl is empty, sync drop generated in elaborator (Fixes #140974) 2025-05-21 18:18:27 +07:00
dianqk
be5d6c5425
gvn: bail out unavoidable non-ssa locals in repeat
We cannot transform `*elem` to `array[idx1]` in the following code,
as `idx1` has already been modified.

```rust
    mir! {
        let array;
        let elem;
        {
            array = [*val; 5];
            elem = &array[idx1];
            idx1 = idx2;
            RET = *elem;
            Return()
        }
    }
```
2025-05-19 21:35:49 +08:00
Stuart Cook
3b22c21dd8
Rollup merge of #140847 - Zalathar:unused-local-file, r=SparrowLii
coverage: Detect unused local file IDs to avoid an LLVM assertion

Each function's coverage metadata contains a *local file table* that maps local file IDs (used by the function's mapping regions) to global file IDs (shared by all functions in the same CGU).

LLVM requires all local file IDs to have at least one mapping region, and has an assertion that will fail if it detects a local file ID with no regions. To make sure that assertion doesn't fire, we need to detect and skip functions whose metadata would trigger it.

(This can't actually happen yet, because currently all of a function's spans must belong to the same file and expansion. But this will be an important edge case when adding expansion region support.)
2025-05-19 21:10:42 +10:00
Stuart Cook
599b08ada8
Rollup merge of #140874 - mejrs:rads, r=WaffleLapkin
make `rustc_attr_parsing` less dominant in the rustc crate graph

It has/had a glob re-export of `rustc_attr_data_structures`, which is a crate much lower in the graph, and a lot of crates were using it *just* (or *mostly*) for that re-export, while they can rely on `rustc_attr_data_structures` directly.

Previous graph:
![graph_1](https://github.com/user-attachments/assets/f4a5f13c-4222-4903-b56d-28c83511fcbd)

Graph with this PR:
![graph_2](https://github.com/user-attachments/assets/1e053d9c-75cc-402b-84df-86229c98277a)

The first commit keeps the re-export, and just changes the dependency if possible. The second commit is the "breaking change" which removes the re-export, and "explicitly" adds the `rustc_attr_data_structures` dependency where needed. It also switches over some src/tools/*.

The second commit is actually a lot more involved than I expected. Please let me know if it's a better idea to back it out and just keep the first commit.
2025-05-19 13:24:54 +10:00
León Orell Valerian Liehr
50b20b73da
Rollup merge of #141218 - dianqk:gvn-overlapping, r=oli-obk
gvn: avoid creating overlapping assignments

Quick fix #141038, as I couldn't find a way to avoid in-place modification. I'm considering handling all `ravlue` modifications within the `visit_statement` function.

r? mir-opt
2025-05-18 18:44:14 +02:00
León Orell Valerian Liehr
3af0c43f06
Rollup merge of #140926 - azhogin:azhogin/async-drop-coroutine-layout-returns-layout-error, r=oli-obk
Return value of coroutine_layout fn changed to Result with LayoutError

Continue of https://github.com/rust-lang/rust/pull/140902:
`coroutine_layout` fn is now returns `Result` with `LayoutError` to have consistent error with `layout_of_uncached`.
`async_drop_coroutine_layout` fn is now return `LayoutError::TooGeneric` in case of not-fully-specialized `async_drop_in_place<T>::{closure}` coroutine.
2025-05-18 18:44:12 +02:00
dianqk
d2e5a3d131
gvn: avoid creating overlapping assignments 2025-05-18 18:42:00 +08:00
Matthias Krüger
8e30998c18
Rollup merge of #141051 - compiler-errors:less-erase, r=nnethercote
Remove some unnecessary erases

Some nits I pulled out of https://github.com/rust-lang/rust/pull/140814.
2025-05-17 10:33:10 +02:00
bendn
de8e8641ae
dont handle bool transmute 2025-05-15 20:20:39 +07:00
Pietro Albini
2ce08ca5d6
update cfg(bootstrap) 2025-05-12 15:33:37 +02:00
Andrew Zhogin
ba80d820e5 Return value of coroutine_layout fn changed to Result with LayoutError 2025-05-11 23:53:13 +07:00
mejrs
684b7b70f4 don't depend on rustc_attr_parsing if rustc_data_structures will do 2025-05-09 23:16:55 +02:00
Zalathar
339556eb02 coverage: Enlarge empty spans during MIR instrumentation, not codegen
This allows us to assume that coverage spans will only be discarded during
codegen in very unusual situations.
2025-05-10 00:24:01 +10:00
Michael Goulet
292ecd5242 Remove some unnecessary erases 2025-05-08 16:20:57 +00:00
bendn
3b4c4938c5
add signed integers to unnecessary_lints to ensure feature parity with clippy 2025-05-08 23:16:49 +07:00
Guillaume Gomez
82c99c41c0
Rollup merge of #140234 - nnethercote:separate-Analysis-and-Results, r=davidtwco
Separate dataflow analysis and results

`Analysis` gets put into `Results` with `EntryStates`, by `iterate_to_fixpoint`. This has two problems:
- `Results` is passed various places where only `Analysis` is needed.
- `EntryStates` is passed around mutably everywhere even though it is immutable.

This commit mostly separates `Analysis` from `Results` and fixes these two problems.

r? `@davidtwco`
2025-05-07 18:19:04 +02:00
Zalathar
77a7ae4e9f coverage: Handle hole spans without dividing spans into buckets
Because we no longer merge non-adjacent spans, there is no need to use buckets
to prevent merging across hole spans.
2025-05-06 20:42:40 +10:00
Zalathar
4d5a1acebf coverage: Only merge adjacent coverage spans
This also removes some manipulation of the function signature span that only
made sense in the context of merging non-adjacent spans.
2025-05-06 20:42:25 +10:00
Guillaume Gomez
ab7623e93c
Rollup merge of #140115 - dianqk:gvn-matchbr, r=oli-obk
mir-opt: execute MatchBranchSimplification after GVN

This can provide more opportunities for MatchBranchSimplification.

Currently, rustc does not optimize the following code into a single statement at mir-opt, and this PR fixes the first case.

```rust
pub fn match1(c: bool, v1: i32, v2: i32) -> i32 {
    if c { v1 - v2 } else { v1 - v2 }
}

pub fn match2(c: bool, v1: i32) -> i32 {
    if c { v1 - 1 } else { v1 - 1 }
}
```

https://rust.godbolt.org/z/Y8xPMjrfM

r? mir-opt
2025-05-05 21:32:30 +02:00
Guillaume Gomez
677a5aca7f
Rollup merge of #140080 - dianqk:one-mirpatch, r=oli-obk
mir-opt: Use one MirPatch in MatchBranchSimplification
2025-05-05 21:32:30 +02:00
bors
243c5a35e1 Auto merge of #140453 - Zoxc:next-disambiguator, r=oli-obk
Remove global `next_disambiguator` state and handle it with a `DisambiguatorState` type

This removes `Definitions.next_disambiguator` as it doesn't guarantee deterministic def paths when `create_def` is called in parallel. Instead a new `DisambiguatorState` type is passed as a mutable reference to `create_def` to help create unique def paths. `create_def` calls with distinct  `DisambiguatorState` instances must ensure that that the def paths are unique without its help.

Anon associated types did rely on this global state for uniqueness and are changed to use (method they're defined in + their position in the method return type) as the `DefPathData` to ensure uniqueness. This also means that the method they're defined in appears in error messages, which is nicer.

`DefPathData::NestedStatic` is added to use for nested data inside statics instead of reusing `DefPathData::AnonConst` to avoid conflicts with those.

cc `@oli-obk`
2025-05-05 11:50:43 +00:00
John Kåre Alsaker
9f8c6d57bf Add comment on creation of SyntheticCoroutineBody 2025-05-04 19:14:35 +02:00
bors
d7df5bdf29 Auto merge of #140464 - oli-obk:successors-mut-perf, r=petrochenkov
Use a closure instead of three chained iterators

Fixes the perf regression from #123948

That PR had chained a third option to the iterator which apparently didn't optimize well
2025-05-03 10:43:38 +00:00
Matthias Krüger
3648f3aa77
Rollup merge of #140458 - azhogin:azhogin/async-drop-fix-dropped-tuple-ice, r=oli-obk
Fix for async drop ice with partly dropped tuple

Fixes https://github.com/rust-lang/rust/issues/140427.
Problem was with block data access with block id from new added blocks in patch.
2025-04-30 22:36:39 +02:00
bors
0fbb922e53 Auto merge of #140023 - cjgillot:arena-try-alloc, r=BoxyUwU
Introduce Arena::try_alloc_from_iter.

`alloc_from_iter` already collects the iterator for reentrancy. So adding an early exit for a fallible iterator integrates naturally into the code. This avoids the other solution to allocate and dump the allocation.
2025-04-29 21:06:15 +00:00
Oli Scherer
9193dfe435 Use a closure instead of three chained iterators 2025-04-29 14:58:21 +00:00