Skip to content

change the type of the argument of drop_in_place lang item to &mut _#154327

Open
WaffleLapkin wants to merge 10 commits intorust-lang:mainfrom
WaffleLapkin:drop_in_place_ref
Open

change the type of the argument of drop_in_place lang item to &mut _#154327
WaffleLapkin wants to merge 10 commits intorust-lang:mainfrom
WaffleLapkin:drop_in_place_ref

Conversation

@WaffleLapkin
Copy link
Copy Markdown
Member

@WaffleLapkin WaffleLapkin commented Mar 24, 2026

View all comments

We used to special case core::ptr::drop_in_place when computing LLVM argument attributes with this hack:

let drop_target_pointee_info = drop_target_pointee.and_then(|pointee| {
assert_eq!(pointee, layout.ty.builtin_deref(true).unwrap());
assert_eq!(offset, Size::ZERO);
// The argument to `drop_in_place` is semantically equivalent to a mutable reference.
let mutref = Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, pointee);
let layout = cx.layout_of(mutref).unwrap();
layout.pointee_info_at(&cx, offset)
});
if let Some(pointee) = drop_target_pointee_info.or_else(|| layout.pointee_info_at(&cx, offset))

This is because even though drop_in_place takes a *mut T it is semantically a &mut T (remember how &mut Self is passed to Drop::drop). This is apparently relevant for perf.

This PR replaces this hack with a simpler solution -- it makes drop_in_place a thin wrapper around newly added core::ptr::drop_glue, which is the actual lang item and takes a &mut T:

pub const unsafe fn drop_in_place<T: PointeeSized>(to_drop: *mut T)
where
T: [const] Destruct,
{
// Due to historic reasons, `drop_in_place` takes a pointer rather than a reference,
// which results in worse codegen since we don't apply noalias/dereferenceable llvm
// attributes to pointer arguments. To workaround this without breaking public
// interface, `drop_in_place` calls the lang item, rather than being one directly.
// SAFETY:
// - compiler glue has the same safety requirements as this function
// - the pointer must be valid as per the safety requirement of this function
unsafe { drop_glue(&mut *to_drop) }
}
/// Helper function for `drop_in_place`.
#[lang = "drop_glue"]
const unsafe fn drop_glue<T: PointeeSized>(_: &mut T)
where
T: [const] Destruct,
{
// Code here does not matter - this is replaced by the
// real drop glue by the compiler.
}


The rest of the PR is blessing tests and cleaning up things which are not necessary after this change.

One thing that is a bit awkward is that now that drop_glue is the actual lang item, a lot of the comments referring to drop_in_place are outdated. Should I try fixing that?

I've also changed async_drop_in_place to take a &mut T, and it simplified the code handling it a bit. (since it's unstable we don't need to introduce a wrapper)


cc @RalfJung
Closes #154274

@rustbot rustbot added PG-exploit-mitigations Project group: Exploit mitigations 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 Mar 24, 2026
@RalfJung
Copy link
Copy Markdown
Member

Oh wow I hadn't expected my wish to be fulfilled before I could even finish my PR that motivated me to express the wish in the first place. :-) ❤️

Comment thread library/core/src/ptr/mod.rs Outdated
@rust-log-analyzer

This comment has been minimized.

Comment thread compiler/rustc_const_eval/src/interpret/place.rs
Comment thread library/core/src/ptr/mod.rs Outdated
@rust-log-analyzer

This comment has been minimized.

Comment thread compiler/rustc_const_eval/src/interpret/place.rs
@WaffleLapkin WaffleLapkin force-pushed the drop_in_place_ref branch 2 times, most recently from 794fcf5 to ea0c913 Compare April 5, 2026 18:31
@rustbot rustbot added A-run-make Area: port run-make Makefiles to rmake.rs A-test-infra-minicore Area: `minicore` test auxiliary and `//@ add-core-stubs` T-rust-analyzer Relevant to the rust-analyzer team, which will review and decide on the PR/issue. labels Apr 5, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

Comment thread compiler/rustc_const_eval/src/interpret/place.rs Outdated
@RalfJung
Copy link
Copy Markdown
Member

RalfJung commented Apr 6, 2026

I like the interpreter changes and renames. Not sure what the status of the rest of the PR is, but if you submit just those as a separate PR I'll r+.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Apr 6, 2026
…s, r=RalfJung

Slightly refactor mplace<->ptr conversions

split off of rust-lang#154327

r? RalfJung
@rust-log-analyzer

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Apr 11, 2026
…ottmcm

Don't try to remove `drop_in_place` calls in `RemoveUnneededDrops`



As per my justification in #154327 (comment)

