Skip to content

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

Closed
@eddyb

Description

@eddyb

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

Metadata

Metadata

Assignees

Labels

A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlA-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions