Skip to content

Match statement with a patterns referring to an out of scope identifier give incomplete warning #6712

@jbeeko

Description

@jbeeko

Related #5475.

What

The following code compiles with warnings which could be improved upon.

module DUs =
    type Countries = 
    | CA
    | US
    | GB
let x = DUs.CA

let output = 
    match x with 
    | US -> "US"
    | CA -> "CA"
    | _ -> "XX"
output

There is a warning on | US -> "US" that the value US is unused.
image

The default pattern has a warning that the rule will never be matched.
image

Why

If when seeing the warnings you not taking into account that the pattern can be a value or a binding this is confusing in two ways.

  1. In the above example is is obvious that US is out of scope. But in larger files this may not be obvious. Moreover normally referencing US without opening DUs would result in "The value or constructor US is not defined..." The message that it will never be used does not really point in the right direction.
  2. The warning that the default pattern will never be matched is also confusing. Why not, should they not always match. And if the CA is never used why does the default not match?

And when this code is run it results in "US" which can also be confusing.

Finally because there are only warnings this could result in a bug.

How

The compiler is assuming that | CA is a binding of x to CA not used on the right and emits a message to explain that situation. It should also suggest that CA might be an undefined value. Something like

"Binding CA is not used in this rule or CA refers to a value not defined..."

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions