on master help sign in
Merge branch 'pr/1290' into dev

# Conflicts: # src/Reporting/Error/Syntax.hs # src/Validate.hs

commit8677ff75f5583defef164fa7ff773597dd564f4f authorEvan Czaplicki <info@elm-lang.org> dateWed 27 Apr 2016, 19:33 (11 years ago) parent17618de74bc3f2
3 files changed+40−5
src/Reporting/Error/Syntax.hs+16 src/Validate.hs+20−5 tests/test-files/bad/RepeatedArgument.elm+4
src/Reporting/Error/Syntax.hs+16
@@ -28,6 +28,8 @@ data Error
2828
2929 | InfixDuplicate String
3030 | TypeWithoutDefinition String
31+
32+ | DuplicateArgument String String
3133 | DuplicateFieldName String
3234 | DuplicateValueDeclaration String
3335 | DuplicateTypeDeclaration String
@@ -184,6 +186,20 @@ toReport _localizer err =
184186 ++ " " ++ valueName ++ " = 42"
185187 )
186188
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+
187203 DuplicateFieldName name ->
188204 Report.report
189205 "DUPLICATE FIELD"
src/Validate.hs+20−5
@@ -1,8 +1,7 @@
11{-# OPTIONS_GHC -Wall #-}
22module Validate (Result, module') where
33
4−import Prelude hiding (init)
5−import Control.Monad (foldM, when)
4+import Control.Monad (foldM_, when)
65import qualified Data.Map as Map
76import qualified Data.Maybe as Maybe
87import qualified Data.Set as Set
@@ -428,8 +427,8 @@ validateDefPattern pattern body =
428427
429428 args ->
430429 case pattern of
431− A.A _ (Pattern.Var _) ->
432− return ()
430+ A.A _ (Pattern.Var funcName) ->
431+ checkArguments funcName args
433432
434433 _ ->
435434 let
@@ -439,6 +438,22 @@ validateDefPattern pattern body =
439438 Result.throw (R.merge start end) (Error.BadFunctionName (length args))
440439
441440
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+
442457
443458-- VALIDATE EXPRESSIONS
444459
@@ -509,7 +524,7 @@ expression (A.A ann sourceExpression) =
509524 else
510525 return (Set.insert field seenFields)
511526 in
512− do _ <- foldM checkDups Set.empty fields
527+ do foldM_ checkDups Set.empty fields
513528 Record <$> T.traverse second fields
514529
515530 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