@@ -1,6 +1,7 @@
11{-# OPTIONS_GHC -Wall #-}
22module Reporting.Error.Type where
33
4+import qualified Data.Char as Char
45import Text.PrettyPrint ((<+>))
56import qualified Text.PrettyPrint as P
67
@@ -44,8 +45,9 @@ data Hint
4445 | BinopLeft Var.Canonical Region.Region
4546 | BinopRight Var.Canonical Region.Region
4647 | Binop Var.Canonical
47− | BadArgument Region.Region
48− | ExtraArgument Region.Region
48+ | Function (Maybe Var.Canonical)
49+ | UnexpectedArg (Maybe Var.Canonical) Int Region.Region
50+ | FunctionArity (Maybe Var.Canonical) Int Region.Region
4951 | BadTypeAnnotation String
5052 | Instance String
5153 | Literal String
@@ -53,7 +55,6 @@ data Hint
5355 | Shader
5456 | Range
5557 | Lambda
56− | App
5758 | Record
5859
5960
@@ -116,7 +117,7 @@ hintToString hint =
116117 CaseBranch branchNumber region ->
117118 ( Just region
118119 , "The branches of this case-expression return different types of values.\n\n"
119− ++ "I noticed the mismatch in branch #" ++ show branchNumber ++ ", but go through and make sure every\n"
120+ ++ "I noticed the mismatch in the " ++ ordinalize branchNumber ++ " branch, but go through and make sure every\n"
120121 ++ "branch returns the same type of value."
121122 )
122123
@@ -135,7 +136,7 @@ hintToString hint =
135136 MultiIfBranch branchNumber region ->
136137 ( Just region
137138 , "The branches of this if-expression return different types of values.\n\n"
138− ++ "I noticed the mismatch in branch #" ++ show branchNumber ++ ", but go through and make sure every\n"
139+ ++ "I noticed the mismatch in the " ++ ordinalize branchNumber ++ " branch, but go through and make sure every\n"
139140 ++ "branch returns the same type of value."
140141 )
141142
@@ -148,7 +149,7 @@ hintToString hint =
148149 ListElement elementNumber region ->
149150 ( Just region
150151 , "Not all elements of this list are the same type of value.\n\n"
151− ++ "I noticed the mismatch in element #" ++ show elementNumber ++ ", but go through and make sure every\n"
152+ ++ "I noticed the mismatch in the " ++ ordinalize elementNumber ++ " element, but go through and make sure every\n"
152153 ++ "element is the same type of value."
153154 )
154155
@@ -174,15 +175,21 @@ hintToString hint =
174175 ++ "does not match how it is used elsewhere."
175176 )
176177
177− BadArgument region ->
178+ Function maybeName ->
179+ ( Nothing
180+ , "The return type of " ++ funcName maybeName ++ " is being used in unexpected ways."
181+ )
182+
183+ UnexpectedArg maybeName index region ->
178184 ( Just region
179− , "This argument is causing a type mismatch."
185+ , "The " ++ ordinalize index ++ " argument to " ++ funcName maybeName
186+ ++ " has an unexpected type."
180187 )
181188
182− ExtraArgument region ->
189+ FunctionArity maybeName index region ->
183190 ( Just region
184− , "This expression is mistakenly being used as a function.\n"
185− ++ "Maybe you provided an extra argument?"
191+ , capitalize (funcName maybeName) ++ " is not expecting a "
192+ ++ ordinalize index ++ " argument."
186193 )
187194
188195 BadTypeAnnotation name ->
@@ -228,11 +235,6 @@ hintToString hint =
228235 , "This anonymous function is being used in an unexpected way."
229236 )
230237
231− App ->
232− ( Nothing
233− , "The result of this function application is being used in an unexpected way."
234− )
235−
236238 Record ->
237239 ( Nothing
238240 , "This record is being used in an unexpected way."
@@ -244,3 +246,40 @@ prettyOperator (Var.Canonical _ opName) =
244246 if Help.isOp opName
245247 then "(" ++ opName ++ ")"
246248 else "`" ++ opName ++ "`"
249+
250+
251+funcName :: Maybe Var.Canonical -> String
252+funcName maybeVar =
253+ case maybeVar of
254+ Nothing ->
255+ "this function"
256+
257+ Just var ->
258+ "function " ++ prettyOperator var
259+
260+
261+capitalize :: String -> String
262+capitalize string =
263+ case string of
264+ [] -> []
265+ c : cs ->
266+ Char.toUpper c : cs
267+
268+
269+ordinalize :: Int -> String
270+ordinalize number =
271+ let
272+ remainder10 =
273+ number `mod` 10
274+
275+ remainder100 =
276+ number `mod` 100
277+
278+ ending
279+ | remainder100 `elem` [11..13] = "th"
280+ | remainder10 == 1 = "st"
281+ | remainder10 == 2 = "nd"
282+ | remainder10 == 3 = "rd"
283+ | otherwise = "th"
284+ in
285+ show number ++ ending