Repository navigation
Document MaybeUninit<T> inline assembly operands - #2428
Open
traviscross wants to merge 4 commits into
Open
traviscross wants to merge 4 commits into
traviscross wants to merge 4 commits into
Conversation
In the list of types allowed as inline assembly operands, some items end with a period while others don't. Let's add a trailing period to the items missing one.
For inline assembly, we list the set of types that are allowed as operands. And, for each register class, we list the set of value types that can be used with it. And we say that input and output expressions of an `inout` operand must have the same type. In Rust 1.74, we started allowing `MaybeUninit<T>` types in these operands (see rust-lang/rust#114790), treating them as `T` for the purpose of these rules. But we didn't document that. Let's do so.
In the special types and traits chapter, we describe the language- relevant behaviors of each listed type or trait. For `MaybeUninit<T>`, let's add a note summarizing and pointing at the rules related to using the type as an inline assembly operand.
Contributor
Author
|
@Amanieu: What do you think? Look OK? cc @rust-lang/lang |
traviscross
commented
Nov 28, 2026
Comment on lines
+912
to
+927
| // Passing a possibly uninitialized value through a register gives a | ||
| // value that is initialized. If the input was uninitialized, the | ||
| // result is an arbitrary value. | ||
| fn freeze(x: MaybeUninit<u64>) -> u64 { | ||
| let y: u64; | ||
| unsafe { asm!("/* {} */", inout(reg) x => y); } | ||
| y | ||
| } | ||
|
|
||
| assert_eq!(freeze(MaybeUninit::new(5)), 5); | ||
| let _: u64 = freeze(MaybeUninit::uninit()); | ||
| # } | ||
| ``` | ||
|
|
||
| > [!NOTE] | ||
| > The rules stated in this chapter imply that implementing `freeze` via inline assembly is sound. See [asm.register-operands.maybe-uninit] and [asm.rules.reg-not-input]. But whether this is sound is an open question. See [RFC 3605]. |
Contributor
Author
There was a problem hiding this comment.
@RalfJung: I particularly want to highlight this for your attention. It's not clear to me, given our other stated rules and our support for MaybeUnit<T> in inline assembly operands, how to avoid the implication that freeze must be sound. Thoughts?
Some inline assembly operands have special coercion behavior. This allows `&T` to be accepted (even though reference types are not directly accepted), by coercion to `*const T`. But `MaybeUninit<&T>` is not accepted (even though `MaybeUninit<U>` is otherwise treated as `U`). To explain this, we need to document the special coercion behavior of expressions used as inline assembly operands. So let's do that. These contexts are not general coercion sites (as the rules are limited and special). Still, these are coercion sites of a kind, so let's add a note to our list of coercion sites and point to this rule. And let's clarify, in our list of types acceptable for inline assembly operands, that we mean only (thin) raw pointers (and not references).
traviscross
force-pushed
the
TC/asm-maybe-uninit
branch
from
November 28, 2026 17:38
1dbd67e to
509c22c
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
For inline assembly, we list the set of types that are allowed as operands. And, for each register class, we list the set of value types that can be used with it. And we say that input and output expressions of an
inoutoperand must have the same type.In Rust 1.74, we started allowing
MaybeUninit<T>types in these operands (see rust-lang/rust#114790), treating them asTfor the purpose of these rules.But we didn't document that. Let's do so.