Skip to content

Use MaybeDangling in std#149614

Open
WaffleLapkin wants to merge 1 commit intorust-lang:mainfrom
WaffleLapkin:dangle-maybe
Open

Use MaybeDangling in std#149614
WaffleLapkin wants to merge 1 commit intorust-lang:mainfrom
WaffleLapkin:dangle-maybe

Conversation

@WaffleLapkin
Copy link
Copy Markdown
Member

@WaffleLapkin WaffleLapkin commented Dec 3, 2025

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Dec 3, 2025
@rust-log-analyzer

This comment has been minimized.

@WaffleLapkin WaffleLapkin force-pushed the dangle-maybe branch 2 times, most recently from c428102 to 5bd5888 Compare December 3, 2025 20:47
@rust-log-analyzer

This comment has been minimized.

Comment on lines 187 to 192
#[stable(feature = "manually_drop", since = "1.20.0")]
impl<T: ?Sized + PartialEq> PartialEq for ManuallyDrop<T> {
fn eq(&self, other: &Self) -> bool {
self.value.as_ref().eq(other.value.as_ref())
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You also need to implement StructuralPartialEq, since ManuallyDrop in a const can currently be used in patterns on stable.

@joboet
Copy link
Copy Markdown
Member

joboet commented Dec 5, 2025

I don't know how much this is still an experiment, but if you're looking for more coverage, you might want to replace the MaybeUninit-based manual MaybeDangling implementation in std:

// Pass `f` in `MaybeUninit` because actually that closure might *run longer than the lifetime of `F`*.
// See <https://github.com/rust-lang/rust/issues/101983> for more details.
// To prevent leaks we use a wrapper that drops its contents.
#[repr(transparent)]
struct MaybeDangling<T>(MaybeUninit<T>);
impl<T> MaybeDangling<T> {
fn new(x: T) -> Self {
MaybeDangling(MaybeUninit::new(x))
}
fn into_inner(self) -> T {
// Make sure we don't drop.
let this = ManuallyDrop::new(self);
// SAFETY: we are always initialized.
unsafe { this.0.assume_init_read() }
}
}
impl<T> Drop for MaybeDangling<T> {
fn drop(&mut self) {
// SAFETY: we are always initialized.
unsafe { self.0.assume_init_drop() };
}
}

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@WaffleLapkin WaffleLapkin force-pushed the dangle-maybe branch 2 times, most recently from ac4154d to 259187e Compare December 13, 2025 13:16
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Dec 15, 2025
…m, r=jdonszelmann

Simplify how inline asm handles `MaybeUninit`

This is just better, but this is also allows it to handle changes from rust-lang#149614 (i.e. `ManuallyDrop` containing `MaybeDangle`).
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Dec 15, 2025
…m, r=jdonszelmann

Simplify how inline asm handles `MaybeUninit`

This is just better, but this is also allows it to handle changes from rust-lang#149614 (i.e. `ManuallyDrop` containing `MaybeDangle`).
Zalathar added a commit to Zalathar/rust that referenced this pull request Dec 16, 2025
…m, r=jdonszelmann

Simplify how inline asm handles `MaybeUninit`

This is just better, but this is also allows it to handle changes from rust-lang#149614 (i.e. `ManuallyDrop` containing `MaybeDangle`).
Zalathar added a commit to Zalathar/rust that referenced this pull request Dec 16, 2025
…m, r=jdonszelmann

Simplify how inline asm handles `MaybeUninit`

This is just better, but this is also allows it to handle changes from rust-lang#149614 (i.e. `ManuallyDrop` containing `MaybeDangle`).
rust-timer added a commit that referenced this pull request Dec 16, 2025
Rollup merge of #149950 - WaffleLapkin:inlines-ur-mu-into-asm, r=jdonszelmann

Simplify how inline asm handles `MaybeUninit`

This is just better, but this is also allows it to handle changes from #149614 (i.e. `ManuallyDrop` containing `MaybeDangle`).
Kobzol pushed a commit to Kobzol/rustc_codegen_cranelift that referenced this pull request Dec 29, 2025
…szelmann

Simplify how inline asm handles `MaybeUninit`

This is just better, but this is also allows it to handle changes from rust-lang/rust#149614 (i.e. `ManuallyDrop` containing `MaybeDangle`).
christian-schilling pushed a commit to christian-schilling/rustc_codegen_cranelift that referenced this pull request Jan 27, 2026
…szelmann

Simplify how inline asm handles `MaybeUninit`

This is just better, but this is also allows it to handle changes from rust-lang/rust#149614 (i.e. `ManuallyDrop` containing `MaybeDangle`).
christian-schilling pushed a commit to christian-schilling/rustc_codegen_cranelift that referenced this pull request Jan 27, 2026
…szelmann

Simplify how inline asm handles `MaybeUninit`

This is just better, but this is also allows it to handle changes from rust-lang/rust#149614 (i.e. `ManuallyDrop` containing `MaybeDangle`).
@rust-bors

This comment has been minimized.

@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. PG-exploit-mitigations Project group: Exploit mitigations labels Mar 5, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-bors

This comment has been minimized.

@WaffleLapkin WaffleLapkin added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. PG-exploit-mitigations Project group: Exploit mitigations labels Apr 6, 2026
@WaffleLapkin WaffleLapkin changed the title Add MaybeDangling Use MaybeDangling in std Apr 6, 2026
@WaffleLapkin
Copy link
Copy Markdown
Member Author

@rustbot reroll

@WaffleLapkin WaffleLapkin marked this pull request as ready for review April 6, 2026 14:58
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Apr 6, 2026
…op-matching, r=JohnTitor

implement `StructuralPartialEq` for `MaybeDangling`

This fixes -- a stable-to-stable regression where constants of type `ManuallyDrop<T>` would not be allowed to be used as a pattern due to `MaybeDangling<T>` in `ManuallyDrop<T>` not implementing `StructuralPartialEq`.

Fixes rust-lang#154890

I'm sorry, @theemathas, I forgot to address your [comment](rust-lang#149614 (comment)) 😭
jhpratt added a commit to jhpratt/rust that referenced this pull request Apr 6, 2026
…op-matching, r=JohnTitor

implement `StructuralPartialEq` for `MaybeDangling`

This fixes -- a stable-to-stable regression where constants of type `ManuallyDrop<T>` would not be allowed to be used as a pattern due to `MaybeDangling<T>` in `ManuallyDrop<T>` not implementing `StructuralPartialEq`.

Fixes rust-lang#154890

I'm sorry, @theemathas, I forgot to address your [comment](rust-lang#149614 (comment)) 😭
rust-timer added a commit that referenced this pull request Apr 7, 2026
Rollup merge of #154891 - WaffleLapkin:deregress-manually-drop-matching, r=JohnTitor

implement `StructuralPartialEq` for `MaybeDangling`

This fixes -- a stable-to-stable regression where constants of type `ManuallyDrop<T>` would not be allowed to be used as a pattern due to `MaybeDangling<T>` in `ManuallyDrop<T>` not implementing `StructuralPartialEq`.

Fixes #154890

I'm sorry, @theemathas, I forgot to address your [comment](#149614 (comment)) 😭
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 9, 2026

This PR was rebased onto a different main 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.

@WaffleLapkin
Copy link
Copy Markdown
Member Author

@rustbot reroll

@rustbot rustbot assigned jhpratt and unassigned ibraheemdev Apr 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants