Skip to content

Codegen issue when type is None | Some('a) #5169

Closed

Description

@diaozheng999

Whenever there is an option-like variant being introduced, such as:

module NotOption = {
  type t<'a> = None | Some('a)
}

The constructors generated do not match the patterns:

let a = NotOption.None // 0, ok..
let b = NotOption.Some(3) // 3, should generate { _0: 3 }

let f = notOption => switch notOption {
| NotOption.None => (...) // pattern compiles to an undefined check, should be checking 0
| NotOption.Some(_) => (...)
}

Adding a type equality, such as below, will also result in code that compiles but emits incorrect code at runtime.

module NotOption = {
  type t<'a> = option<'a> = None | Some('a)
}

let a = NotOption.None // 0, not undefined
let b = NotOption.Some(3) // 3

In more detail, we have:

module NotOption = {
  type t<'a> = None | Some('a)
}

let a = NotOption.None // 0
let b = NotOption.Some(1) // 1
let c = { open NotOption; None } // undefined
let d = { open NotOption; Some(1) } // 1

// changing the order doesn't resolve the issue
module TotallyNotOption = {
  type t<'a> = Some('a) | None
}

let e = TotallyNotOption.None // 0
let f = TotallyNotOption.Some(1) // 1
let g = { open TotallyNotOption; None } // undefined
let h = { open TotallyNotOption; Some(1) } // 1

// adding a third variant resolves this issue
module NotOption2 = {
  type t<'a> = None | Some('a) | Bogus
}

let i = NotOption2.None // 0
let j = NotOption2.Some(1) // { _0: 1 }
let k = { open NotOption2; None } // 0
let l = { open NotOption2; Some(1) } // { _0: 1 }

Example with unrelated type with the same variants as option

https://rescript-lang.org/try?code=LYewJgrgNgpgBAeQA4BcCWIB2cC8cDeAUHHCgJ5LwoA8A5AIYB8ucIqGmdTuxJcAPnAByWGLxKCAyiGAwAFAwCUhAL6FCsFHCQtk6LADoRmMRphaATiwswAZnICMys1tvY8OnI14BnAO5oKADGABbaBLyCehwG0rJyAG6KuMwWBkFYKDCYKD4sCZGI7IbG8F5wcsokaoRuckjOAFI+BlAgAOZyaRk52bnJAPQDcD4hINBgcABG8A6EQA

Example with redefined constructors for option

https://rescript-lang.org/try?code=LYewJgrgNgpgBAeQA4BcCWIB2cC8cDeAUHHCgJ5LwoA8A5AIYB8ucIqGmdTuxJcAPnAByWGLxKCAyiGAwAFAwCUhAL6FCsFHCQtk6LADoRmMRphaAzi3ytK2PRwDcw0XDVmtAJxaeYAMzkARmUPOD9sPB0cRl4LAHc0FABjAAttAl5BB0NpWTkAN0VcZk8DJKwUGEwUKzx8zMR2Q2N4aLg5ZRJ3cLkkEIApCwMoEABzOVLy6qqaooB6ObgLFJBoMDgAI3hAwiA

Activity

  1. bobzhang commented on Jun 7, 2021

    @bobzhang
    Member

    this seems to be a bug, but in general it is not recommended to redefine built in types

  2. bobzhang commented on Jun 8, 2021

    @bobzhang
    Member

    @diaozheng999 will send a fix and cherry pick to 9.1

  3. added a commit that references this issue on Jun 11, 2021
    5255464
  4. added 3 commits that reference this issue on Jun 11, 2021
    6e73ee9
    53d2507
    eaff193
  5. added theissue type on Jul 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions