git.iko.soy/rowtype-yoga/purescript-native
on a4888d2 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 (onErrorMessages (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 (ExternInstanceDeclaration ident _ _ _) = Just ident
53 getDeclIdent (TypeInstanceDeclaration ident _ _ _ _) = Just ident
54 getDeclIdent (BindingGroupDeclaration _) = error "lint: binding groups should not be desugared yet."
55 getDeclIdent _ = Nothing
57 lintDeclaration :: Declaration -> m ()
58 lintDeclaration d =
59 let (f, _, _, _, _) = everythingWithContextOnValues moduleNames mempty mappend stepD stepE stepB def def
61 f' :: Declaration -> MultipleErrors
62 f' (PositionedDeclaration pos _ dec) = onErrorMessages (PositionedError pos) (f' dec)
63 f' dec@(ValueDeclaration name _ _ _) = onErrorMessages (ErrorInValueDeclaration name) (f dec <> checkTypeVarsInDecl dec)
64 f' (TypeDeclaration name ty) = onErrorMessages (ErrorInTypeDeclaration name) (checkTypeVars ty)
65 f' dec = f dec <> checkTypeVarsInDecl dec
67 in tell (f' d)
68 where
69 def s _ = (s, mempty)
71 stepD :: S.Set Ident -> Declaration -> (S.Set Ident, MultipleErrors)
72 stepD s (TypeClassDeclaration name _ _ decls) = (s, foldr go mempty decls)
73 where
74 go :: Declaration -> MultipleErrors -> MultipleErrors
75 go (PositionedDeclaration _ _ d') errs = go d' errs
76 go (TypeDeclaration op@(Op _) _) errs = errorMessage (ClassOperator name op) <> errs
77 go _ errs = errs
78 stepD s _ = (s, mempty)
80 stepE :: S.Set Ident -> Expr -> (S.Set Ident, MultipleErrors)
81 stepE s (Abs (Left name) _) = bindName s name
82 stepE s (Let ds' _) =
83 case mapAccumL bindName s (nub (mapMaybe getDeclIdent ds')) of
84 (s', es) -> (s', mconcat es)
85 stepE s _ = (s, mempty)
87 stepB :: S.Set Ident -> Binder -> (S.Set Ident, MultipleErrors)
88 stepB s (VarBinder name) = bindName s name
89 stepB s (NamedBinder name _) = bindName s name
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)