rust/compiler/rustc_middle/src
Jubilee f542909d1c
Rollup merge of #138780 - trifectatechfoundation:loop_match_attr, r=oli-obk,traviscross
Add `#[loop_match]` for improved DFA codegen

tracking issue: https://github.com/rust-lang/rust/issues/132306
project goal: https://github.com/rust-lang/rust-project-goals/issues/258

This PR adds the `#[loop_match]` attribute, which aims to improve code generation for state machines. For some (very exciting) benchmarks, see https://github.com/rust-lang/rust-project-goals/issues/258#issuecomment-2732965199

Currently, a very restricted syntax pattern is accepted. We'd like to get feedback and merge this now before we go too far in a direction that others have concerns with.

## current state

We accept code that looks like this

```rust
#[loop_match]
loop {
    state = 'blk: {
        match state {
            State::A => {
                #[const_continue]
                break 'blk State::B
            }
            State::B => { /* ... */ }
            /* ... */
        }
    }
}
```

- a loop should have the same semantics with and without `#[loop_match]`: normal `continue` and `break` continue to work
- `#[const_continue]` is only allowed in loops annotated with `#[loop_match]`
- the loop body needs to have this particular shape (a single assignment to the match scrutinee, with the body a labelled block containing just a match)

## future work

- perform const evaluation on the `break` value
- support more state/scrutinee types

## maybe future work

- allow `continue 'label value` syntax, which `#[const_continue]` could then use.
- allow the match to be on an arbitrary expression (e.g. `State::Initial`)
- attempt to also optimize `break`/`continue` expressions that are not marked with `#[const_continue]`

r? ``@traviscross``
2025-06-24 19:45:30 -07:00
..
dep_graph Ensure query keys are printed with reduced queries 2025-06-03 20:56:52 +00:00
hir fix 142891 2025-06-23 12:20:02 +02:00
hooks Move naked fn checks to hir_typeck 2025-05-30 16:08:44 +00:00
infer yeet CanonicalVarInfo 2025-05-23 12:10:53 +00:00
middle [Arm64EC] Only decorate functions with # 2025-06-23 12:38:35 -07:00
mir Unimplement unsized_locals 2025-06-13 01:16:36 +02:00
query Rollup merge of #142619 - klensy:or_fun_call, r=nnethercote 2025-06-18 18:06:51 +02:00
thir Add #[loop_match] for improved DFA codegen 2025-06-23 20:43:04 +02:00
traits Rollup merge of #142458 - oli-obk:dyn-incompat, r=compiler-errors 2025-06-22 17:35:32 +02:00
ty Rollup merge of #142801 - compiler-errors:gen-blocks, r=oli-obk 2025-06-21 10:53:28 +02:00
util Remove attribute #[rustc_error] 2025-03-30 01:32:21 +03:00
arena.rs Initial support for dynamically linked crates 2025-05-04 22:03:15 +03:00
error.rs Replace some Option<Span> with Span and use DUMMY_SP instead of None 2025-06-05 14:14:59 +00:00
lib.rs Use gen blocks in the compiler instead of from_coroutine 2025-06-20 18:57:52 +00:00
lint.rs decouple "reporting in deps" from future incompatibility reason 2025-06-03 10:49:22 +02:00
macros.rs Fold visit into ty 2025-03-15 06:34:36 +00:00
metadata.rs Re-export more rustc_span::symbol things from rustc_span. 2024-12-18 13:38:53 +11:00
tests.rs Give an item related to issue 27438 a more meaningful name 2024-04-30 22:27:19 +02:00
thir.rs Add #[loop_match] for improved DFA codegen 2025-06-23 20:43:04 +02:00
values.rs Use search_for_cycle_permutation to look for variances_of 2025-04-27 09:38:18 +02:00