git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Warnings for missing top-level type declarations, fix #1333
commit0f5cf75bad60eed760df0798a52638714aab18ca authorPhil Freeman <paf31@cantab.net> dateSun 11 Oct 2015, 14:30 (11 years ago) parent9345724
2 files changed+41−30
src/Language/PureScript/Errors.hs+8 src/Language/PureScript/Sugar/TypeDeclarations.hs+33−30
src/Language/PureScript/Errors.hs+8
@@ -141,6 +141,7 @@ data SimpleErrorMessage
141141 | ShadowedTypeVar String
142142 | UnusedTypeVar String
143143 | WildcardInferredType Type
144+ | MissingTypeDeclaration Ident
144145 | NotExhaustivePattern [[Binder]] Bool
145146 | OverlappingPattern [[Binder]] Bool
146147 | IncompleteExhaustivityCheck
@@ -279,6 +280,7 @@ errorCode em = case unwrapErrorMessage em of
279280 ShadowedTypeVar{} -> "ShadowedTypeVar"
280281 UnusedTypeVar{} -> "UnusedTypeVar"
281282 WildcardInferredType{} -> "WildcardInferredType"
283+ MissingTypeDeclaration{} -> "MissingTypeDeclaration"
282284 NotExhaustivePattern{} -> "NotExhaustivePattern"
283285 OverlappingPattern{} -> "OverlappingPattern"
284286 IncompleteExhaustivityCheck{} -> "IncompleteExhaustivityCheck"
@@ -660,6 +662,12 @@ prettyPrintSingleError full level e = prettyPrintErrorMessage . positionHintsFir
660662 paras [ line "The wildcard type definition has the inferred type "
661663 , indent $ typeAsBox ty
662664 ]
665+ renderSimpleErrorMessage (MissingTypeDeclaration ident) =
666+ paras [ line $ "No type declaration was provided for the top-level declaration of " ++ showIdent ident ++ "."
667+ , line "It is good practice to provide type declarations as a form of documentation."
668+ , line "Consider using a type wildcard to display the inferred type:"
669+ , indent $ line $ showIdent ident ++ " :: _"
670+ ]
663671 renderSimpleErrorMessage (NotExhaustivePattern bs b) =
664672 paras $ [ line "A case expression could not be determined to cover all inputs."
665673 , line "The following additional cases are required to cover all inputs:\n"
src/Language/PureScript/Sugar/TypeDeclarations.hs+33−30
@@ -15,10 +15,10 @@
1515-----------------------------------------------------------------------------
1616
1717{-# LANGUAGE FlexibleContexts #-}
18+{-# LANGUAGE ScopedTypeVariables #-}
1819{-# LANGUAGE CPP #-}
1920
2021module Language.PureScript.Sugar.TypeDeclarations (
21− desugarTypeDeclarations,
2222 desugarTypeDeclarationsModule
2323) where
2424
@@ -27,6 +27,7 @@ import Control.Applicative
2727#endif
2828import Control.Monad (forM)
2929import Control.Monad.Error.Class (MonadError(..))
30+import Control.Monad.Writer.Class (MonadWriter(tell))
3031
3132import Language.PureScript.AST
3233import Language.PureScript.Names
@@ -37,36 +38,38 @@ import Language.PureScript.Traversals
3738-- |
3839-- Replace all top level type declarations in a module with type annotations
3940--
40−desugarTypeDeclarationsModule :: (Functor m, Applicative m, MonadError MultipleErrors m) => [Module] -> m [Module]
41+desugarTypeDeclarationsModule :: forall m. (Functor m, Applicative m, MonadError MultipleErrors m, MonadWriter MultipleErrors m) => [Module] -> m [Module]
4142desugarTypeDeclarationsModule ms = forM ms $ \(Module ss coms name ds exps) ->
4243 rethrow (addHint (ErrorInModule name)) $
4344 Module ss coms name <$> desugarTypeDeclarations ds <*> pure exps
44−
45−-- |
46−-- Replace all top level type declarations with type annotations
47−--
48−desugarTypeDeclarations :: (Functor m, Applicative m, MonadError MultipleErrors m) => [Declaration] -> m [Declaration]
49−desugarTypeDeclarations (PositionedDeclaration pos com d : ds) = do
50− (d' : ds') <- rethrowWithPosition pos $ desugarTypeDeclarations (d : ds)
51− return (PositionedDeclaration pos com d' : ds')
52−desugarTypeDeclarations (TypeDeclaration name ty : d : rest) = do
53− (_, nameKind, val) <- fromValueDeclaration d
54− desugarTypeDeclarations (ValueDeclaration name nameKind [] (Right (TypedValue True val ty)) : rest)
5545 where
56− fromValueDeclaration :: (Functor m, Applicative m, MonadError MultipleErrors m) => Declaration -> m (Ident, NameKind, Expr)
57− fromValueDeclaration (ValueDeclaration name' nameKind [] (Right val)) | name == name' = return (name', nameKind, val)
58− fromValueDeclaration (PositionedDeclaration pos com d') = do
59− (ident, nameKind, val) <- rethrowWithPosition pos $ fromValueDeclaration d'
60− return (ident, nameKind, PositionedValue pos com val)
61− fromValueDeclaration _ = throwError . errorMessage $ OrphanTypeDeclaration name
62−desugarTypeDeclarations [TypeDeclaration name _] = throwError . errorMessage $ OrphanTypeDeclaration name
63−desugarTypeDeclarations (ValueDeclaration name nameKind bs val : rest) = do
64− let (_, f, _) = everywhereOnValuesTopDownM return go return
65− f' (Left gs) = Left <$> mapM (pairM return f) gs
66− f' (Right v) = Right <$> f v
67− (:) <$> (ValueDeclaration name nameKind bs <$> f' val) <*> desugarTypeDeclarations rest
68− where
69− go (Let ds val') = Let <$> desugarTypeDeclarations ds <*> pure val'
70− go other = return other
71−desugarTypeDeclarations (d:ds) = (:) d <$> desugarTypeDeclarations ds
72−desugarTypeDeclarations [] = return []
46+
47+ desugarTypeDeclarations :: [Declaration] -> m [Declaration]
48+ desugarTypeDeclarations (PositionedDeclaration pos com d : ds) = do
49+ (d' : ds') <- rethrowWithPosition pos $ desugarTypeDeclarations (d : ds)
50+ return (PositionedDeclaration pos com d' : ds')
51+ desugarTypeDeclarations (TypeDeclaration name ty : d : rest) = do
52+ (_, nameKind, val) <- fromValueDeclaration d
53+ desugarTypeDeclarations (ValueDeclaration name nameKind [] (Right (TypedValue True val ty)) : rest)
54+ where
55+ fromValueDeclaration :: Declaration -> m (Ident, NameKind, Expr)
56+ fromValueDeclaration (ValueDeclaration name' nameKind [] (Right val)) | name == name' = return (name', nameKind, val)
57+ fromValueDeclaration (PositionedDeclaration pos com d') = do
58+ (ident, nameKind, val) <- rethrowWithPosition pos $ fromValueDeclaration d'
59+ return (ident, nameKind, PositionedValue pos com val)
60+ fromValueDeclaration _ = throwError . errorMessage $ OrphanTypeDeclaration name
61+ desugarTypeDeclarations [TypeDeclaration name _] = throwError . errorMessage $ OrphanTypeDeclaration name
62+ desugarTypeDeclarations (ValueDeclaration name nameKind bs val : rest) = do
63+ case val of
64+ Right TypedValue{} -> return ()
65+ Left _ -> error "desugarTypeDeclarations: cases were not desugared"
66+ _ -> tell (addHint (ErrorInValueDeclaration name) $ errorMessage $ MissingTypeDeclaration name)
67+ let (_, f, _) = everywhereOnValuesTopDownM return go return
68+ f' (Left gs) = Left <$> mapM (pairM return f) gs
69+ f' (Right v) = Right <$> f v
70+ (:) <$> (ValueDeclaration name nameKind bs <$> f' val) <*> desugarTypeDeclarations rest
71+ where
72+ go (Let ds val') = Let <$> desugarTypeDeclarations ds <*> pure val'
73+ go other = return other
74+ desugarTypeDeclarations (d:ds) = (:) d <$> desugarTypeDeclarations ds
75+ desugarTypeDeclarations [] = return []