git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Fix #1662, display extra type info in instance errors
b90fc77Phil Freeman
src/Language/PureScript/Errors.hs+41−15
@@ -370,6 +370,15 @@ onTypesInErrorMessageM f (ErrorMessage hints simple) = ErrorMessage <$> traverse
370 gSimple (ExprDoesNotHaveType e t) = ExprDoesNotHaveType e <$> f t
370 gSimple (ExprDoesNotHaveType e t) = ExprDoesNotHaveType e <$> f t
371 gSimple (CannotApplyFunction t e) = CannotApplyFunction <$> f t <*> pure e
371 gSimple (CannotApplyFunction t e) = CannotApplyFunction <$> f t <*> pure e
372 gSimple (InvalidInstanceHead t) = InvalidInstanceHead <$> f t
372 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+
373 gSimple other = pure other
382 gSimple other = pure other
374
383
375 gHint (ErrorInSubsumption t1 t2) = ErrorInSubsumption <$> f t1 <*> f t2
384 gHint (ErrorInSubsumption t1 t2) = ErrorInSubsumption <$> f t1 <*> f t2
@@ -377,6 +386,7 @@ onTypesInErrorMessageM f (ErrorMessage hints simple) = ErrorMessage <$> traverse
377 gHint (ErrorCheckingType e t) = ErrorCheckingType e <$> f t
386 gHint (ErrorCheckingType e t) = ErrorCheckingType e <$> f t
378 gHint (ErrorCheckingKind t) = ErrorCheckingKind <$> f t
387 gHint (ErrorCheckingKind t) = ErrorCheckingKind <$> f t
379 gHint (ErrorInApplication e1 t1 e2) = ErrorInApplication e1 <$> f t1 <*> pure e2
388 gHint (ErrorInApplication e1 t1 e2) = ErrorInApplication e1 <$> f t1 <*> pure e2
389+ gHint (ErrorInInstance cl ts) = ErrorInInstance cl <$> traverse f ts
380 gHint other = pure other
390 gHint other = pure other
381
391
382-- |
392-- |
@@ -392,26 +402,33 @@ prettyPrintSingleError full level e = do
392 -- Pretty print an ErrorMessage
402 -- Pretty print an ErrorMessage
393 prettyPrintErrorMessage :: TypeMap -> ErrorMessage -> Box.Box
403 prettyPrintErrorMessage :: TypeMap -> ErrorMessage -> Box.Box
394 prettyPrintErrorMessage typeMap (ErrorMessage hints simple) =
404 prettyPrintErrorMessage typeMap (ErrorMessage hints simple) =
395− paras $
405+ paras
396− foldr renderHint (indent (renderSimpleErrorMessage simple)) hints
406+ [ foldr renderHint (indent (renderSimpleErrorMessage simple)) hints
397− : typeInformation
407+ , Box.moveDown 1 typeInformation
398− ++ [ Box.moveDown 1 $ paras [ line $ "See " ++ wikiUri ++ " for more information, "
408+ , Box.moveDown 1 $ paras [ line $ "See " ++ wikiUri ++ " for more information, "
399− , line $ "or to contribute content related to this " ++ levelText ++ "."
409+ , line $ "or to contribute content related to this " ++ levelText ++ "."
400− ]
410+ ]
401− ]
411+ ]
402 where
412 where
403 wikiUri :: String
413 wikiUri :: String
404 wikiUri = "https://github.com/purescript/purescript/wiki/Error-Code-" ++ errorCode e
414 wikiUri = "https://github.com/purescript/purescript/wiki/Error-Code-" ++ errorCode e
405
415
406− typeInformation :: [Box.Box]
416+ typeInformation :: Box.Box
407− typeInformation = M.elems . M.mapMaybe skolemInfo . umSkolemMap $ typeMap
417+ typeInformation | not (null types) = Box.hsep 1 Box.left [ line "where", paras types]
418+ | otherwise = Box.emptyBox 0 0
408 where
419 where
420+ types :: [Box.Box]
421+ types = map skolemInfo (M.elems (umSkolemMap typeMap)) ++
422+ map unknownInfo (M.elems (umUnknownMap typeMap))
423+
409− skolemInfo :: (String, Int, Maybe SourceSpan) -> Maybe Box.Box
424+ skolemInfo :: (String, Int, Maybe SourceSpan) -> Box.Box
410− skolemInfo (name, s, Just ss) =
425+ skolemInfo (name, s, ss) =
426+ paras $
411− Just . Box.moveDown 1 $ paras [ line $ "(" ++ name ++ show s ++ " is a rigid type variable"
427+ line (name ++ show s ++ " is a rigid type variable")
412− , line $ " bound at " ++ displayStartEndPos ss ++ ")"
428+ : foldMap (return . line . (" bound at " ++) . displayStartEndPos) ss
413− ]
429+
414− skolemInfo _ = Nothing
430+ unknownInfo :: Int -> Box.Box
431+ unknownInfo u = line $ "_" ++ show u ++ " is an unknown type"
415
432
416 renderSimpleErrorMessage :: SimpleErrorMessage -> Box.Box
433 renderSimpleErrorMessage :: SimpleErrorMessage -> Box.Box
417 renderSimpleErrorMessage (CannotGetFileInfo path) =
434 renderSimpleErrorMessage (CannotGetFileInfo path) =
@@ -613,7 +630,16 @@ prettyPrintSingleError full level e = do
613 , indent $ Box.hsep 1 Box.left [ line (showQualified runProperName nm)
630 , indent $ Box.hsep 1 Box.left [ line (showQualified runProperName nm)
614 , Box.vcat Box.left (map typeAtomAsBox ts)
631 , Box.vcat Box.left (map typeAtomAsBox ts)
615 ]
632 ]
633+ , paras [ line "The instance head contains unknown type variables. Consider adding a type annotation."
634+ | any containsUnknowns ts
635+ ]
616 ]
636 ]
637+ where
638+ containsUnknowns :: Type -> Bool
639+ containsUnknowns = everythingOnTypes (||) go
640+ where
641+ go TUnknown{} = True
642+ go _ = False
617 renderSimpleErrorMessage (PossiblyInfiniteInstance nm ts) =
643 renderSimpleErrorMessage (PossiblyInfiniteInstance nm ts) =
618 paras [ line "Type class instance for"
644 paras [ line "Type class instance for"
619 , indent $ Box.hsep 1 Box.left [ line (showQualified runProperName nm)
645 , indent $ Box.hsep 1 Box.left [ line (showQualified runProperName nm)