@@ -2791,31 +2791,35 @@ 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
(
2805
- $( #[ $attr: meta] ) *
2806
- ( $( $arg: ident : $ty: ty = $val: expr) , * $( , ) ?) $( -> $ret: ty ) ?:
2811
+ @capture { $( $arg: ident : $ty: ty = $val: expr) , * $( , ) ? } $( -> $ret: ty ) ? :
2807
2812
if const
2808
2813
$( #[ $compiletime_attr: meta] ) * $compiletime: block
2809
2814
else
2810
2815
$( #[ $runtime_attr: meta] ) * $runtime: block
2811
2816
) => { {
2812
- $( #[ $attr] ) *
2813
2817
$( #[ $runtime_attr] ) *
2818
+ #[ inline]
2814
2819
fn runtime( $( $arg: $ty) , * ) $( -> $ret ) ? {
2815
2820
$runtime
2816
2821
}
2817
2822
2818
- $( #[ $attr] ) *
2819
2823
$( #[ $compiletime_attr] ) *
2820
2824
const fn compiletime( $( $arg: $ty) , * ) $( -> $ret ) ? {
2821
2825
// Don't warn if one of the arguments is unused.
@@ -2829,16 +2833,14 @@ pub(crate) macro const_eval_select {
2829
2833
// We support leaving away the `val` expressions for *all* arguments
2830
2834
// (but not for *some* arguments, that's too tricky).
2831
2835
(
2832
- $( #[ $attr: meta] ) *
2833
- ( $( $arg: ident : $ty: ty) , * $( , ) ?) -> $ret: ty:
2836
+ @capture { $( $arg: ident : $ty: ty) , * $( , ) ? } $( -> $ret: ty ) ? :
2834
2837
if const
2835
2838
$( #[ $compiletime_attr: meta] ) * $compiletime: block
2836
2839
else
2837
2840
$( #[ $runtime_attr: meta] ) * $runtime: block
2838
2841
) => {
2839
2842
$crate:: intrinsics:: const_eval_select!(
2840
- $( #[ $attr] ) *
2841
- ( $( $arg : $ty = $arg) , * ) -> $ret:
2843
+ @capture { $( $arg : $ty = $arg) , * } $( -> $ret) ? :
2842
2844
if const
2843
2845
$( #[ $compiletime_attr] ) * $compiletime
2844
2846
else
@@ -3794,7 +3796,7 @@ pub(crate) const fn miri_promise_symbolic_alignment(ptr: *const (), align: usize
3794
3796
}
3795
3797
3796
3798
const_eval_select ! (
3797
- ( ptr: * const ( ) , align: usize ) :
3799
+ @capture { ptr: * const ( ) , align: usize } :
3798
3800
if const {
3799
3801
// Do nothing.
3800
3802
} else {
0 commit comments