Skip to content

Use HsOuterExplicit in instance sigs in deriving-generated code

Ryan Scott requested to merge wip/T18914 into master

Issue #18914 (closed) revealed that GeneralizedNewtypeDeriving would generate code that mentions unbound type variables, which is dangerously fragile. The problem (and fix) is described in the new Wrinkle: Use HsOuterExplicit in Note [GND and QuantifiedConstraints]. The gist of it: make sure to put the top-level foralls in deriving-generated instance signatures in an HsOuterExplicit to ensure that they scope over the bodies of methods correctly. A side effect of this process is that it will expand any type synonyms in the instance signature, which will surface any foralls that are hidden underneath type synonyms (such as in the test case for #18914 (closed)).

While I was in town, I also performed some maintenance on NewHsTypeX, which powers GeneralizedNewtypeDeriving:

  • I renamed NewHsTypeX to HsCoreTy, which more accurately describes its intended purpose (#15706 (closed)).
  • To make sure that mistakes similar to #18914 (closed) do not occur later, I added an additional validity check when renaming HsCoreTys that complains if an HsCoreTys contains an out-of-scope type variable. See the new Note [Renaming HsCoreTys] in GHC.Rename.HsType for the details.

Fixes #15706 (closed). Fixes #18914 (closed).

Edited by Ryan Scott

Merge request reports