git.iko.soy/rowtype-yoga/purescript-native
on 8703a73 help sign in
97 lines 3.9 kB history raw
1-- |
2-- This module implements a simple linting pass on the PureScript AST.
3--
4module Language.PureScript.Linter (lint, module L) where
6import Prelude.Compat
8import Control.Monad.Writer.Class
10import Data.List (nub, (\\))
11import Data.Maybe (mapMaybe)
12import Data.Monoid
13import qualified Data.Set as S
15import Language.PureScript.AST
16import Language.PureScript.Crash
17import Language.PureScript.Errors
18import Language.PureScript.Linter.Exhaustive as L
19import Language.PureScript.Linter.Imports as L
20import Language.PureScript.Names
21import Language.PureScript.Types
23-- | Lint the PureScript AST.
24-- |
25-- | Right now, this pass only performs a shadowing check.
26lint :: forall m. (MonadWriter MultipleErrors m) => Module -> m ()
27lint (Module _ _ mn ds _) = censor (addHint (ErrorInModule mn)) $ mapM_ lintDeclaration ds
28 where
29 moduleNames :: S.Set Ident
30 moduleNames = S.fromList (nub (mapMaybe getDeclIdent ds))
32 getDeclIdent :: Declaration -> Maybe Ident
33 getDeclIdent (PositionedDeclaration _ _ d) = getDeclIdent d
34 getDeclIdent (ValueDeclaration ident _ _ _) = Just ident
35 getDeclIdent (ExternDeclaration ident _) = Just ident
36 getDeclIdent (TypeInstanceDeclaration ident _ _ _ _) = Just ident
37 getDeclIdent (BindingGroupDeclaration _) = internalError "lint: binding groups should not be desugared yet."
38 getDeclIdent _ = Nothing
40 lintDeclaration :: Declaration -> m ()
41 lintDeclaration = tell . f
42 where
43 (warningsInDecl, _, _, _, _) = everythingWithScope (\_ _ -> mempty) stepE stepB (\_ _ -> mempty) stepDo
45 f :: Declaration -> MultipleErrors
46 f (PositionedDeclaration pos _ dec) = addHint (PositionedError pos) (f dec)
47 f dec@(ValueDeclaration name _ _ _) = addHint (ErrorInValueDeclaration name) (warningsInDecl moduleNames dec <> checkTypeVarsInDecl dec)
48 f (TypeDeclaration name ty) = addHint (ErrorInTypeDeclaration name) (checkTypeVars ty)
49 f dec = warningsInDecl moduleNames dec <> checkTypeVarsInDecl dec
51 stepE :: S.Set Ident -> Expr -> MultipleErrors
52 stepE s (Abs (Left name) _) | name `S.member` s = errorMessage (ShadowedName name)
53 stepE s (Let ds' _) = foldMap go ds'
54 where
55 go d | Just i <- getDeclIdent d
56 , i `S.member` s = errorMessage (ShadowedName i)
57 | otherwise = mempty
58 stepE _ _ = mempty
60 stepB :: S.Set Ident -> Binder -> MultipleErrors
61 stepB s (VarBinder name) | name `S.member` s = errorMessage (ShadowedName name)
62 stepB s (NamedBinder name _) | name `S.member` s = errorMessage (ShadowedName name)
63 stepB _ _ = mempty
65 stepDo :: S.Set Ident -> DoNotationElement -> MultipleErrors
66 stepDo s (DoNotationLet ds') = foldMap go ds'
67 where
68 go d | Just i <- getDeclIdent d
69 , i `S.member` s = errorMessage (ShadowedName i)
70 | otherwise = mempty
71 stepDo _ _ = mempty
73 checkTypeVarsInDecl :: Declaration -> MultipleErrors
74 checkTypeVarsInDecl d = let (f, _, _, _, _) = accumTypes checkTypeVars in f d
76 checkTypeVars :: Type -> MultipleErrors
77 checkTypeVars ty = everythingWithContextOnTypes S.empty mempty mappend step ty <> findUnused ty
78 where
79 step :: S.Set String -> Type -> (S.Set String, MultipleErrors)
80 step s (ForAll tv _ _) = bindVar s tv
81 step s _ = (s, mempty)
82 bindVar :: S.Set String -> String -> (S.Set String, MultipleErrors)
83 bindVar = bind ShadowedTypeVar
84 findUnused :: Type -> MultipleErrors
85 findUnused ty' =
86 let used = usedTypeVariables ty'
87 declared = everythingOnTypes (++) go ty'
88 unused = nub declared \\ nub used
89 in foldl (<>) mempty $ map (errorMessage . UnusedTypeVar) unused
90 where
91 go :: Type -> [String]
92 go (ForAll tv _ _) = [tv]
93 go _ = []
95 bind :: (Ord a) => (a -> SimpleErrorMessage) -> S.Set a -> a -> (S.Set a, MultipleErrors)
96 bind mkError s name | name `S.member` s = (s, errorMessage (mkError name))
97 | otherwise = (S.insert name s, mempty)