on 626b319 help sign in
elk/src/Reporting/Error/Pattern.hs
93 lines 2.6 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.Region as Region
10import qualified Reporting.Report as Report
14data Error
15 = Incomplete Origin [Pattern.Pattern]
16 | Redundant Region.Region Int
19data Origin
20 = Arg
21 | LetBound
22 | Case
26-- TO REPORT
29toReport :: RenderType.Localizer -> Error -> Report.Report
30toReport _localizer err =
31 case err of
32 Incomplete Arg unhandled ->
33 Report.report
34 "PARTIAL PATTERN"
35 Nothing
36 "This pattern does not cover all possible inputs."
37 (text (unhandledError unhandled toCaseMessage))
39 Incomplete LetBound unhandled ->
40 Report.report
41 "PARTIAL PATTERN"
42 Nothing
43 "The pattern used here does not cover all possible values."
44 (text (unhandledError unhandled toCaseMessage))
46 Incomplete Case unhandled ->
47 Report.report
48 "MISSING PATTERNS"
49 Nothing
50 "This `case` does not have branches for all possibilities."
51 (text (unhandledError unhandled (missingBranchesMessage (length unhandled))))
53 Redundant region index ->
54 Report.report
55 "REDUNDANT PATTERN"
56 (Just region)
57 ("The " ++ Help.ordinalize index ++ " pattern is redundant. Remove it.")
58 (text "Any value with this shape will be handled by a previous pattern.")
61unhandledError :: [Pattern.Pattern] -> String -> String
62unhandledError unhandledPatterns relevantMessage =
63 let
64 (visiblePatterns, rest) =
65 splitAt 4 unhandledPatterns
67 patternList =
68 map (Pattern.toString False) visiblePatterns
69 ++ if null rest then [] else ["..."]
70 in
71 "You need to account for the following values:\n"
72 ++ concatMap ("\n " ++) patternList ++ "\n"
73 ++ "\n"
74 ++ relevantMessage ++ "\n"
75 ++ "\n"
76 ++ "If you are seeing this error for the first time, check out these hints:\n"
77 ++ Help.hintLink "missing-patterns" ++ "\n"
78 ++ "The recommendations about wildcard patterns and `Debug.crash` are important!"
81toCaseMessage :: String
82toCaseMessage =
83 "Switch to a `case` expression to handle all possible patterns."
86missingBranchesMessage :: Int -> String
87missingBranchesMessage numUnhandled =
88 if numUnhandled == 1 then
89 "Add a branch to cover this pattern!"
91 else
92 "Add branches to cover each of these patterns!"