Skip to content

Commit 5cfd71a

Browse files
committed
error on non-cfg attributes on assembly
1 parent c7b63c8 commit 5cfd71a

File tree

5 files changed

+57
-17
lines changed

5 files changed

+57
-17
lines changed

compiler/rustc_builtin_macros/messages.ftl

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
builtin_macros_alloc_error_must_be_fn = alloc_error_handler must be a function
22
builtin_macros_alloc_must_statics = allocators must be statics
33
4+
builtin_macros_asm_attribute_not_supported =
5+
this attribute is not supported on assembly
6+
47
builtin_macros_asm_cfg =
5-
attributes on assembly are unstable
8+
the `#[cfg(/* ... */)]` and `#[cfg_attr(/* ... */)]` attributes on assembly are unstable
69
710
builtin_macros_asm_clobber_abi = clobber_abi
811
builtin_macros_asm_clobber_no_reg = asm with `clobber_abi` must specify explicit registers for outputs

compiler/rustc_builtin_macros/src/asm.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,19 @@ impl AsmAttrVec {
7878
attributes.push(attr);
7979
}
8080

81-
if !attributes.is_empty() && !ecx.ecfg.features.asm_cfg() {
82-
let span = span_start.to(p.prev_token.span);
83-
feature_err(ecx.sess, sym::asm_cfg, span, fluent::builtin_macros_asm_cfg).emit();
81+
for attr in attributes.iter() {
82+
match attr.name() {
83+
Some(sym::cfg | sym::cfg_attr) => {
84+
if !ecx.ecfg.features.asm_cfg() {
85+
let span = span_start.to(p.prev_token.span);
86+
feature_err(ecx.sess, sym::asm_cfg, span, fluent::builtin_macros_asm_cfg)
87+
.emit();
88+
}
89+
}
90+
_ => {
91+
ecx.dcx().emit_err(errors::AsmAttributeNotSupported { span: attr.span() });
92+
}
93+
}
8494
}
8595

8696
Ok(Self(attributes))

compiler/rustc_builtin_macros/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,13 @@ pub(crate) struct AsmRequiresTemplate {
795795
pub(crate) span: Span,
796796
}
797797

798+
#[derive(Diagnostic)]
799+
#[diag(builtin_macros_asm_attribute_not_supported)]
800+
pub(crate) struct AsmAttributeNotSupported {
801+
#[primary_span]
802+
pub(crate) span: Span,
803+
}
804+
798805
#[derive(Diagnostic)]
799806
#[diag(builtin_macros_asm_expected_comma)]
800807
pub(crate) struct AsmExpectedComma {

tests/ui/feature-gates/feature-gate-asm_cfg.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use std::arch::{asm, global_asm, naked_asm};
55

66
global_asm!(
77
"nop",
8-
#[cfg(false)] //~ ERROR attributes on assembly are unstable
8+
#[cfg(false)]
9+
//~^ ERROR the `#[cfg(/* ... */)]` and `#[cfg_attr(/* ... */)]` attributes on assembly are unstable
910
"nop"
1011
);
1112

@@ -14,10 +15,12 @@ global_asm!(
1415
extern "C" fn naked() {
1516
naked_asm!(
1617
"mov rax, 5",
17-
#[cfg(false)] //~ ERROR attributes on assembly are unstable
18+
#[cfg(false)]
19+
//~^ ERROR the `#[cfg(/* ... */)]` and `#[cfg_attr(/* ... */)]` attributes on assembly are unstable
1820
"mov rax, {a}",
1921
"ret",
20-
#[cfg(false)] //~ ERROR attributes on assembly are unstable
22+
#[cfg(false)]
23+
//~^ ERROR the `#[cfg(/* ... */)]` and `#[cfg_attr(/* ... */)]` attributes on assembly are unstable
2124
a = const 10,
2225
)
2326
}
@@ -26,9 +29,20 @@ fn asm() {
2629
unsafe {
2730
asm!(
2831
"nop",
29-
#[cfg(false)] //~ ERROR attributes on assembly are unstable
32+
#[cfg(false)]
33+
//~^ ERROR the `#[cfg(/* ... */)]` and `#[cfg_attr(/* ... */)]` attributes on assembly are unstable
3034
clobber_abi("C"),
3135
clobber_abi("C"), //~ ERROR `C` ABI specified multiple times
3236
);
3337
}
3438
}
39+
40+
fn bad_attribute() {
41+
unsafe {
42+
asm!(
43+
#[inline]
44+
//~^ ERROR this attribute is not supported on assembly
45+
"nop"
46+
)
47+
};
48+
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0658]: attributes on assembly are unstable
1+
error[E0658]: the `#[cfg(/* ... */)]` and `#[cfg_attr(/* ... */)]` attributes on assembly are unstable
22
--> $DIR/feature-gate-asm_cfg.rs:8:5
33
|
44
LL | #[cfg(false)]
@@ -8,8 +8,8 @@ LL | #[cfg(false)]
88
= help: add `#![feature(asm_cfg)]` to the crate attributes to enable
99
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
1010

11-
error[E0658]: attributes on assembly are unstable
12-
--> $DIR/feature-gate-asm_cfg.rs:17:9
11+
error[E0658]: the `#[cfg(/* ... */)]` and `#[cfg_attr(/* ... */)]` attributes on assembly are unstable
12+
--> $DIR/feature-gate-asm_cfg.rs:18:9
1313
|
1414
LL | #[cfg(false)]
1515
| ^^^^^^^^^^^^^
@@ -18,8 +18,8 @@ LL | #[cfg(false)]
1818
= help: add `#![feature(asm_cfg)]` to the crate attributes to enable
1919
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
2020

21-
error[E0658]: attributes on assembly are unstable
22-
--> $DIR/feature-gate-asm_cfg.rs:20:9
21+
error[E0658]: the `#[cfg(/* ... */)]` and `#[cfg_attr(/* ... */)]` attributes on assembly are unstable
22+
--> $DIR/feature-gate-asm_cfg.rs:22:9
2323
|
2424
LL | #[cfg(false)]
2525
| ^^^^^^^^^^^^^
@@ -28,8 +28,8 @@ LL | #[cfg(false)]
2828
= help: add `#![feature(asm_cfg)]` to the crate attributes to enable
2929
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
3030

31-
error[E0658]: attributes on assembly are unstable
32-
--> $DIR/feature-gate-asm_cfg.rs:29:13
31+
error[E0658]: the `#[cfg(/* ... */)]` and `#[cfg_attr(/* ... */)]` attributes on assembly are unstable
32+
--> $DIR/feature-gate-asm_cfg.rs:32:13
3333
|
3434
LL | #[cfg(false)]
3535
| ^^^^^^^^^^^^^
@@ -38,14 +38,20 @@ LL | #[cfg(false)]
3838
= help: add `#![feature(asm_cfg)]` to the crate attributes to enable
3939
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
4040

41+
error: this attribute is not supported on assembly
42+
--> $DIR/feature-gate-asm_cfg.rs:43:13
43+
|
44+
LL | #[inline]
45+
| ^^^^^^^^^
46+
4147
error: `C` ABI specified multiple times
42-
--> $DIR/feature-gate-asm_cfg.rs:31:13
48+
--> $DIR/feature-gate-asm_cfg.rs:35:13
4349
|
4450
LL | clobber_abi("C"),
4551
| ---------------- previously specified here
4652
LL | clobber_abi("C"),
4753
| ^^^^^^^^^^^^^^^^
4854

49-
error: aborting due to 5 previous errors
55+
error: aborting due to 6 previous errors
5056

5157
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)