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

Do not promote values with const drop that need to be dropped #89988

Merged
merged 2 commits into from
Oct 19, 2021

Conversation

tmiasko
Copy link
Contributor

@tmiasko tmiasko commented Oct 17, 2021

Changes from #88558 allowed using ~const Drop in constants by
introducing a new NeedsNonConstDrop qualif.

The new qualif was also used for promotion purposes, and allowed
promotion to happen for values that needs to be dropped but which
do have a const drop impl.

Since for promoted the drop implementation is never executed,
this lead to observable change in behaviour. For example:

struct Panic();

impl const Drop for Panic {
    fn drop(&mut self) {
        panic!();
    }
}

fn main() {
    let _ = &Panic();
}

Restore the use of NeedsDrop qualif during promotion to avoid the issue.

@rust-highfive
Copy link
Collaborator

r? @jackh726

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 17, 2021
@tmiasko
Copy link
Contributor Author

tmiasko commented Oct 17, 2021

@oli-obk
Copy link
Contributor

oli-obk commented Oct 17, 2021

r? @oli-obk

@rust-highfive rust-highfive assigned oli-obk and unassigned jackh726 Oct 17, 2021
@tmiasko tmiasko force-pushed the unpromote-const-drop branch 2 times, most recently from 11496d4 to f8ea04e Compare October 18, 2021 06:43
@oli-obk
Copy link
Contributor

oli-obk commented Oct 18, 2021

@bors
Copy link
Contributor

bors commented Oct 18, 2021

📌 Commit f8ea04e3a33eeb29438889e162c977a3fd5314d4 has been approved by oli-obk

@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 18, 2021
}

/// Constant containing an ADT that implements non-const `Drop`.
/// This must be ruled out because we cannot run `Drop` during compile-time.
pub struct NeedsNonConstDrop;
Copy link
Member

Choose a reason for hiding this comment

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

Why do we have both NeedsDrop and NeedsNonConrstDrop? That seems rather redundant.

Copy link
Contributor

Choose a reason for hiding this comment

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

We only want to detect nonconst drops in const items, as const drops can actually get executed just fine.

Copy link
Member

Choose a reason for hiding this comment

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

So why does check_consts have NeedsDrop then? Promotion is a separate pass after all.

@oli-obk
Copy link
Contributor

oli-obk commented Oct 18, 2021

@bors r-

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Oct 18, 2021
Changes from rust-lang#88558 allowed using `~const Drop` in constants by
introducing a new `NeedsNonConstDrop` qualif.

The new qualif was also used for promotion purposes, and allowed
promotion to happen for values that needs to be dropped but which
do have a const drop impl.

Since for promoted the drop implementation is never executed,
this lead to observable change in behaviour. For example:

```rust

struct Panic();

impl const Drop for Panic {
    fn drop(&mut self) {
        panic!();
    }
}

fn main() {
    let _ = &Panic();
}
```

Restore the use of `NeedsDrop` qualif during promotion to avoid the issue.
@oli-obk
Copy link
Contributor

oli-obk commented Oct 18, 2021

@bors r+

@bors
Copy link
Contributor

bors commented Oct 18, 2021

📌 Commit 915a581 has been approved by oli-obk

@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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 18, 2021
/// This must be ruled out because implicit promotion would remove side-effects
/// that occur as part of dropping that value. N.B., the implicit promotion has
/// to reject const Drop implementations because even if side-effects are ruled
/// out through other means, the execution of the drop could diverge.
Copy link
Member

Choose a reason for hiding this comment

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

"diverge" means "running into an endless loop", right? Panics seem like a much simpler example to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The sentence was intended to encompass panicking. In the context of rustc we generally use diverge to describe a computation that does not return normally, but I can see where you are coming from.

Copy link
Member

Choose a reason for hiding this comment

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

Oh I see. Yeah I think that is not standard terminology, at least not in my peer group. ;)

bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 19, 2021
…askrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#89766 (RustWrapper: adapt for an LLVM API change)
 - rust-lang#89867 (Fix macro_rules! duplication when reexported in the same module)
 - rust-lang#89941 (removing TLS support in x86_64-unknown-none-hermitkernel)
 - rust-lang#89956 (Suggest a case insensitive match name regardless of levenshtein distance)
 - rust-lang#89988 (Do not promote values with const drop that need to be dropped)
 - rust-lang#89997 (Add test for issue rust-lang#84957 - `str.as_bytes()` in a `const` expression)
 - rust-lang#90002 (:arrow_up: rust-analyzer)
 - rust-lang#90034 (Tiny tweak to Iterator::unzip() doc comment example.)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit a0724d7 into rust-lang:master Oct 19, 2021
@rustbot rustbot added this to the 1.58.0 milestone Oct 19, 2021
@tmiasko tmiasko deleted the unpromote-const-drop branch October 19, 2021 08:15
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants