Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Constant folding a pattern match on an unboxed variant produces an incorrect result #6950

@Innf107

Description

@Innf107

Given an unboxed variant like this, Primary has the same runtime representation as Color("primary")

@unboxed
type color =
  | @as("primary") Primary
  | @as("secondary") Secondary
  | Color(string)

This means that a function like

let f = x =>
  switch x {
  | Color(x) => x
  | _ => "not Color"
  }

somewhat surprisingly produces "not Color" when called on Color("primary").

However, the optimizer treats this like a regular variant and so constant folding f(Color("primary")) produces "primary", which differs from the result of the generated JS.

Full playground example

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    Status

    Backlog

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions