on 563d322 help sign in
elk/src/Reporting/Error/Pattern.hs
76 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 ++ incompleteExplanation
35 )
37 Redundant ->
38 Report.simple
39 "REDUNDANT PATTERN"
40 "The following pattern is redundant."
41 "Any value with this shape will be handled by a previous pattern."
44viewPatternList :: [Pattern.Pattern] -> String
45viewPatternList unhandledPatterns =
46 let
47 (showPatterns, rest) =
48 splitAt 4 unhandledPatterns
49 in
50 concatMap ((++) "\n ") $
51 map (Pattern.toString False) showPatterns
52 ++ if null rest then [] else ["..."]
55incompleteExplanation :: String
56incompleteExplanation =
57 unlines
58 [ "Did you add a new tag to a union type? Add how it should be handled here and"
59 , "you will be all set!"
60 , ""
61 , "If you want to go quick and implement this later, you can end the pattern with:"
62 , ""
63 , " _ ->"
64 , " Debug.crash \"TODO\""
65 , ""
66 , "When I see cases that end with this pattern, I augment the runtime error with"
67 , "the module name, the relevant line numbers, and the value led down that branch."
68 , ""
69 , "That said, you almost never want this in production code. If you think there"
70 , "are certain cases that are impossible in practice, consider refining how you"
71 , "model the data. If it can never happen in practice, you can probably define a"
72 , "union type such that the \"impossible\" cannot even be constructed in the"
73 , "first place. That is how you make something \"impossible\" truly impossible."
74 , ""
75 , "I won't say \"I told you so\" in the runtime error, but I'll be thinking it ;)"
76 ]