on master help sign in
Detect duplicate fields in type and type alias declarations

Fix #1323

commitb7403d02266bb700d6aefac7c60cb3513700e17c authorEvan Czaplicki <evancz@users.noreply.github.com> dateThu 24 Nov 2016, 14:09 (10 years ago) parent0d6c87f
6 files changed+36−33
src/AST/Type.hs+1−1 src/Canonicalize/Type.hs+2−2 src/Elm/Compiler/Type.hs+2−2 src/Parse/Type.hs+1−1 src/Reporting/Error/Canonicalize.hs+2−2 src/Validate.hs+28−25
src/AST/Type.hs+1−1
@@ -30,7 +30,7 @@ data Raw'
3030 | RVar String
3131 | RType Var.Raw
3232 | RApp Raw [Raw]
33− | RRecord [(String, Raw)] (Maybe Raw)
33+ | RRecord [(A.Located String, Raw)] (Maybe Raw)
3434
3535
3636data Canonical
src/Canonicalize/Type.hs+2−2
@@ -21,7 +21,7 @@ tipe env annType@(A.A _ typ) =
2121 go =
2222 tipe env
2323
24− goSnd (name,t) =
24+ goField (A.A _ name, t) =
2525 (,) name <$> go t
2626 in
2727 case typ of
@@ -38,7 +38,7 @@ tipe env annType@(A.A _ typ) =
3838 T.Lambda <$> go a <*> go b
3939
4040 T.RRecord fields ext ->
41− T.Record <$> Trav.traverse goSnd fields <*> Trav.traverse go ext
41+ T.Record <$> Trav.traverse goField fields <*> Trav.traverse go ext
4242
4343
4444canonicalizeApp :: Env.Env -> T.Raw -> [T.Raw] -> Result T.Canonical
src/Elm/Compiler/Type.hs+2−2
@@ -5,7 +5,7 @@ module Elm.Compiler.Type
55 )
66 where
77
8−import Control.Arrow (second)
8+import Control.Arrow ((***))
99import Data.Aeson ((.:), (.=))
1010import qualified Data.Aeson as Json
1111import qualified Data.Aeson.Types as Json
@@ -212,7 +212,7 @@ fromRawType (A.A _ astType) =
212212 App (fromRawType t) (map fromRawType ts)
213213
214214 Type.RRecord fields ext ->
215− Record (map (second fromRawType) fields) (fmap fromRawType ext)
215+ Record (map (A.drop *** fromRawType) fields) (fmap fromRawType ext)
216216
217217
218218
src/Parse/Type.hs+1−1
@@ -45,7 +45,7 @@ record =
4545 return (Type.RRecord fields (Just (A.map Type.RVar ext)))
4646
4747 field =
48− do lbl <- rLabel
48+ do lbl <- addLocation rLabel
4949 whitespace >> hasType >> whitespace
5050 (,) lbl <$> expr
5151
src/Reporting/Error/Canonicalize.hs+2−2
@@ -1,7 +1,7 @@
11{-# OPTIONS_GHC -Wall #-}
22module Reporting.Error.Canonicalize where
33
4−import Control.Arrow (second)
4+import Control.Arrow ((***))
55import qualified Data.Char as Char
66import Text.PrettyPrint.ANSI.Leijen (Doc, dullyellow, fillSep, indent, text)
77
@@ -355,7 +355,7 @@ unsafePromote (A.A _ rawType) =
355355 Type.App (unsafePromote func) (map unsafePromote args)
356356
357357 Type.RRecord fields ext ->
358− Type.Record (map (second unsafePromote) fields) (fmap unsafePromote ext)
358+ Type.Record (map (A.drop *** unsafePromote) fields) (fmap unsafePromote ext)
359359
360360
361361
src/Validate.hs+28−25
@@ -622,32 +622,34 @@ detectDuplicates tag names =
622622
623623checkTypeVarsInUnion :: A.Commented (D.Union Type.Raw) -> Result wrn ()
624624checkTypeVarsInUnion (A.A (region,_) (D.Type name boundVars ctors)) =
625− case diff boundVars (concatMap freeVars (concatMap snd ctors)) of
626− (_, []) ->
627− return ()
625+ do allFreeVars <- concat <$> traverse freeVars (concatMap snd ctors)
626+ case diff boundVars allFreeVars of
627+ (_, []) ->
628+ return ()
628629
629− (_, unbound) ->
630− Result.throw region
631− (Error.UnboundTypeVarsInUnion name boundVars unbound)
630+ (_, unbound) ->
631+ Result.throw region
632+ (Error.UnboundTypeVarsInUnion name boundVars unbound)
632633
633634
634635checkTypeVarsInAlias :: A.Commented (D.Alias Type.Raw) -> Result wrn ()
635636checkTypeVarsInAlias (A.A (region,_) (D.Type name boundVars tipe)) =
636− case diff boundVars (freeVars tipe) of
637− ([], []) ->
638− return ()
637+ do allFreeVars <- freeVars tipe
638+ case diff boundVars allFreeVars of
639+ ([], []) ->
640+ return ()
639641
640− ([], unbound) ->
641− Result.throw region
642− (Error.UnboundTypeVarsInAlias name boundVars unbound)
642+ ([], unbound) ->
643+ Result.throw region
644+ (Error.UnboundTypeVarsInAlias name boundVars unbound)
643645
644− (unused, []) ->
645− Result.throw region
646− (Error.UnusedTypeVarsInAlias name boundVars unused)
646+ (unused, []) ->
647+ Result.throw region
648+ (Error.UnusedTypeVarsInAlias name boundVars unused)
647649
648− (unused, unbound) ->
649− Result.throw region
650− (Error.MessyTypeVarsInAlias name boundVars unused unbound)
650+ (unused, unbound) ->
651+ Result.throw region
652+ (Error.MessyTypeVarsInAlias name boundVars unused unbound)
651653
652654
653655diff :: [String] -> [A.Located String] -> ([String], [String])
@@ -664,21 +666,22 @@ diff left right =
664666 )
665667
666668
667−freeVars :: Type.Raw -> [A.Located String]
669+freeVars :: Type.Raw -> Result wrn [A.Located String]
668670freeVars (A.A region tipe) =
669671 case tipe of
670672 Type.RLambda t1 t2 ->
671− freeVars t1 ++ freeVars t2
673+ (++) <$> freeVars t1 <*> freeVars t2
672674
673675 Type.RVar x ->
674− [A.A region x]
676+ Result.ok [A.A region x]
675677
676678 Type.RType _ ->
677− []
679+ Result.ok []
678680
679681 Type.RApp t ts ->
680− concatMap freeVars (t:ts)
682+ concat <$> traverse freeVars (t:ts)
681683
682684 Type.RRecord fields ext ->
683− maybe [] freeVars ext
684− ++ concatMap (freeVars . snd) fields
685+ do checkDuplicateFields region fields
686+ let types = Maybe.maybeToList ext ++ map snd fields
687+ concat <$> traverse freeVars types