Skip to content

Document MaybeUninit<T> inline assembly operands - #2428

Open
traviscross wants to merge 4 commits into
masterfrom
TC/asm-maybe-uninit
Open

traviscross wants to merge 4 commits into
masterfrom
TC/asm-maybe-uninit

Conversation

@traviscross

Copy link
Copy Markdown
Contributor

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 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.
@rustbot rustbot added the S-waiting-on-review Status: The marked PR is awaiting review from a maintainer label Nov 28, 2026
@traviscross traviscross added I-lang-nominated Nominated for discussion during a lang team meeting. P-lang-drag-1 Lang team prioritization drag level 1. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. labels Nov 28, 2026
@traviscross

traviscross commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor Author

@Amanieu: What do you think? Look OK?

cc @rust-lang/lang

Comment thread src/inline-assembly.md
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].

@traviscross traviscross Nov 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@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?

View changes since the review

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).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

I-lang-nominated Nominated for discussion during a lang team meeting. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. P-lang-drag-1 Lang team prioritization drag level 1. 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.

2 participants