git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Fix #1662, display extra type info in instance errors
commitb90fc77eaf11606d36c78ce714e3a5ab9ba59b5d authorPhil Freeman <paf31@cantab.net> dateMon 23 Nov 2015, 17:40 (11 years ago) parenta824830
1 file changed+41−15
src/Language/PureScript/Errors.hs+41−15
src/Language/PureScript/Errors.hs+41−15
@@ -370,6 +370,15 @@ onTypesInErrorMessageM f (ErrorMessage hints simple) = ErrorMessage <$> traverse
370370 gSimple (ExprDoesNotHaveType e t) = ExprDoesNotHaveType e <$> f t
371371 gSimple (CannotApplyFunction t e) = CannotApplyFunction <$> f t <*> pure e
372372 gSimple (InvalidInstanceHead t) = InvalidInstanceHead <$> f t
373+ gSimple (NoInstanceFound cl ts) = NoInstanceFound cl <$> traverse f ts
374+ gSimple (OverlappingInstances cl ts insts) = OverlappingInstances cl <$> traverse f ts <*> pure insts
375+ gSimple (PossiblyInfiniteInstance cl ts) = PossiblyInfiniteInstance cl <$> traverse f ts
376+ gSimple (CannotDerive cl ts) = CannotDerive cl <$> traverse f ts
377+ gSimple (ExpectedType ty k) = ExpectedType <$> f ty <*> pure k
378+ gSimple (OrphanInstance nm cl ts) = OrphanInstance nm cl <$> traverse f ts
379+ gSimple (WildcardInferredType ty) = WildcardInferredType <$> f ty
380+ gSimple (MissingTypeDeclaration nm ty) = MissingTypeDeclaration nm <$> f ty
381+
373382 gSimple other = pure other
374383
375384 gHint (ErrorInSubsumption t1 t2) = ErrorInSubsumption <$> f t1 <*> f t2
@@ -377,6 +386,7 @@ onTypesInErrorMessageM f (ErrorMessage hints simple) = ErrorMessage <$> traverse
377386 gHint (ErrorCheckingType e t) = ErrorCheckingType e <$> f t
378387 gHint (ErrorCheckingKind t) = ErrorCheckingKind <$> f t
379388 gHint (ErrorInApplication e1 t1 e2) = ErrorInApplication e1 <$> f t1 <*> pure e2
389+ gHint (ErrorInInstance cl ts) = ErrorInInstance cl <$> traverse f ts
380390 gHint other = pure other
381391
382392-- |
@@ -392,26 +402,33 @@ prettyPrintSingleError full level e = do
392402 -- Pretty print an ErrorMessage
393403 prettyPrintErrorMessage :: TypeMap -> ErrorMessage -> Box.Box
394404 prettyPrintErrorMessage typeMap (ErrorMessage hints simple) =
395− paras $
396− foldr renderHint (indent (renderSimpleErrorMessage simple)) hints
397− : typeInformation
398− ++ [ Box.moveDown 1 $ paras [ line $ "See " ++ wikiUri ++ " for more information, "
399− , line $ "or to contribute content related to this " ++ levelText ++ "."
400− ]
401− ]
405+ paras
406+ [ foldr renderHint (indent (renderSimpleErrorMessage simple)) hints
407+ , Box.moveDown 1 typeInformation
408+ , Box.moveDown 1 $ paras [ line $ "See " ++ wikiUri ++ " for more information, "
409+ , line $ "or to contribute content related to this " ++ levelText ++ "."
410+ ]
411+ ]
402412 where
403413 wikiUri :: String
404414 wikiUri = "https://github.com/purescript/purescript/wiki/Error-Code-" ++ errorCode e
405415
406− typeInformation :: [Box.Box]
407− typeInformation = M.elems . M.mapMaybe skolemInfo . umSkolemMap $ typeMap
416+ typeInformation :: Box.Box
417+ typeInformation | not (null types) = Box.hsep 1 Box.left [ line "where", paras types]
418+ | otherwise = Box.emptyBox 0 0
408419 where
409− skolemInfo :: (String, Int, Maybe SourceSpan) -> Maybe Box.Box
410− skolemInfo (name, s, Just ss) =
411− Just . Box.moveDown 1 $ paras [ line $ "(" ++ name ++ show s ++ " is a rigid type variable"
412− , line $ " bound at " ++ displayStartEndPos ss ++ ")"
413− ]
414− skolemInfo _ = Nothing
420+ types :: [Box.Box]
421+ types = map skolemInfo (M.elems (umSkolemMap typeMap)) ++
422+ map unknownInfo (M.elems (umUnknownMap typeMap))
423+
424+ skolemInfo :: (String, Int, Maybe SourceSpan) -> Box.Box
425+ skolemInfo (name, s, ss) =
426+ paras $
427+ line (name ++ show s ++ " is a rigid type variable")
428+ : foldMap (return . line . (" bound at " ++) . displayStartEndPos) ss
429+
430+ unknownInfo :: Int -> Box.Box
431+ unknownInfo u = line $ "_" ++ show u ++ " is an unknown type"
415432
416433 renderSimpleErrorMessage :: SimpleErrorMessage -> Box.Box
417434 renderSimpleErrorMessage (CannotGetFileInfo path) =
@@ -613,7 +630,16 @@ prettyPrintSingleError full level e = do
613630 , indent $ Box.hsep 1 Box.left [ line (showQualified runProperName nm)
614631 , Box.vcat Box.left (map typeAtomAsBox ts)
615632 ]
633+ , paras [ line "The instance head contains unknown type variables. Consider adding a type annotation."
634+ | any containsUnknowns ts
635+ ]
616636 ]
637+ where
638+ containsUnknowns :: Type -> Bool
639+ containsUnknowns = everythingOnTypes (||) go
640+ where
641+ go TUnknown{} = True
642+ go _ = False
617643 renderSimpleErrorMessage (PossiblyInfiniteInstance nm ts) =
618644 paras [ line "Type class instance for"
619645 , indent $ Box.hsep 1 Box.left [ line (showQualified runProperName nm)