git.iko.soy/rowtype-yoga/purescript-native
on 0888ada help sign in
107 lines 4.1 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 = f dec <> checkShadowedTypeVars dec
65 in tell (f' d)
66 where
67 def s _ = (s, mempty)
69 stepD :: S.Set Ident -> Declaration -> (S.Set Ident, MultipleErrors)
70 stepD s (TypeClassDeclaration name _ _ decls) = (s, foldr go mempty decls)
71 where
72 go :: Declaration -> MultipleErrors -> MultipleErrors
73 go (PositionedDeclaration _ _ d') errs = go d' errs
74 go (TypeDeclaration op@(Op _) _) errs = errorMessage (ClassOperator name op) <> errs
75 go _ errs = errs
76 stepD s _ = (s, mempty)
78 stepE :: S.Set Ident -> Expr -> (S.Set Ident, MultipleErrors)
79 stepE s (Abs (Left name) _) = bindName s name
80 stepE s (Let ds' _) =
81 case mapAccumL bindName s (nub (mapMaybe getDeclIdent ds')) of
82 (s', es) -> (s', mconcat es)
83 stepE s _ = (s, mempty)
85 stepB :: S.Set Ident -> Binder -> (S.Set Ident, MultipleErrors)
86 stepB s (VarBinder name) = bindName s name
87 stepB s (NamedBinder name _) = bindName s name
88 stepB s _ = (s, mempty)
90 bindName :: S.Set Ident -> Ident -> (S.Set Ident, MultipleErrors)
91 bindName = bind ShadowedName
93 checkShadowedTypeVars :: Declaration -> MultipleErrors
94 checkShadowedTypeVars d =
95 let (f, _, _, _, _) = accumTypes go in f d
96 where
97 go :: Type -> MultipleErrors
98 go = everythingWithContextOnTypes S.empty mempty mappend step
99 step :: S.Set String -> Type -> (S.Set String, MultipleErrors)
100 step s (ForAll tv _ _) = bindVar s tv
101 step s _ = (s, mempty)
102 bindVar :: S.Set String -> String -> (S.Set String, MultipleErrors)
103 bindVar = bind ShadowedTypeVar
105 bind :: (Ord a) => (a -> SimpleErrorMessage) -> S.Set a -> a -> (S.Set a, MultipleErrors)
106 bind mkError s name | name `S.member` s = (s, errorMessage (mkError name))
107 | otherwise = (S.insert name s, mempty)