Skip to content

Commit 5aa7f6f

Browse files
authored
Merge pull request #195 from well-typed/separate-bang-from-type
Separate HsSrcBang out of field type
2 parents 6885855 + c52f28c commit 5aa7f6f

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

large-records/src/Data/Record/Internal/GHC/TemplateHaskellStyle.hs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -529,11 +529,11 @@ equalP x y = inheritLoc x $
529529
--
530530
-- NOTE: The GHC AST (but not TH) supports declaring multiple record fields
531531
-- with the same type. We do not support this here (since we follow TH).
532-
recC :: LIdP GhcPs -> [(LIdP GhcPs, LHsType GhcPs)] -> LConDecl GhcPs
532+
recC :: LIdP GhcPs -> [(LIdP GhcPs, LHsType GhcPs, HsSrcBang)] -> LConDecl GhcPs
533533
recC = forallRecC [] []
534534

535535
-- | Inverse to 'recC'
536-
viewRecC :: LConDecl GhcPs -> Maybe (LIdP GhcPs, [(LIdP GhcPs, LHsType GhcPs)])
536+
viewRecC :: LConDecl GhcPs -> Maybe (LIdP GhcPs, [(LIdP GhcPs, LHsType GhcPs, HsSrcBang)])
537537
viewRecC
538538
(L _
539539
ConDeclH98 {
@@ -549,14 +549,14 @@ viewRecC
549549
}
550550
) = (conName ,) <$> mapM viewRecField fields
551551
where
552-
viewRecField :: LConDeclField GhcPs -> Maybe (LIdP GhcPs, LHsType GhcPs)
552+
viewRecField :: LConDeclField GhcPs -> Maybe (LIdP GhcPs, LHsType GhcPs, HsSrcBang)
553553
viewRecField
554554
(L _
555555
ConDeclField {
556556
cd_fld_names = [L _ name]
557557
, cd_fld_type = ty
558558
}
559-
) = Just $ (viewFieldOcc name, ty)
559+
) = Just (viewFieldOcc name, getBangType ty, getBangStrictness ty)
560560
viewRecField _otherwise = Nothing
561561

562562
viewFieldOcc :: FieldOcc GhcPs -> LIdP GhcPs
@@ -566,7 +566,7 @@ viewRecC
566566
#endif
567567
viewRecC _otherwise = Nothing
568568

569-
pattern RecC :: LIdP GhcPs -> [(LIdP GhcPs, LHsType GhcPs)] -> LConDecl GhcPs
569+
pattern RecC :: LIdP GhcPs -> [(LIdP GhcPs, LHsType GhcPs, HsSrcBang)] -> LConDecl GhcPs
570570
pattern RecC conName args <- (viewRecC -> Just (conName, args))
571571
where
572572
RecC = recC
@@ -577,26 +577,34 @@ forallRecC ::
577577
[LIdP GhcPs] -- ^ @forallC@ argument: bound type variables
578578
-> [LHsType GhcPs] -- ^ @forallC@ argument: context
579579
-> LIdP GhcPs -- ^ @recC@ argument: record constructor name
580-
-> [(LIdP GhcPs, LHsType GhcPs)] -- ^ @recC@ argument: record fields
580+
-> [(LIdP GhcPs, LHsType GhcPs, HsSrcBang)] -- ^ @recC@ argument: record fields
581581
-> LConDecl GhcPs
582582
forallRecC vars ctxt conName args = inheritLoc conName $ ConDeclH98 {
583583
con_ext = defExt
584584
, con_name = conName
585585
, con_forall = inheritLoc conName True
586586
, con_ex_tvs = map (setDefaultSpecificity . mkBndr) vars
587587
, con_mb_cxt = Just (inheritLoc conName ctxt)
588-
, con_args = RecCon (inheritLoc conName $ map (uncurry mkRecField) args)
588+
, con_args = RecCon (inheritLoc conName $ map mkRecField args)
589589
, con_doc = Nothing
590590
}
591591
where
592592
mkBndr :: LIdP GhcPs -> LHsTyVarBndr GhcPs
593593
mkBndr name = inheritLoc name $ userTyVar name
594594

595-
mkRecField :: LIdP GhcPs -> LHsType GhcPs -> LConDeclField GhcPs
596-
mkRecField name ty = inheritLoc name $ ConDeclField {
595+
optionalBang :: HsSrcBang -> LHsType GhcPs -> LHsType GhcPs
596+
optionalBang bang = noLocA . HsBangTy defExt
597+
#if __GLASGOW_HASKELL__ >= 912
598+
(case bang of HsSrcBang _ b -> b)
599+
#else
600+
bang
601+
#endif
602+
603+
mkRecField :: (LIdP GhcPs, LHsType GhcPs, HsSrcBang) -> LConDeclField GhcPs
604+
mkRecField (name, ty, bang) = inheritLoc name $ ConDeclField {
597605
cd_fld_ext = defExt
598606
, cd_fld_names = [inheritLoc name $ mkFieldOcc name]
599-
, cd_fld_type = ty
607+
, cd_fld_type = optionalBang bang ty
600608
, cd_fld_doc = Nothing
601609
}
602610

large-records/src/Data/Record/Internal/Plugin/CodeGen.hs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,19 +124,11 @@ genDatatype Record{..} = pure $
124124
| (i, _) <- zip [1 :: Int ..] recordFields
125125
]
126126

127-
optionalBang :: HsSrcBang -> LHsType GhcPs -> LHsType GhcPs
128-
optionalBang bang = noLocA . HsBangTy defExt
129-
#if __GLASGOW_HASKELL__ >= 912
130-
(case bang of HsSrcBang _ b -> b)
131-
#else
132-
bang
133-
#endif
134-
135127
fieldContext :: LIdP GhcPs -> Field -> LHsType GhcPs
136128
fieldContext var fld = equalP (VarT var) (fieldType fld)
137129

138-
fieldExistentialType :: LIdP GhcPs -> Field -> (LIdP GhcPs, LHsType GhcPs)
139-
fieldExistentialType var fld = (fieldName fld, optionalBang (fieldStrictness fld) $ VarT var)
130+
fieldExistentialType :: LIdP GhcPs -> Field -> (LIdP GhcPs, LHsType GhcPs, HsSrcBang)
131+
fieldExistentialType var fld = (fieldName fld, VarT var, fieldStrictness fld)
140132

141133
-- | Generate conversion to and from an array
142134
--

large-records/src/Data/Record/Internal/Plugin/Record.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ viewRecord annLoc options decl =
9898

9999
viewField ::
100100
MonadError Exception m
101-
=> (LIdP GhcPs, LHsType GhcPs) -> m (Int -> Field)
102-
viewField (name, typ) =
103-
return $ Field name (parensT (getBangType typ)) (getBangStrictness typ)
101+
=> (LIdP GhcPs, LHsType GhcPs, HsSrcBang) -> m (Int -> Field)
102+
viewField (name, typ, bang) =
103+
return $ Field name (parensT typ) bang
104104

105105
viewRecordDerivings ::
106106
MonadError Exception m

0 commit comments

Comments
 (0)