git.iko.soy/rowtype-yoga/purescript-native
on c0274e2 help sign in
120 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 #-}
19module Language.PureScript.Linter (lint, module L) where
21import Prelude ()
22import Prelude.Compat
24import Data.List (mapAccumL, nub, (\\))
25import Data.Maybe (mapMaybe)
26import Data.Monoid
28import qualified Data.Set as S
30import Control.Monad.Writer.Class
32import Language.PureScript.Crash
33import Language.PureScript.AST
34import Language.PureScript.Names
35import Language.PureScript.Errors
36import Language.PureScript.Types
37import Language.PureScript.Linter.Exhaustive as L
38import Language.PureScript.Linter.Imports as L
40-- | Lint the PureScript AST.
41-- |
42-- | Right now, this pass only performs a shadowing check.
43lint :: forall m. (Applicative m, MonadWriter MultipleErrors m) => Module -> m ()
44lint (Module _ _ mn ds _) = censor (addHint (ErrorInModule mn)) $ mapM_ lintDeclaration ds
45 where
46 moduleNames :: S.Set Ident
47 moduleNames = S.fromList (nub (mapMaybe getDeclIdent ds))
49 getDeclIdent :: Declaration -> Maybe Ident
50 getDeclIdent (PositionedDeclaration _ _ d) = getDeclIdent d
51 getDeclIdent (ValueDeclaration ident _ _ _) = Just ident
52 getDeclIdent (ExternDeclaration ident _) = Just ident
53 getDeclIdent (TypeInstanceDeclaration ident _ _ _ _) = Just ident
54 getDeclIdent (BindingGroupDeclaration _) = internalError "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) = addHint (PositionedError pos) (f' dec)
63 f' dec@(ValueDeclaration name _ _ _) = addHint (ErrorInValueDeclaration name) (f dec <> checkTypeVarsInDecl dec)
64 f' (TypeDeclaration name ty) = addHint (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 (TypedBinder _ b) = stepB s b
91 stepB s _ = (s, mempty)
93 bindName :: S.Set Ident -> Ident -> (S.Set Ident, MultipleErrors)
94 bindName = bind ShadowedName
96 checkTypeVarsInDecl :: Declaration -> MultipleErrors
97 checkTypeVarsInDecl d = let (f, _, _, _, _) = accumTypes checkTypeVars in f d
99 checkTypeVars :: Type -> MultipleErrors
100 checkTypeVars ty = everythingWithContextOnTypes S.empty mempty mappend step ty <> findUnused ty
101 where
102 step :: S.Set String -> Type -> (S.Set String, MultipleErrors)
103 step s (ForAll tv _ _) = bindVar s tv
104 step s _ = (s, mempty)
105 bindVar :: S.Set String -> String -> (S.Set String, MultipleErrors)
106 bindVar = bind ShadowedTypeVar
107 findUnused :: Type -> MultipleErrors
108 findUnused ty' =
109 let used = usedTypeVariables ty'
110 declared = everythingOnTypes (++) go ty'
111 unused = nub declared \\ nub used
112 in foldl (<>) mempty $ map (errorMessage . UnusedTypeVar) unused
113 where
114 go :: Type -> [String]
115 go (ForAll tv _ _) = [tv]
116 go _ = []
118 bind :: (Ord a) => (a -> SimpleErrorMessage) -> S.Set a -> a -> (S.Set a, MultipleErrors)
119 bind mkError s name | name `S.member` s = (s, errorMessage (mkError name))
120 | otherwise = (S.insert name s, mempty)