git.iko.soy/rowtype-yoga/purescript-native
on 32bc8cc help sign in
102 lines 4.3 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 (TypeClassDeclaration name args _ _ decs) = addHint (ErrorInTypeClassDeclaration name) (foldMap (f' (S.fromList $ fst <$> args)) decs)
48 f dec = f' S.empty dec
50 f' :: S.Set String -> Declaration -> MultipleErrors
51 f' s (PositionedDeclaration pos _ dec) = addHint (PositionedError pos) (f' s dec)
52 f' s dec@(ValueDeclaration name _ _ _) = addHint (ErrorInValueDeclaration name) (warningsInDecl moduleNames dec <> checkTypeVarsInDecl s dec)
53 f' s (TypeDeclaration name ty) = addHint (ErrorInTypeDeclaration name) (checkTypeVars s ty)
54 f' s dec = warningsInDecl moduleNames dec <> checkTypeVarsInDecl s dec
56 stepE :: S.Set Ident -> Expr -> MultipleErrors
57 stepE s (Abs (Left name) _) | name `S.member` s = errorMessage (ShadowedName name)
58 stepE s (Let ds' _) = foldMap go ds'
59 where
60 go d | Just i <- getDeclIdent d
61 , i `S.member` s = errorMessage (ShadowedName i)
62 | otherwise = mempty
63 stepE _ _ = mempty
65 stepB :: S.Set Ident -> Binder -> MultipleErrors
66 stepB s (VarBinder name) | name `S.member` s = errorMessage (ShadowedName name)
67 stepB s (NamedBinder name _) | name `S.member` s = errorMessage (ShadowedName name)
68 stepB _ _ = mempty
70 stepDo :: S.Set Ident -> DoNotationElement -> MultipleErrors
71 stepDo s (DoNotationLet ds') = foldMap go ds'
72 where
73 go d | Just i <- getDeclIdent d
74 , i `S.member` s = errorMessage (ShadowedName i)
75 | otherwise = mempty
76 stepDo _ _ = mempty
78 checkTypeVarsInDecl :: S.Set String -> Declaration -> MultipleErrors
79 checkTypeVarsInDecl s d = let (f, _, _, _, _) = accumTypes (checkTypeVars s) in f d
81 checkTypeVars :: S.Set String -> Type -> MultipleErrors
82 checkTypeVars set ty = everythingWithContextOnTypes set mempty mappend step ty <> findUnused ty
83 where
84 step :: S.Set String -> Type -> (S.Set String, MultipleErrors)
85 step s (ForAll tv _ _) = bindVar s tv
86 step s _ = (s, mempty)
87 bindVar :: S.Set String -> String -> (S.Set String, MultipleErrors)
88 bindVar = bind ShadowedTypeVar
89 findUnused :: Type -> MultipleErrors
90 findUnused ty' =
91 let used = usedTypeVariables ty'
92 declared = everythingOnTypes (++) go ty'
93 unused = nub declared \\ nub used
94 in foldl (<>) mempty $ map (errorMessage . UnusedTypeVar) unused
95 where
96 go :: Type -> [String]
97 go (ForAll tv _ _) = [tv]
98 go _ = []
100 bind :: (Ord a) => (a -> SimpleErrorMessage) -> S.Set a -> a -> (S.Set a, MultipleErrors)
101 bind mkError s name | name `S.member` s = (s, errorMessage (mkError name))
102 | otherwise = (S.insert name s, mempty)