From 58e0705cab7bf2f0ae09c978a5520bf4960d1072 Mon Sep 17 00:00:00 2001 From: Folkert de Vries Date: Sat, 25 Oct 2025 00:23:20 +0200 Subject: [PATCH] document `cfg` conditions on inline assembly templates and operands --- src/attributes.md | 2 ++ src/inline-assembly.md | 27 ++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/attributes.md b/src/attributes.md index 9fd9c3ec85..cf18f26bf8 100644 --- a/src/attributes.md +++ b/src/attributes.md @@ -108,6 +108,7 @@ Attributes may be applied to many forms in the language: * [Function][functions], [closure] and [function pointer] parameters accept outer attributes. This includes attributes on variadic parameters denoted with `...` in function pointers and [external blocks][variadic functions]. +* [Inline assembly] template strings and operands accept outer attributes. r[attributes.meta] ## Meta item attribute syntax @@ -410,3 +411,4 @@ The following is an index of all built-in attributes. [variadic functions]: items/external-blocks.html#variadic-functions [`diagnostic::on_unimplemented`]: attributes/diagnostics.md#the-diagnosticon_unimplemented-attribute [`diagnostic::do_not_recommend`]: attributes/diagnostics.md#the-diagnosticdo_not_recommend-attribute +[Inline assembly]: inline-assembly.md diff --git a/src/inline-assembly.md b/src/inline-assembly.md index 41bff25213..e1f6901410 100644 --- a/src/inline-assembly.md +++ b/src/inline-assembly.md @@ -49,15 +49,19 @@ r[asm.syntax] The following grammar specifies the arguments that can be passed to the `asm!`, `global_asm!` and `naked_asm!` macros. ```grammar,assembly -@root AsmArgs -> FormatString (`,` FormatString)* (`,` AsmOperand)* `,`? +@root AsmArgs -> AsmAttrFormatString (`,` AsmAttrFormatString)* (`,` AsmAttrOperand)* `,`? FormatString -> STRING_LITERAL | RAW_STRING_LITERAL | MacroInvocation +AsmAttrFormatString -> (OuterAttribute)* FormatString + AsmOperand -> ClobberAbi | AsmOptions | RegOperand +AsmAttrOperand -> (OuterAttribute)* AsmOperand + ClobberAbi -> `clobber_abi` `(` Abi (`,` Abi)* `,`? `)` AsmOptions -> @@ -266,6 +270,27 @@ Further constraints on the directives used by inline assembly are indicated by [ [format-syntax]: std::fmt#syntax [rfc-2795]: https://github.com/rust-lang/rfcs/pull/2795 +r[asm.attributes] +## Attributes + +r[asm.attributes.supported-attributes] +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. + +r[asm.attributes.starts-with-template] +Syntactically there must be at least one template string before the first operand. + +```rust, ignore +// This is rejected because `a = out(reg) x` does not parse as a template string. +core::arch::asm!( + #[cfg(false)] + a = out(reg) x, //~ ERROR expected token: `,` + "", +); +``` + +[`cfg`]: conditional-compilation.md#the-cfg-attribute +[`cfg_attr`]: conditional-compilation.md#the-cfg_attr-attribute + r[asm.operand-type] ## Operand type