r? scottmcm
Comment thread tests/mir-opt/inline/inline_empty_drop_glue.slice_in_place.Inline.diff Outdated
@rust-log-analyzer

This comment has been minimized.

Comment thread compiler/rustc_codegen_cranelift/example/mini_core.rs Outdated
github-actions Bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request Apr 13, 2026
…ottmcm

Don't try to remove `drop_in_place` calls in `RemoveUnneededDrops`



As per my justification in rust-lang/rust#154327 (comment)

r? scottmcm
@WaffleLapkin
Copy link
Copy Markdown
Member Author

(#155044 is merged, ci is passing, this PR is ready for review again :3)

Copy link
Copy Markdown
Member

@scottmcm scottmcm left a comment

Choose a reason for hiding this comment

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

I skimmed over this and it looks good, but TBH I don't really feel like I understand the CTFE and async implications that are the only non-obvious bits to feel comfortable signing-off.

View changes since this review

@RalfJung
Copy link
Copy Markdown
Member

RalfJung commented Apr 15, 2026

The rustc_const_eval and rustc_ty_utils changes LGTM.

But yeah it'd be good to get someone who has seen the async drop stuff before to look at that. Cc @oli-obk @azhogin

Comment thread library/core/src/ptr/mod.rs Outdated
Co-authored-by: Ralf Jung <post@ralfj.de>
github-actions Bot pushed a commit to rust-lang/rust-analyzer that referenced this pull request Apr 16, 2026
…ottmcm

Don't try to remove `drop_in_place` calls in `RemoveUnneededDrops`



As per my justification in rust-lang/rust#154327 (comment)

r? scottmcm
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 17, 2026

☔ The latest upstream changes (presumably #155207) made this pull request unmergeable. Please resolve the merge conflicts.

Copy link
Copy Markdown
Member

@RalfJung RalfJung left a comment

Choose a reason for hiding this comment

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

Looking over the changes in the async part again -- they seem fairly mechanical, and I assume there's some sort of MIR validation / typeck running which ensures that the types all match. So I'd be happy to sign off on these.

What I really don't know how to review are the gigantic mir-opt test suite diffs. How do you all usually deal with that? @rust-lang/wg-mir-opt @scottmcm @saethlin

View changes since this review

@@ -8,6 +8,8 @@
pub fn start(_: isize, _: *const *const u8) -> isize {
// No item produced for this, it's a no-op drop and so is removed.
unsafe {
// FIXME: is not removed on opt-level=0 anymore...
//~ MONO_ITEM fn std::ptr::drop_in_place::<u32> @@ drop_glue_noop-cgu.0[External]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So the crucial part that is still tested is that we don't see any drop_glue::<u32>, right? Maybe add a comment explaining that (instead of the FIXME which seems expected with the given mir-opt-level).

Comment on lines +28 to 31
// FIXME: drop_in_place isn't an intrinsic....
// This is the interesting thing in this test case: Normally we would
// not have drop-glue for the unsized [StructWithDtor]. This has to be
// generated though when the drop_in_place() intrinsic is used.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Seems like you can just update the comment then?

);
println!("AsyncUnion::Dropper::poll: {}, {}", unsafe { self.signed }, unsafe {
self.unsigned
},);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It'd be better to avoid formatting-only changes... and in particular this one here doesn't like it is properly formatted now? This looked a lot better before.

@saethlin
Copy link
Copy Markdown
Member

saethlin commented Apr 21, 2026

What I really don't know how to review are the gigantic mir-opt test suite diffs. How do you all usually deal with that?

mir-opt diffs look good to me.

Most of the diff here is from tests that check that drop-related code is inlined (it is) and then there's a bunch of diff in the specifics of the SourceScope data for PreCodegen tests, and those tests aren't worried about debuginfo. If you can visually exclude those cases, the diff isn't too bad.

Also for what it's worth I don't have a lot of patience for these mir-opt diffs for this reason, we have support for FileCheck annotations in mir-opt tests. If something is really that critical, it should be tested for with FileCheck.

@RalfJung
Copy link
Copy Markdown
Member

If something is really that critical, it should be tested for with FileCheck.

It should, yeah, but is it?^^

Thanks for taking a look. :)

@WaffleLapkin looks like we're just one rebase and a few nits away from landing this then. :D

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

Labels

A-run-make Area: port run-make Makefiles to rmake.rs A-test-infra-minicore Area: `minicore` test auxiliary and `//@ add-core-stubs` PG-exploit-mitigations Project group: Exploit mitigations S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. 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. T-rust-analyzer Relevant to the rust-analyzer team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

internal drop_in_place shim should take &mut arguments

8 participants