Skip to content

Commit f7c88c8

Browse files
committed
First attempt at a better over-application error.
1 parent 142863d commit f7c88c8

File tree

4 files changed

+41
-12
lines changed

4 files changed

+41
-12
lines changed

parser-typechecker/package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ library:
1111
other-modules: Paths_unison_parser_typechecker
1212

1313
dependencies:
14+
- recover-rtti
1415
- ListLike
1516
- aeson
1617
- async

parser-typechecker/src/Unison/PrintError.hs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,34 @@ renderTypeError e env src = case e of
275275
"need to have the same type."
276276
]
277277
NotFunctionApplication {..} ->
278-
mconcat
279-
[ "This looks like a function call, but with a ",
280-
style Type1 (renderType' env ft),
281-
" where the function should be. Are you missing an operator?\n\n",
282-
annotatedAsStyle Type1 src f,
283-
debugSummary note
284-
]
278+
case Type.arity ft of
279+
0 ->
280+
mconcat
281+
[ "It looks like this expression is being called like a function\n\n",
282+
annotatedAsStyle Type2 src f,
283+
"\n\n but the thing being applied has the type: ",
284+
style Type2 (renderType' env ft),
285+
"\n\n",
286+
debugSummary note
287+
]
288+
arity ->
289+
mconcat
290+
[ "It looks like this function call\n\n",
291+
annotatedAsStyle Type2 src f,
292+
"\n\nis being applied to ",
293+
Pr.shown (length args),
294+
" arguments, but it has the type\n\n",
295+
style Type2 (renderType' env ft),
296+
"\n\nwhich only accepts ",
297+
Pr.shown arity,
298+
maybePlural " arguments" arity <> ".\n\n",
299+
"Did you apply the function to too many arguments? \n\n",
300+
debugSummary note
301+
]
302+
where
303+
maybePlural word n
304+
| n == 1 = word
305+
| otherwise = word <> "s"
285306
FunctionApplication {..} ->
286307
let fte = Type.removePureEffects False ft
287308
fteFreeVars = Set.map TypeVar.underlying $ ABT.freeVars fte

parser-typechecker/src/Unison/Typechecker/TypeError.hs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
module Unison.Typechecker.TypeError where
44

55
import Data.List.NonEmpty (NonEmpty)
6+
import Debug.RecoverRTTI (anythingToString)
67
import Unison.ABT qualified as ABT
8+
import Unison.Debug qualified as Debug
79
import Unison.KindInference (KindError)
810
import Unison.Pattern (Pattern)
911
import Unison.Prelude hiding (whenM)
@@ -58,7 +60,8 @@ data TypeError v loc
5860
| NotFunctionApplication
5961
{ f :: C.Term v loc,
6062
ft :: C.Type v loc,
61-
note :: C.ErrorNote v loc
63+
note :: C.ErrorNote v loc,
64+
args :: [C.Term v loc]
6265
}
6366
| AbilityCheckFailure
6467
{ ambient :: [C.Type v loc],
@@ -321,15 +324,18 @@ applyingNonFunction :: (Var v) => Ex.ErrorExtractor v loc (TypeError v loc)
321324
applyingNonFunction = do
322325
_ <- Ex.typeMismatch
323326
n <- Ex.errorNote
324-
(f, ft) <- Ex.unique $ do
327+
Debug.debugM Debug.Temp "NOTE" ("applyingNonFunction" :: Text, anythingToString n)
328+
(f, ft, args) <- Ex.unique $ do
325329
Ex.pathStart
326-
(arity0Type, _arg, _argNum) <- Ex.inSynthesizeApp
330+
_synthApp <- Ex.inSynthesizeApp
327331
(_, f, ft, args) <- Ex.inFunctionCall
332+
-- for_ funcCalls \(arity0Type, f, ft, _args) -> do
333+
-- Debug.debugM Debug.Temp "applyingNonFunction" ("arity0Type: " :: Text, arity0Type, "f: " :: Text, f, "ft: " :: Text, ft, "args" :: Text, args)
328334
let expectedArgCount = Type.arity ft
329335
foundArgCount = length args
330336
-- unexpectedArgLoc = ABT.annotation arg
331-
whenM (expectedArgCount < foundArgCount) $ pure (f, arity0Type)
332-
pure $ NotFunctionApplication f (Type.cleanup ft) n
337+
whenM (expectedArgCount < foundArgCount) $ pure (f, ft, args)
338+
pure $ NotFunctionApplication f (Type.cleanup ft) n args
333339

334340
-- | Want to collect this info:
335341
-- The `n`th argument to `f` is `foundType`, but I was expecting `expectedType`.

parser-typechecker/unison-parser-typechecker.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ library
219219
, network-uri
220220
, nonempty-containers
221221
, pretty-simple
222+
, recover-rtti
222223
, regex-tdfa
223224
, semialign
224225
, semigroups

0 commit comments

Comments
 (0)