on 5b5230c help sign in
elk/src/Reporting/Error/Pattern.hs
92 lines 2.5 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2module Reporting.Error.Pattern where
4import Text.PrettyPrint.ANSI.Leijen (text)
6import qualified Nitpick.Pattern as Pattern
7import qualified Reporting.Error.Helpers as Help
8import qualified Reporting.Render.Type as RenderType
9import qualified Reporting.Report as Report
13data Error
14 = Incomplete Origin [Pattern.Pattern]
15 | Redundant
18data Origin
19 = Arg
20 | LetBound
21 | Case
25-- TO REPORT
28toReport :: RenderType.Localizer -> Error -> Report.Report
29toReport _localizer err =
30 case err of
31 Incomplete Arg unhandled ->
32 Report.report
33 "PARTIAL PATTERN"
34 Nothing
35 "This pattern does not cover all possible inputs."
36 (text (unhandledError unhandled toCaseMessage))
38 Incomplete LetBound unhandled ->
39 Report.report
40 "PARTIAL PATTERN"
41 Nothing
42 "The pattern used here does not cover all possible values."
43 (text (unhandledError unhandled toCaseMessage))
45 Incomplete Case unhandled ->
46 Report.report
47 "MISSING PATTERNS"
48 Nothing
49 "This `case` does not have branches for all possibilities."
50 (text (unhandledError unhandled (missingBranchesMessage (length unhandled))))
52 Redundant ->
53 Report.report
54 "REDUNDANT PATTERN"
55 Nothing
56 "The following pattern is redundant. Remove it."
57 (text "Any value with this shape will be handled by a previous pattern.")
60unhandledError :: [Pattern.Pattern] -> String -> String
61unhandledError unhandledPatterns relevantMessage =
62 let
63 (visiblePatterns, rest) =
64 splitAt 4 unhandledPatterns
66 patternList =
67 map (Pattern.toString False) visiblePatterns
68 ++ if null rest then [] else ["..."]
69 in
70 "You need to account for the following values:\n"
71 ++ concatMap ("\n " ++) patternList ++ "\n"
72 ++ "\n"
73 ++ relevantMessage ++ "\n"
74 ++ "\n"
75 ++ "If you are seeing this error for the first time, check out these hints:\n"
76 ++ Help.hintLink "missing-patterns" ++ "\n"
77 ++ "The recommendations about wildcard patterns and `Debug.crash` are important!"
80toCaseMessage :: String
81toCaseMessage =
82 "Switch to a `case` expression to handle all possible patterns."
85missingBranchesMessage :: Int -> String
86missingBranchesMessage numUnhandled =
87 if numUnhandled == 1 then
88 "Add a branch to cover this pattern!"
90 else
91 "Add branches to cover each of these patterns!"