on master help sign in
Clean up check for duplicate arguments
74bc3f2Evan Czaplicki
src/Reporting/Error/Syntax.hs+15−12
@@ -19,6 +19,7 @@ data Error
19 | TypeWithoutDefinition String
19 | TypeWithoutDefinition String
20 | PortWithoutAnnotation String
20 | PortWithoutAnnotation String
21 | UnexpectedPort
21 | UnexpectedPort
22+ | DuplicateArgument String String
22 | DuplicateFieldName String
23 | DuplicateFieldName String
23 | DuplicateValueDeclaration String
24 | DuplicateValueDeclaration String
24 | DuplicateTypeDeclaration String
25 | DuplicateTypeDeclaration String
@@ -27,7 +28,6 @@ data Error
27 | UnboundTypeVarsInAlias String [String] [String]
28 | UnboundTypeVarsInAlias String [String] [String]
28 | UnusedTypeVarsInAlias String [String] [String]
29 | UnusedTypeVarsInAlias String [String] [String]
29 | MessyTypeVarsInAlias String [String] [String] [String]
30 | MessyTypeVarsInAlias String [String] [String] [String]
30− | RepeatedArgument String
31
31
32
32
33-- TO REPORT
33-- TO REPORT
@@ -110,6 +110,20 @@ toReport _localizer err =
110 \ if the module is imported twice, do we send values out the port twice?"
110 \ if the module is imported twice, do we send values out the port twice?"
111 )
111 )
112
112
113+ DuplicateArgument funcName argName ->
114+ Report.report
115+ "DUPLICATE ARGUMENT"
116+ Nothing
117+ ( "The name `" ++ argName
118+ ++ "` is used more than once in the arguments of `"
119+ ++ funcName ++ "`."
120+ )
121+ ( Help.reflowParagraph $
122+ "Rename things until `" ++ argName ++ "` is used only once.\
123+ \ Otherwise how can we tell which one you want when you\
124+ \ say `" ++ argName ++ "` it in the body of your function?"
125+ )
126+
113 DuplicateFieldName name ->
127 DuplicateFieldName name ->
114 Report.report
128 Report.report
115 "DUPLICATE FIELD"
129 "DUPLICATE FIELD"
@@ -190,17 +204,6 @@ toReport _localizer err =
190 ]
204 ]
191 )
205 )
192
206
193− RepeatedArgument name ->
194− Report.report
195− "FUNCTION ARGUMENT REPEATED"
196− Nothing
197− ( "The argument `" ++ name ++ "` is defined multiple times."
198− )
199− ( Help.reflowParagraph $
200− "You should rename one of the occurrences of `" ++ name
201− ++ "` so that each argument is defined only once."
202− )
203−
204
207
205unboundTypeVars :: String -> String -> [String] -> [String] -> Report.Report
208unboundTypeVars :: String -> String -> [String] -> [String] -> Report.Report
206unboundTypeVars declKind typeName givenVars unboundVars@(tvar:_) =
209unboundTypeVars declKind typeName givenVars unboundVars@(tvar:_) =
src/Validate.hs+15−31
@@ -1,7 +1,7 @@
1{-# OPTIONS_GHC -Wall #-}
1{-# OPTIONS_GHC -Wall #-}
2module Validate (declarations) where
2module Validate (declarations) where
3
3
4−import Control.Monad (foldM)
4+import Control.Monad (foldM_)
5import qualified Data.Map as Map
5import qualified Data.Map as Map
6import qualified Data.Set as Set
6import qualified Data.Set as Set
7import qualified Data.Foldable as F
7import qualified Data.Foldable as F
@@ -183,8 +183,8 @@ checkDefinition (Valid.Definition pattern body _) =
183
183
184 args ->
184 args ->
185 case pattern of
185 case pattern of
186− A.A _ (Pattern.Var _) ->
186+ A.A _ (Pattern.Var funcName) ->
187− checkArguments args
187+ checkArguments funcName args
188
188
189 _ ->
189 _ ->
190 let
190 let
@@ -194,36 +194,20 @@ checkDefinition (Valid.Definition pattern body _) =
194 Result.throw (R.merge start end) (Error.BadFunctionName (length args))
194 Result.throw (R.merge start end) (Error.BadFunctionName (length args))
195
195
196
196
197−
198−checkArguments :: [Pattern.Pattern R.Region var] -> Result.Result wrn Error.Error ()
197+checkArguments :: String -> [Pattern.RawPattern] -> Result.Result wrn Error.Error ()
199−checkArguments args =
198+checkArguments funcName args =
200 let
199 let
200+ vars =
201− vars = concatMap (Pattern.boundVarList) args
201+ concatMap Pattern.boundVars args
202− in
203− case firstDuplicate vars of
204− Nothing ->
205− return ()
206−
207− Just var ->
208− let
209− (A.A start _) = head args
210− (A.A end _) = last args
211− in
212− Result.throw (R.merge start end) (Error.RepeatedArgument var)
213−
214
202
215−firstDuplicate :: [String] -> Maybe String
203+ checkDups seenArgs (A.A region arg) =
216−firstDuplicate vars =
204+ if Set.member arg seenArgs then
217− case vars of
205+ Result.throw region (Error.DuplicateArgument funcName arg)
218− [] ->
219− Nothing
220−
221− var : rest ->
222− if var `elem` rest then
223− Just var
224
206
225− else
207+ else
226− firstDuplicate rest
208+ return (Set.insert arg seenArgs)
209+ in
210+ foldM_ checkDups Set.empty vars
227
211
228
212
229-- VALIDATE EXPRESSIONS
213-- VALIDATE EXPRESSIONS
@@ -294,7 +278,7 @@ expression (A.A ann sourceExpression) =
294 else
278 else
295 return (Set.insert field seenFields)
279 return (Set.insert field seenFields)
296 in
280 in
297− do _ <- foldM checkDups Set.empty fields
281+ do foldM_ checkDups Set.empty fields
298 Record <$> T.traverse second fields
282 Record <$> T.traverse second fields
299
283
300 Let defs body ->
284 Let defs body ->