Skip to content

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

Closed

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

Activity

  1. added
    A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html
    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)
    on Jul 1, 2019
  2. Centril commented on Jul 1, 2019

    @Centril
    Contributor

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

  3. added
    C-bugCategory: This is a bug.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Jul 1, 2019
  4. self-assigned this
    on Jul 1, 2019
  5. added a commit that references this issue on Jul 10, 2019
    a2cbae8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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