From 25081239a78878b089460c85e2c0465072b0ccb2 Mon Sep 17 00:00:00 2001 From: Alexander Wood Date: Tue, 4 Jun 2024 14:22:50 +0100 Subject: [PATCH] feat(type-infer): :lipstick: Improve output of NotFunctionType error #29 --- src/Elara/TypeInfer/Error.hs | 11 +++++++++++ src/Elara/TypeInfer/Infer.hs | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Elara/TypeInfer/Error.hs b/src/Elara/TypeInfer/Error.hs index 3b84e8a..3aa0393 100644 --- a/src/Elara/TypeInfer/Error.hs +++ b/src/Elara/TypeInfer/Error.hs @@ -77,6 +77,7 @@ data TypeInferenceError where (Context SourceRegion) -> TypeInferenceError NotFunctionType :: + SourceRegion -> SourceRegion -> (Type SourceRegion) -> TypeInferenceError @@ -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) [] [] diff --git a/src/Elara/TypeInfer/Infer.hs b/src/Elara/TypeInfer/Infer.hs index a84aad6..35f55f3 100644 --- a/src/Elara/TypeInfer/Infer.hs +++ b/src/Elara/TypeInfer/Infer.hs @@ -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 ⊣ Δ @@ -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