Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Innf107 opened this issue Aug 13, 2024 · 2 comments
Labels
Milestone

Comments

@Innf107
Copy link

Innf107 commented Aug 13, 2024

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

@cristianoc cristianoc added the bug label Aug 13, 2024
@cristianoc
Copy link
Collaborator

Good catch.

@cknitt cknitt added this to the v12 milestone Aug 25, 2024
@tsnobip
Copy link
Contributor

tsnobip commented Sep 26, 2024

as discussed today, there are two issues here, the wrong inlining/optimization when using a literal and the use of literal inside the "catch-all" case.

There are 3 things we could do:

  1. check if the inlining/optimization can be improved (but the semantic here is unclear anyway)
  2. raise a warning when using a catch-all case with a literal (it was not really meant to be used that way, it was designed to handle the cases not handled at compile time, otherwise the defined variant cases are here for that)
  3. even better, raise a warning only when using in the catch-all case a literal that overlaps with the existing defined variant cases

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Backlog
Development

No branches or pull requests

4 participants