on master help sign in
Avoid cascading errors

When a constraint fails, rather than propagating some bad type through, just assume the type is “anything”. This means the user will get the root issue, but not a bunch of stuff that comes from that. Once it is fixed, they will see any subsequent errors, if there are any.

commit19f39f86ebebe5a8332a2cfdfbf99baa07312f75 authorEvan Czaplicki <info@elm-lang.org> dateThu 9 Jul 2015, 20:18 (11 years ago) parent8972f1e
2 files changed+42−23
src/Type/Type.hs+16−9 src/Type/Unify.hs+26−14
src/Type/Type.hs+16−9
@@ -92,6 +92,7 @@ data Flex
9292 | Flexible
9393 | Constant
9494 | Is SuperType
95+ | Error
9596 deriving (Eq)
9697
9798data SuperType
@@ -152,15 +153,21 @@ namedVar flex name = UF.fresh $ Descriptor
152153
153154
154155variable :: Flex -> IO Variable
155−variable flex = UF.fresh $ Descriptor
156− { structure = Nothing
157− , rank = noRank
158− , flex = flex
159− , name = Nothing
160− , copy = Nothing
161− , mark = noMark
162− , alias = Nothing
163− }
156+variable flex =
157+ UF.fresh (descriptor flex)
158+
159+
160+descriptor :: Flex -> Descriptor
161+descriptor flex =
162+ Descriptor
163+ { structure = Nothing
164+ , rank = noRank
165+ , flex = flex
166+ , name = Nothing
167+ , copy = Nothing
168+ , mark = noMark
169+ , alias = Nothing
170+ }
164171
165172
166173-- CONSTRAINT HELPERS
src/Type/Unify.hs+26−14
@@ -53,6 +53,9 @@ typeError
5353typeError leftType rightType note =
5454 do leftSrcType <- liftIO (toSrcType leftType)
5555 rightSrcType <- liftIO (toSrcType rightType)
56+ liftIO $ do
57+ UF.setDescriptor leftType (Type.descriptor Error)
58+ UF.setDescriptor rightType (Type.descriptor Error)
5659 throwError (RawMismatch (leftSrcType, rightSrcType, note))
5760
5861
@@ -118,8 +121,11 @@ actuallyUnify variable1 variable2 = do
118121
119122 unifyNumber svar (Var.Canonical home name) =
120123 case home of
121− Var.BuiltIn | name `elem` ["Int","Float"] -> flexAndUnify svar
122− Var.Local | List.isPrefixOf "number" name -> flexAndUnify svar
124+ Var.BuiltIn | name `elem` ["Int","Float"] ->
125+ flexAndUnify svar
126+
127+ Var.Local | List.isPrefixOf "number" name ->
128+ flexAndUnify svar
123129 _ ->
124130 typeError variable1 variable2 Nothing
125131
@@ -134,9 +140,12 @@ actuallyUnify variable1 variable2 = do
134140
135141 unifyComparable v (Var.Canonical home name) =
136142 case home of
137− Var.BuiltIn | name `elem` ["Int","Float","Char","String"] -> flexAndUnify v
138− Var.Local | List.isPrefixOf "comparable" name -> flexAndUnify v
139− _ -> comparableError Nothing
143+ Var.BuiltIn | name `elem` ["Int","Float","Char","String"] ->
144+ flexAndUnify v
145+ Var.Local | List.isPrefixOf "comparable" name ->
146+ flexAndUnify v
147+ _ ->
148+ comparableError Nothing
140149
141150 unifyComparableStructure varSuper varFlex =
142151 do struct <- liftIO $ collectApps varFlex
@@ -148,11 +157,11 @@ actuallyUnify variable1 variable2 = do
148157 do flexAndUnify varSuper
149158 unifyHelp v =<< liftIO (variable $ Is Comparable)
150159
151− Tuple vs
152− | length vs > 6 ->
160+ Tuple vs ->
161+ if length vs > 6 then
153162 comparableError $ Just $
154163 "Furthermore, it cannot be a tuple with more than 6 elements."
155− | otherwise ->
164+ else
156165 do flexAndUnify varSuper
157166 cmpVars <- liftIO $ forM [1..length vs] $ \_ -> variable (Is Comparable)
158167 zipWithM_ unifyHelp vs cmpVars
@@ -194,14 +203,17 @@ actuallyUnify variable1 variable2 = do
194203 _ -> typeError variable1 variable2 Nothing
195204
196205 case (structure desc1, structure desc2) of
197− (Nothing, Nothing) | flex desc1 == Flexible && flex desc1 == Flexible ->
198− merge
206+ (Nothing, Nothing)
207+ | flex desc1 == Flexible && flex desc1 == Flexible ->
208+ merge
199209
200− (Nothing, _) | flex desc1 == Flexible ->
201− merge2
210+ (Nothing, _)
211+ | flex desc1 == Flexible -> merge2
212+ | flex desc1 == Error -> return ()
202213
203− (_, Nothing) | flex desc2 == Flexible ->
204− merge1
214+ (_, Nothing)
215+ | flex desc2 == Flexible -> merge1
216+ | flex desc2 == Error -> return ()
205217
206218 (Just (Var1 v), _) ->
207219 unifyHelp v variable2