Skip to content

Conversation

@folkertdev
Copy link
Contributor

@folkertdev folkertdev commented Oct 24, 2025

Reference PR for:

@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Oct 24, 2025
Comment on lines 270 to 271
r[asm.ts-args.attributes]
Only the [`cfg`] and [`cfg_attr`] attributes are accepted on inline assembly template strings and operands. Other attributes are parsed, but rejected when the assembly macro is expanded.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I said to put it here, I had momentarily forgotten that this applied to operands as well.

Given that, probably it shouldn't go here. There's not really another obvious section for it, though. Probably syntax is the closest, but maybe it'd be better to just create a new asm.attributes section, even just for this one purpose, as we're describing particularly what is semantically accepted, so it's not a great fit for syntax.

Probably too, we'll want to use the phrase "accepted semantically" in this sentence to be more clear upfront about the distinction we're making.

Copy link
Contributor

@traviscross traviscross Oct 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also please describe the rule imposed in rust-lang/rust#147736 (comment):

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.

// This is rejected because `a = out(reg) x` does not parse as an expresion.
asm!(
	#[cfg(false)]
	a = out(reg) x, //~ ERROR expected token: `,`
	"",
);

Let's include the example in the text.

* [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.
Copy link
Contributor

@petrochenkov petrochenkov Oct 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, it may be a mostly theoretical concern, but attributes are not supported here.
asm! rather have an attribute-like syntax in its DSL - rust-lang/rust#147736 (comment).

The "only cfg and cfg_attr" restriction is unique to all attribute positions and is a direct consequence of the above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This list is about allowed positions, and with asm_cfg attributes are parsed on template strings and operands, so saying that they are a valid position for attributes feels right.

But yeah they aren't really supported like in the other places listed here.

@folkertdev folkertdev force-pushed the asm-cfg branch 3 times, most recently from ab6234f to 8fc2f56 Compare October 25, 2025 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: The marked PR is awaiting review from a maintainer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants