Commit 9fc40c73 authored by Simon Peyton Jones's avatar Simon Peyton Jones

Refactor the kind-checking of tyvar binders

The refactoring here is driven by the ghastly mess described in
comment:24 of Trac #1520.  The overall goal is to simplify the
kind-checking of typev-variable binders, and in particular to narrow
the use of the "in-scope tyvar binder" stuff,
which is needed only for associated types: see the new
Note [Kind-checking tyvar binders for associated types] in TcHsType.

Now

* The "in-scope tyvar binder" stuff is done only in
     - kcLHsQTyVars, which is used for the LHsQTyVars of a
       data/newtype, or type family declaration.

     - tcFamTyPats, which is used for associated family instances;
       it now calls tcImplicitQTKBndrs, which in turn usese
       newFlexiKindedQTyVar

* tcExpicitTKBndrs (which is used only for function signatures,
  data con signatures, pattern synonym signatures, and expression
  type signatures) now does not go via the "in-scope tyvar binder"
  stuff at all.

While I'm still not happy with all this code, the code is generally
simpler, and I think this is a useful step forward. It does cure
the problem too.

(It's hard to trigger the problem in vanilla Haskell code, because
the renamer would normally use different names for nested binders,
so I can't offer a test.)
parent 95324f01
......@@ -516,17 +516,11 @@ data TyClDecl pass
-- 'ApiAnnotation.AnnWhere',
-- For details on above see note [Api annotations] in ApiAnnotation
DataDecl { tcdDExt :: XDataDecl pass -- ^ Post renamer, CUSK flag, FVs
, tcdLName :: Located (IdP pass) -- ^ Type constructor
, tcdTyVars :: LHsQTyVars pass -- ^ Type variables; for an
-- associated type
-- these include outer binders
-- Eg class T a where
-- type F a :: *
-- type F a = a -> a
-- Here the type decl for 'f'
-- includes 'a' in its tcdTyVars
, tcdFixity :: LexicalFixity -- ^ Fixity used in the declaration
DataDecl { tcdDExt :: XDataDecl pass -- ^ Post renamer, CUSK flag, FVs
, tcdLName :: Located (IdP pass) -- ^ Type constructor
, tcdTyVars :: LHsQTyVars pass -- ^ Type variables
-- See Note [TyVar binders for associated declarations]
, tcdFixity :: LexicalFixity -- ^ Fixity used in the declaration
, tcdDataDefn :: HsDataDefn pass }
| ClassDecl { tcdCExt :: XClassDecl pass, -- ^ Post renamer, FVs
......@@ -539,8 +533,7 @@ data TyClDecl pass
tcdSigs :: [LSig pass], -- ^ Methods' signatures
tcdMeths :: LHsBinds pass, -- ^ Default methods
tcdATs :: [LFamilyDecl pass], -- ^ Associated types;
tcdATDefs :: [LTyFamDefltEqn pass],
-- ^ Associated type defaults
tcdATDefs :: [LTyFamDefltEqn pass], -- ^ Associated type defaults
tcdDocs :: [LDocDecl] -- ^ Haddock docs
}
-- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnClass',
......@@ -558,6 +551,27 @@ data DataDeclRn = DataDeclRn
, tcdFVs :: NameSet }
deriving Data
{- Note [TyVar binders for associated decls]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
For an /associated/ data, newtype, or type-family decl, the LHsQTyVars
/includes/ outer binders. For example
class T a where
data D a c
type F a b :: *
type F a b = a -> a
Here the data decl for 'D', and type-family decl for 'F', both include 'a'
in their LHsQTyVars (tcdTyVars and fdTyVars resp).
Ditto any implicit binders in the hsq_implicit field of the LHSQTyVars.
The idea is that the associated type is really a top-level decl in its
own right. However we are careful to use the same name 'a', so that
we can match things up.
c.f. Note [Associated type tyvar names] in Class.hs
Note [Family instance declaration binders]
-}
type instance XFamDecl (GhcPass _) = NoExt
type instance XSynDecl GhcPs = NoExt
......@@ -966,6 +980,7 @@ data FamilyDecl pass = FamilyDecl
, fdInfo :: FamilyInfo pass -- type/data, closed/open
, fdLName :: Located (IdP pass) -- type constructor
, fdTyVars :: LHsQTyVars pass -- type variables
-- See Note [TyVar binders for associated declarations]
, fdFixity :: LexicalFixity -- Fixity used in the declaration
, fdResultSig :: LFamilyResultSig pass -- result signature
, fdInjectivityAnn :: Maybe (LInjectivityAnn pass) -- optional injectivity ann
......@@ -1490,6 +1505,8 @@ HsImplicitBndrs in FamInstEqn. Note in particular
For associated type family default instances (TyFamDefltEqn), instead of using
type patterns with binders in a surrounding HsImplicitBndrs, we use raw type
variables (LHsQTyVars) in the feqn_pats field of FamEqn.
c.f. Note [TyVar binders for associated declarations]
-}
-- | Type Family Instance Equation
......
This diff is collapsed.
......@@ -1615,7 +1615,8 @@ tcFamTyPats fam_tc mb_clsinfo
; (fam_used_tvs, (typats, (more_typats, res_kind)))
<- solveEqualities $ -- See Note [Constraints in patterns]
tcImplicitTKBndrs FamInstSkol tv_names $
tcImplicitQTKBndrs FamInstSkol tv_names $
-- See Note [Kind-checking tyvar binders for associated types]
do { let loc = nameSrcSpan fam_name
lhs_fun = L loc (HsTyVar noExt NotPromoted
(L loc fam_name))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment