git.iko.soy/rowtype-yoga/purescript-native
on 38034d2 help sign in
119 lines 4.7 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : Language.PureScript.Linter
4-- Copyright : (c) 2013-15 Phil Freeman, (c) 2014-15 Gary Burgess
5-- License : MIT (http://opensource.org/licenses/MIT)
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
9-- Portability :
10--
11-- | This module implements a simple linting pass on the PureScript AST.
12--
13-----------------------------------------------------------------------------
15{-# LANGUAGE GeneralizedNewtypeDeriving #-}
16{-# LANGUAGE FlexibleContexts #-}
17{-# LANGUAGE ScopedTypeVariables #-}
18{-# LANGUAGE CPP #-}
20module Language.PureScript.Linter (lint, module L) where
22import Data.List (mapAccumL, nub, (\\))
23import Data.Maybe (mapMaybe)
24import Data.Monoid
26import qualified Data.Set as S
28#if __GLASGOW_HASKELL__ < 710
29import Control.Applicative
30#endif
31import Control.Monad.Writer.Class
33import Language.PureScript.AST
34import Language.PureScript.Names
35import Language.PureScript.Errors
36import Language.PureScript.Types
37import Language.PureScript.Linter.Exhaustive as L
39-- | Lint the PureScript AST.
40-- |
41-- | Right now, this pass only performs a shadowing check.
42lint :: forall m. (Applicative m, MonadWriter MultipleErrors m) => Module -> m ()
43lint (Module _ _ mn ds _) = censor (addHint (ErrorInModule mn)) $ mapM_ lintDeclaration ds
44 where
45 moduleNames :: S.Set Ident
46 moduleNames = S.fromList (nub (mapMaybe getDeclIdent ds))
48 getDeclIdent :: Declaration -> Maybe Ident
49 getDeclIdent (PositionedDeclaration _ _ d) = getDeclIdent d
50 getDeclIdent (ValueDeclaration ident _ _ _) = Just ident
51 getDeclIdent (ExternDeclaration ident _) = Just ident
52 getDeclIdent (TypeInstanceDeclaration ident _ _ _ _) = Just ident
53 getDeclIdent (BindingGroupDeclaration _) = error "lint: binding groups should not be desugared yet."
54 getDeclIdent _ = Nothing
56 lintDeclaration :: Declaration -> m ()
57 lintDeclaration d =
58 let (f, _, _, _, _) = everythingWithContextOnValues moduleNames mempty mappend stepD stepE stepB def def
60 f' :: Declaration -> MultipleErrors
61 f' (PositionedDeclaration pos _ dec) = addHint (PositionedError pos) (f' dec)
62 f' dec@(ValueDeclaration name _ _ _) = addHint (ErrorInValueDeclaration name) (f dec <> checkTypeVarsInDecl dec)
63 f' (TypeDeclaration name ty) = addHint (ErrorInTypeDeclaration name) (checkTypeVars ty)
64 f' dec = f dec <> checkTypeVarsInDecl dec
66 in tell (f' d)
67 where
68 def s _ = (s, mempty)
70 stepD :: S.Set Ident -> Declaration -> (S.Set Ident, MultipleErrors)
71 stepD s (TypeClassDeclaration name _ _ decls) = (s, foldr go mempty decls)
72 where
73 go :: Declaration -> MultipleErrors -> MultipleErrors
74 go (PositionedDeclaration _ _ d') errs = go d' errs
75 go (TypeDeclaration op@(Op _) _) errs = errorMessage (ClassOperator name op) <> errs
76 go _ errs = errs
77 stepD s _ = (s, mempty)
79 stepE :: S.Set Ident -> Expr -> (S.Set Ident, MultipleErrors)
80 stepE s (Abs (Left name) _) = bindName s name
81 stepE s (Let ds' _) =
82 case mapAccumL bindName s (nub (mapMaybe getDeclIdent ds')) of
83 (s', es) -> (s', mconcat es)
84 stepE s _ = (s, mempty)
86 stepB :: S.Set Ident -> Binder -> (S.Set Ident, MultipleErrors)
87 stepB s (VarBinder name) = bindName s name
88 stepB s (NamedBinder name _) = bindName s name
89 stepB s (TypedBinder _ b) = stepB s b
90 stepB s _ = (s, mempty)
92 bindName :: S.Set Ident -> Ident -> (S.Set Ident, MultipleErrors)
93 bindName = bind ShadowedName
95 checkTypeVarsInDecl :: Declaration -> MultipleErrors
96 checkTypeVarsInDecl d = let (f, _, _, _, _) = accumTypes checkTypeVars in f d
98 checkTypeVars :: Type -> MultipleErrors
99 checkTypeVars ty = everythingWithContextOnTypes S.empty mempty mappend step ty <> findUnused ty
100 where
101 step :: S.Set String -> Type -> (S.Set String, MultipleErrors)
102 step s (ForAll tv _ _) = bindVar s tv
103 step s _ = (s, mempty)
104 bindVar :: S.Set String -> String -> (S.Set String, MultipleErrors)
105 bindVar = bind ShadowedTypeVar
106 findUnused :: Type -> MultipleErrors
107 findUnused ty' =
108 let used = usedTypeVariables ty'
109 declared = everythingOnTypes (++) go ty'
110 unused = nub declared \\ nub used
111 in foldl (<>) mempty $ map (errorMessage . UnusedTypeVar) unused
112 where
113 go :: Type -> [String]
114 go (ForAll tv _ _) = [tv]
115 go _ = []
117 bind :: (Ord a) => (a -> SimpleErrorMessage) -> S.Set a -> a -> (S.Set a, MultipleErrors)
118 bind mkError s name | name `S.member` s = (s, errorMessage (mkError name))
119 | otherwise = (S.insert name s, mempty)