Skip to content

Conversation

clarfonthey
Copy link
Contributor

@clarfonthey clarfonthey commented Sep 4, 2025

Tracking: #109342
Supercedes: #145725

Makes methods const:

  • core::ptr::drop_in_place
  • core::mem::ManuallyDrop::drop
  • core::mem::MaybeUninit::assume_init_drop
  • <[core::mem::MaybeUninit<_>]>::assume_init_drop
  • <*mut _>::drop_in_place
  • core::ptr::NonNull::drop_in_place

@rustbot
Copy link
Collaborator

rustbot commented Sep 4, 2025

r? @ibraheemdev

rustbot has assigned @ibraheemdev.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added 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. labels Sep 4, 2025
@rust-log-analyzer

This comment has been minimized.

@clarfonthey
Copy link
Contributor Author

Was hoping just the destruct bound was the issue, but apparently not. Will have to dig deeper into this.

Until then,

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 4, 2025
@rustbot
Copy link
Collaborator

rustbot commented Sep 4, 2025

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot
Copy link
Collaborator

rustbot commented Sep 4, 2025

The Miri subtree was changed

cc @rust-lang/miri

@clarfonthey
Copy link
Contributor Author

Oh, the scary-looking errors were extremely simple to fix now that I actually investigated them. They were just copying the function signature in the error, which had obviously changed. Hopefully that's all.

@clarfonthey
Copy link
Contributor Author

That was all, it turns out.

@rustbot ready

@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 Sep 5, 2025
@RalfJung
Copy link
Member

RalfJung commented Sep 5, 2025

Please add a test that actually invokes drop_in_place in a const (and checks the destructor gets run).

@clarfonthey
Copy link
Contributor Author

clarfonthey commented Sep 5, 2025

Thank you not only for reminding me to write that test, but also for helping me notice that I somehow missed ManuallyDrop::drop

Destructor tests are weird to write, but what I have should be reasonable, I think.

@rustbot
Copy link
Collaborator

rustbot commented Sep 5, 2025

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

@theemathas
Copy link
Contributor

theemathas commented Sep 6, 2025

This code compiles with this PR. Is this intended?

#![feature(const_trait_impl, const_destruct, const_drop_in_place)]

use std::ptr::drop_in_place;

struct Thing(i32);

impl const Drop for Thing {
    fn drop(&mut self) {}
}

static mut X: Thing = {
    unsafe {
        drop_in_place(&raw mut X);
    }
    Thing(1)
};

This also compiles:

#![feature(const_trait_impl, const_destruct, const_drop_in_place)]

use std::ptr::drop_in_place;

struct Thing(i32);

impl const Drop for Thing {
    fn drop(&mut self) {}
}

static mut Y: Thing = Thing(2);

const X: i32 = {
    unsafe {
        drop_in_place(&raw mut Y);
    }
    1
};

This also compiles:

#![feature(const_trait_impl, const_destruct, const_drop_in_place)]

use std::ptr::drop_in_place;

struct Thing(i32);

impl const Drop for Thing {
    fn drop(&mut self) {}
}

static Y: Thing = Thing(2);

const X: i32 = {
    unsafe {
        drop_in_place(&raw const Y as *mut Thing);
    }
    1
};

@theemathas
Copy link
Contributor

This has... interesting interaction with packed structs. The following code compiles:

#![feature(const_trait_impl, const_destruct, const_drop_in_place)]

use std::ptr::drop_in_place;

struct Thing(i32);

impl const Drop for Thing {
    fn drop(&mut self) {
        self.0 = 2;
    }
}

#[repr(packed)]
struct Packed(Thing);

static Y: Packed = Packed(Thing(1));

const X: () = {
    unsafe {
        drop_in_place((&raw const Y).cast_mut());
    }
};

See also #143411

@clarfonthey
Copy link
Contributor Author

clarfonthey commented Sep 6, 2025

I mean, you've certainly pointed out a lot of compelling reasons why we shouldn't stabilise this feature right now, but also, we can't really test that all those cases aren't occurring without having the unstable constness in the first place.

It's working, just… well, we now have new cases of UB to track and prevent. This is also why we explicitly track which unstable const features are used in stable items, to ensure that we don't accidentally stabilise something unsound. It would, for example, prevent us from properly stabilising const select_unpredictable, which uses this method, until these issues are fixed.

@oli-obk
Copy link
Contributor

oli-obk commented Sep 10, 2025

This code compiles with this PR. Is this intended?

I think that's a question for opsem to answer. I don't see why this is a new issue considering you can also do all the same at runtime right now (and it would error at compile-time if any mutation would actually happen in the drop impl, at least I think it would?). Or am I missing the point of your code examples entirely? 😅

@RalfJung
Copy link
Member

You can't really do this at runtime? Recursive static initializers are a magic special case.
But it is similar to #143047.

@theemathas
Copy link
Contributor

Turns out that Miri also behaves strangely around drop_in_place with static mut at run time. I've reported this as rust-lang/miri#4574.

@oli-obk
Copy link
Contributor

oli-obk commented Sep 10, 2025

You can't really do this at runtime? Recursive static initializers are a magic special case.

Ah that's what I was missing. But even then, only the first case uses that to get references to uninit memory in the drop impl. The others I don't see a difference to runtime.

@clarfonthey
Copy link
Contributor Author

clarfonthey commented Sep 26, 2025

Would someone from wg-const-eval be willing to do a proper review of this? I chose the set of methods mostly as just, anything that just defers to ptr::drop_in_place, since I figure they should all be under the same feature flag, and it makes more sense for one of the wg-const-eval folks to review this instead of just rerolling the libs pool, since it is pretty fundamental to const-eval.

(I'm asking this instead of trying to roll wg-const-eval for the review since I don't think that works, but if it does, I can do that instead.)

@ibraheemdev
Copy link
Member

r? wg-const-eval

@rustbot rustbot assigned RalfJung and unassigned ibraheemdev Oct 14, 2025
@RalfJung
Copy link
Member

Sorry, I don't have time for more reviews currently.
r? @fee1-dead

@rustbot rustbot assigned fee1-dead and unassigned RalfJung Oct 14, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 14, 2025

fee1-dead is currently at their maximum review capacity.
They may take a while to respond.

Copy link
Contributor

@oli-obk oli-obk left a comment

Choose a reason for hiding this comment

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

Sorry it took so long

View changes since this review

@oli-obk
Copy link
Contributor

oli-obk commented Oct 14, 2025

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Oct 14, 2025

📌 Commit 07d3e92 has been approved by oli-obk

It is now in the queue for this repository.

@bors bors 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 Oct 14, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Oct 14, 2025
…=oli-obk

Unstably constify `ptr::drop_in_place` and related methods

Tracking: rust-lang#109342
Supercedes: rust-lang#145725

Makes methods const:

* `core::ptr::drop_in_place`
* `core::mem::ManuallyDrop::drop`
* `core::mem::MaybeUninit::assume_init_drop`
* `<[core::mem::MaybeUninit<_>]>::assume_init_drop`
* `<*mut _>::drop_in_place`
* `core::ptr::NonNull::drop_in_place`
bors added a commit that referenced this pull request Oct 14, 2025
Rollup of 7 pull requests

Successful merges:

 - #146187 (Unstably constify `ptr::drop_in_place` and related methods)
 - #146503 (std: improve handling of timed condition variable waits on macOS)
 - #147421 (Add check if span is from macro expansion)
 - #147630 (Bitset cleanups)
 - #147666 (Replace manual implementation with `carrying_mul_add`)
 - #147669 (fix missing link to `std::char` in `std` docs)
 - #147673 (pretty print u128 with display)

r? `@ghost`
`@rustbot` modify labels: rollup
jdonszelmann added a commit to jdonszelmann/rust that referenced this pull request Oct 14, 2025
…=oli-obk

Unstably constify `ptr::drop_in_place` and related methods

Tracking: rust-lang#109342
Supercedes: rust-lang#145725

Makes methods const:

* `core::ptr::drop_in_place`
* `core::mem::ManuallyDrop::drop`
* `core::mem::MaybeUninit::assume_init_drop`
* `<[core::mem::MaybeUninit<_>]>::assume_init_drop`
* `<*mut _>::drop_in_place`
* `core::ptr::NonNull::drop_in_place`
bors added a commit that referenced this pull request Oct 14, 2025
Rollup of 7 pull requests

Successful merges:

 - #146187 (Unstably constify `ptr::drop_in_place` and related methods)
 - #146503 (std: improve handling of timed condition variable waits on macOS)
 - #147421 (Add check if span is from macro expansion)
 - #147630 (Bitset cleanups)
 - #147666 (Replace manual implementation with `carrying_mul_add`)
 - #147669 (fix missing link to `std::char` in `std` docs)
 - #147673 (pretty print u128 with display)

r? `@ghost`
`@rustbot` modify labels: rollup
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Oct 14, 2025
…=oli-obk

Unstably constify `ptr::drop_in_place` and related methods

Tracking: rust-lang#109342
Supercedes: rust-lang#145725

Makes methods const:

* `core::ptr::drop_in_place`
* `core::mem::ManuallyDrop::drop`
* `core::mem::MaybeUninit::assume_init_drop`
* `<[core::mem::MaybeUninit<_>]>::assume_init_drop`
* `<*mut _>::drop_in_place`
* `core::ptr::NonNull::drop_in_place`
bors added a commit that referenced this pull request Oct 14, 2025
Rollup of 12 pull requests

Successful merges:

 - #146187 (Unstably constify `ptr::drop_in_place` and related methods)
 - #146503 (std: improve handling of timed condition variable waits on macOS)
 - #147526 (Move computation of allocator shim contents to cg_ssa)
 - #147630 (Bitset cleanups)
 - #147638 (bpf: return results larger than one register indirectly)
 - #147666 (Replace manual implementation with `carrying_mul_add`)
 - #147669 (fix missing link to `std::char` in `std` docs)
 - #147673 (pretty print u128 with display)
 - #147677 (Fewer exceptions in `span()` on parsed attributes)
 - #147680 (Fix ICE caused by associated_item_def_ids on wrong type in resolve diag)
 - #147682 (convert `rustc_main` to the new attribute parsing infrastructure)
 - #147683 (only check duplicates on old/unparsed attributes)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 252974a into rust-lang:master Oct 15, 2025
10 checks passed
@rustbot rustbot added this to the 1.92.0 milestone Oct 15, 2025
rust-timer added a commit that referenced this pull request Oct 15, 2025
Rollup merge of #146187 - clarfonthey:const-drop-in-place, r=oli-obk

Unstably constify `ptr::drop_in_place` and related methods

Tracking: #109342
Supercedes: #145725

Makes methods const:

* `core::ptr::drop_in_place`
* `core::mem::ManuallyDrop::drop`
* `core::mem::MaybeUninit::assume_init_drop`
* `<[core::mem::MaybeUninit<_>]>::assume_init_drop`
* `<*mut _>::drop_in_place`
* `core::ptr::NonNull::drop_in_place`
@clarfonthey clarfonthey deleted the const-drop-in-place branch October 15, 2025 00:40
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request Oct 15, 2025
Rollup of 12 pull requests

Successful merges:

 - rust-lang/rust#146187 (Unstably constify `ptr::drop_in_place` and related methods)
 - rust-lang/rust#146503 (std: improve handling of timed condition variable waits on macOS)
 - rust-lang/rust#147526 (Move computation of allocator shim contents to cg_ssa)
 - rust-lang/rust#147630 (Bitset cleanups)
 - rust-lang/rust#147638 (bpf: return results larger than one register indirectly)
 - rust-lang/rust#147666 (Replace manual implementation with `carrying_mul_add`)
 - rust-lang/rust#147669 (fix missing link to `std::char` in `std` docs)
 - rust-lang/rust#147673 (pretty print u128 with display)
 - rust-lang/rust#147677 (Fewer exceptions in `span()` on parsed attributes)
 - rust-lang/rust#147680 (Fix ICE caused by associated_item_def_ids on wrong type in resolve diag)
 - rust-lang/rust#147682 (convert `rustc_main` to the new attribute parsing infrastructure)
 - rust-lang/rust#147683 (only check duplicates on old/unparsed attributes)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

9 participants