Skip to content

Commit 5f856d4

Browse files
committed
asm cfg: add additional tests
1 parent 097220d commit 5f856d4

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

tests/ui/asm/cfg-parse-error.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::arch::asm;
55

66
fn main() {
77
unsafe {
8-
// Templates are not allowed after operands (even if the operands are configured out).
98
asm!(
109
"",
1110
#[cfg(false)]
@@ -28,19 +27,31 @@ fn main() {
2827
//~^ ERROR expected one of `#`, `clobber_abi`, `const`, `in`, `inlateout`, `inout`, `label`, `lateout`, `options`, `out`, or `sym`, found `""`
2928
);
3029

31-
// This is currently accepted because `const { 5 }` parses as an expression.
3230
asm!(
33-
#[cfg(false)]
31+
#[cfg_attr(true, cfg(false))]
3432
const {
3533
5
3634
},
3735
"",
3836
);
39-
// This is not accepted because `a = out(reg) x` is not a valid expresion.
37+
38+
// This is not accepted because `a = out(reg) x` is not a valid expression.
4039
asm!(
4140
#[cfg(false)]
4241
a = out(reg) x, //~ ERROR expected token: `,`
4342
"",
4443
);
44+
45+
// For now, any non-cfg attributes are rejected
46+
asm!(
47+
#[rustfmt::skip] //~ ERROR this attribute is not supported on assembly
48+
"",
49+
);
50+
51+
// For now, any non-cfg attributes are rejected
52+
asm!(
53+
#![rustfmt::skip] //~ ERROR an inner attribute is not permitted in this context
54+
"",
55+
);
4556
}
4657
}

tests/ui/asm/cfg-parse-error.stderr

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
11
error: expected one of `#`, `clobber_abi`, `const`, `in`, `inlateout`, `inout`, `label`, `lateout`, `options`, `out`, or `sym`, found `""`
2-
--> $DIR/cfg-parse-error.rs:17:13
2+
--> $DIR/cfg-parse-error.rs:16:13
33
|
44
LL | a = out(reg) x,
55
| - expected one of 11 possible tokens
66
LL | "",
77
| ^^ unexpected token
88

99
error: expected one of `#`, `clobber_abi`, `const`, `in`, `inlateout`, `inout`, `label`, `lateout`, `options`, `out`, or `sym`, found `""`
10-
--> $DIR/cfg-parse-error.rs:27:13
10+
--> $DIR/cfg-parse-error.rs:26:13
1111
|
1212
LL | },
1313
| - expected one of 11 possible tokens
1414
LL | "",
1515
| ^^ unexpected token
1616

1717
error: expected token: `,`
18-
--> $DIR/cfg-parse-error.rs:42:26
18+
--> $DIR/cfg-parse-error.rs:41:26
1919
|
2020
LL | a = out(reg) x,
2121
| ^ expected `,`
2222

23-
error: aborting due to 3 previous errors
23+
error: this attribute is not supported on assembly
24+
--> $DIR/cfg-parse-error.rs:47:13
25+
|
26+
LL | #[rustfmt::skip]
27+
| ^^^^^^^^^^^^^^^^
28+
29+
error: an inner attribute is not permitted in this context
30+
--> $DIR/cfg-parse-error.rs:53:13
31+
|
32+
LL | #![rustfmt::skip]
33+
| ^^^^^^^^^^^^^^^^^
34+
|
35+
= note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files
36+
= note: outer attributes, like `#[test]`, annotate the item following them
37+
38+
error: aborting due to 5 previous errors
2439

tests/ui/asm/cfg.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,28 @@ extern "C" fn first_template() -> u64 {
7878
)
7979
}
8080

81+
#[unsafe(naked)]
82+
extern "C" fn true_and_false() -> u64 {
83+
naked_asm!(
84+
"mov rax, 5",
85+
#[cfg(true)]
86+
#[cfg(false)]
87+
"mov rax, 10",
88+
"ret",
89+
)
90+
}
91+
92+
#[unsafe(naked)]
93+
extern "C" fn false_and_true() -> u64 {
94+
naked_asm!(
95+
"mov rax, 5",
96+
#[cfg(false)]
97+
#[cfg(true)]
98+
"mov rax, 10",
99+
"ret",
100+
)
101+
}
102+
81103
pub fn main() {
82104
std::cfg_match! {
83105
reva => {
@@ -97,4 +119,7 @@ pub fn main() {
97119
}
98120
options();
99121
clobber_abi();
122+
123+
assert_eq!(true_and_false(), 5);
124+
assert_eq!(false_and_true(), 5);
100125
}

0 commit comments

Comments
 (0)