git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Fix #1825, generate fresh binder names unless all names in case are equal
commit75876fefb22811198038e0684ba12c66cd34fbb5 authorPhil Freeman <paf31@cantab.net> dateSat 16 Jan 2016, 15:56 (11 years ago) parent20ecf43
2 files changed+18−18
examples/failing/1825.purs+9 src/Language/PureScript/Sugar/CaseDeclarations.hs+9−18
examples/failing/1825.pursnew file+9
@@ -0,0 +1,9 @@
1+-- @shouldFailWith UnknownValue
2+
3+module Main where
4+
5+data W = X | Y | Z
6+
7+bad X a = a
8+bad Y _ = a
9+bad Z a = a
src/Language/PureScript/Sugar/CaseDeclarations.hs+9−18
@@ -150,7 +150,7 @@ toTuple _ = internalError "Not a value declaration"
150150makeCaseDeclaration :: forall m. (Functor m, Applicative m, MonadSupply m, MonadError MultipleErrors m) => Ident -> [([Binder], Either [(Guard, Expr)] Expr)] -> m Declaration
151151makeCaseDeclaration ident alternatives = do
152152 let namedArgs = map findName . fst <$> alternatives
153− argNames = map join $ foldl1 resolveNames namedArgs
153+ argNames = foldl1 resolveNames namedArgs
154154 args <- if allUnique (catMaybes argNames)
155155 then mapM argName argNames
156156 else replicateM (length argNames) freshIdent'
@@ -160,14 +160,11 @@ makeCaseDeclaration ident alternatives = do
160160 return $ ValueDeclaration ident Public [] (Right value)
161161 where
162162 -- We will construct a table of potential names.
163− -- VarBinders will become Just (Just _) which is a potential name.
164− -- NullBinder will become Just Nothing, which indicates that we may
163+ -- VarBinders will become Just _ which is a potential name.
164+ -- Everything else becomes Nothing, which indicates that we
165165 -- have to generate a name.
166− -- Everything else becomes Nothing, which indicates that we definitely
167− -- have to generate a name.
168− findName :: Binder -> Maybe (Maybe Ident)
169− findName NullBinder = Just Nothing
170− findName (VarBinder name) = Just (Just name)
166+ findName :: Binder -> Maybe Ident
167+ findName (VarBinder name) = Just name
171168 findName (PositionedBinder _ _ binder) = findName binder
172169 findName _ = Nothing
173170
@@ -182,19 +179,13 @@ makeCaseDeclaration ident alternatives = do
182179
183180 -- Combine two lists of potential names from two case alternatives
184181 -- by zipping correspoding columns.
185− resolveNames :: [Maybe (Maybe Ident)] ->
186− [Maybe (Maybe Ident)] ->
187− [Maybe (Maybe Ident)]
182+ resolveNames :: [Maybe Ident] -> [Maybe Ident] -> [Maybe Ident]
188183 resolveNames = zipWith resolveName
189184
190185 -- Resolve a pair of names. VarBinder beats NullBinder, and everything
191186 -- else results in Nothing.
192− resolveName :: Maybe (Maybe Ident) ->
193− Maybe (Maybe Ident) ->
194− Maybe (Maybe Ident)
195− resolveName (Just (Just a)) (Just (Just b))
196− | a == b = Just (Just a)
187+ resolveName :: Maybe Ident -> Maybe Ident -> Maybe Ident
188+ resolveName (Just a) (Just b)
189+ | a == b = Just a
197190 | otherwise = Nothing
198− resolveName (Just Nothing) a = a
199− resolveName a (Just Nothing) = a
200191 resolveName _ _ = Nothing