@@ -150,8 +150,15 @@ addRANExp dont_change_datacons needRANsTyCons ddfs ex =
150150 else do
151151 let -- n elements after the first packed one require RAN's.
152152 needRANsExp = L. drop (length args - n) args
153-
154- rans <- mkRANs needRANsExp
153+ fields = lookupDataCon ddfs dcon
154+ needsRanFields = L. drop (length fields - n) fields
155+ ranTyFields = map (\ ty -> case ty of
156+ PackedTy tycon _ -> let dd = lookupDDef ddfs tycon
157+ in getCursorTypeForDataCon ddfs dd
158+ _ -> CursorTy
159+ ) needsRanFields
160+
161+ rans <- mkRANs ranTyFields needRANsExp
155162 let ranArgs = L. map (\ (v,_,_,_) -> VarE v) rans
156163 return $ mkLets rans (DataConE loc (toAbsRANDataCon dcon) (ranArgs ++ args))
157164
@@ -243,15 +250,16 @@ withRANDDefs :: Out a => S.Set TyCon -> DDefs (UrTy a) -> DDefs (UrTy a)
243250withRANDDefs needRANsTyCons ddfs = M. map go ddfs
244251 where
245252 -- go :: DDef a -> DDef b
246- go dd@ DDef {dataCons} =
253+ go dd@ DDef {dataCons, memLayout } =
247254 let dcons' = L. foldr (\ (dcon,tys) acc ->
248255 case numRANsDataCon ddfs dcon of
249256 0 -> acc
250257 n -> -- Not all types have random access nodes.
251258 if not (getTyOfDataCon ddfs dcon `S.member` needRANsTyCons)
252259 then acc
253260 else
254- let tys' = [(False ,CursorTy ) | _ <- [1 .. n]] ++ tys
261+ let ranTy = getCursorTypeForDataCon ddfs dd
262+ tys' = [(False ,ranTy) | _ <- [1 .. n]] ++ tys
255263 dcon' = toAbsRANDataCon dcon
256264
257265 _tys'' = (False ,IntTy ) : [(False ,IntTy ) | _ <- [1 .. n]] ++ tys
@@ -295,9 +303,9 @@ AddFixed for this purpose.
295303'mb_most_recent_ran' in the fold below tracks most recent random access nodes.
296304
297305-}
298- mkRANs :: [Exp1 ] -> PassM [(Var , [() ], Ty1 , Exp1 )]
299- mkRANs needRANsExp =
300- snd <$> foldlM (\ (mb_most_recent_ran, acc) arg -> do
306+ mkRANs :: [( UrTy () )] -> [ Exp1 ] -> PassM [(Var , [() ], Ty1 , Exp1 )]
307+ mkRANs ranTys needRANsExp =
308+ snd <$> foldlM (\ (mb_most_recent_ran, acc) ( arg, ranTy) -> do
301309 i <- gensym " ran"
302310 -- See Note [Reusing RAN's in case expressions]
303311 let rhs = case arg of
@@ -310,8 +318,8 @@ mkRANs needRANsExp =
310318 FloatE {} -> Ext (L1. AddFixed (fromJust mb_most_recent_ran) (fromJust (sizeOfTy FloatTy )))
311319 LitSymE {} -> Ext (L1. AddFixed (fromJust mb_most_recent_ran) (fromJust (sizeOfTy SymTy )))
312320 oth -> error $ " addRANExp: Expected trivial expression, got: " ++ sdoc oth
313- return (Just i, acc ++ [(i,[] ,CursorTy , rhs)]))
314- (Nothing , [] ) needRANsExp
321+ return (Just i, acc ++ [(i,[] ,ranTy , rhs)]))
322+ (Nothing , [] ) ( zip needRANsExp ranTys)
315323
316324--------------------------------------------------------------------------------
317325
0 commit comments