]> git.lizzy.rs Git - rust.git/blob - src/test/ui/asm/type-check-2.rs
Add tests for global_asm!
[rust.git] / src / test / ui / asm / type-check-2.rs
1 // only-x86_64
2
3 #![feature(asm, global_asm, repr_simd, never_type)]
4
5 #[repr(simd)]
6 struct SimdNonCopy(f32, f32, f32, f32);
7
8 fn main() {
9     unsafe {
10         // Inputs must be initialized
11
12         let x: u64;
13         asm!("{}", in(reg) x);
14         //~^ ERROR use of possibly-uninitialized variable: `x`
15         let mut y: u64;
16         asm!("{}", inout(reg) y);
17         //~^ ERROR use of possibly-uninitialized variable: `y`
18         let _ = y;
19
20         // Outputs require mutable places
21
22         let v: Vec<u64> = vec![0, 1, 2];
23         asm!("{}", in(reg) v[0]);
24         asm!("{}", out(reg) v[0]);
25         //~^ ERROR cannot borrow `v` as mutable, as it is not declared as mutable
26         asm!("{}", inout(reg) v[0]);
27         //~^ ERROR cannot borrow `v` as mutable, as it is not declared as mutable
28
29         // Const operands must be integer or floats, and must be constants.
30
31         asm!("{}", const 0);
32         asm!("{}", const 0i32);
33         asm!("{}", const 0f32);
34         asm!("{}", const 0 as *mut u8);
35         //~^ ERROR asm `const` arguments must be integer or floating-point values
36
37         // This currently causes an ICE: https://github.com/rust-lang/rust/issues/81857
38         // asm!("{}", const &0);
39         // ERROR asm `const` arguments must be integer or floating-point values
40
41         // Sym operands must point to a function or static
42
43         const C: i32 = 0;
44         static S: i32 = 0;
45         asm!("{}", sym S);
46         asm!("{}", sym main);
47         asm!("{}", sym C);
48         //~^ ERROR asm `sym` operand must point to a fn or static
49         asm!("{}", sym x);
50         //~^ ERROR asm `sym` operand must point to a fn or static
51
52         // Register operands must be Copy
53
54         asm!("{}", in(xmm_reg) SimdNonCopy(0.0, 0.0, 0.0, 0.0));
55         //~^ ERROR arguments for inline assembly must be copyable
56
57         // Register operands must be integers, floats, SIMD vectors, pointers or
58         // function pointers.
59
60         asm!("{}", in(reg) 0i64);
61         asm!("{}", in(reg) 0f64);
62         asm!("{}", in(xmm_reg) std::arch::x86_64::_mm_setzero_ps());
63         asm!("{}", in(reg) 0 as *const u8);
64         asm!("{}", in(reg) 0 as *mut u8);
65         asm!("{}", in(reg) main as fn());
66         asm!("{}", in(reg) |x: i32| x);
67         //~^ ERROR cannot use value of type
68         asm!("{}", in(reg) vec![0]);
69         //~^ ERROR cannot use value of type `Vec<i32>` for inline assembly
70         asm!("{}", in(reg) (1, 2, 3));
71         //~^ ERROR cannot use value of type `(i32, i32, i32)` for inline assembly
72         asm!("{}", in(reg) [1, 2, 3]);
73         //~^ ERROR cannot use value of type `[i32; 3]` for inline assembly
74
75         // Register inputs (but not outputs) allow references and function types
76
77         let mut f = main;
78         let mut r = &mut 0;
79         asm!("{}", in(reg) f);
80         asm!("{}", inout(reg) f);
81         //~^ ERROR cannot use value of type `fn() {main}` for inline assembly
82         asm!("{}", in(reg) r);
83         asm!("{}", inout(reg) r);
84         //~^ ERROR cannot use value of type `&mut i32` for inline assembly
85         let _ = (f, r);
86
87         // Type checks ignore never type
88
89         let u: ! = unreachable!();
90         asm!("{}", in(reg) u);
91     }
92 }
93
94 // Const operands must be integer or floats, and must be constants.
95
96 global_asm!("{}", const 0);
97 global_asm!("{}", const 0i32);
98 global_asm!("{}", const 0f32);
99 global_asm!("{}", const 0 as *mut u8);
100 //~^ ERROR asm `const` arguments must be integer or floating-point values