diff options
author | Matthew Pickering <matthewtpickering@gmail.com> | 2021-11-22 13:25:17 +0000 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2021-11-25 01:07:40 -0500 |
commit | 4d34bf15e3bebb9f31467278e609f64a3e5b255d (patch) | |
tree | 933431e43586562ed4dab6d2fac0ae7acbd89f5b /compiler | |
parent | d530c46c89acd85a5cb0d1d1dc87fd1960f654b0 (diff) | |
download | haskell-4d34bf15e3bebb9f31467278e609f64a3e5b255d.tar.gz |
Don't use implicit lifting when deriving Lift
It isn't much more complicated to be more precise when deriving Lift so
we now generate
```
data Foo = Foo Int Bool
instance Lift Foo where
lift (Foo a b) = [| Foo $(lift a) $(lift b) |]
liftTyped (Foo a b) = [|| Foo $$(lift a) $$(lift b) |]
```
This fixes #20688 which complained about using implicit lifting in the
derived code.
Diffstat (limited to 'compiler')
-rw-r--r-- | compiler/GHC/Tc/Deriv/Generate.hs | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/compiler/GHC/Tc/Deriv/Generate.hs b/compiler/GHC/Tc/Deriv/Generate.hs index e3856765ec..a061674af9 100644 --- a/compiler/GHC/Tc/Deriv/Generate.hs +++ b/compiler/GHC/Tc/Deriv/Generate.hs @@ -1632,11 +1632,14 @@ Example: ==> instance (Lift a) => Lift (Foo a) where - lift (Foo a) = [| Foo a |] - lift ((:^:) u v) = [| (:^:) u v |] + lift (Foo a) = [| Foo $(lift a) |] + lift ((:^:) u v) = [| (:^:) $(lift u) $(lift v) |] - liftTyped (Foo a) = [|| Foo a ||] - liftTyped ((:^:) u v) = [|| (:^:) u v ||] + liftTyped (Foo a) = [|| Foo $$(liftTyped a) ||] + liftTyped ((:^:) u v) = [|| (:^:) $$(liftTyped u) $$(liftTyped v) ||] + +Note that we use explicit splices here in order to not trigger the implicit +lifting warning in derived code. (See #20688) -} @@ -1646,15 +1649,18 @@ gen_Lift_binds loc (DerivInstTys{ dit_rep_tc = tycon (listToBag [lift_bind, liftTyped_bind], emptyBag) where lift_bind = mkFunBindEC 1 loc lift_RDR (nlHsApp pure_Expr) - (map (pats_etc mk_exp) data_cons) + (map (pats_etc mk_exp mk_usplice liftName) data_cons) liftTyped_bind = mkFunBindEC 1 loc liftTyped_RDR (nlHsApp unsafeCodeCoerce_Expr . nlHsApp pure_Expr) - (map (pats_etc mk_texp) data_cons) + (map (pats_etc mk_texp mk_tsplice liftTypedName) data_cons) mk_exp = ExpBr noExtField mk_texp = TExpBr noExtField + + mk_usplice = HsUntypedSplice EpAnnNotUsed DollarSplice + mk_tsplice = HsTypedSplice EpAnnNotUsed DollarSplice data_cons = getPossibleDataCons tycon tycon_args - pats_etc mk_bracket data_con + pats_etc mk_bracket mk_splice lift_name data_con = ([con_pat], lift_Expr) where con_pat = nlConVarPat data_con_RDR as_needed @@ -1663,7 +1669,13 @@ gen_Lift_binds loc (DerivInstTys{ dit_rep_tc = tycon as_needed = take con_arity as_RDRs lift_Expr = noLocA (HsBracket noAnn (mk_bracket br_body)) br_body = nlHsApps (Exact (dataConName data_con)) - (map nlHsVar as_needed) + (map lift_var as_needed) + + lift_var :: RdrName -> LHsExpr (GhcPass 'Parsed) + lift_var x = noLocA (HsSpliceE EpAnnNotUsed (mk_splice x (nlHsPar (mk_lift_expr x)))) + + mk_lift_expr :: RdrName -> LHsExpr (GhcPass 'Parsed) + mk_lift_expr x = nlHsApps (Exact lift_name) [nlHsVar x] {- ************************************************************************ |