@@ -2791,31 +2791,35 @@ where
27912791/// A macro to make it easier to invoke const_eval_select. Use as follows:
27922792/// ```rust,ignore (just a macro example)
27932793/// const_eval_select!(
2794- /// #[inline]
2795- /// (arg1: i32 = some_expr, arg2: T = other_expr) -> U:
2796- /// if const {
2794+ /// @capture { arg1: i32 = some_expr, arg2: T = other_expr } -> U:
2795+ /// if const #[attributes_for_const_arm] {
27972796/// // Compile-time code goes here.
2798- /// } else {
2797+ /// } else #[attributes_for_runtime_arm] {
27992798/// // Run-time code goes here.
28002799/// }
28012800/// )
28022801/// ```
2802+ /// The `@capture` block declares which surrounding variables / expressions can be
2803+ /// used inside the `if const`.
2804+ /// Note that the two arms of this `if` really each become their own function, which is why the
2805+ /// macro supports setting attributes for those functions. The runtime function is always
2806+ /// markes as `#[inline]`.
2807+ ///
2808+ /// See [`const_eval_select()`] for the rules and requirements around that intrinsic.
28032809pub ( crate ) macro const_eval_select {
28042810 (
2805- $( #[ $attr: meta] ) *
2806- ( $( $arg: ident : $ty: ty = $val: expr) , * $( , ) ?) $( -> $ret: ty ) ?:
2811+ @capture { $( $arg: ident : $ty: ty = $val: expr) , * $( , ) ? } $( -> $ret: ty ) ? :
28072812 if const
28082813 $( #[ $compiletime_attr: meta] ) * $compiletime: block
28092814 else
28102815 $( #[ $runtime_attr: meta] ) * $runtime: block
28112816 ) => { {
2812- $( #[ $attr] ) *
28132817 $( #[ $runtime_attr] ) *
2818+ #[ inline]
28142819 fn runtime( $( $arg: $ty) , * ) $( -> $ret ) ? {
28152820 $runtime
28162821 }
28172822
2818- $( #[ $attr] ) *
28192823 $( #[ $compiletime_attr] ) *
28202824 const fn compiletime( $( $arg: $ty) , * ) $( -> $ret ) ? {
28212825 // Don't warn if one of the arguments is unused.
@@ -2829,16 +2833,14 @@ pub(crate) macro const_eval_select {
28292833 // We support leaving away the `val` expressions for *all* arguments
28302834 // (but not for *some* arguments, that's too tricky).
28312835 (
2832- $( #[ $attr: meta] ) *
2833- ( $( $arg: ident : $ty: ty) , * $( , ) ?) -> $ret: ty:
2836+ @capture { $( $arg: ident : $ty: ty) , * $( , ) ? } $( -> $ret: ty ) ? :
28342837 if const
28352838 $( #[ $compiletime_attr: meta] ) * $compiletime: block
28362839 else
28372840 $( #[ $runtime_attr: meta] ) * $runtime: block
28382841 ) => {
28392842 $crate:: intrinsics:: const_eval_select!(
2840- $( #[ $attr] ) *
2841- ( $( $arg : $ty = $arg) , * ) -> $ret:
2843+ @capture { $( $arg : $ty = $arg) , * } $( -> $ret) ? :
28422844 if const
28432845 $( #[ $compiletime_attr] ) * $compiletime
28442846 else
@@ -3794,7 +3796,7 @@ pub(crate) const fn miri_promise_symbolic_alignment(ptr: *const (), align: usize
37943796 }
37953797
37963798 const_eval_select ! (
3797- ( ptr: * const ( ) , align: usize ) :
3799+ @capture { ptr: * const ( ) , align: usize } :
37983800 if const {
37993801 // Do nothing.
38003802 } else {
0 commit comments