@@ -1,26 +1,54 @@
1{-# OPTIONS_GHC -Wall #-}
1{-# OPTIONS_GHC -Wall #-}
2module Reporting.Error.Pattern where
2module Reporting.Error.Pattern where
4+import qualified AST.Module.Name as ModuleName
5+import qualified AST.Variable as Var
4import qualified Nitpick.Pattern as Pattern
6import qualified Nitpick.Pattern as Pattern
5import qualified Reporting.PrettyPrint as P
7import qualified Reporting.PrettyPrint as P
6import qualified Reporting.Report as Report
8import qualified Reporting.Report as Report
10− = Incomplete [Pattern.Pattern]
12+ = Incomplete Origin [Pattern.Pattern]
11 | Redundant
13 | Redundant
14-- TO REPORT
22-- TO REPORT
16toReport :: P.Dealiaser -> Error -> Report.Report
24toReport :: P.Dealiaser -> Error -> Report.Report
17toReport dealiaser err =
25toReport dealiaser err =
18 case err of
26 case err of
19− Incomplete unhandled ->
27+ Incomplete Arg unhandled ->
30+ "This argument does not cover all possible inputs."
31+ ( "Any of these patterns can cause a crash:\n"
32+ ++ viewPatternList unhandled
34+ ++ partialExplanation unhandled
37+ Incomplete LetBound unhandled ->
20 Report.simple
38 Report.simple
21− "MISSING PATTERN"
39+ "PARTIAL PATTERN"
22− "The following `case` does not handle all possible inputs."
40+ "The pattern used here does not cover all possible values."
23− ( "The following patterns are not being handled:\n"
41+ ( "Any of these patterns can cause a crash:\n"
42+ ++ viewPatternList unhandled
44+ ++ partialExplanation unhandled
47+ Incomplete Case unhandled ->
49+ "INCOMPLETE PATTERN MATCH"
50+ "This `case` does not have branches for all possibilities."
51+ ( "Any of these patterns can cause a crash:\n"
24 ++ viewPatternList unhandled
52 ++ viewPatternList unhandled
26 ++ incompleteExplanation
54 ++ incompleteExplanation
@@ -44,25 +72,60 @@ viewPatternList unhandledPatterns =
44 ++ if null rest then [] else ["..."]
72 ++ if null rest then [] else ["..."]
75+partialExplanation :: [Pattern.Pattern] -> String
76+partialExplanation unhandledPatterns =
78+ "You probably want to switch to a `case` expression to handle all possible\n"
79+ ++ "patterns in a simple way. If you think some cases are impossible, you can use\n"
80+ ++ "`Debug.crash` to make that thinking explicit in your code.\n"
81+ ++ "<http://package.elm-lang.org/packages/elm-lang/core/latest/Debug#crash>"
84+ if isNothing unhandledPatterns then
86+ "\n\nSometimes you *know* a value will never be Nothing, though such cases are very\n"
87+ ++ "rare. If you are certain you are in that case and that there is no better way\n"
88+ ++ "to do things, I'd recommend creating a function `unsafe` that makes the danger\n"
89+ ++ "explicit in the code. <https://github.com/elm-lang/core/issues/215>"
91+ else if isEmptyList unhandledPatterns then
93+ "\n\nSometimes you *know* a list cannot be empty, though such cases are relatively\n"
94+ ++ "rare. If you are certain you are in that case and that there is no better\n"
95+ ++ "way to do things, I'd recommend creating a function like `unsafe` that makes\n"
96+ ++ "the danger explicit in the code. <https://github.com/elm-lang/core/issues/215>"
103+isNothing :: [Pattern.Pattern] -> Bool
104+isNothing unhandledPatterns =
105+ case unhandledPatterns of
106+ [Pattern.Data (Var.Canonical (Var.Module moduleName) "Nothing") []] ->
107+ moduleName == ModuleName.inCore ["Maybe"]
113+isEmptyList :: [Pattern.Pattern] -> Bool
114+isEmptyList unhandledPatterns =
115+ case unhandledPatterns of
116+ [Pattern.Data (Var.Canonical Var.BuiltIn "[]") []] ->
47incompleteExplanation :: String
123incompleteExplanation :: String
48incompleteExplanation =
124incompleteExplanation =
50− [ "Did you add a new tag to a union type? Add how it should be handled here and"
125+ "Did you add a new tag to a union type? Add how it should be handled here and\n"
51− , "you will be all set!"
126+ ++ "you will be all set!\n"
53− , "If you want to go quick and implement this later, you can end the pattern with:"
128+ ++ "If you want to go quick and implement this later, you may want to look into\n"
54− , ""
129+ ++ "using `Debug.crash` which has some cool error reporting abilities.\n"
55− , " _ ->"
130+ ++ "<http://package.elm-lang.org/packages/elm-lang/core/latest/Debug#crash>"
56− , " Debug.crash \"TODO\""
131+
58− , "When I see cases that end with this pattern, I augment the runtime error with"
59− , "the module name, the relevant line numbers, and the value led down that branch."
61− , "That said, you almost never want this in production code. If you think there"
62− , "are certain cases that are impossible in practice, consider refining how you"
63− , "model the data. If it can never happen in practice, you can probably define a"
64− , "union type such that the \"impossible\" cannot even be constructed in the"
65− , "first place. That is how you make something \"impossible\" truly impossible."
67− , "I won't say \"I told you so\" in the runtime error, but I'll be thinking it ;)"