Skip to content

open type does not bring enum cases into scope in patterns #17630

Open
@brianrourkeboll

Description

@brianrourkeboll

I meant to open this years ago, but I guess I never did.

Repro steps

type E = A = 1 | B = 2

open type E

let f x =
    match x with
    | A -> () // This binds a new value `A` instead of matching the enum case E.A, which is otherwise in scope unqualified.

Expected behavior

Enum cases brought into scope with an open type declaration (or an AutoOpen on the enum definition) should be in scope in pattern matching as unqualified literals, just like union cases or active pattern cases.

E.g., if you change the enum in the example above to a union type, A is brought into scope in something like this:

module M =
    type E = A | B

open type M.E

let f x =
    match x with
    | A -> ()
    | B -> ()

Actual behavior

Enum cases brought into scope with an open type declaration (or an AutoOpen on the enum definition) are not in scope in pattern matching as unqualified literals.

Known workarounds

N/A.

Related information

.NET 8, 9.

Note

I guess this probably can't be fixed, since there may now be existing code that relies on the current behavior — although it is quite possible that some of that existing code is wrong, because it was written with the understanding that open type would behave the same for enums as it does for unions, etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-Compiler-CheckingType checking, attributes and all aspects of logic checkingBreaking-changeDescribes a bug which is also a breaking change.BugImpact-Low(Internal MS Team use only) Describes an issue with limited impact on existing code.help wanted

    Type

    Projects

    Status

    New

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions