Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MIRI disagree with the documentation of ptr::drop_in_place #112015

Closed
SkiFire13 opened this issue May 27, 2023 · 3 comments · Fixed by #125739
Closed

MIRI disagree with the documentation of ptr::drop_in_place #112015

SkiFire13 opened this issue May 27, 2023 · 3 comments · Fixed by #125739
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools T-opsem Relevant to the opsem team

Comments

@SkiFire13
Copy link
Contributor

Location

https://doc.rust-lang.org/stable/std/ptr/fn.drop_in_place.html

Summary

The documentation for ptr::drop_in_place claims that:

This is semantically equivalent to calling ptr::read and discarding the result.

However when tested in a playground MIRI complained about the use of ptr::read but not of ptr::drop_in_place: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=c3dfe878ef015b93f051785b67239752

Although the ptr::drop_in_place documentation lists a bunch of cases where they differ, none of them seems to apply to this one.

Some details of the reproduction:

  • it uses #[may_dangle] both because the issue derived from this user.rust-lang.com thread on #[may_dangle] and because I didn't find a quicker way to create a dangling reference. However this doesn't seem fundamental to the issue and likely is not required.
  • this seems to only work with exclusive references, MIRI won't complain with shared ones
@SkiFire13 SkiFire13 added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label May 27, 2023
@Noratrieb Noratrieb added the T-opsem Relevant to the opsem team label May 27, 2023
@slightlyoutofphase
Copy link
Contributor

Neither function has a "real" Rust implementation, but it does seem like their internal instantiation occurs in different places within the compiler. Probably this is related to that in some way.

@RalfJung
Copy link
Member

RalfJung commented Aug 27, 2023

Yeah the documentation is imprecise and conservative. It mostly wants to make sure people don't cause UB.^^ So we document the safety constraint as equivalent to ptr::read just to be safe; it is actually weaker. However we don't yet want to commit to how much weaker it is, so we're saying they are actually the same.

This is a somewhat dubious use of "semantically equivalent" -- basically the equivalence is a library-level spec and what you are doing causes library UB, but currently does not cause language UB. The library might change in a way that it can become language UB in the future.

@RalfJung
Copy link
Member

RalfJung commented Sep 9, 2023

Also the claim "This is semantically equivalent to calling ptr::read and discarding the result" is definitely wrong for pinned types, where it is important that we do the drop in-place.

It might be better to just remove this sentence.

EDI: Actually that part is spelled out in the docs.

@bors bors closed this as completed in 60c2d80 May 30, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue May 30, 2024
Rollup merge of rust-lang#125739 - RalfJung:drop-in-place-docs, r=workingjubilee

drop_in_place: weaken the claim of equivalence with drop(ptr.read())

The two are *not* semantically equivalent in all cases, so let's not be so definite about this.

Fixes rust-lang#112015
github-actions bot pushed a commit to rust-lang/miri that referenced this issue Jun 5, 2024
drop_in_place: weaken the claim of equivalence with drop(ptr.read())

The two are *not* semantically equivalent in all cases, so let's not be so definite about this.

Fixes rust-lang/rust#112015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools T-opsem Relevant to the opsem team
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants