Skip to content

Commit

Permalink
feat(type-infer): 💄 Improve output of NotFunctionType error
Browse files Browse the repository at this point in the history
  • Loading branch information
bristermitten committed Jun 4, 2024
1 parent 92b960a commit 2508123
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/Elara/TypeInfer/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ data TypeInferenceError where
(Context SourceRegion) ->
TypeInferenceError
NotFunctionType ::
SourceRegion ->
SourceRegion ->
(Type SourceRegion) ->
TypeInferenceError
Expand Down Expand Up @@ -277,4 +278,14 @@ instance ReportableError TypeInferenceError where
[(sourceRegionToDiagnosePosition loc, Where "Referenced here")]
[]
report (KindInferError e) = report e
report (NotFunctionType loc appLoc a) = do
writeReport $
Err
Nothing
(vsep ["Type error: The following type is not a function type:", pretty a])
[ (sourceRegionToDiagnosePosition loc, Where "This should be a function type, but isn't")
, (sourceRegionToDiagnosePosition appLoc, Where "Applied here")
]
[ Hint "Perhaps you applied too many arguments to a function?"
]
report e = writeReport $ Err Nothing (showColored e) [] []
4 changes: 2 additions & 2 deletions src/Elara/TypeInfer/Infer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1480,7 +1480,7 @@ inferApplication Type.Function{..} e = do

pure (e', output)
inferApplication Type.VariableType{..} _ = throw (NotNecessarilyFunctionType location name)
inferApplication _A _ = throw (NotFunctionType (location _A) _A)
inferApplication _A _B = throw (NotFunctionType (location _A) (_B ^. sourceRegion) _A)

{- | This corresponds to the judgment:
> Γ ⊢ e ⇐ A • e ⇒⇒ C ⊣ Δ
Expand Down Expand Up @@ -1521,7 +1521,7 @@ inferPatternApplication Type.Function{..} e = do

pure (e', output)
inferPatternApplication Type.VariableType{..} _ = throw (NotNecessarilyFunctionType location name)
inferPatternApplication _A _ = throw (NotFunctionType (location _A) _A)
inferPatternApplication _A _B = throw (NotFunctionType (location _A) (_B ^. sourceRegion) _A)

-- Helper functions for displaying errors

Expand Down

0 comments on commit 2508123

Please sign in to comment.