|
| 1 | +#![feature(const_fn, const_let)] |
| 2 | +#![allow(unused_must_use)] |
| 3 | + |
| 4 | +// Try to make CTFE actually do a lot of computation, without producing a big result. |
| 5 | +// And without support for loops. |
| 6 | + |
| 7 | +macro_rules! const_repeat { |
| 8 | + // Base case: Use 16 at the end to avoid function calls at the leaves as much as possibele. |
| 9 | + ([16] $e: expr, $T: ty) => {{ |
| 10 | + $e; $e; $e; $e; |
| 11 | + $e; $e; $e; $e; |
| 12 | + $e; $e; $e; $e; |
| 13 | + $e; $e; $e; $e |
| 14 | + }}; |
| 15 | + ([1] $e: expr, $T: ty) => {{ |
| 16 | + $e |
| 17 | + }}; |
| 18 | + // Recursive case: Take a 16 |
| 19 | + ([16 $($n: tt)*] $e: expr, $T: ty) => {{ |
| 20 | + const fn e() -> $T { const_repeat!([$($n)*] $e, $T) } |
| 21 | + e(); e(); e(); e(); |
| 22 | + e(); e(); e(); e(); |
| 23 | + e(); e(); e(); e(); |
| 24 | + e(); e(); e(); e() |
| 25 | + }}; |
| 26 | + // Recursive case: Take a 4 |
| 27 | + ([4 $($n: tt)*] $e: expr, $T: ty) => {{ |
| 28 | + const fn e() -> $T { const_repeat!([$($n)*] $e, $T) } |
| 29 | + e(); e(); e(); e() |
| 30 | + }}; |
| 31 | + // Recursive case: Take a 2 |
| 32 | + ([2 $($n: tt)*] $e: expr, $T: ty) => {{ |
| 33 | + const fn e() -> $T { const_repeat!([$($n)*] $e, $T) } |
| 34 | + e(); e() |
| 35 | + }}; |
| 36 | + // |
| 37 | + ($e: expr, $T: ty) => (const_repeat!([16 16 16 16 16] $e, $T)); |
| 38 | +} |
| 39 | +macro_rules! expensive_static { |
| 40 | + ($name: ident : $T: ty = $e : expr) => |
| 41 | + (pub static $name : $T = const_repeat!($e, $T);) |
| 42 | +} |
| 43 | + |
| 44 | +pub trait Trait: Sync {} |
| 45 | +impl Trait for u32 {} |
| 46 | +const fn nop<T>(t: T) -> T { t } |
| 47 | +const fn inc(i: i32) -> i32 { i + 1 } |
| 48 | + |
| 49 | +expensive_static!(RELOCATIONS : &'static str = "hello"); |
| 50 | +expensive_static!(FIELDS: &'static i32 = &("bar", 42, "foo", 3.14).1); |
| 51 | +expensive_static!(CHECKED_INDEX: u8 = b"foomp"[3]); |
| 52 | +expensive_static!(UNSIZING: &'static [u8] = b"foo"); |
| 53 | +expensive_static!(UNSIZE_TRAIT: &'static Trait = &42u32); |
| 54 | +expensive_static!(CHAIN: usize = 42i32 as u8 as u64 as i8 as isize as usize); |
| 55 | +expensive_static!(OPS: i32 = ((((10 >> 1) + 3) * 7) / 2 - 12) << 4); |
| 56 | +expensive_static!(FORCE_ALLOC: i32 = *****(&&&&&5)); |
| 57 | +expensive_static!(CONST_FN_SIMPLE: i32 = inc(42)); |
0 commit comments