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

Improve Error Reporting: Unnecessarily vague pattern match warning #3765

Open
Tracked by #1103
isaacabraham opened this issue Oct 17, 2017 · 2 comments
Open
Tracked by #1103
Labels
Area-Compiler-PatternMatching pattern compilation, active patterns, performance, codegen Feature Improvement Impact-Medium (Internal MS Team use only) Describes an issue with moderate impact on existing code. Theme-Simple-F# A cross-community initiative called "Simple F#", keeping people in the sweet spot of the language.
Milestone

Comments

@isaacabraham
Copy link
Contributor

What

Pattern matching provides an exhaustiveness warning if you fail to match against all cases. However, the match seems to be generalised as much as possible for e.g. tuples or records, even when it is clear that there is only a single case outstanding e.g given the following code:

type Colour = Red | Yellow
type Size = Big | Small

let describe thing =
    match thing with
    | Red, Big -> "Big and red"
    | Red, Small -> "Small and red"
    | Yellow, Big -> "Big and yellow"

we are given the following warning:

warning FS0025: Incomplete pattern matches on this expression. For example, the value '(_,Small)' may indicate a case not covered by the pattern(s).

In reality, there is only one case outstanding - Yellow, Small.

Why

There are two reasons that this should be improved:

  1. If there is only one outstanding value, it's easier to reason about if the compiler gave that case. This could also lead to a code refactoring to implement that case.
  2. Leaving the _ in may be confusing to newcomers. A concrete example would be easier to understand.

How

I believe that the warning should be changed to always give a fully concrete example where possible e.g. numbers, strings, DU cases etc.. for all elements in a Tuple or Record.

Whilst we're at it, the wording could be slightly improved, with more guidance:

warning FS0025: The pattern match over this expression cannot be proved to account for all cases. For example, the value '(Yellow, Small)' may indicate a case not covered. You should account for all cases either by adding extra patterns for each case, or through the use of the _ (wildcard) pattern. Leaving unhandled cases in a pattern match can lead to runtime errors, and is not recommended.

I know that this is more verbose, but adding the explanation at the end will again provide a bit of guidance to new developers as well as explaining the risks of not doing it.

@cartermp cartermp added this to the Unknown milestone Aug 25, 2018
@dsyme dsyme added the Theme-Simple-F# A cross-community initiative called "Simple F#", keeping people in the sweet spot of the language. label Sep 16, 2021
@dsyme dsyme added the Impact-Medium (Internal MS Team use only) Describes an issue with moderate impact on existing code. label Nov 4, 2021
@isaacabraham
Copy link
Contributor Author

Would there be support for adding the more verbose message as a first step (as above), which could be done relatively cheaply?

@dsyme
Copy link
Contributor

dsyme commented Jan 21, 2022

Would there be support for adding the more verbose message as a first step (as above), which could be done relatively cheaply?

In principle yes. The problem is making sure the search for that one remaining solution doesn't explode exponentially in other cases.

@dsyme dsyme added Area-Compiler-PatternMatching pattern compilation, active patterns, performance, codegen and removed Area-Compiler labels Mar 31, 2022
@vzarytovskii vzarytovskii reopened this Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compiler-PatternMatching pattern compilation, active patterns, performance, codegen Feature Improvement Impact-Medium (Internal MS Team use only) Describes an issue with moderate impact on existing code. Theme-Simple-F# A cross-community initiative called "Simple F#", keeping people in the sweet spot of the language.
Projects
Archived in project
Development

No branches or pull requests

5 participants