File: ctfe-id-unlimited.rs

package info (click to toggle)
rustc 1.87.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 925,564 kB
  • sloc: xml: 158,127; python: 36,039; javascript: 19,761; sh: 19,737; cpp: 18,981; ansic: 13,133; asm: 4,376; makefile: 710; perl: 29; lisp: 28; ruby: 19; sql: 11
file content (34 lines) | stat: -rw-r--r-- 1,178 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//@ revisions: become return
//@ [become] run-pass
#![expect(incomplete_features)]
#![feature(explicit_tail_calls)]

// This is an identity function (`|x| x`), but implemented using recursion.
// Each step we increment accumulator and decrement the number.
//
// With normal calls this fails compilation because of the recursion limit,
// but with tail calls/`become` we don't grow the stack/spend recursion limit
// so this should compile.
const fn rec_id(n: u32) -> u32 {
    const fn inner(acc: u32, n: u32) -> u32 {
        match n {
            0 => acc,
            #[cfg(r#become)] _ => become inner(acc + 1, n - 1),
            #[cfg(r#return)] _ => return inner(acc + 1, n - 1),
        }
    }

    inner(0, n)
}

// Some relatively big number that is higher than recursion limit
const ORIGINAL: u32 = 12345;
// Original number, but with identity function applied
// (this is the same, but requires execution of the recursion)
const ID_ED: u32 = rec_id(ORIGINAL); //[return]~ error: evaluation of constant value failed
// Assert to make absolutely sure the computation actually happens
const ASSERT: () = assert!(ORIGINAL == ID_ED);

fn main() {
    let _ = ASSERT;
}