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``
This commit is contained in:
@@ -657,6 +657,19 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
|
||||
EncodeCrossCrate::Yes, min_generic_const_args, experimental!(type_const),
|
||||
),
|
||||
|
||||
// The `#[loop_match]` and `#[const_continue]` attributes are part of the
|
||||
// lang experiment for RFC 3720 tracked in:
|
||||
//
|
||||
// - https://github.com/rust-lang/rust/issues/132306
|
||||
gated!(
|
||||
const_continue, Normal, template!(Word), ErrorFollowing,
|
||||
EncodeCrossCrate::No, loop_match, experimental!(const_continue)
|
||||
),
|
||||
gated!(
|
||||
loop_match, Normal, template!(Word), ErrorFollowing,
|
||||
EncodeCrossCrate::No, loop_match, experimental!(loop_match)
|
||||
),
|
||||
|
||||
// ==========================================================================
|
||||
// Internal attributes: Stability, deprecation, and unsafe:
|
||||
// ==========================================================================
|
||||
|
||||
Reference in New Issue
Block a user