about summary refs log tree commit diff
path: root/tests/ui/target-feature/struct-target-features.rs
blob: feb479b6dc816bd3ffa68d813cacb0f3cac14ea3 (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//@ only-x86_64
#![feature(struct_target_features)]
//~^ WARNING the feature `struct_target_features` is incomplete and may not be safe to use and/or cause compiler crashes
#![feature(target_feature_11)]

use std::arch::x86_64::*;

#[target_feature(enable = "avx")]
//~^ ERROR attribute should be applied to a function definition or unit struct
struct Invalid(u32);

#[target_feature(enable = "avx")]
struct Avx {}

#[target_feature(enable = "sse")]
struct Sse();

#[target_feature(enable = "avx")]
fn avx() {}

trait TFAssociatedType {
    type Assoc;
}

impl TFAssociatedType for () {
    type Assoc = Avx;
}

fn avx_self(_: <() as TFAssociatedType>::Assoc) {
    avx();
}

fn avx_avx(_: Avx) {
    avx();
}

extern "C" fn bad_fun(_: Avx) {}
//~^ ERROR cannot use a struct with target features in a function with non-Rust ABI

#[inline(always)]
//~^ ERROR cannot use `#[inline(always)]` with `#[target_feature]`
fn inline_fun(_: Avx) {}
//~^ ERROR cannot use a struct with target features in a #[inline(always)] function

trait Simd {
    fn do_something(&self);
}

impl Simd for Avx {
    fn do_something(&self) {
        unsafe {
            println!("{:?}", _mm256_setzero_ps());
        }
    }
}

impl Simd for Sse {
    fn do_something(&self) {
        unsafe {
            println!("{:?}", _mm_setzero_ps());
        }
    }
}

struct WithAvx {
    #[allow(dead_code)]
    avx: Avx,
}

impl Simd for WithAvx {
    fn do_something(&self) {
        unsafe {
            println!("{:?}", _mm256_setzero_ps());
        }
    }
}

#[inline(never)]
fn dosomething<S: Simd>(simd: &S) {
    simd.do_something();
}

fn avxfn(_: &Avx) {}

fn main() {
    Avx {};
    //~^ ERROR initializing type with `target_feature` attr is unsafe and requires unsafe function or block [E0133]

    if is_x86_feature_detected!("avx") {
        let avx = unsafe { Avx {} };
        avxfn(&avx);
        dosomething(&avx);
        dosomething(&WithAvx { avx });
    }
    if is_x86_feature_detected!("sse") {
        dosomething(&unsafe { Sse {} })
    }
}