Skip to content

Compiler does not catch typing error in pattern matching when using literals #528

Description

@blumu

When literals are used in pattern matching there is no check that the subsequent expression/pattern argument list is empty, e.g.

module Color =
    let [<Literal>] Yellow = "Yellow"

let isRed inp = 
    match inp with
    | Color.Yellow arg ->   // should get an error here
        failwith "Don't know this color"

nor that the right arrow in the pattern match is not missing, e.g.

let isRed = function
    | Color.Red ->
        true
    | Color.Yellow               /// Notice the missing -> here
        you can insert what you want here like booleans false "strings" _and numbers 42. Yes it still compiles
    | _ -> 
        failwith "Don't know this color"

As a side effect the match on Color.Yellow never holds.

For reference when using discriminated unions instead of literals you get the expected behavior:

module Color =
    type Y = | Yellow | Green

let isRed inp = 
    match inp with
    | Color.Yellow arg -> // we do get an error here
        failwith "Don't know this color"

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugImpact-Medium(Internal MS Team use only) Describes an issue with moderate impact on existing code.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions