File: ctfe-id-unlimited.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (35 lines) | stat: -rw-r--r-- 1,191 bytes parent folder | download | duplicates (3)
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
35
//@ 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),
            //[return]~^ error: evaluation of constant value failed
        }
    }

    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);
// Assert to make absolutely sure the computation actually happens
const ASSERT: () = assert!(ORIGINAL == ID_ED);

fn main() {
    let _ = ASSERT;
}