Skip to content

Commit ba01420

Browse files
authored
Unrolled build for #147736
Rollup merge of #147736 - folkertdev:stabilize-asm-cfg, r=jdonszelmann Stabilize `asm_cfg` tracking issue: #140364 closes #140364 Reference PR: - rust-lang/reference#2063 # Request for Stabilization ## Summary The `cfg_asm` feature allows `#[cfg(...)]` and `#[cfg_attr(...)]` on the arguments of the assembly macros, for instance: ```rust asm!( // or global_asm! or naked_asm! "nop", #[cfg(target_feature = "sse2")] "nop", // ... #[cfg(target_feature = "sse2")] a = const 123, // only used on sse2 ); ``` ## Semantics Templates, operands, `options` and `clobber_abi` in the assembly macros (`asm!`, `naked_asm!` and `global_asm!`) can be annotated with `#[cfg(...)]` and `#[cfg_attr(...)]`. When the condition evaluates to true, the annotated argument has no effect, and is completely ignored when expanding the assembly macro. ## Documentation reference PR: rust-lang/reference#2063 ## Tests - [tests/ui/asm/cfg.rs](https://github.com/rust-lang/rust/blob/master/tests/ui/asm/cfg.rs) checks that `cfg`'d arguments where the condition evaluates to false have no effect - [tests/ui/asm/cfg-parse-error.rs](https://github.com/rust-lang/rust/blob/master/tests/ui/asm/cfg.rs) checks the parsing rules (parsing effectively assumes that the cfg conditions are all true) ## History - #140279 - #140367 # Resolved questions **how are other attributes handled** Other attributes are parsed, but explicitly rejected. # unresolved questions **operand before template** The current implementation expects at least one template string before any operands. In the example below, if the `cfg` condition evaluates to true, the assembly block is ill-formed. But even when it evaluates to `false` this block is rejected, because the parser still expects just a template (a template is parsed as an expression and then validated to ensure that it is or expands to a string literal). Changing how this works is difficult. ```rust // This is rejected because `a = out(reg) x` does not parse as an expresion. asm!( #[cfg(false)] a = out(reg) x, //~ ERROR expected token: `,` "", ); ``` **lint on positional arguments?** Adding a lint to warn on the definition or use of positional arguments being `cfg`'d out was discussed in #140279 (comment) and subsequent comments. Such a lint is not currently implemented, but that may not be a blocker based on the comments there. r? `@traviscross` (I'm assuming you'll reassign as needed)
2 parents 0f6dae4 + 261d7eb commit ba01420

File tree

10 files changed

+12
-131
lines changed

10 files changed

+12
-131
lines changed

compiler/rustc_builtin_macros/messages.ftl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ builtin_macros_alloc_must_statics = allocators must be statics
33
44
builtin_macros_asm_attribute_not_supported =
55
this attribute is not supported on assembly
6-
builtin_macros_asm_cfg =
7-
the `#[cfg(/* ... */)]` and `#[cfg_attr(/* ... */)]` attributes on assembly are unstable
86
97
builtin_macros_asm_clobber_abi = clobber_abi
108
builtin_macros_asm_clobber_no_reg = asm with `clobber_abi` must specify explicit registers for outputs

compiler/rustc_builtin_macros/src/asm.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ use rustc_expand::base::*;
66
use rustc_index::bit_set::GrowableBitSet;
77
use rustc_parse::parser::asm::*;
88
use rustc_session::lint;
9-
use rustc_session::parse::feature_err;
109
use rustc_span::{ErrorGuaranteed, InnerSpan, Span, Symbol, sym};
1110
use rustc_target::asm::InlineAsmArch;
1211
use smallvec::smallvec;
1312
use {rustc_ast as ast, rustc_parse_format as parse};
1413

14+
use crate::errors;
1515
use crate::util::{ExprToSpannedString, expr_to_spanned_string};
16-
use crate::{errors, fluent_generated as fluent};
1716

