Skip to content

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

Open
WaffleLapkin wants to merge 8 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 8 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
@rustbot rustbot assigned RalfJung and unassigned TaKO8Ki May 4, 2026
@rustbot

This comment was marked as resolved.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 4, 2026
@rust-log-analyzer

This comment has been minimized.

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

RalfJung commented May 4, 2026

LGTM apart from the problem that CI already pointed out. :)

StatementKind::Assign(Box::new((Place::from(cor_ref_local), reborrow))),
StatementKind::Assign(Box::new((
Place::from(cor_ref_local),
Rvalue::Use(Operand::Move(Place::from(cor_ref_tmp_local)), WithRetag::No),
Copy link
Copy Markdown
Member

@RalfJung RalfJung May 4, 2026

Choose a reason for hiding this comment

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

Suggested change
Rvalue::Use(Operand::Move(Place::from(cor_ref_tmp_local)), WithRetag::No),
Rvalue::Use(Operand::Move(Place::from(cor_ref_tmp_local)), WithRetag::Yes),

All assignments generated during MIR building should have retags.

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ugh, got confused with other changes. Fixed.

Is WithRetags::No for cases where an assignment did not exist in the original MIR, but got introduced by an optimization, and thus doing a retag would introduce UB, which did not exist in the original MIR?

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.

Yes, that's one major usecase for WithRetags::No.

@RalfJung
Copy link
Copy Markdown
Member

RalfJung commented May 4, 2026

r=me with CI green

@WaffleLapkin
Copy link
Copy Markdown
Member Author

@bors r=RalfJung,scottmcm,saethlin

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented May 4, 2026

📌 Commit e0ecfbb has been approved by RalfJung,scottmcm,saethlin

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 4, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request May 4, 2026
…ottmcm,saethlin

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



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

https://github.com/rust-lang/rust/blob/db5e2dc248fe5bb26f70d7baec46a3bca9fa3e1d/compiler/rustc_ty_utils/src/abi.rs#L383-L392

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`:

https://github.com/rust-lang/rust/blob/d2563d5003bbecff1efc40c1f5673ceec603825b/library/core/src/ptr/mod.rs#L810-L833

------

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
@rust-log-analyzer
Copy link
Copy Markdown
Collaborator

The job dist-x86_64-mingw failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0522`.
Command `D:\a\rust\rust\build\x86_64-pc-windows-gnu\stage0\bin\rustc.exe --cfg bootstrap --target x86_64-pc-windows-gnu --emit=obj -o D:\a\rust\rust\build\x86_64-pc-windows-gnu\native\rtstartup\rsbegin.o D:\a\rust\rust\library\rtstartup\rsbegin.rs` failed with exit code 1
Created at: src\bootstrap\src\core\build_steps\compile.rs:933:31
Executed at: src\bootstrap\src\core\build_steps\compile.rs:945:22

--- BACKTRACE vvv
   0: std::backtrace_rs::backtrace::win64::trace
             at /rustc/ef0fb8a2563200e322fa4419f09f65a63742038c/library\std\src\..\..\backtrace\src\backtrace/win64.rs:85:14
   1: std::backtrace_rs::backtrace::trace_unsynchronized::<<std::backtrace::Backtrace>::create::{closure#0}>
             at /rustc/ef0fb8a2563200e322fa4419f09f65a63742038c/library\std\src\..\..\backtrace\src\backtrace/mod.rs:66:14
   2: <std::backtrace::Backtrace>::create
             at /rustc/ef0fb8a2563200e322fa4419f09f65a63742038c/library\std\src/backtrace.rs:331:13
   3: <std::backtrace::Backtrace>::capture
             at /rustc/ef0fb8a2563200e322fa4419f09f65a63742038c/library\std\src/backtrace.rs:296:9
   4: <bootstrap::utils::exec::DeferredCommand>::finish_process
             at .\src\bootstrap\src\utils\exec.rs:939:17
   5: <bootstrap::utils::exec::DeferredCommand>::wait_for_output::<&bootstrap::utils::exec::ExecutionContext>
             at .\src\bootstrap\src\utils\exec.rs:831:21
   6: <bootstrap::utils::exec::ExecutionContext>::run
             at .\src\bootstrap\src\utils\exec.rs:741:45
   7: <bootstrap::utils::exec::BootstrapCommand>::run::<&bootstrap::core::builder::Builder>
             at .\src\bootstrap\src\utils\exec.rs:339:27
   8: <bootstrap::core::build_steps::compile::StartupObjects as bootstrap::core::builder::Step>::run
             at .\src\bootstrap\src\core\build_steps\compile.rs:945:22
   9: <bootstrap::core::builder::Builder>::ensure::<bootstrap::core::build_steps::compile::StartupObjects>
             at .\src\bootstrap\src\core\builder\mod.rs:1595:36
  10: <bootstrap::core::build_steps::compile::Std as bootstrap::core::builder::Step>::run
             at .\src\bootstrap\src\core\build_steps\compile.rs:213:39
  11: <bootstrap::core::builder::Builder>::ensure::<bootstrap::core::build_steps::compile::Std>
             at .\src\bootstrap\src\core\builder\mod.rs:1595:36
  12: <bootstrap::core::builder::Builder>::std
             at .\src\bootstrap\src\core\builder\mod.rs:1294:18
  13: bootstrap::core::build_steps::doc::prepare_doc_compiler
             at .\src\bootstrap\src\core\build_steps\doc.rs:840:13
  14: <bootstrap::core::build_steps::doc::TheBook as bootstrap::core::builder::Step>::make_run
             at .\src\bootstrap\src\core\build_steps\doc.rs:231:29
  15: <bootstrap::core::builder::StepDescription>::maybe_run
             at .\src\bootstrap\src\core\builder\mod.rs:476:13
  16: bootstrap::core::builder::cli_paths::match_paths_to_steps_and_run
             at .\src\bootstrap\src\core\builder\cli_paths.rs:141:22
  17: <bootstrap::core::builder::Builder>::run_step_descriptions
             at .\src\bootstrap\src\core\builder\mod.rs:1138:9
  18: <bootstrap::core::builder::Builder>::run_default_doc_steps
             at .\src\bootstrap\src\core\builder\mod.rs:1122:14
  19: <bootstrap::core::build_steps::dist::Docs as bootstrap::core::builder::Step>::run
             at .\src\bootstrap\src\core\build_steps\dist.rs:88:17
  20: <bootstrap::core::builder::Builder>::ensure::<bootstrap::core::build_steps::dist::Docs>
             at .\src\bootstrap\src\core\builder\mod.rs:1595:36
  21: <bootstrap::core::build_steps::dist::Docs as bootstrap::core::builder::Step>::make_run
             at .\src\bootstrap\src\core\build_steps\dist.rs:79:21
  22: <bootstrap::core::builder::StepDescription>::maybe_run
             at .\src\bootstrap\src\core\builder\mod.rs:476:13
  23: bootstrap::core::builder::cli_paths::match_paths_to_steps_and_run
             at .\src\bootstrap\src\core\builder\cli_paths.rs:141:22
  24: <bootstrap::core::builder::Builder>::run_step_descriptions
             at .\src\bootstrap\src\core\builder\mod.rs:1138:9
  25: <bootstrap::core::builder::Builder>::execute_cli
             at .\src\bootstrap\src\core\builder\mod.rs:1117:14
  26: <bootstrap::Build>::build
             at .\src\bootstrap\src\lib.rs:803:25
  27: bootstrap::main
             at .\src\bootstrap\src\bin\main.rs:130:11
  28: <fn() as core::ops::function::FnOnce<()>>::call_once
             at /rustc/ef0fb8a2563200e322fa4419f09f65a63742038c/library\core\src\ops\function.rs:250:5
  29: std::sys::backtrace::__rust_begin_short_backtrace::<fn(), ()>
             at /rustc/ef0fb8a2563200e322fa4419f09f65a63742038c/library\std\src\sys\backtrace.rs:166:18
  30: std::rt::lang_start::<()>::{closure#0}
             at /rustc/ef0fb8a2563200e322fa4419f09f65a63742038c/library\std\src\rt.rs:206:18
  31: <&dyn core::ops::function::Fn<(), Output = i32> + core::panic::unwind_safe::RefUnwindSafe + core::marker::Sync as core::ops::function::FnOnce<()>>::call_once
             at /rustc/ef0fb8a2563200e322fa4419f09f65a63742038c/library\core\src\ops/function.rs:287:21
  32: std::panicking::catch_unwind::do_call::<&dyn core::ops::function::Fn<(), Output = i32> + core::panic::unwind_safe::RefUnwindSafe + core::marker::Sync, i32>
             at /rustc/ef0fb8a2563200e322fa4419f09f65a63742038c/library\std\src/panicking.rs:581:40
  33: std::panicking::catch_unwind::<i32, &dyn core::ops::function::Fn<(), Output = i32> + core::panic::unwind_safe::RefUnwindSafe + core::marker::Sync>
             at /rustc/ef0fb8a2563200e322fa4419f09f65a63742038c/library\std\src/panicking.rs:544:19
  34: std::panic::catch_unwind::<&dyn core::ops::function::Fn<(), Output = i32> + core::panic::unwind_safe::RefUnwindSafe + core::marker::Sync, i32>
             at /rustc/ef0fb8a2563200e322fa4419f09f65a63742038c/library\std\src/panic.rs:359:14
  35: std::rt::lang_start_internal::{closure#0}
             at /rustc/ef0fb8a2563200e322fa4419f09f65a63742038c/library\std\src/rt.rs:175:24
  36: std::panicking::catch_unwind::do_call::<std::rt::lang_start_internal::{closure#0}, isize>
             at /rustc/ef0fb8a2563200e322fa4419f09f65a63742038c/library\std\src/panicking.rs:581:40
---
  44: <unknown>
  45: <unknown>


Command has failed. Rerun with -v to see more details.
Bootstrap failed while executing `dist bootstrap --include-default-paths`
Build completed unsuccessfully in 0:50:08
  local time: Tue May  5 00:50:25 CUT 2026
  network time: Tue, 05 May 2026 00:50:25 GMT
##[error]Process completed with exit code 1.
##[group]Run echo "disk usage:"

@rust-bors rust-bors Bot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels May 5, 2026
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented May 5, 2026

💔 Test for 2e5d870 failed: CI. Failed job:

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