Skip to content
  • Ryan Scott's avatar
    Use NHsCoreTy to embed types into GND-generated code · 42f797b0
    Ryan Scott authored and Marge Bot's avatar Marge Bot committed
    `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.
    42f797b0