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

Initialization by assignment not allowed for "drop-ful" types in constants. #62273

Closed
eddyb opened this issue Jul 1, 2019 · 1 comment · Fixed by #62275
Closed

Initialization by assignment not allowed for "drop-ful" types in constants. #62273

eddyb opened this issue Jul 1, 2019 · 1 comment · Fixed by #62275
Assignees
Labels
A-const-eval Area: constant evaluation (mir interpretation) A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@eddyb
Copy link
Member

eddyb commented Jul 1, 2019

In the following example, FOO and ASSIGN are allowed but FOO_ASSIGN isn't:

pub struct Foo;
impl Drop for Foo {
    fn drop(&mut self) {}
}

pub const FOO: Foo = { let x = Foo; x };
pub const ASSIGN: i32 = { let x; x = 0; x };
pub const FOO_ASSIGN: Foo = { let x; x = Foo; x };

The reason is that x = Foo; is lowered to DropAndReplace in MIR, and that's not handled in const-checking as a Drop and an assignment (which it functionally is).

cc @davidtwco This might also break the [constant; N] promotion (while &T promotion would never encounter DropAndReplace because &T is always Copy).

EDIT: nevermind, promotion works based on temporaries, so it shouldn't be affected.

cc @oli-obk @RalfJung @Centril

@eddyb eddyb added A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html A-const-eval Area: constant evaluation (mir interpretation) labels Jul 1, 2019
@Centril
Copy link
Contributor

Centril commented Jul 1, 2019

I'm going to label this as a bug since it seems covered by #57175 from a T-Lang perspective.

@Centril Centril added C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 1, 2019
@eddyb eddyb self-assigned this Jul 1, 2019
Centril added a commit to Centril/rust that referenced this issue Jul 10, 2019
rustc_mir: treat DropAndReplace as Drop + Assign in qualify_consts.

This slipped through the cracks and never got implemented (thankfully that just meant it was overly conservative and didn't allow assignments that don't *actually* drop the previous value).
Fixes rust-lang#62273.

r? @oli-obk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-eval Area: constant evaluation (mir interpretation) A-mir Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants