@@ -75,6 +75,7 @@ import Unison.Util.AnnotatedText qualified as AT
7575import Unison.Util.ColorText (Color )
7676import Unison.Util.ColorText qualified as Color
7777import Unison.Util.Monoid (intercalateMap )
78+ import Unison.Util.Monoid qualified as Monoid
7879import Unison.Util.Pretty (ColorText , Pretty )
7980import Unison.Util.Pretty qualified as Pr
8081import Unison.Util.Range (Range (.. ), startingLine )
@@ -332,14 +333,6 @@ renderTypeError e env src = case e of
332333 " expression " ,
333334 " need to have the same type."
334335 ]
335- NotFunctionApplication {.. } ->
336- mconcat
337- [ " This looks like a function call, but with a " ,
338- style Type1 (renderType' env ft),
339- " where the function should be. Are you missing an operator?\n\n " ,
340- annotatedAsStyle Type1 src f,
341- debugSummary note
342- ]
343336 ActionRestrictionFailure {.. } ->
344337 mconcat
345338 [ Pr. lines
@@ -361,6 +354,87 @@ renderTypeError e env src = case e of
361354 ],
362355 debugSummary note
363356 ]
357+ FunctionUnderApplied {.. } ->
358+ let expectedTypeStr = style Type2 (renderType' env expectedLeaf)
359+ actualTypeStr = style ErrorSite (renderType' env foundLeaf)
360+ in mconcat
361+ [ " This call-site has type " <> actualTypeStr <> " :\n " ,
362+ showSourceMaybes src [styleAnnotated ErrorSite foundLeaf],
363+ " \n\n " ,
364+ " But I expected the type " <> expectedTypeStr <> " because of:\n " ,
365+ showSourceMaybes
366+ src
367+ [ (,Type1 ) . startingLine <$> (rangeForAnnotated mismatchSite),
368+ (,Type2 ) <$> rangeForAnnotated expectedLeaf
369+ ],
370+ " \n\n " ,
371+ Pr. lines
372+ [ " It looks like the function application is missing these arguments:\n " ,
373+ Pr. indentN 2 $ Monoid. intercalateMap " , " (style Type2 . renderType' env) needArgs
374+ ],
375+ unitHint,
376+ intLiteralSyntaxTip mismatchSite expectedType,
377+ debugNoteLoc
378+ . mconcat
379+ $ [ " \n loc debug:" ,
380+ " \n mismatchSite: " ,
381+ annotatedToEnglish mismatchSite,
382+ " \n foundType: " ,
383+ annotatedToEnglish foundType,
384+ " \n foundLeaf: " ,
385+ annotatedToEnglish foundLeaf,
386+ " \n expectedType: " ,
387+ annotatedToEnglish expectedType,
388+ " \n expectedLeaf: " ,
389+ annotatedToEnglish expectedLeaf,
390+ " \n "
391+ ],
392+ debugSummary note
393+ ]
394+ where
395+ unitHintMsg =
396+ " \n Hint: Actions within a block must have type "
397+ <> style Type2 (renderType' env expectedLeaf)
398+ <> " .\n "
399+ <> " Use "
400+ <> style Type1 " _ = <expr>"
401+ <> " to ignore a result."
402+ unitHint = if giveUnitHint then unitHintMsg else " "
403+ giveUnitHint = case expectedType of
404+ Type. Ref' u | u == unitRef -> case mismatchSite of
405+ Term. Let1Named' v _ _ -> Var. isAction v
406+ _ -> False
407+ _ -> False
408+ NotFunctionApplication {.. } ->
409+ case Type. arityIgnoringEffects ft of
410+ 0 ->
411+ mconcat
412+ [ " It looks like" <> style ErrorSite " this " <> " expression is being called like a function:\n\n " ,
413+ annotatedAsStyle ErrorSite src f,
414+ " \n\n but the thing being applied has the type:\n\n " ,
415+ style Type2 (renderType' env ft),
416+ " \n\n Which doesn't expect any arguments." ,
417+ " \n\n " ,
418+ debugSummary note
419+ ]
420+ arity ->
421+ mconcat
422+ [ " It looks like" <> style ErrorSite " this " <> " function call:\n\n " ,
423+ annotatedAsStyle ErrorSite src f,
424+ " \n\n is being applied to " ,
425+ Pr. blue $ Pr. shown (length args),
426+ " arguments, but it has the type\n\n " ,
427+ Pr. indentN 2 $ style Type2 (renderType' env ft),
428+ " \n\n which only accepts " ,
429+ Pr. blue $ Pr. shown arity,
430+ maybePlural " argument" arity <> " .\n\n " ,
431+ " Maybe you applied the function to too many arguments?\n\n " ,
432+ debugSummary note
433+ ]
434+ where
435+ maybePlural word n
436+ | n == 1 = word
437+ | otherwise = word <> " s"
364438 FunctionApplication {.. } ->
365439 let fte = Type. removePureEffects False ft
366440 fteFreeVars = Set. map TypeVar. underlying $ ABT. freeVars fte
@@ -454,18 +528,14 @@ renderTypeError e env src = case e of
454528 " \n\n " ,
455529 showSourceMaybes
456530 src
457- [ -- these are overwriting the colored ranges for some reason?
458- -- (,Color.ForceShow) <$> rangeForAnnotated mismatchSite
459- -- , (,Color.ForceShow) <$> rangeForType foundType
460- -- , (,Color.ForceShow) <$> rangeForType expectedType
461- -- ,
462- (,Type1 ) . startingLine <$> (rangeForAnnotated mismatchSite),
531+ [ (,Type1 ) . startingLine <$> (rangeForAnnotated mismatchSite),
463532 (,Type2 ) <$> rangeForAnnotated expectedLeaf
464533 ],
465534 fromOverHere'
466535 src
467536 [styleAnnotated Type1 foundLeaf]
468537 [styleAnnotated Type2 expectedLeaf],
538+ missingDelayHint,
469539 unitHint,
470540 intLiteralSyntaxTip mismatchSite expectedType,
471541 debugNoteLoc
@@ -486,6 +556,20 @@ renderTypeError e env src = case e of
486556 debugSummary note
487557 ]
488558 where
559+ missingDelayHint = case additionalInfo of
560+ Nothing -> " "
561+ Just MissingDelay ->
562+ Pr. lines
563+ [ " I expected the expression to be delayed, but it was not." ,
564+ " Are you missing a `do`?"
565+ ]
566+ Just SuperfluousDelay ->
567+ Pr. lines
568+ [ " " ,
569+ " I didn't expect this expression to be delayed, but it was." ,
570+ " Are you using a `do` where you don't need one," ,
571+ " or are you missing a `()` to force an expression?"
572+ ]
489573 unitHintMsg =
490574 " \n Hint: Actions within a block must have type "
491575 <> style Type2 (renderType' env expectedLeaf)
0 commit comments