@@ -2791,31 +2791,36 @@ where
2791
2791
/// A macro to make it easier to invoke const_eval_select. Use as follows:
2792
2792
/// ```rust,ignore (just a macro example)
2793
2793
/// 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] {
2797
2796
/// // Compile-time code goes here.
2798
- /// } else {
2797
+ /// } else #[attributes_for_runtime_arm] {
2799
2798
/// // Run-time code goes here.
2800
2799
/// }
2801
2800
/// )
2802
2801
/// ```
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.
2803
2809
pub ( crate ) macro const_eval_select {
2804
2810
(
2811
+ @capture { $( $arg: ident : $ty: ty = $val: expr) , * $( , ) ? } $( -> $ret: ty ) ? :
2805
2812
$( #[ $attr: meta] ) *
2806
- ( $( $arg: ident : $ty: ty = $val: expr) , * $( , ) ?) $( -> $ret: ty ) ?:
2807
2813
if const
2808
2814
$( #[ $compiletime_attr: meta] ) * $compiletime: block
2809
2815
else
2810
2816
$( #[ $runtime_attr: meta] ) * $runtime: block
2811
2817
) => { {
2812
- $( #[ $attr] ) *
2813
2818
$( #[ $runtime_attr] ) *
2819
+ #[ inline]
2814
2820
fn runtime( $( $arg: $ty) , * ) $( -> $ret ) ? {
2815
2821
$runtime
2816
2822
}
2817
2823
2818
- $( #[ $attr] ) *
2819
2824
$( #[ $compiletime_attr] ) *
2820
2825
const fn compiletime( $( $arg: $ty) , * ) $( -> $ret ) ? {
2821
2826
// Don't warn if one of the arguments is unused.
@@ -2829,16 +2834,14 @@ pub(crate) macro const_eval_select {
2829
2834
// We support leaving away the `val` expressions for *all* arguments
2830
2835
// (but not for *some* arguments, that's too tricky).
2831
2836
(
2832
- $( #[ $attr: meta] ) *
2833
- ( $( $arg: ident : $ty: ty) , * $( , ) ?) -> $ret: ty:
2837
+ @capture { $( $arg: ident : $ty: ty) , * $( , ) ? } $( -> $ret: ty ) ? :
2834
2838
if const
2835
2839
$( #[ $compiletime_attr: meta] ) * $compiletime: block
2836
2840
else
2837
2841
$( #[ $runtime_attr: meta] ) * $runtime: block
2838
2842
) => {
2839
2843
$crate:: intrinsics:: const_eval_select!(
2840
- $( #[ $attr] ) *
2841
- ( $( $arg : $ty = $arg) , * ) -> $ret:
2844
+ @capture { $( $arg : $ty = $arg) , * } $( -> $ret) ? :
2842
2845
if const
2843
2846
$( #[ $compiletime_attr] ) * $compiletime
2844
2847
else
@@ -3794,7 +3797,7 @@ pub(crate) const fn miri_promise_symbolic_alignment(ptr: *const (), align: usize
3794
3797
}
3795
3798
3796
3799
const_eval_select ! (
3797
- ( ptr: * const ( ) , align: usize ) :
3800
+ @capture { ptr: * const ( ) , align: usize } :
3798
3801
if const {
3799
3802
// Do nothing.
3800
3803
} else {
0 commit comments