-
Notifications
You must be signed in to change notification settings - Fork 564
document cfg conditions on inline assembly templates and operands
#2063
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
8fc2f56 to
58e0705
Compare
841e6ba to
99014a3
Compare
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
There was a stray comma on this info string. Let's remove it.
This test is believed to fail, so we should mark it with `compile_fail` rather than `ignore`. Let's wrap the comment. The compiler's error message, in this case, doesn't add any insight; let's remove the details of that.
The sentence "Other attributes are parsed but rejected" has a two-part
compound predicate; that is, the subject is used for two verbs,
separated by a coordinating conjunction ("but"), and is not repeated.
In these cases, a comma is omitted before the coordinating
conjunction, so let's remove the comma.
We have a list that describes where attributes may be applied. We're adding inline assembly template strings and operands to that list, but only certain attributes may be used, and other caveats apply. Let's note that there are restrictions and link to the relevant section for more details, as we do already for certain other items on this list. It may be important, normatively, to note this restriction here if we now or later refer to some attribute as being accepted anywhere that attributes are allowed.
The implementation of attributes for asm macro template strings and operands doesn't use the normal system in `rustc` for handling attributes. This leads to the limitations and may lead to subtle divergences in behavior. Let's make a note about this. For background, see: - rust-lang#2063 (comment) - rust-lang/rust#147736 (comment)
|
I've pushed a series of revisions. Some of this is editorial; some has potential normative effect on other parts of the document where we may refer to other attributes as being accepted wherever attributes are allowed. For details, see the commit messages. With these revisions, this looks good to me. |
…onszelmann Stabilize `asm_cfg` tracking issue: rust-lang#140364 closes rust-lang#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 - rust-lang#140279 - rust-lang#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 rust-lang#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)
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)
Stabilize `asm_cfg` tracking issue: rust-lang/rust#140364 closes rust-lang/rust#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 - rust-lang/rust#140279 - rust-lang/rust#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 rust-lang/rust#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)
Update books ## rust-lang/book 2 commits in f78ab89d7545ac17780e6a367055cc089f4cd2ec..8c0eacd5c4acbb650497454f3a58c9e8083202a4 2025-11-18 15:36:41 UTC to 2025-11-18 15:33:41 UTC - Update ch07-02-defining-modules-to-control-scope-and-privacy.md (rust-lang/book#4570) - use AND for search terms (rust-lang/book#4573) ## rust-lang/reference 4 commits in f9f1d2a4149f02582aec2f8fcdfa5b596193b4e2..f2ac173df9906de5c03b0ee50653321ef1c4ebe8 2025-11-26 02:52:23 UTC to 2025-11-18 21:54:51 UTC - document `cfg` conditions on inline assembly templates and operands (rust-lang/reference#2063) - remove unused "link reference definitions" (rust-lang/reference#2092) - Add review process overview to review-policy.md (rust-lang/reference#2088) - Remove restriction on dereferencing pointers in const (rust-lang/reference#2090) ## rust-lang/rust-by-example 5 commits in f944161716230641605b5e3733e1c81f10047fd4..111cfae2f9c3a43f7b0ff8fa68c51cc8f930637c 2025-11-27 20:16:42 UTC to 2025-11-20 21:40:02 UTC - Use `From::from` fn pointer to convert to boxed errors (rust-lang/rust-by-example#1906) - link the _tuple_ page instead "TupleStruct" (rust-lang/rust-by-example#1909) - enum_use.md: avoid an uncommon term (rust-lang/rust-by-example#1976) - make search less surprising (rust-lang/rust-by-example#1975) - Update documentation for `any` function in iter_any.md (rust-lang/rust-by-example#1973)
Rollup merge of #149424 - rustbot:docs-update, r=ehuss Update books ## rust-lang/book 2 commits in f78ab89d7545ac17780e6a367055cc089f4cd2ec..8c0eacd5c4acbb650497454f3a58c9e8083202a4 2025-11-18 15:36:41 UTC to 2025-11-18 15:33:41 UTC - Update ch07-02-defining-modules-to-control-scope-and-privacy.md (rust-lang/book#4570) - use AND for search terms (rust-lang/book#4573) ## rust-lang/reference 4 commits in f9f1d2a4149f02582aec2f8fcdfa5b596193b4e2..f2ac173df9906de5c03b0ee50653321ef1c4ebe8 2025-11-26 02:52:23 UTC to 2025-11-18 21:54:51 UTC - document `cfg` conditions on inline assembly templates and operands (rust-lang/reference#2063) - remove unused "link reference definitions" (rust-lang/reference#2092) - Add review process overview to review-policy.md (rust-lang/reference#2088) - Remove restriction on dereferencing pointers in const (rust-lang/reference#2090) ## rust-lang/rust-by-example 5 commits in f944161716230641605b5e3733e1c81f10047fd4..111cfae2f9c3a43f7b0ff8fa68c51cc8f930637c 2025-11-27 20:16:42 UTC to 2025-11-20 21:40:02 UTC - Use `From::from` fn pointer to convert to boxed errors (rust-lang/rust-by-example#1906) - link the _tuple_ page instead "TupleStruct" (rust-lang/rust-by-example#1909) - enum_use.md: avoid an uncommon term (rust-lang/rust-by-example#1976) - make search less surprising (rust-lang/rust-by-example#1975) - Update documentation for `any` function in iter_any.md (rust-lang/rust-by-example#1973)
Reference PR for:
asm_cfgrust#147736