Types.hs 66.5 KB
Newer Older
Austin Seipp's avatar
Austin Seipp committed
1 2 3 4
{-
(c) The University of Glasgow 2006
(c) The GRASP/AQUA Project, Glasgow University, 1992-1998

5

6
GHC.Hs.Types: Abstract syntax: user-defined types
Austin Seipp's avatar
Austin Seipp committed
7
-}
8

9
{-# LANGUAGE DeriveDataTypeable #-}
10
{-# LANGUAGE FlexibleContexts #-}
My Nguyen's avatar
My Nguyen committed
11
{-# LANGUAGE FlexibleInstances #-}
12 13 14
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE UndecidableInstances #-} -- Note [Pass sensitive types]
15
                                      -- in module GHC.Hs.PlaceHolder
16
{-# LANGUAGE ConstraintKinds #-}
17
{-# LANGUAGE CPP #-}
18
{-# LANGUAGE TypeFamilies #-}
19

20
module GHC.Hs.Types (
21
        HsType(..), NewHsTypeX(..), LHsType, HsKind, LHsKind,
22
        HsTyVarBndr(..), LHsTyVarBndr, ForallVisFlag(..),
23
        LHsQTyVars(..),
24
        HsImplicitBndrs(..),
25 26 27
        HsWildCardBndrs(..),
        LHsSigType, LHsSigWcType, LHsWcType,
        HsTupleSort(..),
28
        HsContext, LHsContext, noLHsContext,
29
        HsTyLit(..),
30
        HsIPName(..), hsIPNameFS,
My Nguyen's avatar
My Nguyen committed
31 32
        HsArg(..), numVisibleArgs,
        LHsTypeArg,
33

34 35
        LBangType, BangType,
        HsSrcBang(..), HsImplBang(..),
36
        SrcStrictness(..), SrcUnpackedness(..),
Austin Seipp's avatar
Austin Seipp committed
37
        getBangType, getBangStrictness,
38

39
        ConDeclField(..), LConDeclField, pprConDeclFields,
40 41

        HsConDetails(..),
Austin Seipp's avatar
Austin Seipp committed
42

43 44 45 46 47
        FieldOcc(..), LFieldOcc, mkFieldOcc,
        AmbiguousFieldOcc(..), mkAmbiguousFieldOcc,
        rdrNameAmbiguousFieldOcc, selectorAmbiguousFieldOcc,
        unambiguousFieldOcc, ambiguousFieldOcc,

My Nguyen's avatar
My Nguyen committed
48
        mkAnonWildCardTy, pprAnonWildCard,
thomasw's avatar
thomasw committed
49

50 51
        mkHsImplicitBndrs, mkHsWildCardBndrs, hsImplicitBody,
        mkEmptyImplicitBndrs, mkEmptyWildCardBndrs,
52
        mkHsQTvs, hsQTvExplicit, emptyLHsQTvs, isEmptyLHsQTvs,
53
        isHsKindedTyVar, hsTvbAllKinded, isLHsForAllTy,
54
        hsScopedTvs, hsWcScopedTvs, dropWildCards,
55
        hsTyVarName, hsAllLTyVarNames, hsLTyVarLocNames,
56
        hsLTyVarName, hsLTyVarNames, hsLTyVarLocName, hsExplicitLTyVarNames,
57
        splitLHsInstDeclTy, getLHsInstDeclHead, getLHsInstDeclClass_maybe,
58
        splitLHsPatSynTy,
59 60
        splitLHsForAllTy, splitLHsForAllTyInvis,
        splitLHsQualTy, splitLHsSigmaTy, splitLHsSigmaTyInvis,
Simon Peyton Jones's avatar
Simon Peyton Jones committed
61
        splitHsFunType, hsTyGetAppHead_maybe,
My Nguyen's avatar
My Nguyen committed
62
        mkHsOpTy, mkHsAppTy, mkHsAppTys, mkHsAppKindTy,
63
        ignoreParens, hsSigType, hsSigWcType,
64
        hsLTyVarBndrToType, hsLTyVarBndrsToTypes,
65
        hsTyKindSig,
66
        hsConDetailsArgs,
67

Simon Peyton Jones's avatar
Simon Peyton Jones committed
68
        -- Printing
69
        pprHsType, pprHsForAll, pprHsForAllExtra, pprHsExplicitForAll,
70
        pprLHsContext,
71
        hsTypeNeedsParens, parenthesizeHsType, parenthesizeHsContext
72 73
    ) where

74 75
#include "HsVersions.h"

76 77
import GhcPrelude

78
import {-# SOURCE #-} GHC.Hs.Expr ( HsSplice, pprSplice )
79

80
import GHC.Hs.Extension
dreixel's avatar
dreixel committed
81

82
import Id ( Id )
83
import Name( Name, NamedThing(getName) )
84
import RdrName ( RdrName )
85
import DataCon( HsSrcBang(..), HsImplBang(..),
86
                SrcStrictness(..), SrcUnpackedness(..) )
87
import TysWiredIn( mkTupleStr )
88
import Type
89
import GHC.Hs.Doc
90 91
import BasicTypes
import SrcLoc
92
import Outputable
93
import FastString
thomasw's avatar
thomasw committed
94
import Maybes( isJust )
95
import Util ( count, debugIsOn )
96

97
import Data.Data hiding ( Fixity, Prefix, Infix )
98

Austin Seipp's avatar
Austin Seipp committed
99 100 101
{-
************************************************************************
*                                                                      *
102
\subsection{Bang annotations}
Austin Seipp's avatar
Austin Seipp committed
103 104 105
*                                                                      *
************************************************************************
-}
106

107
-- | Located Bang Type
108
type LBangType pass = Located (BangType pass)
109 110

-- | Bang Type
111 112 113 114 115
--
-- In the parser, strictness and packedness annotations bind more tightly
-- than docstrings. This means that when consuming a 'BangType' (and looking
-- for 'HsBangTy') we must be ready to peer behind a potential layer of
-- 'HsDocTy'. See #15206 for motivation and 'getBangType' for an example.
116
type BangType pass  = HsType pass       -- Bangs are in the HsType data type
117 118

getBangType :: LHsType a -> LHsType a
119 120 121 122
getBangType                 (L _ (HsBangTy _ _ lty))       = lty
getBangType (L _ (HsDocTy x (L _ (HsBangTy _ _ lty)) lds)) =
  addCLoc lty lds (HsDocTy x lty lds)
getBangType lty                                            = lty
123

Simon Peyton Jones's avatar
Simon Peyton Jones committed
124
getBangStrictness :: LHsType a -> HsSrcBang
125 126
getBangStrictness                 (L _ (HsBangTy _ s _))     = s
getBangStrictness (L _ (HsDocTy _ (L _ (HsBangTy _ s _)) _)) = s
127
getBangStrictness _ = (HsSrcBang NoSourceText NoSrcUnpack NoSrcStrict)
128

Austin Seipp's avatar
Austin Seipp committed
129 130 131
{-
************************************************************************
*                                                                      *
132
\subsection{Data types}
Austin Seipp's avatar
Austin Seipp committed
133 134
*                                                                      *
************************************************************************
135

136 137
This is the syntax for types as seen in type signatures.

138 139
Note [HsBSig binder lists]
~~~~~~~~~~~~~~~~~~~~~~~~~~
Rik Steenkamp's avatar
Rik Steenkamp committed
140
Consider a binder (or pattern) decorated with a type or kind,
141 142 143 144 145 146 147
   \ (x :: a -> a). blah
   forall (a :: k -> *) (b :: k). blah
Then we use a LHsBndrSig on the binder, so that the
renamer can decorate it with the variables bound
by the pattern ('a' in the first example, 'k' in the second),
assuming that neither of them is in scope already
See also Note [Kind and type-variable binders] in RnTypes
148 149 150

Note [HsType binders]
~~~~~~~~~~~~~~~~~~~~~
Alan Zimmerman's avatar
Alan Zimmerman committed
151
The system for recording type and kind-variable binders in HsTypes
152 153 154 155
is a bit complicated.  Here's how it works.

* In a HsType,
     HsForAllTy   represents an /explicit, user-written/ 'forall'
156 157
                   e.g.   forall a b.   {...} or
                          forall a b -> {...}
Rik Steenkamp's avatar
Rik Steenkamp committed
158
     HsQualTy     represents an /explicit, user-written/ context
159 160
                   e.g.   (Eq a, Show a) => ...
                  The context can be empty if that's what the user wrote
Alan Zimmerman's avatar
Alan Zimmerman committed
161
  These constructors represent what the user wrote, no more
162 163
  and no less.

164 165 166 167
* The ForallVisFlag field of HsForAllTy represents whether a forall is
  invisible (e.g., forall a b. {...}, with a dot) or visible
  (e.g., forall a b -> {...}, with an arrow).

168 169 170 171 172 173 174 175
* HsTyVarBndr describes a quantified type variable written by the
  user.  For example
     f :: forall a (b :: *).  blah
  here 'a' and '(b::*)' are each a HsTyVarBndr.  A HsForAllTy has
  a list of LHsTyVarBndrs.

* HsImplicitBndrs is a wrapper that gives the implicitly-quantified
  kind and type variables of the wrapped thing.  It is filled in by
Rik Steenkamp's avatar
Rik Steenkamp committed
176
  the renamer. For example, if the user writes
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
     f :: a -> a
  the HsImplicitBinders binds the 'a' (not a HsForAllTy!).
  NB: this implicit quantification is purely lexical: we bind any
      type or kind variables that are not in scope. The type checker
      may subsequently quantify over further kind variables.

* HsWildCardBndrs is a wrapper that binds the wildcard variables
  of the wrapped thing.  It is filled in by the renamer
     f :: _a -> _
  The enclosing HsWildCardBndrs binds the wildcards _a and _.

* The explicit presence of these wrappers specifies, in the HsSyn,
  exactly where implicit quantification is allowed, and where
  wildcards are allowed.

* LHsQTyVars is used in data/class declarations, where the user gives
  explicit *type* variable bindings, but we need to implicitly bind
  *kind* variables.  For example
      class C (a :: k -> *) where ...
  The 'k' is implicitly bound in the hsq_tvs field of LHsQTyVars

198 199 200 201 202 203 204 205 206 207
Note [The wildcard story for types]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Types can have wildcards in them, to support partial type signatures,
like       f :: Int -> (_ , _a) -> _a

A wildcard in a type can be

  * An anonymous wildcard,
        written '_'
    In HsType this is represented by HsWildCardTy.
My Nguyen's avatar
My Nguyen committed
208 209
    The renamer leaves it untouched, and it is later given fresh meta tyvars in
    the typechecker.
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

  * A named wildcard,
        written '_a', '_foo', etc
    In HsType this is represented by (HsTyVar "_a")
    i.e. a perfectly ordinary type variable that happens
         to start with an underscore

Note carefully:

* When NamedWildCards is off, type variables that start with an
  underscore really /are/ ordinary type variables.  And indeed, even
  when NamedWildCards is on you can bind _a explicitly as an ordinary
  type variable:
        data T _a _b = MkT _b _a
  Or even:
        f :: forall _a. _a -> _b
  Here _a is an ordinary forall'd binder, but (With NamedWildCards)
227
  _b is a named wildcard.  (See the comments in #10982)
228

My Nguyen's avatar
My Nguyen committed
229 230 231 232 233 234 235
* Named wildcards are bound by the HsWildCardBndrs construct, which wraps
  types that are allowed to have wildcards. Unnamed wildcards however are left
  unchanged until typechecking, where we give them fresh wild tyavrs and
  determine whether or not to emit hole constraints on each wildcard
  (we don't if it's a visible type/kind argument or a type family pattern).
  See related notes Note [Wildcards in visible kind application]
  and Note [Wildcards in visible type application] in TcHsType.hs
236 237 238 239

* After type checking is done, we report what types the wildcards
  got unified with.

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
Note [Ordering of implicit variables]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Since the advent of -XTypeApplications, GHC makes promises about the ordering
of implicit variable quantification. Specifically, we offer that implicitly
quantified variables (such as those in const :: a -> b -> a, without a `forall`)
will occur in left-to-right order of first occurrence. Here are a few examples:

  const :: a -> b -> a       -- forall a b. ...
  f :: Eq a => b -> a -> a   -- forall a b. ...  contexts are included

  type a <-< b = b -> a
  g :: a <-< b               -- forall a b. ...  type synonyms matter

  class Functor f where
    fmap :: (a -> b) -> f a -> f b   -- forall f a b. ...
    -- The f is quantified by the class, so only a and b are considered in fmap

This simple story is complicated by the possibility of dependency: all variables
must come after any variables mentioned in their kinds.

  typeRep :: Typeable a => TypeRep (a :: k)   -- forall k a. ...

The k comes first because a depends on k, even though the k appears later than
the a in the code. Thus, GHC does a *stable topological sort* on the variables.
By "stable", we mean that any two variables who do not depend on each other
preserve their existing left-to-right ordering.

267 268 269
Implicitly bound variables are collected by the extract- family of functions
(extractHsTysRdrTyVars, extractHsTyVarBndrsKVs, etc.) in RnTypes.
These functions thus promise to keep left-to-right ordering.
270 271
Look for pointers to this note to see the places where the action happens.

272 273 274 275 276 277 278 279 280 281 282
Note that we also maintain this ordering in kind signatures. Even though
there's no visible kind application (yet), having implicit variables be
quantified in left-to-right order in kind signatures is nice since:

* It's consistent with the treatment for type signatures.
* It can affect how types are displayed with -fprint-explicit-kinds (see
  #15568 for an example), which is a situation where knowing the order in
  which implicit variables are quantified can be useful.
* In the event that visible kind application is implemented, the order in
  which we would expect implicit variables to be ordered in kinds will have
  already been established.
Austin Seipp's avatar
Austin Seipp committed
283
-}
284

285
-- | Located Haskell Context
286
type LHsContext pass = Located (HsContext pass)
Alan Zimmerman's avatar
Alan Zimmerman committed
287
      -- ^ 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnUnit'
288 289
      -- For details on above see note [Api annotations] in ApiAnnotation

290 291 292 293 294 295 296 297
noLHsContext :: LHsContext pass
-- Use this when there is no context in the original program
-- It would really be more kosher to use a Maybe, to distinguish
--     class () => C a where ...
-- from
--     class C a where ...
noLHsContext = noLoc []

298
-- | Haskell Context
299
type HsContext pass = [LHsType pass]
300

301
-- | Located Haskell Type
302
type LHsType pass = Located (HsType pass)
Alan Zimmerman's avatar
Alan Zimmerman committed
303 304
      -- ^ May have 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnComma' when
      --   in a list
305 306

      -- For details on above see note [Api annotations] in ApiAnnotation
307 308

-- | Haskell Kind
309
type HsKind pass = HsType pass
310 311

-- | Located Haskell Kind
312
type LHsKind pass = Located (HsKind pass)
313 314 315
      -- ^ 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'

      -- For details on above see note [Api annotations] in ApiAnnotation
316

317
--------------------------------------------------
318 319
--             LHsQTyVars
--  The explicitly-quantified binders in a data/type declaration
320

321
-- | Located Haskell Type Variable Binder
322
type LHsTyVarBndr pass = Located (HsTyVarBndr pass)
323
                         -- See Note [HsType binders]
324

325
-- | Located Haskell Quantified Type Variables
326
data LHsQTyVars pass   -- See Note [HsType binders]
327
  = HsQTvs { hsq_ext :: XHsQTvs pass
Simon Peyton Jones's avatar
Simon Peyton Jones committed
328 329 330 331

           , hsq_explicit :: [LHsTyVarBndr pass]
                -- Explicit variables, written by the user
                -- See Note [HsForAllTy tyvar binders]
332 333 334
    }
  | XLHsQTyVars (XXLHsQTyVars pass)

335 336 337
type HsQTvsRn = [Name]  -- Implicit variables
  -- For example, in   data T (a :: k1 -> k2) = ...
  -- the 'a' is explicit while 'k1', 'k2' are implicit
Simon Peyton Jones's avatar
Simon Peyton Jones committed
338

339
type instance XHsQTvs GhcPs = NoExtField
340 341
type instance XHsQTvs GhcRn = HsQTvsRn
type instance XHsQTvs GhcTc = HsQTvsRn
342

343
type instance XXLHsQTyVars  (GhcPass _) = NoExtCon
344

345
mkHsQTvs :: [LHsTyVarBndr GhcPs] -> LHsQTyVars GhcPs
346
mkHsQTvs tvs = HsQTvs { hsq_ext = noExtField, hsq_explicit = tvs }
347

348
hsQTvExplicit :: LHsQTyVars pass -> [LHsTyVarBndr pass]
349
hsQTvExplicit = hsq_explicit
350

351
emptyLHsQTvs :: LHsQTyVars GhcRn
352
emptyLHsQTvs = HsQTvs { hsq_ext = [], hsq_explicit = [] }
353

354
isEmptyLHsQTvs :: LHsQTyVars GhcRn -> Bool
355 356 357
isEmptyLHsQTvs (HsQTvs { hsq_ext = imp, hsq_explicit = exp })
  = null imp && null exp
isEmptyLHsQTvs _ = False
358

359
------------------------------------------------
360
--            HsImplicitBndrs
361 362
-- Used to quantify the implicit binders of a type
--    * Implicit binders of a type signature (LHsSigType/LHsSigWcType)
363 364
--    * Patterns in a type/data family instance (HsTyPats)

365
-- | Haskell Implicit Binders
366
data HsImplicitBndrs pass thing   -- See Note [HsType binders]
367 368 369 370
  = HsIB { hsib_ext  :: XHsIB pass thing -- after renamer: [Name]
                                         -- Implicitly-bound kind & type vars
                                         -- Order is important; see
                                         -- Note [Ordering of implicit variables]
371
                                         -- in RnTypes
372

373
         , hsib_body :: thing            -- Main payload (type or list of types)
374
    }
375 376
  | XHsImplicitBndrs (XXHsImplicitBndrs pass thing)

377
type instance XHsIB              GhcPs _ = NoExtField
378 379
type instance XHsIB              GhcRn _ = [Name]
type instance XHsIB              GhcTc _ = [Name]
380

381
type instance XXHsImplicitBndrs  (GhcPass _) _ = NoExtCon
382

383
-- | Haskell Wildcard Binders
384
data HsWildCardBndrs pass thing
385 386
    -- See Note [HsType binders]
    -- See Note [The wildcard story for types]
387
  = HsWC { hswc_ext :: XHsWC pass thing
388
                -- after the renamer
My Nguyen's avatar
My Nguyen committed
389 390
                -- Wild cards, only named
                -- See Note [Wildcards in visible kind application]
391

392 393 394 395
         , hswc_body :: thing
                -- Main payload (type or list of types)
                -- If there is an extra-constraints wildcard,
                -- it's still there in the hsc_body.
396
    }
397 398
  | XHsWildCardBndrs (XXHsWildCardBndrs pass thing)

399
type instance XHsWC              GhcPs b = NoExtField
400 401 402
type instance XHsWC              GhcRn b = [Name]
type instance XHsWC              GhcTc b = [Name]

403
type instance XXHsWildCardBndrs  (GhcPass _) b = NoExtCon
404

405
-- | Located Haskell Signature Type
406
type LHsSigType   pass = HsImplicitBndrs pass (LHsType pass)    -- Implicit only
407 408

-- | Located Haskell Wildcard Type
409
type LHsWcType    pass = HsWildCardBndrs pass (LHsType pass)    -- Wildcard only
410 411

-- | Located Haskell Signature Wildcard Type
412
type LHsSigWcType pass = HsWildCardBndrs pass (LHsSigType pass) -- Both
413 414 415

-- See Note [Representing type signatures]

416
hsImplicitBody :: HsImplicitBndrs (GhcPass p) thing -> thing
417
hsImplicitBody (HsIB { hsib_body = body }) = body
418
hsImplicitBody (XHsImplicitBndrs nec) = noExtCon nec
419

420
hsSigType :: LHsSigType (GhcPass p) -> LHsType (GhcPass p)
421 422
hsSigType = hsImplicitBody

423
hsSigWcType :: LHsSigWcType pass -> LHsType pass
424
hsSigWcType sig_ty = hsib_body (hswc_body sig_ty)
425

426
dropWildCards :: LHsSigWcType pass -> LHsSigType pass
427
-- Drop the wildcard part of a LHsSigWcType
428
dropWildCards sig_ty = hswc_body sig_ty
429 430 431 432 433 434 435 436 437

{- Note [Representing type signatures]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HsSigType is used to represent an explicit user type signature
such as   f :: a -> a
     or   g (x :: a -> a) = x

A HsSigType is just a HsImplicitBndrs wrapping a LHsType.
 * The HsImplicitBndrs binds the /implicitly/ quantified tyvars
438
 * The LHsType binds the /explicitly/ quantified tyvars
439 440 441 442

E.g. For a signature like
   f :: forall (a::k). blah
we get
443
   HsIB { hsib_vars = [k]
444 445 446
        , hsib_body = HsForAllTy { hst_bndrs = [(a::*)]
                                 , hst_body = blah }
The implicit kind variable 'k' is bound by the HsIB;
447
the explicitly forall'd tyvar 'a' is bound by the HsForAllTy
448 449
-}

450
mkHsImplicitBndrs :: thing -> HsImplicitBndrs GhcPs thing
451
mkHsImplicitBndrs x = HsIB { hsib_ext  = noExtField
452
                           , hsib_body = x }
453

454
mkHsWildCardBndrs :: thing -> HsWildCardBndrs GhcPs thing
455
mkHsWildCardBndrs x = HsWC { hswc_body = x
456
                           , hswc_ext  = noExtField }
457 458 459

-- Add empty binders.  This is a bit suspicious; what if
-- the wrapped thing had free type variables?
460
mkEmptyImplicitBndrs :: thing -> HsImplicitBndrs GhcRn thing
461
mkEmptyImplicitBndrs x = HsIB { hsib_ext = []
462
                              , hsib_body = x }
463

464
mkEmptyWildCardBndrs :: thing -> HsWildCardBndrs GhcRn thing
465
mkEmptyWildCardBndrs x = HsWC { hswc_body = x
466
                              , hswc_ext  = [] }
467

468

469
--------------------------------------------------
Mateusz Kowalczyk's avatar
Typo  
Mateusz Kowalczyk committed
470
-- | These names are used early on to store the names of implicit
471
-- parameters.  They completely disappear after type-checking.
Jan Stolarek's avatar
Jan Stolarek committed
472
newtype HsIPName = HsIPName FastString
473
  deriving( Eq, Data )
474 475 476 477 478 479 480 481 482 483 484 485

hsIPNameFS :: HsIPName -> FastString
hsIPNameFS (HsIPName n) = n

instance Outputable HsIPName where
    ppr (HsIPName n) = char '?' <> ftext n -- Ordinary implicit parameters

instance OutputableBndr HsIPName where
    pprBndr _ n   = ppr n         -- Simple for now
    pprInfixOcc  n = ppr n
    pprPrefixOcc n = ppr n

486
--------------------------------------------------
487 488

-- | Haskell Type Variable Binder
489
data HsTyVarBndr pass
490
  = UserTyVar        -- no explicit kinding
491
         (XUserTyVar pass)
492
         (Located (IdP pass))
493
        -- See Note [Located RdrNames] in GHC.Hs.Expr
494
  | KindedTyVar
495
         (XKindedTyVar pass)
496 497
         (Located (IdP pass))
         (LHsKind pass)  -- The user-supplied kind signature
Alan Zimmerman's avatar
Alan Zimmerman committed
498 499 500
        -- ^
        --  - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen',
        --          'ApiAnnotation.AnnDcolon', 'ApiAnnotation.AnnClose'
501 502

        -- For details on above see note [Api annotations] in ApiAnnotation
503 504 505 506

  | XTyVarBndr
      (XXTyVarBndr pass)

507 508
type instance XUserTyVar    (GhcPass _) = NoExtField
type instance XKindedTyVar  (GhcPass _) = NoExtField
509

510
type instance XXTyVarBndr   (GhcPass _) = NoExtCon
511

512
-- | Does this 'HsTyVarBndr' come with an explicit kind annotation?
513
isHsKindedTyVar :: HsTyVarBndr pass -> Bool
514 515
isHsKindedTyVar (UserTyVar {})   = False
isHsKindedTyVar (KindedTyVar {}) = True
516
isHsKindedTyVar (XTyVarBndr {})  = False
517

518
-- | Do all type variables in this 'LHsQTyVars' come with kind annotations?
519
hsTvbAllKinded :: LHsQTyVars pass -> Bool
520
hsTvbAllKinded = all (isHsKindedTyVar . unLoc) . hsQTvExplicit
521

522 523 524 525 526
instance NamedThing (HsTyVarBndr GhcRn) where
  getName (UserTyVar _ v) = unLoc v
  getName (KindedTyVar _ v _) = unLoc v
  getName (XTyVarBndr nec) = noExtCon nec

527
-- | Haskell Type
528
data HsType pass
529
  = HsForAllTy   -- See Note [HsType binders]
530 531 532 533
      { hst_xforall :: XForAllTy pass
      , hst_fvf     :: ForallVisFlag -- Is this `forall a -> {...}` or
                                     --         `forall a. {...}`?
      , hst_bndrs   :: [LHsTyVarBndr pass]
534
                                       -- Explicit, user-supplied 'forall a b c'
535
      , hst_body    :: LHsType pass      -- body type
536
      }
Alan Zimmerman's avatar
Alan Zimmerman committed
537 538
      -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnForall',
      --         'ApiAnnotation.AnnDot','ApiAnnotation.AnnDarrow'
539 540
      -- For details on above see note [Api annotations] in ApiAnnotation

541
  | HsQualTy   -- See Note [HsType binders]
542 543 544
      { hst_xqual :: XQualTy pass
      , hst_ctxt  :: LHsContext pass       -- Context C => blah
      , hst_body  :: LHsType pass }
545

546 547 548 549
  | HsTyVar  (XTyVar pass)
              PromotionFlag    -- Whether explicitly promoted,
                               -- for the pretty printer
             (Located (IdP pass))
550 551
                  -- Type variable, type constructor, or data constructor
                  -- see Note [Promotions (HsTyVar)]
552
                  -- See Note [Located RdrNames] in GHC.Hs.Expr
Alan Zimmerman's avatar
Alan Zimmerman committed
553
      -- ^ - 'ApiAnnotation.AnnKeywordId' : None
554

555 556
      -- For details on above see note [Api annotations] in ApiAnnotation

557 558
  | HsAppTy             (XAppTy pass)
                        (LHsType pass)
559
                        (LHsType pass)
Alan Zimmerman's avatar
Alan Zimmerman committed
560
      -- ^ - 'ApiAnnotation.AnnKeywordId' : None
561

562 563
      -- For details on above see note [Api annotations] in ApiAnnotation

My Nguyen's avatar
My Nguyen committed
564 565 566 567
  | HsAppKindTy         (XAppKindTy pass) -- type level type app
                        (LHsType pass)
                        (LHsKind pass)

568 569
  | HsFunTy             (XFunTy pass)
                        (LHsType pass)   -- function type
570
                        (LHsType pass)
571
      -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnRarrow',
572

573 574
      -- For details on above see note [Api annotations] in ApiAnnotation

575 576
  | HsListTy            (XListTy pass)
                        (LHsType pass)  -- Element type
Alan Zimmerman's avatar
Alan Zimmerman committed
577 578
      -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'['@,
      --         'ApiAnnotation.AnnClose' @']'@
579

580 581
      -- For details on above see note [Api annotations] in ApiAnnotation

582 583
  | HsTupleTy           (XTupleTy pass)
                        HsTupleSort
584
                        [LHsType pass]  -- Element types (length gives arity)
Alan Zimmerman's avatar
Alan Zimmerman committed
585 586
    -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'(' or '(#'@,
    --         'ApiAnnotation.AnnClose' @')' or '#)'@
587

588 589
    -- For details on above see note [Api annotations] in ApiAnnotation

590 591
  | HsSumTy             (XSumTy pass)
                        [LHsType pass]  -- Element types (length gives arity)
592 593 594 595 596
    -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'(#'@,
    --         'ApiAnnotation.AnnClose' '#)'@

    -- For details on above see note [Api annotations] in ApiAnnotation

597 598
  | HsOpTy              (XOpTy pass)
                        (LHsType pass) (Located (IdP pass)) (LHsType pass)
Alan Zimmerman's avatar
Alan Zimmerman committed
599
      -- ^ - 'ApiAnnotation.AnnKeywordId' : None
600

601 602
      -- For details on above see note [Api annotations] in ApiAnnotation

603
  | HsParTy             (XParTy pass)
604
                        (LHsType pass)   -- See Note [Parens in HsSyn] in GHC.Hs.Expr
Simon Peyton Jones's avatar
Simon Peyton Jones committed
605 606
        -- Parenthesis preserved for the precedence re-arrangement in RnTypes
        -- It's important that a * (b + c) doesn't get rearranged to (a*b) + c!
Alan Zimmerman's avatar
Alan Zimmerman committed
607 608
      -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'('@,
      --         'ApiAnnotation.AnnClose' @')'@
609

610 611
      -- For details on above see note [Api annotations] in ApiAnnotation

612 613
  | HsIParamTy          (XIParamTy pass)
                        (Located HsIPName) -- (?x :: ty)
614 615
                        (LHsType pass)   -- Implicit parameters as they occur in
                                         -- contexts
Alan Zimmerman's avatar
Alan Zimmerman committed
616 617 618 619
      -- ^
      -- > (?x :: ty)
      --
      -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
batterseapower's avatar
batterseapower committed
620

621 622
      -- For details on above see note [Api annotations] in ApiAnnotation

623 624 625 626 627
  | HsStarTy            (XStarTy pass)
                        Bool             -- Is this the Unicode variant?
                                         -- Note [HsStarTy]
      -- ^ - 'ApiAnnotation.AnnKeywordId' : None

628 629
  | HsKindSig           (XKindSig pass)
                        (LHsType pass)  -- (ty :: kind)
630
                        (LHsKind pass)  -- A type with a kind signature
Alan Zimmerman's avatar
Alan Zimmerman committed
631 632 633 634 635
      -- ^
      -- > (ty :: kind)
      --
      -- - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'('@,
      --         'ApiAnnotation.AnnDcolon','ApiAnnotation.AnnClose' @')'@
636

637 638
      -- For details on above see note [Api annotations] in ApiAnnotation

639 640
  | HsSpliceTy          (XSpliceTy pass)
                        (HsSplice pass)   -- Includes quasi-quotes
Alan Zimmerman's avatar
Alan Zimmerman committed
641 642
      -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'$('@,
      --         'ApiAnnotation.AnnClose' @')'@
643

644 645
      -- For details on above see note [Api annotations] in ApiAnnotation

646 647
  | HsDocTy             (XDocTy pass)
                        (LHsType pass) LHsDocString -- A documented type
Alan Zimmerman's avatar
Alan Zimmerman committed
648
      -- ^ - 'ApiAnnotation.AnnKeywordId' : None
649

650 651
      -- For details on above see note [Api annotations] in ApiAnnotation

652 653
  | HsBangTy    (XBangTy pass)
                HsSrcBang (LHsType pass)   -- Bang-style type annotations
Alan Zimmerman's avatar
Alan Zimmerman committed
654 655 656 657 658
      -- ^ - 'ApiAnnotation.AnnKeywordId' :
      --         'ApiAnnotation.AnnOpen' @'{-\# UNPACK' or '{-\# NOUNPACK'@,
      --         'ApiAnnotation.AnnClose' @'#-}'@
      --         'ApiAnnotation.AnnBang' @\'!\'@

659 660
      -- For details on above see note [Api annotations] in ApiAnnotation

661 662
  | HsRecTy     (XRecTy pass)
                [LConDeclField pass]    -- Only in data type declarations
Alan Zimmerman's avatar
Alan Zimmerman committed
663 664
      -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @'{'@,
      --         'ApiAnnotation.AnnClose' @'}'@
665

666 667
      -- For details on above see note [Api annotations] in ApiAnnotation

668 669 670
  -- | HsCoreTy (XCoreTy pass) Type -- An escape hatch for tunnelling a *closed*
  --                                -- Core Type through HsSyn.
  --     -- ^ - 'ApiAnnotation.AnnKeywordId' : None
dreixel's avatar
dreixel committed
671

672 673
      -- For details on above see note [Api annotations] in ApiAnnotation

674
  | HsExplicitListTy       -- A promoted explicit list
675
        (XExplicitListTy pass)
676
        PromotionFlag      -- whether explcitly promoted, for pretty printer
677
        [LHsType pass]
Alan Zimmerman's avatar
Alan Zimmerman committed
678 679
      -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @"'["@,
      --         'ApiAnnotation.AnnClose' @']'@
680

681 682
      -- For details on above see note [Api annotations] in ApiAnnotation

683
  | HsExplicitTupleTy      -- A promoted explicit tuple
684
        (XExplicitTupleTy pass)
685
        [LHsType pass]
Alan Zimmerman's avatar
Alan Zimmerman committed
686 687
      -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnOpen' @"'("@,
      --         'ApiAnnotation.AnnClose' @')'@
dreixel's avatar
dreixel committed
688

689 690
      -- For details on above see note [Api annotations] in ApiAnnotation

691
  | HsTyLit (XTyLit pass) HsTyLit      -- A promoted numeric literal.
Alan Zimmerman's avatar
Alan Zimmerman committed
692
      -- ^ - 'ApiAnnotation.AnnKeywordId' : None
693

694 695
      -- For details on above see note [Api annotations] in ApiAnnotation

696
  | HsWildCardTy (XWildCardTy pass)  -- A type wildcard
697
      -- See Note [The wildcard story for types]
Alan Zimmerman's avatar
Alan Zimmerman committed
698
      -- ^ - 'ApiAnnotation.AnnKeywordId' : None
699 700

      -- For details on above see note [Api annotations] in ApiAnnotation
701 702 703 704 705 706 707 708 709 710 711 712 713 714

  -- For adding new constructors via Trees that Grow
  | XHsType
      (XXType pass)

data NewHsTypeX
  = NHsCoreTy Type -- An escape hatch for tunnelling a *closed*
                   -- Core Type through HsSyn.
    deriving Data
      -- ^ - 'ApiAnnotation.AnnKeywordId' : None

instance Outputable NewHsTypeX where
  ppr (NHsCoreTy ty) = ppr ty

715 716 717 718 719 720 721 722 723 724 725 726 727
type instance XForAllTy        (GhcPass _) = NoExtField
type instance XQualTy          (GhcPass _) = NoExtField
type instance XTyVar           (GhcPass _) = NoExtField
type instance XAppTy           (GhcPass _) = NoExtField
type instance XFunTy           (GhcPass _) = NoExtField
type instance XListTy          (GhcPass _) = NoExtField
type instance XTupleTy         (GhcPass _) = NoExtField
type instance XSumTy           (GhcPass _) = NoExtField
type instance XOpTy            (GhcPass _) = NoExtField
type instance XParTy           (GhcPass _) = NoExtField
type instance XIParamTy        (GhcPass _) = NoExtField
type instance XStarTy          (GhcPass _) = NoExtField
type instance XKindSig         (GhcPass _) = NoExtField
728

729
type instance XAppKindTy       (GhcPass _) = SrcSpan -- Where the `@` lives
My Nguyen's avatar
My Nguyen committed
730

731 732
type instance XSpliceTy        GhcPs = NoExtField
type instance XSpliceTy        GhcRn = NoExtField
733 734
type instance XSpliceTy        GhcTc = Kind

735 736 737
type instance XDocTy           (GhcPass _) = NoExtField
type instance XBangTy          (GhcPass _) = NoExtField
type instance XRecTy           (GhcPass _) = NoExtField
738

739 740
type instance XExplicitListTy  GhcPs = NoExtField
type instance XExplicitListTy  GhcRn = NoExtField
741 742
type instance XExplicitListTy  GhcTc = Kind

743 744
type instance XExplicitTupleTy GhcPs = NoExtField
type instance XExplicitTupleTy GhcRn = NoExtField
745 746
type instance XExplicitTupleTy GhcTc = [Kind]

747
type instance XTyLit           (GhcPass _) = NoExtField
748

749
type instance XWildCardTy      (GhcPass _) = NoExtField
750 751 752

type instance XXType         (GhcPass _) = NewHsTypeX

dreixel's avatar
dreixel committed
753

754
-- Note [Literal source text] in BasicTypes for SourceText fields in
Alan Zimmerman's avatar
Alan Zimmerman committed
755
-- the following
756
-- | Haskell Type Literal
757
data HsTyLit
Alan Zimmerman's avatar
Alan Zimmerman committed
758 759
  = HsNumTy SourceText Integer
  | HsStrTy SourceText FastString
760
    deriving Data
761

thomasw's avatar
thomasw committed
762

Austin Seipp's avatar
Austin Seipp committed
763
{-
764 765 766 767
Note [HsForAllTy tyvar binders]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
After parsing:
  * Implicit => empty
768
    Explicit => the variables the user wrote
769 770 771 772 773

After renaming
  * Implicit => the *type* variables free in the type
    Explicit => the variables the user wrote (renamed)

774 775 776
Qualified currently behaves exactly as Implicit,
but it is deprecated to use it for implicit quantification.
In this case, GHC 7.10 gives a warning; see
777
Note [Context quantification] in RnTypes, and #4426.
778
In GHC 8.0, Qualified will no longer bind variables
779 780
and this will become an error.

781
The kind variables bound in the hsq_implicit field come both
782 783 784 785
  a) from the kind signatures on the kind vars (eg k1)
  b) from the scope of the forall (eg k2)
Example:   f :: forall (a::k1) b. T a (b::k2)

786

787 788 789 790
Note [Unit tuples]
~~~~~~~~~~~~~~~~~~
Consider the type
    type instance F Int = ()
Austin Seipp's avatar
Austin Seipp committed
791 792
We want to parse that "()"
    as HsTupleTy HsBoxedOrConstraintTuple [],
793 794 795 796 797 798 799
NOT as HsTyVar unitTyCon

Why? Because F might have kind (* -> Constraint), so we when parsing we
don't know if that tuple is going to be a constraint tuple or an ordinary
unit tuple.  The HsTupleSort flag is specifically designed to deal with
that, but it has to work for unit tuples too.

dreixel's avatar
dreixel committed
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
Note [Promotions (HsTyVar)]
~~~~~~~~~~~~~~~~~~~~~~~~~~~
HsTyVar: A name in a type or kind.
  Here are the allowed namespaces for the name.
    In a type:
      Var: not allowed
      Data: promoted data constructor
      Tv: type variable
      TcCls before renamer: type constructor, class constructor, or promoted data constructor
      TcCls after renamer: type constructor or class constructor
    In a kind:
      Var, Data: not allowed
      Tv: kind variable
      TcCls: kind constructor or promoted type constructor

815 816 817
  The 'Promoted' field in an HsTyVar captures whether the type was promoted in
  the source code by prefixing an apostrophe.

818
Note [HsStarTy]
819
~~~~~~~~~~~~~~~
820 821 822 823
When the StarIsType extension is enabled, we want to treat '*' and its Unicode
variant identically to 'Data.Kind.Type'. Unfortunately, doing so in the parser
would mean that when we pretty-print it back, we don't know whether the user
wrote '*' or 'Type', and lose the parse/ppr roundtrip property.
824

825 826 827 828
As a workaround, we parse '*' as HsStarTy (if it stands for 'Data.Kind.Type')
and then desugar it to 'Data.Kind.Type' in the typechecker (see tc_hs_type).
When '*' is a regular type operator (StarIsType is disabled), HsStarTy is not
involved.
829

dreixel's avatar
dreixel committed
830 831 832 833 834 835 836

Note [Promoted lists and tuples]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Notice the difference between
   HsListTy    HsExplicitListTy
   HsTupleTy   HsExplicitListTupleTy

Austin Seipp's avatar
Austin Seipp committed
837
E.g.    f :: [Int]                      HsListTy
dreixel's avatar
dreixel committed
838

Austin Seipp's avatar
Austin Seipp committed
839 840 841
        g3  :: T '[]                   All these use
        g2  :: T '[True]                  HsExplicitListTy
        g1  :: T '[True,False]
dreixel's avatar
dreixel committed
842 843 844 845
        g1a :: T [True,False]             (can omit ' where unambiguous)

  kind of T :: [Bool] -> *        This kind uses HsListTy!

Austin Seipp's avatar
Austin Seipp committed
846 847 848
E.g.    h :: (Int,Bool)                 HsTupleTy; f is a pair
        k :: S '(True,False)            HsExplicitTypleTy; S is indexed by
                                           a type-level pair of booleans
dreixel's avatar
dreixel committed
849 850
        kind of S :: (Bool,Bool) -> *   This kind uses HsExplicitTupleTy

dreixel's avatar
dreixel committed
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
Note [Distinguishing tuple kinds]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Apart from promotion, tuples can have one of three different kinds:

        x :: (Int, Bool)                -- Regular boxed tuples
        f :: Int# -> (# Int#, Int# #)   -- Unboxed tuples
        g :: (Eq a, Ord a) => a         -- Constraint tuples

For convenience, internally we use a single constructor for all of these,
namely HsTupleTy, but keep track of the tuple kind (in the first argument to
HsTupleTy, a HsTupleSort). We can tell if a tuple is unboxed while parsing,
because of the #. However, with -XConstraintKinds we can only distinguish
between constraint and boxed tuples during type checking, in general. Hence the
four constructors of HsTupleSort:
Austin Seipp's avatar
Austin Seipp committed
866

dreixel's avatar
dreixel committed
867 868 869
        HsUnboxedTuple                  -> Produced by the parser
        HsBoxedTuple                    -> Certainly a boxed tuple
        HsConstraintTuple               -> Certainly a constraint tuple
Austin Seipp's avatar
Austin Seipp committed
870
        HsBoxedOrConstraintTuple        -> Could be a boxed or a constraint
dreixel's avatar
dreixel committed
871 872
                                        tuple. Produced by the parser only,
                                        disappears after type checking
Austin Seipp's avatar
Austin Seipp committed
873
-}
dreixel's avatar
dreixel committed
874

875
-- | Haskell Tuple Sort
batterseapower's avatar
batterseapower committed
876
data HsTupleSort = HsUnboxedTuple
dreixel's avatar
dreixel committed
877 878 879
                 | HsBoxedTuple
                 | HsConstraintTuple
                 | HsBoxedOrConstraintTuple
880
                 deriving Data
batterseapower's avatar
batterseapower committed
881

882
-- | Located Constructor Declaration Field
883
type LConDeclField pass = Located (ConDeclField pass)
Alan Zimmerman's avatar
Alan Zimmerman committed
884 885
      -- ^ May have 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnComma' when
      --   in a list
886 887

      -- For details on above see note [Api annotations] in ApiAnnotation
888 889

-- | Constructor Declaration Field
890
data ConDeclField pass  -- Record fields have Haddoc docs on them
891 892
  = ConDeclField { cd_fld_ext  :: XConDeclField pass,
                   cd_fld_names :: [LFieldOcc pass],
893 894
                                   -- ^ See Note [ConDeclField passs]
                   cd_fld_type :: LBangType pass,
895
                   cd_fld_doc  :: Maybe LHsDocString }
Alan Zimmerman's avatar
Alan Zimmerman committed
896
      -- ^ - 'ApiAnnotation.AnnKeywordId' : 'ApiAnnotation.AnnDcolon'
897 898

      -- For details on above see note [Api annotations] in ApiAnnotation
899 900
  | XConDeclField (XXConDeclField pass)

901 902
type instance XConDeclField  (GhcPass _) = NoExtField
type instance XXConDeclField (GhcPass _) = NoExtCon
903

904 905
instance OutputableBndrId p
       => Outputable (ConDeclField (GhcPass p)) where
906 907
  ppr (ConDeclField _ fld_n fld_ty _) = ppr fld_n <+> dcolon <+> ppr fld_ty
  ppr (XConDeclField x) = ppr x
908 909 910

-- HsConDetails is used for patterns/expressions *and* for data type
-- declarations
911
-- | Haskell Constructor Details
912 913 914 915
data HsConDetails arg rec
  = PrefixCon [arg]             -- C p1 p2 p3
  | RecCon    rec               -- C { x = p1, y = p2 }
  | InfixCon  arg arg           -- p1 `C` p2
916
  deriving Data
917 918 919 920 921 922

instance (Outputable arg, Outputable rec)
         => Outputable (HsConDetails arg rec) where
  ppr (PrefixCon args) = text "PrefixCon" <+> ppr args
  ppr (RecCon rec)     = text "RecCon:" <+> ppr rec
  ppr (InfixCon l r)   = text "InfixCon:" <+> ppr [l, r]
923

924 925 926 927 928 929 930 931
hsConDetailsArgs ::
     HsConDetails (LHsType a) (Located [LConDeclField a])
  -> [LHsType a]
hsConDetailsArgs details = case details of
  InfixCon a b -> [a,b]
  PrefixCon xs -> xs
  RecCon r -> map (cd_fld_type . unLoc) (unLoc r)

932
{-
933
Note [ConDeclField passs]
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951
~~~~~~~~~~~~~~~~~~~~~~~~~

A ConDeclField contains a list of field occurrences: these always
include the field label as the user wrote it.  After the renamer, it
will additionally contain the identity of the selector function in the
second component.

Due to DuplicateRecordFields, the OccName of the selector function
may have been mangled, which is why we keep the original field label
separately.  For example, when DuplicateRecordFields is enabled

    data T = MkT { x :: Int }

gives

    ConDeclField { cd_fld_names = [L _ (FieldOcc "x" $sel:x:MkT)], ... }.
-}

952
-----------------------
953 954 955 956
-- A valid type must have a for-all at the top of the type, or of the fn arg
-- types

---------------------
957
hsWcScopedTvs :: LHsSigWcType GhcRn -> [Name]
958 959 960 961 962
-- Get the lexically-scoped type variables of a HsSigType
--  - the explicitly-given forall'd type variables
--  - the named wildcars; see Note [Scoping of named wildcards]
-- because they scope in the same way
hsWcScopedTvs sig_ty
963
  | HsWC { hswc_ext = nwcs, hswc_body = sig_ty1 }  <- sig_ty
964
  , HsIB { hsib_ext = vars
965
         , hsib_body = sig_ty2 } <- sig_ty1
966
  = case sig_ty2 of
967 968 969 970
      L _ (HsForAllTy { hst_fvf = vis_flag
                      , hst_bndrs = tvs }) ->
        ASSERT( vis_flag == ForallInvis ) -- See Note [hsScopedTvs vis_flag]
        vars ++ nwcs ++ hsLTyVarNames tvs
971
      _                                    -> nwcs
972 973
hsWcScopedTvs (HsWC _ (XHsImplicitBndrs nec)) = noExtCon nec
hsWcScopedTvs (XHsWildCardBndrs nec) = noExtCon nec
974

975
hsScopedTvs :: LHsSigType GhcRn -> [Name]
976 977
-- Same as hsWcScopedTvs, but for a LHsSigType
hsScopedTvs sig_ty
978
  | HsIB { hsib_ext = vars
979
         , hsib_body = sig_ty2 } <- sig_ty
980 981 982 983
  , L _ (HsForAllTy { hst_fvf = vis_flag
                    , hst_bndrs = tvs }) <- sig_ty2
  = ASSERT( vis_flag == ForallInvis ) -- See Note [hsScopedTvs vis_flag]
    vars ++ hsLTyVarNames tvs
984 985
  | otherwise
  = []
986 987 988 989 990 991 992 993 994 995 996 997 998

{- Note [Scoping of named wildcards]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Consider
  f :: _a -> _a
  f x = let g :: _a -> _a
            g = ...
        in ...

Currently, for better or worse, the "_a" variables are all the same. So
although there is no explicit forall, the "_a" scopes over the definition.
I don't know if this is a good idea, but there it is.
-}
999

1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
{- Note [hsScopedTvs vis_flag]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-XScopedTypeVariables can be defined in terms of a desugaring to
-XTypeAbstractions (GHC Proposal #50):

    fn :: forall a b c. tau(a,b,c)            fn :: forall a b c. tau(a,b,c)
    fn = defn(a,b,c)                   ==>    fn @x @y @z = defn(x,y,z)

That is, for every type variable of the leading 'forall' in the type signature,
we add an invisible binder at term level.

This model does not extend to visible forall, as discussed here:

* https://gitlab.haskell.org/ghc/ghc/issues/16734#note_203412
* https://github.com/ghc-proposals/ghc-proposals/pull/238

The conclusion of these discussions can be summarized as fol