guibou / PyF

Haskell QuasiQuoter for String Formatting
BSD 3-Clause "New" or "Revised" License
66 stars 13 forks source link

Pattern matches are non-exhaustive #118

Closed erikd closed 1 year ago

erikd commented 2 years ago

Compiling git HEAD I get:

src/PyF/Formatters.hs:177:9: warning: [-Wincomplete-uni-patterns]
    Pattern match(es) are non-exhaustive
    In a pattern binding:
        Patterns not matched:
            FractionalRepr Negative [] [] []
            FractionalRepr Negative [] [] (_:_)
            FractionalRepr Negative [] (_:_) []
            FractionalRepr Negative [] (_:_) (_:_)
            ...
    |
177 |     let (FractionalRepr Positive aa bb cc) = reprFractional fmt precision (abs f)
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

src/PyF/Formatters.hs:191:22: warning: [-Wincomplete-uni-patterns]
    Pattern match(es) are non-exhaustive
    In a pattern binding:
        Patterns not matched:
            ([], [], (_:_))
            ([], (_:_), (_:_))
            ((_:_), [], (_:_))
            ((_:_), (_:_), (_:_))
    |
191 |       Percent -> let (a, b, "") = splitFractional (Numeric.showFFloatAlt precision (iAbs * 100) "") in (a, b, "%")

Even if these extra options are basically impossible, the compiler can't prove that so the dev has to satisfy the compiler.

guibou commented 2 years ago

Thank you for opening this issue.

Yes, this part of the code is old and I never really took the time to design a solution.

As you said, in theory, that's basically impossible, but software engineering is full of realized impossible stories.

I'm open to suggestion, otherwise I'll try to fix that.

erikd commented 2 years ago

Even just a wild card pattern match like:

  | isNegativeZero f =
      case reprFractional fmt precision (abs f) of
        FractionalRepr _ aa bb cc -> FractionalRepr Negative aa bb cc
        other -> error $ "reprFractional (isNegativeZero f): The impossible happened : " ++ show other

would be preferable as it satisfies the compiler and if the impossible does actually happen gives a better error message than the normal incomplete pattern match exception.