File: zst_no_llvm_alloc.rs

package info (click to toggle)
rustc 1.89.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 906,624 kB
  • sloc: xml: 158,148; python: 34,888; javascript: 19,595; sh: 19,221; ansic: 13,046; cpp: 7,144; asm: 4,376; makefile: 692; lisp: 174; sql: 15
file content (29 lines) | stat: -rw-r--r-- 1,085 bytes parent folder | download | duplicates (14)
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
//@ run-pass

#[repr(align(4))]
struct Foo;

static FOO: Foo = Foo;

fn main() {
    // There's no stable guarantee that these are true.
    // However, we want them to be true so that our LLVM IR and runtime are a bit faster:
    // a constant address is cheap and doesn't result in relocations in comparison to a "real"
    // global somewhere in the data section.
    let x: &'static () = &();
    assert_eq!(x as *const () as usize, 1);
    let x: &'static Foo = &Foo;
    assert_eq!(x as *const Foo as usize, 4);

    // The exact addresses returned by these library functions are not necessarily stable guarantees
    // but for now we assert that we're still matching.
    #[allow(dangling_pointers_from_temporaries)]
    {
        assert_eq!(<Vec<i32>>::new().as_ptr(), <&[i32]>::default().as_ptr());
        assert_eq!(<Box<[i32]>>::default().as_ptr(), (&[]).as_ptr());
    };

    // statics must have a unique address (see https://github.com/rust-lang/rust/issues/18297, not
    // clear whether this is a stable guarantee)
    assert_ne!(&FOO as *const Foo as usize, 4);
}