Skip to content

Commit ab6234f

Browse files
committed
document cfg conditions on inline assembly templates and operands
1 parent 76d5c46 commit ab6234f

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/attributes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ Attributes may be applied to many forms in the language:
108108
* [Function][functions], [closure] and [function pointer]
109109
parameters accept outer attributes. This includes attributes on variadic parameters
110110
denoted with `...` in function pointers and [external blocks][variadic functions].
111+
* [Inline assembly] template strings and operands accept outer attributes.
111112
112113
r[attributes.meta]
113114
## Meta item attribute syntax
@@ -410,3 +411,4 @@ The following is an index of all built-in attributes.
410411
[variadic functions]: items/external-blocks.html#variadic-functions
411412
[`diagnostic::on_unimplemented`]: attributes/diagnostics.md#the-diagnosticon_unimplemented-attribute
412413
[`diagnostic::do_not_recommend`]: attributes/diagnostics.md#the-diagnosticdo_not_recommend-attribute
414+
[Inline assembly](inline-assembly.md)

src/inline-assembly.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,19 @@ r[asm.syntax]
4949
The following grammar specifies the arguments that can be passed to the `asm!`, `global_asm!` and `naked_asm!` macros.
5050

5151
```grammar,assembly
52-
@root AsmArgs -> FormatString (`,` FormatString)* (`,` AsmOperand)* `,`?
52+
@root AsmArgs -> AsmAttrFormatString (`,` AsmAttrFormatString)* (`,` AsmAttrOperand)* `,`?
5353
5454
FormatString -> STRING_LITERAL | RAW_STRING_LITERAL | MacroInvocation
5555
56+
AsmAttrFormatString -> (OuterAttribute)* FormatString
57+
5658
AsmOperand ->
5759
ClobberAbi
5860
| AsmOptions
5961
| RegOperand
6062
63+
AsmAttrOperand -> (OuterAttribute)* AsmOperand
64+
6165
ClobberAbi -> `clobber_abi` `(` Abi (`,` Abi)* `,`? `)`
6266
6367
AsmOptions ->
@@ -266,6 +270,27 @@ Further constraints on the directives used by inline assembly are indicated by [
266270
[format-syntax]: std::fmt#syntax
267271
[rfc-2795]: https://github.com/rust-lang/rfcs/pull/2795
268272

273+
r[asm.attributes]
274+
## Attributes
275+
276+
r[asm.attributes.supported-attributes]
277+
Only the [`cfg`] and [`cfg_attr`] attributes are accepted semantically on inline assembly template strings and operands. Other attributes are parsed, but rejected when the assembly macro is expanded.
278+
279+
r[asm.attributes.starts-with-template]
280+
Syntactically there must be at least one template string before the first operand.
281+
282+
```rust, ignore
283+
// This is rejected because `a = out(reg) x` does not parse as a template string.
284+
core::arch::asm!(
285+
#[cfg(false)]
286+
a = out(reg) x, //~ ERROR expected token: `,`
287+
"",
288+
);
289+
```
290+
291+
[`cfg`]: ../conditional-compilation.md#the-cfg-attribute
292+
[`cfg_attr`]: ../conditional-compilation.md#the-cfg_attr-attribute
293+
269294
r[asm.operand-type]
270295
## Operand type
271296

0 commit comments

Comments
 (0)