File: offset_of.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 (75 lines) | stat: -rw-r--r-- 1,531 bytes parent folder | download | duplicates (4)
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
//@ test-mir-pass: DataflowConstProp
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY

use std::marker::PhantomData;
use std::mem::offset_of;

struct Alpha {
    x: u8,
    y: u16,
    z: Beta,
}

struct Beta(u8, u8);

struct Gamma<T> {
    x: u8,
    y: u16,
    _t: T,
}

#[repr(C)]
struct Delta<T> {
    _phantom: PhantomData<T>,
    x: u8,
    y: u16,
}

// EMIT_MIR offset_of.concrete.DataflowConstProp.diff

// CHECK-LABEL: fn concrete(
fn concrete() {
    // CHECK: debug x => [[x:_.*]];
    // CHECK: debug y => [[y:_.*]];
    // CHECK: debug z0 => [[z0:_.*]];
    // CHECK: debug z1 => [[z1:_.*]];

    // CHECK: [[x]] = const 4_usize
    let x = offset_of!(Alpha, x);

    // CHECK: [[y]] = const 0_usize
    let y = offset_of!(Alpha, y);

    // CHECK: [[z0]] = const 2_usize
    let z0 = offset_of!(Alpha, z.0);

    // CHECK: [[z1]] = const 3_usize
    let z1 = offset_of!(Alpha, z.1);
}

// EMIT_MIR offset_of.generic.DataflowConstProp.diff

// CHECK-LABEL: fn generic(
fn generic<T>() {
    // CHECK: debug gx => [[gx:_.*]];
    // CHECK: debug gy => [[gy:_.*]];
    // CHECK: debug dx => [[dx:_.*]];
    // CHECK: debug dy => [[dy:_.*]];

    // CHECK: [[gx]] = OffsetOf(Gamma<T>, [(0, 0)]);
    let gx = offset_of!(Gamma<T>, x);

    // CHECK: [[gy]] = OffsetOf(Gamma<T>, [(0, 1)]);
    let gy = offset_of!(Gamma<T>, y);

    // CHECK: [[dx]] = const 0_usize
    let dx = offset_of!(Delta<T>, x);

    // CHECK: [[dy]] = const 2_usize
    let dy = offset_of!(Delta<T>, y);
}

fn main() {
    concrete();
    generic::<()>();
}