Skip to content

Unsigned-repr discriminant enum sign-extends in 'as' casts (u8 200 as i32 is -56) #1454

Closed

Description

@ehartford

Spec: §4.4a ("Casting: value as i32 extracts the underlying integer (identity cast)"; the representation type is one of i8 … u64).

An enum whose repr is unsigned sign-extends when cast to a wider integer:

enum Kind: u8:
    Red = 1
    Hi = 200

enum W: u16:
    A = 1
    B = 40000

fn main:
    print(Kind.Hi as i32)
    print(Kind.Hi as i64)
    print(Kind.Hi as u32)
    print(W.B as i32)

Prints -56, -56, 200, -25536; expected 200, 200, 200, 40000. The cast treats the repr as signed; as u32 happens to be right.

Found while exhausting the #1444 matrix (stage1 of origin/main 1c76062 and of the #1444 branch; the cast path is not the match path #1444 fixes).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions