Use NHsCoreTy to embed types into GND-generated code
`GeneralizedNewtypeDeriving` is in the unique situation where it must produce an `LHsType GhcPs` from a Core `Type`. Historically, this was done with the `typeToLHsType` function, which walked over the entire `Type` and attempted to construct an `LHsType` with the same overall structure. `typeToLHsType` is quite complicated, however, and has been the subject of numerous bugs over the years (e.g., #14579). Luckily, there is an easier way to accomplish the same thing: the `XHsType` constructor of `HsType`. `XHsType` bundles an `NHsCoreTy`, which allows embedding a Core `Type` directly into an `HsType`, avoiding the need to laboriously convert from one to another (as `typeToLHsType` did). Moreover, renaming and typechecking an `XHsType` is simple, since one doesn't need to do anything to a Core `Type`... ...well, almost. For the reasons described in `Note [Typechecking NHsCoreTys]` in `GHC.Tc.Gen.HsType`, we must apply a substitution that we build from the local `tcl_env` type environment. But that's a relatively modest price to pay. Now that `GeneralizedNewtypeDeriving` uses `NHsCoreTy`, the `typeToLHsType` function no longer has any uses in GHC, so this patch rips it out. Some additional tweaks to `hsTypeNeedsParens` were necessary to make the new `-ddump-deriv` output correctly parenthesized, but other than that, this patch is quite straightforward. This is a mostly internal refactoring, although it is likely that `GeneralizedNewtypeDeriving`-generated code will now need fewer language extensions in certain situations than it did before.
Showing
- compiler/GHC/Core/Type.hs 4 additions, 4 deletionscompiler/GHC/Core/Type.hs
- compiler/GHC/Hs/Type.hs 39 additions, 25 deletionscompiler/GHC/Hs/Type.hs
- compiler/GHC/Hs/Utils.hs 1 addition, 137 deletionscompiler/GHC/Hs/Utils.hs
- compiler/GHC/Tc/Deriv/Generate.hs 8 additions, 5 deletionscompiler/GHC/Tc/Deriv/Generate.hs
- compiler/GHC/Tc/Gen/HsType.hs 54 additions, 2 deletionscompiler/GHC/Tc/Gen/HsType.hs
- compiler/GHC/Tc/TyCl/Instance.hs 5 additions, 3 deletionscompiler/GHC/Tc/TyCl/Instance.hs
- testsuite/tests/deriving/should_compile/T14578.stderr 18 additions, 31 deletionstestsuite/tests/deriving/should_compile/T14578.stderr
- testsuite/tests/deriving/should_compile/T14579.stderr 16 additions, 14 deletionstestsuite/tests/deriving/should_compile/T14579.stderr
- testsuite/tests/deriving/should_fail/T15073.stderr 1 addition, 2 deletionstestsuite/tests/deriving/should_fail/T15073.stderr
- testsuite/tests/deriving/should_fail/deriving-via-fail5.stderr 2 additions, 2 deletions...uite/tests/deriving/should_fail/deriving-via-fail5.stderr
Loading
Please register or sign in to comment