1817
/// Validated assembly arguments, ready for macro expansion.
1918
struct ValidatedAsmArgs {
@@ -64,22 +63,13 @@ fn validate_asm_args<'a>(
6463

6564
for arg in args {
6665
for attr in arg.attributes.0.iter() {
67-
match attr.name() {
68-
Some(sym::cfg | sym::cfg_attr) => {
69-
if !ecx.ecfg.features.asm_cfg() {
70-
let span = attr.span();
71-
feature_err(ecx.sess, sym::asm_cfg, span, fluent::builtin_macros_asm_cfg)
72-
.emit();
73-
}
74-
}
75-
_ => {
76-
ecx.dcx().emit_err(errors::AsmAttributeNotSupported { span: attr.span() });
77-
}
66+
if !matches!(attr.name(), Some(sym::cfg | sym::cfg_attr)) {
67+
ecx.dcx().emit_err(errors::AsmAttributeNotSupported { span: attr.span() });
7868
}
7969
}
8070

8171
// Skip arguments that are configured out.
82-
if ecx.ecfg.features.asm_cfg() && strip_unconfigured.configure(arg.attributes).is_none() {
72+
if strip_unconfigured.configure(arg.attributes).is_none() {
8373
continue;
8474
}
8575

compiler/rustc_feature/src/accepted.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ declare_features! (
6060
(accepted, adx_target_feature, "1.61.0", Some(44839)),
6161
/// Allows explicit discriminants on non-unit enum variants.
6262
(accepted, arbitrary_enum_discriminant, "1.66.0", Some(60553)),
63+
/// Allows #[cfg(...)] on inline assembly templates and operands.
64+
(accepted, asm_cfg, "CURRENT_RUSTC_VERSION", Some(140364)),
6365
/// Allows using `const` operands in inline assembly.
6466
(accepted, asm_const, "1.82.0", Some(93332)),
6567
/// Allows using `label` operands in inline assembly.

compiler/rustc_feature/src/unstable.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,6 @@ declare_features! (
382382
(unstable, arbitrary_self_types, "1.23.0", Some(44874)),
383383
/// Allows inherent and trait methods with arbitrary self types that are raw pointers.
384384
(unstable, arbitrary_self_types_pointers, "1.83.0", Some(44874)),
385-
/// Allows #[cfg(...)] on inline assembly templates and operands.
386-
(unstable, asm_cfg, "1.89.0", Some(140364)),
387385
/// Enables experimental inline assembly support for additional architectures.
388386
(unstable, asm_experimental_arch, "1.58.0", Some(93335)),
389387
/// Enables experimental register support in inline assembly.

library/compiler-builtins/compiler-builtins/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#![feature(compiler_builtins)]
88
#![feature(core_intrinsics)]
99
#![feature(linkage)]
10-
#![feature(asm_cfg)]
1110
#![feature(naked_functions)]
1211
#![feature(repr_simd)]
1312
#![feature(macro_metavar_expr_concat)]

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
//@ needs-asm-support
2-
#![feature(asm_cfg)]
32

43
use std::arch::asm;
54

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
error: expected one of `#`, `clobber_abi`, `const`, `in`, `inlateout`, `inout`, `label`, `lateout`, `options`, `out`, or `sym`, found `""`
2-
--> $DIR/cfg-parse-error.rs:16:13
2+
--> $DIR/cfg-parse-error.rs:15: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:26:13
10+
--> $DIR/cfg-parse-error.rs:25: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:41:26
18+
--> $DIR/cfg-parse-error.rs:40:26
1919
|
2020
LL | a = out(reg) x,
2121
| ^ expected `,`
2222

2323
error: this attribute is not supported on assembly
24-
--> $DIR/cfg-parse-error.rs:47:13
24+
--> $DIR/cfg-parse-error.rs:46:13
2525
|
2626
LL | #[rustfmt::skip]
2727
| ^^^^^^^^^^^^^^^^
2828

2929
error: an inner attribute is not permitted in this context
30-
--> $DIR/cfg-parse-error.rs:53:13
30+
--> $DIR/cfg-parse-error.rs:52:13
3131
|
3232
LL | #![rustfmt::skip]
3333
| ^^^^^^^^^^^^^^^^^

tests/ui/asm/cfg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//@ revisions: reva revb
44
//@ only-x86_64
55
//@ run-pass
6-
#![feature(asm_cfg, cfg_select)]
6+
#![feature(cfg_select)]
77

88
use std::arch::{asm, naked_asm};
99

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

Lines changed: 0 additions & 48 deletions
This file was deleted.

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

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)