File: single_use_consts.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 (80 lines) | stat: -rw-r--r-- 2,463 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//@ test-mir-pass: SingleUseConsts
//@ compile-flags: -C debuginfo=full
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY

trait MyTrait {
    const ASSOC_BOOL: bool;
    const ASSOC_INT: i32;
}

// EMIT_MIR single_use_consts.if_const.SingleUseConsts.diff
fn if_const<T: MyTrait>() -> i32 {
    // CHECK-LABEL: fn if_const(
    // CHECK: switchInt(const <T as MyTrait>::ASSOC_BOOL)
    if T::ASSOC_BOOL { 7 } else { 42 }
}

// EMIT_MIR single_use_consts.match_const.SingleUseConsts.diff
fn match_const<T: MyTrait>() -> &'static str {
    // CHECK-LABEL: fn match_const(
    // CHECK: switchInt(const <T as MyTrait>::ASSOC_INT)
    match T::ASSOC_INT {
        7 => "hello",
        42 => "towel",
        _ => "world",
    }
}

// EMIT_MIR single_use_consts.if_const_debug.SingleUseConsts.diff
fn if_const_debug<T: MyTrait>() -> i32 {
    // CHECK-LABEL: fn if_const_debug(
    // CHECK: my_bool => const <T as MyTrait>::ASSOC_BOOL;
    // FIXME: `if` forces a temporary (unlike `match`), so the const isn't direct
    // CHECK: _3 = const <T as MyTrait>::ASSOC_BOOL;
    // CHECK: switchInt(move _3)
    let my_bool = T::ASSOC_BOOL;
    do_whatever();
    if my_bool { 7 } else { 42 }
}

// EMIT_MIR single_use_consts.match_const_debug.SingleUseConsts.diff
fn match_const_debug<T: MyTrait>() -> &'static str {
    // CHECK-LABEL: fn match_const_debug(
    // CHECK: my_int => const <T as MyTrait>::ASSOC_INT;
    // CHECK: switchInt(const <T as MyTrait>::ASSOC_INT)
    let my_int = T::ASSOC_INT;
    do_whatever();
    match my_int {
        7 => "hello",
        42 => "towel",
        _ => "world",
    }
}

// EMIT_MIR single_use_consts.never_used_debug.SingleUseConsts.diff
#[allow(unused_variables)]
fn never_used_debug<T: MyTrait>() {
    // CHECK-LABEL: fn never_used_debug(
    // CHECK: my_int => const <T as MyTrait>::ASSOC_INT;
    // CHECK-NOT: ASSOC_INT
    // CHECK: nop
    // CHECK-NOT: ASSOC_INT
    let my_int = T::ASSOC_INT;
}

// EMIT_MIR single_use_consts.assign_const_to_return.SingleUseConsts.diff
fn assign_const_to_return<T: MyTrait>() -> bool {
    // CHECK-LABEL: fn assign_const_to_return(
    // CHECK: _0 = const <T as MyTrait>::ASSOC_BOOL;
    T::ASSOC_BOOL
}

// EMIT_MIR single_use_consts.keep_parameter.SingleUseConsts.diff
fn keep_parameter<T: MyTrait>(mut other: i32) {
    // CHECK-LABEL: fn keep_parameter(
    // CHECK: _1 = const <T as MyTrait>::ASSOC_INT;
    // CHECK: _0 = const ();
    other = T::ASSOC_INT;
}

fn do_whatever() {}