on master help sign in
Improve error messages for bad return types (annotation /= def)
commitdcce9516f2b195bde85a985fd8b7c6b8af73ec1a authorEvan Czaplicki <evancz@users.noreply.github.com> dateTue 20 Sep 2016, 12:54 (10 years ago) parente481f00
2 files changed+60−27
src/Reporting/Error/Helpers.hs+7−1 src/Reporting/Error/Type.hs+53−26
src/Reporting/Error/Helpers.hs+7−1
@@ -1,7 +1,8 @@
11{-# OPTIONS_GHC -Wall #-}
22module Reporting.Error.Helpers
33 ( (|>)
4− , functionName, hintLink, stack, reflowParagraph
4+ , functionName, args
5+ , hintLink, stack, reflowParagraph
56 , commaSep, capitalize, ordinalize, drawCycle
67 , findPotentialTypos, findTypoPairs, vetTypos
78 , nearbyNames, distance, maybeYouWant
@@ -47,6 +48,11 @@ functionName opName =
4748 "`" ++ opName ++ "`"
4849
4950
51+args :: Int -> String
52+args n =
53+ show n ++ if n == 1 then " argument" else " arguments"
54+
55+
5056hintLink :: String -> String
5157hintLink fileName =
5258 "<https://github.com/elm-lang/elm-compiler/blob/"
src/Reporting/Error/Type.hs+53−26
@@ -69,7 +69,7 @@ data Hint
6969 | Function (Maybe Var.Canonical)
7070 | UnexpectedArg (Maybe Var.Canonical) Int Int Region.Region
7171 | FunctionArity (Maybe Var.Canonical) Int Int Region.Region
72− | ReturnType String Int
72+ | ReturnType String Int Int Region.Region
7373 | Instance String
7474 | Literal String
7575 | Pattern Pattern
@@ -306,7 +306,7 @@ mismatchToReport localizer (MismatchInfo hint leftType rightType maybeReason) =
306306 UnexpectedArg maybeName 1 1 region ->
307307 report
308308 (Just region)
309− ("The argument to " ++ funcName maybeName ++ " is causing a mismatch.")
309+ ("The argument to " ++ funcName maybeName ++ " is causing a mismatch.")
310310 ( cmpHint
311311 (Help.capitalize (funcName maybeName) ++ " is expecting the argument to be:")
312312 "But it is:"
@@ -356,37 +356,34 @@ mismatchToReport localizer (MismatchInfo hint leftType rightType maybeReason) =
356356 (text "Maybe you forgot some parentheses? Or a comma?")
357357
358358 FunctionArity maybeName expected actual region ->
359− let
360− s = if expected == 1 then "" else "s"
361− in
362− report
363− (Just region)
364− ( Help.capitalize (funcName maybeName) ++ " is expecting " ++ show expected
365− ++ " argument" ++ s ++ ", but was given " ++ show actual ++ "."
366− )
367− (text "Maybe you forgot some parentheses? Or a comma?")
368−
369− ReturnType name 0 ->
370359 report
371− Nothing
372− ("The type annotation for " ++ Help.functionName name ++ " does not match its definition.")
360+ (Just region)
361+ ( Help.capitalize (funcName maybeName) ++ " is expecting "
362+ ++ Help.args expected ++ ", but was given " ++ show actual ++ "."
363+ )
364+ (text "Maybe you forgot some parentheses? Or a comma?")
365+
366+ ReturnType name typeArity argArity region ->
367+ if typeArity == 0 || argArity == 0 then
368+ report
369+ (Just region)
370+ ("The definition of " ++ Help.functionName name ++ " does not match its type annotation.")
373371 ( cmpHint
374− ( "The type annotation says " ++ Help.functionName name
375− ++ " should be a:"
372+ ( "The type annotation for " ++ Help.functionName name ++ " says it is a:"
376373 )
377− "But the value you defined has this type:"
378− []
374+ "But the definition (shown above) is a:"
375+ (arityHint typeArity argArity)
379376 )
380377
381− ReturnType name arity ->
382− report
383− Nothing
384− ("The " ++ Help.functionName name ++ " function does not match the type annotation.")
378+ else
379+ report
380+ (Just region)
381+ ("The definition of " ++ Help.functionName name ++ " does not match its type annotation.")
385382 ( cmpHint
386− "The definition produces values of this type:"
387− ( "But type annotation says " ++ Help.functionName name ++ " should return:"
383+ ( "The type annotation for " ++ Help.functionName name ++ " says it always returns:"
388384 )
389− []
385+ "But the returned value (shown above) is a:"
386+ (arityHint typeArity argArity)
390387 )
391388
392389 Instance name ->
@@ -571,6 +568,36 @@ functionHint maybeName =
571568
572569
573570
571+-- ARITY HINTS
572+
573+
574+arityHint :: Int -> Int -> [String]
575+arityHint typeArity argArity =
576+ if typeArity == argArity then
577+ []
578+
579+ else
580+ [ "The type annotation says there " ++ sayArgs typeArity
581+ ++ ", but there " ++ sayArgs argArity ++ " named in the definition."
582+ ++ " It is best practice for each argument in the type to correspond"
583+ ++ " to a named argument in the definition, so try that first!"
584+ ]
585+
586+
587+sayArgs :: Int -> String
588+sayArgs n =
589+ case n of
590+ 0 ->
591+ "are NO arguments"
592+
593+ 1 ->
594+ "is 1 argument"
595+
596+ _ ->
597+ "are " ++ show n ++ " arguments"
598+
599+
600+
574601-- MISMATCH HELPERS
575602
576603