on 99a44a0 help sign in
elk/src/Reporting/Error/Pattern.hs
69 lines 2.5 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2module Reporting.Error.Pattern where
4import qualified Data.Char as Char
5import Text.PrettyPrint ((<+>))
6import qualified Text.PrettyPrint as P
8import qualified AST.Helpers as Help
9import qualified AST.Type as Type
10import qualified AST.Variable as Var
11import qualified Nitpick.Pattern as Pattern
12import qualified Reporting.PrettyPrint as P
13import qualified Reporting.Region as Region
14import qualified Reporting.Report as Report
17data Error
18 = Incomplete [Pattern.Pattern]
19 | Redundant
22-- TO REPORT
24toReport :: P.Dealiaser -> Error -> Report.Report
25toReport dealiaser err =
26 case err of
27 Incomplete unhandled ->
28 Report.simple
29 "MISSING PATTERN"
30 "The following `case` does not handle all possible inputs."
31 ( "The following patterns are not being handled:\n"
32 ++ viewPatternList unhandled
33 ++ "\n\n"
34 ++ "Did you add a new tag to a union type? Add how it should be handled here and"
35 ++ "you will be all set!"
36 ++ ""
37 ++ "If you want to go quick and implement this later, you can end the pattern with:"
38 ++ ""
39 ++ " _ ->"
40 ++ " Debug.crash \"TODO\""
41 ++ ""
42 ++ "When I see cases that end with this pattern, I augment the runtime error with"
43 ++ "the module name, the relevant line numbers, and the value led down that branch."
44 ++ ""
45 ++ "That said, you almost never want this in production code. If you think there"
46 ++ "are certain cases that are impossible in practice, consider refining how you"
47 ++ "model the data. If it can never happen in practice, you can probably define a"
48 ++ "union type such that the \"impossible\" cannot even be constructed in the"
49 ++ "first place. That is how you make something \"impossible\" truly impossible."
50 ++ ""
51 ++ "I won't say \"I told you so\" in the runtime error, but I'll be thinking it ;)"
52 )
54 Redundant ->
55 Report.simple
56 "REDUNDANT PATTERN"
57 "The following pattern is redundant."
58 "Any value with this shape will be handled by a previous pattern."
61viewPatternList :: [Pattern.Pattern] -> String
62viewPatternList unhandledPatterns =
63 let
64 (showPatterns, rest) =
65 splitAt 4 unhandledPatterns
66 in
67 concatMap ((++) "\n ") $
68 map Pattern.toString showPatterns
69 ++ if null rest then [] else ["..."]