on master help sign in
Merge branch 'pr/1290' into dev
8677ff7Evan Czaplicki
src/Reporting/Error/Syntax.hs+16
@@ -28,6 +28,8 @@ data Error
28
28
29 | InfixDuplicate String
29 | InfixDuplicate String
30 | TypeWithoutDefinition String
30 | TypeWithoutDefinition String
31+
32+ | DuplicateArgument String String
31 | DuplicateFieldName String
33 | DuplicateFieldName String
32 | DuplicateValueDeclaration String
34 | DuplicateValueDeclaration String
33 | DuplicateTypeDeclaration String
35 | DuplicateTypeDeclaration String
@@ -184,6 +186,20 @@ toReport _localizer err =
184 ++ " " ++ valueName ++ " = 42"
186 ++ " " ++ valueName ++ " = 42"
185 )
187 )
186
188
189+ DuplicateArgument funcName argName ->
190+ Report.report
191+ "DUPLICATE ARGUMENT"
192+ Nothing
193+ ( "The name `" ++ argName
194+ ++ "` is used more than once in the arguments of `"
195+ ++ funcName ++ "`."
196+ )
197+ ( Help.reflowParagraph $
198+ "Rename things until `" ++ argName ++ "` is used only once.\
199+ \ Otherwise how can we tell which one you want when you\
200+ \ say `" ++ argName ++ "` it in the body of your function?"
201+ )
202+
187 DuplicateFieldName name ->
203 DuplicateFieldName name ->
188 Report.report
204 Report.report
189 "DUPLICATE FIELD"
205 "DUPLICATE FIELD"
src/Validate.hs+20−5
@@ -1,8 +1,7 @@
1{-# OPTIONS_GHC -Wall #-}
1{-# OPTIONS_GHC -Wall #-}
2module Validate (Result, module') where
2module Validate (Result, module') where
3
3
4−import Prelude hiding (init)
5−import Control.Monad (foldM, when)
4+import Control.Monad (foldM_, when)
6import qualified Data.Map as Map
5import qualified Data.Map as Map
7import qualified Data.Maybe as Maybe
6import qualified Data.Maybe as Maybe
8import qualified Data.Set as Set
7import qualified Data.Set as Set
@@ -428,8 +427,8 @@ validateDefPattern pattern body =
428
427
429 args ->
428 args ->
430 case pattern of
429 case pattern of
431− A.A _ (Pattern.Var _) ->
430+ A.A _ (Pattern.Var funcName) ->
432− return ()
431+ checkArguments funcName args
433
432
434 _ ->
433 _ ->
435 let
434 let
@@ -439,6 +438,22 @@ validateDefPattern pattern body =
439 Result.throw (R.merge start end) (Error.BadFunctionName (length args))
438 Result.throw (R.merge start end) (Error.BadFunctionName (length args))
440
439
441
440
441+checkArguments :: String -> [Pattern.Raw] -> Result wrn ()
442+checkArguments funcName args =
443+ let
444+ vars =
445+ concatMap Pattern.boundVars args
446+
447+ checkDups seenArgs (A.A region arg) =
448+ if Set.member arg seenArgs then
449+ Result.throw region (Error.DuplicateArgument funcName arg)
450+
451+ else
452+ return (Set.insert arg seenArgs)
453+ in
454+ foldM_ checkDups Set.empty vars
455+
456+
442
457
443-- VALIDATE EXPRESSIONS
458-- VALIDATE EXPRESSIONS
444
459
@@ -509,7 +524,7 @@ expression (A.A ann sourceExpression) =
509 else
524 else
510 return (Set.insert field seenFields)
525 return (Set.insert field seenFields)
511 in
526 in
512− do _ <- foldM checkDups Set.empty fields
527+ do foldM_ checkDups Set.empty fields
513 Record <$> T.traverse second fields
528 Record <$> T.traverse second fields
514
529
515 Let defs body ->
530 Let defs body ->
tests/test-files/bad/RepeatedArgument.elmnew file+4
@@ -0,0 +1,4 @@
1+
2+foo x x = 42
3+
4+bar x (x, y) = 42