on master help sign in
Avoid repeated arguments at compile time. Fix #809
commitef280ed35a3e437fccede3b93e0fcceafff3ef41 authorYoann Blein <yoann.blein@free.fr> dateTue 16 Feb 2016, 19:40 (11 years ago) parentd77c8d6
3 files changed+49−1
src/Reporting/Error/Syntax.hs+12 src/Validate.hs+33−1 tests/test-files/bad/RepeatedArgument.elm+4
src/Reporting/Error/Syntax.hs+12
@@ -27,6 +27,7 @@ data Error
2727 | UnboundTypeVarsInAlias String [String] [String]
2828 | UnusedTypeVarsInAlias String [String] [String]
2929 | MessyTypeVarsInAlias String [String] [String] [String]
30+ | RepeatedArgument String
3031
3132
3233-- TO REPORT
@@ -189,6 +190,17 @@ toReport _localizer err =
189190 ]
190191 )
191192
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+
192204
193205unboundTypeVars :: String -> String -> [String] -> [String] -> Report.Report
194206unboundTypeVars declKind typeName givenVars unboundVars@(tvar:_) =
src/Validate.hs+33−1
@@ -184,7 +184,7 @@ checkDefinition (Valid.Definition pattern body _) =
184184 args ->
185185 case pattern of
186186 A.A _ (Pattern.Var _) ->
187− return ()
187+ checkArguments args
188188
189189 _ ->
190190 let
@@ -194,6 +194,38 @@ checkDefinition (Valid.Definition pattern body _) =
194194 Result.throw (R.merge start end) (Error.BadFunctionName (length args))
195195
196196
197+
198+checkArguments :: [Pattern.Pattern R.Region var] -> Result.Result wrn Error.Error ()
199+checkArguments args =
200+ let
201+ vars = concatMap (Pattern.boundVarList) 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+
215+firstDuplicate :: [String] -> Maybe String
216+firstDuplicate vars =
217+ case vars of
218+ [] ->
219+ Nothing
220+
221+ var : rest ->
222+ if var `elem` rest then
223+ Just var
224+
225+ else
226+ firstDuplicate rest
227+
228+
197229-- VALIDATE EXPRESSIONS
198230
199231expression :: Source.Expr -> Result.Result wrn Error.Error Valid.Expr
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