Skip to content

Decide on semantics for enum -> integer casts on non-Copy enums. #35941

Closed

Description

Casting an enum which does not derive Copy to an integer moves atm:

enum E {
    A = 0,
    B = 1,
    C = 2
}

fn main() {
    let e = E::C;
    assert_eq!(e as u32, 2);
    assert_eq!(e as u32, 2); // error: use of moved value: `e`
}

This matches the behavior of other coercions/casts, specifically unsizing ones (Box<T> -> Box<Trait>).

However, one difference arises between old trans and MIR trans, wrt drops:

enum E {
    A = 0,
    B = 1,
    C = 2
}

impl Drop for E {
    fn drop(&mut self) {
        println!("drop");
    }
}

fn main() {
    let e = E::C;
    assert_eq!(e as u32, 2);
}

On stable, old trans will run the destructor exactly once. After #35764, MIR trans doesn't run it at all.
The reason is that the move that borrowck knows about causes the drops in MIR to be statically elided, whereas old trans forgot(?) to drop-fill e, resulting in the variable being dropped at end of scope.

Should the drop execute or not? IMO it's good to have uniformity between all moving operations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-MIRArea: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.htmlC-enhancementCategory: An issue proposing an enhancement or a PR with one.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language 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