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

Too lenient #[may_dangle] on BTreeMap; misses PhantomData for dropck #99408

Closed
steffahn opened this issue Jul 18, 2022 · 6 comments · Fixed by #99413
Closed

Too lenient #[may_dangle] on BTreeMap; misses PhantomData for dropck #99408

steffahn opened this issue Jul 18, 2022 · 6 comments · Fixed by #99413
Assignees
Labels
C-bug Category: This is a bug. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@steffahn
Copy link
Member

steffahn commented Jul 18, 2022

struct PrintOnDrop<'a>(&'a str);

impl Drop for PrintOnDrop<'_> {
    fn drop(&mut self) {
        println!("printint: {}", self.0);
    }
}

use std::collections::BTreeMap;

fn main() {
    let s = String::from("Hello World!");
    let map = BTreeMap::from_iter([((), PrintOnDrop(&s))]);
    drop(s);
}

(playground)

printing: ��J��U��J�

@rustbot label T-libs, I-unsound
@rustbot claim

@steffahn steffahn added the C-bug Category: This is a bug. label Jul 18, 2022
@rustbot rustbot added I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness T-libs Relevant to the library team, which will review and decide on the PR/issue. I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Jul 18, 2022
@steffahn
Copy link
Member Author

This is actually also a (not too recent) regression (in nightly-2019-10-18).

********************************************************************************
Regression in nightly-2019-10-18
********************************************************************************

fetching https://static.rust-lang.org/dist/2019-10-17/channel-rust-nightly-git-commit-hash.txt
nightly manifest 2019-10-17: 40 B / 40 B [===================================================================] 100.00 % 737.29 KB/s converted 2019-10-17 to 0e8a4b441c5da21a2cb19448728ade5baa299c66
fetching https://static.rust-lang.org/dist/2019-10-18/channel-rust-nightly-git-commit-hash.txt
nightly manifest 2019-10-18: 40 B / 40 B [===================================================================] 100.00 % 772.54 KB/s converted 2019-10-18 to fa0f7d0080d8e7e9eb20aa9cbf8013f96c81287f
looking for regression commit between 2019-10-17 and 2019-10-18
fetching (via remote github) commits from max(0e8a4b441c5da21a2cb19448728ade5baa299c66, 2019-10-15) to fa0f7d0080d8e7e9eb20aa9cbf8013f96c81287f
ending github query because we found starting sha: 0e8a4b441c5da21a2cb19448728ade5baa299c66
get_commits_between returning commits, len: 7
  commit[0] 2019-10-16UTC: Auto merge of #65445 - ehuss:update-cargo-books, r=alexcrichton
  commit[1] 2019-10-16UTC: Auto merge of #63756 - Zoxc:sharded-dep-graph-1, r=nikomatsakis
  commit[2] 2019-10-17UTC: Auto merge of #65234 - GuillaumeGomez:long-err-explanation-E0573, r=kinnison
  commit[3] 2019-10-17UTC: Auto merge of #65251 - tlively:emscripten-upstream-upgrade, r=tlively
  commit[4] 2019-10-17UTC: Auto merge of #59953 - eddyb:soa-metadata, r=michaelwoerister
  commit[5] 2019-10-17UTC: Auto merge of #64595 - Mark-Simulacrum:trivial-query, r=pnkfelix
  commit[6] 2019-10-17UTC: Auto merge of #65495 - Centril:rollup-tguwjt5, r=Centril

So maybe #64595 made a difference?


@rustbot label regression-from-stable-to-stable

@rustbot rustbot added the regression-from-stable-to-stable Performance or correctness regression from one stable version to another. label Jul 18, 2022
@steffahn
Copy link
Member Author

steffahn commented Jul 18, 2022

This is what changed apparently, the following compiles successfully since nightly 2019-10-18:

#![feature(dropck_eyepatch)]

use std::mem::ManuallyDrop;

struct S<T>(ManuallyDrop<T>);

unsafe impl<#[may_dangle] T> Drop for S<T> {
    fn drop(&mut self) {}
}

struct NonTrivialDrop<'a>(&'a str);
impl<'a> Drop for NonTrivialDrop<'a> {
    fn drop(&mut self) {}
}

fn main() {
    let s = String::from("string");
    let _t = S(ManuallyDrop::new(NonTrivialDrop(&s)));
    drop(s);
}

I mean, yeah, this change was good IMO, so it is just the library that needs fixing, dropck itself seems fine.

@m-ou-se
Copy link
Member

m-ou-se commented Jul 20, 2022

It's probably time we move https://forge.rust-lang.org/libs/maintaining-std.html#is-there-a-manual-drop-implementation to the std-dev-guide.

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jul 21, 2022
Add `PhantomData` marker for dropck to `BTreeMap`

closes rust-lang#99408
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jul 21, 2022
Add `PhantomData` marker for dropck to `BTreeMap`

closes rust-lang#99408
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jul 21, 2022
Add `PhantomData` marker for dropck to `BTreeMap`

closes rust-lang#99408
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jul 21, 2022
Add `PhantomData` marker for dropck to `BTreeMap`

closes rust-lang#99408
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jul 21, 2022
Add `PhantomData` marker for dropck to `BTreeMap`

closes rust-lang#99408
@bors bors closed this as completed in 110fdb6 Jul 21, 2022
@apiraino apiraino removed the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Oct 26, 2022
@RalfJung
Copy link
Member

So maybe #64595 made a difference?

That PR sounds like it was meant to be an optimization, not changing behavior. There are no new tests. And yet it turns out that it did change behavior!
Cc @Mark-Simulacrum @pnkfelix

Specifically, I think this is when dropck started exploiting that ManuallyDrop does not drop its fields. I will add a corresponding testcase to #103413.

@Mark-Simulacrum
Copy link
Member

Yes, I seem to recall us fixing fallout from that PR a while back - it did indeed (unintentionally) change behavior, though I don't have in cache what went wrong with the changes.

@RalfJung
Copy link
Member

By "fixing" you mean bring back the original behavior? That does not seem to be entirely the case; as this issue shows, one change in behavior did remain in place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants