summaryrefslogtreecommitdiff
path: root/compiler/GHC/Hs
diff options
context:
space:
mode:
authorRyan Scott <ryan.gl.scott@gmail.com>2020-11-05 11:28:53 -0500
committerMarge Bot <ben+marge-bot@smart-cactus.org>2020-12-18 05:50:33 -0500
commit7a93435b991513b71174c807b4e99ad90f4e5058 (patch)
treeb73379e691967b81a751462b2b051a91ab0ee2ed /compiler/GHC/Hs
parent659fcb14937e60510e3eea4c1211ea117419905b (diff)
downloadhaskell-7a93435b991513b71174c807b4e99ad90f4e5058.tar.gz
Use HsOuterExplicit in instance sigs in deriving-generated code
Issue #18914 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 `forall`s 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 `forall`s that are hidden underneath type synonyms (such as in the test case for #18914). 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). I also made `HsCoreTy` a type synonym instead of a newtype, as making it a distinct data type wasn't buying us much. * To make sure that mistakes similar to #18914 do not occur later, I added an additional validity check when renaming `HsCoreTy`s that complains if an `HsCoreTy`s contains an out-of-scope type variable. See the new `Note [Renaming HsCoreTys]` in `GHC.Rename.HsType` for the details. Fixes #15706. Fixes #18914. Bumps the `haddock` submodule.
Diffstat (limited to 'compiler/GHC/Hs')
-rw-r--r--compiler/GHC/Hs/Type.hs29
1 files changed, 10 insertions, 19 deletions
diff --git a/compiler/GHC/Hs/Type.hs b/compiler/GHC/Hs/Type.hs
index dde27857ec..1b1e56b314 100644
--- a/compiler/GHC/Hs/Type.hs
+++ b/compiler/GHC/Hs/Type.hs
@@ -24,7 +24,7 @@ module GHC.Hs.Type (
HsArrow(..), arrowToHsType,
hsLinear, hsUnrestricted, isUnrestricted,
- HsType(..), NewHsTypeX(..), LHsType, HsKind, LHsKind,
+ HsType(..), HsCoreTy, LHsType, HsKind, LHsKind,
HsForAllTelescope(..), HsTyVarBndr(..), LHsTyVarBndr,
LHsQTyVars(..),
HsOuterTyVarBndrs(..), HsOuterFamEqnTyVarBndrs, HsOuterSigTyVarBndrs,
@@ -1040,12 +1040,6 @@ data HsType pass
-- For details on above see note [Api annotations] in GHC.Parser.Annotation
- -- | HsCoreTy (XCoreTy pass) Type -- An escape hatch for tunnelling a *closed*
- -- -- Core Type through HsSyn.
- -- -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : None
-
- -- For details on above see note [Api annotations] in GHC.Parser.Annotation
-
| HsExplicitListTy -- A promoted explicit list
(XExplicitListTy pass)
PromotionFlag -- whether explicitly promoted, for pretty printer
@@ -1078,16 +1072,13 @@ data HsType pass
| XHsType
(XXType pass)
-data NewHsTypeX
- = NHsCoreTy Type -- An escape hatch for tunnelling a *closed*
- -- Core Type through HsSyn.
- -- See also Note [Typechecking NHsCoreTys] in
- -- GHC.Tc.Gen.HsType.
- deriving Data
- -- ^ - 'GHC.Parser.Annotation.AnnKeywordId' : None
-
-instance Outputable NewHsTypeX where
- ppr (NHsCoreTy ty) = ppr ty
+-- An escape hatch for tunnelling a Core 'Type' through 'HsType'.
+-- For more details on how this works, see:
+--
+-- * @Note [Renaming HsCoreTys]@ in "GHC.Rename.HsType"
+--
+-- * @Note [Typechecking HsCoreTys]@ in "GHC.Tc.Gen.HsType"
+type HsCoreTy = Type
type instance XForAllTy (GhcPass _) = NoExtField
type instance XQualTy (GhcPass _) = NoExtField
@@ -1125,7 +1116,7 @@ type instance XTyLit (GhcPass _) = NoExtField
type instance XWildCardTy (GhcPass _) = NoExtField
-type instance XXType (GhcPass _) = NewHsTypeX
+type instance XXType (GhcPass _) = HsCoreTy
-- Note [Literal source text] in GHC.Types.Basic for SourceText fields in
@@ -2256,7 +2247,7 @@ hsTypeNeedsParens p = go_hs_ty
go_hs_ty (HsOpTy{}) = p >= opPrec
go_hs_ty (HsParTy{}) = False
go_hs_ty (HsDocTy _ (L _ t) _) = go_hs_ty t
- go_hs_ty (XHsType (NHsCoreTy ty)) = go_core_ty ty
+ go_hs_ty (XHsType ty) = go_core_ty ty
go_core_ty (TyVarTy{}) = False
go_core_ty (AppTy{}) = p >= appPrec