summaryrefslogtreecommitdiff
path: root/compiler/GHC/Types/GREInfo.hs
blob: 23d734b7d1debb4cac53fa152fa1d334d5995521 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
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
267
268
269
270
271
272
273
274
275
276
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DerivingStrategies #-}

-- | Renamer-level information about 'Name's.
--
-- Renamer equivalent of 'TyThing'.
module GHC.Types.GREInfo where

import GHC.Prelude

import GHC.Types.Basic
import GHC.Types.FieldLabel
import GHC.Types.Name
import GHC.Types.Unique
import GHC.Types.Unique.Set
import GHC.Utils.Outputable
import GHC.Utils.Panic

import Control.DeepSeq ( NFData(..), deepseq )

import Data.Data ( Data )
import Data.List.NonEmpty ( NonEmpty )
import qualified Data.List.NonEmpty as NonEmpty

{-**********************************************************************
*                                                                      *
                           GREInfo
*                                                                      *
************************************************************************

Note [GREInfo]
~~~~~~~~~~~~~~
In the renamer, we sometimes need a bit more information about a 'Name', e.g.
whether it is a type constructor, class, data constructor, record field, etc.

For example, when typechecking record construction, the renamer needs to look
up the fields of the data constructor being used (see e.g. GHC.Rename.Pat.rnHsRecFields).
Extra information also allows us to provide better error messages when a fatal
error occurs in the renamer, as it allows us to distinguish classes, type families,
type synonyms, etc.

For imported Names, we have access to the full type information in the form of
a TyThing (although see Note [Retrieving the GREInfo from interfaces]).
However, for Names in the module currently being renamed, we don't
yet have full information. Instead of using TyThing, we use the GREInfo type,
and this information gets affixed to each element in the GlobalRdrEnv.

This allows us to treat imported and local Names in a consistent manner:
always look at the GREInfo.

Note [Retrieving the GREInfo from interfaces]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If we have a TyThing, we can easily compute the corresponding GREInfo: this is
done in GHC.Types.TyThing.tyThingGREInfo.

However, one often needs to produce GlobalRdrElts (and thus their GREInfos)
directly after loading interface files, before they are typechecked. For example:

  - GHC.Tc.Module.tcRnModuleTcRnM first calls tcRnImports, which starts off
    calling rnImports which transitively calls filterImports. That function
    is responsible for coughing up GlobalRdrElts (and their GREInfos) obtained
    from interfaces, but we will only typecheck the interfaces after we have
    finished processing the imports (see e.g. the logic at the start of tcRnImports
    which sets eps_is_boot, which decides whether we should look in the boot
    or non-boot interface for any particular module).
  - GHC.Tc.Utils.Backpack.mergeSignatures first loads the relevant signature
    interfaces to merge them, but only later on does it typecheck them.

In both of these examples, what's important is that we **lazily** produce the
GREInfo: it should only be consulted once the interfaces have been typechecked,
which will add the necessary information to the type-level environment.
In particular, the respective functions 'filterImports' and 'mergeSignatures'
should NOT force the gre_info field.

We delay the loading of interfaces by making the gre_info field of 'GlobalRdrElt'
a thunk which, when forced, loads the interface, looks up the 'Name' in the type
environment to get its associated TyThing, and computes the GREInfo from that.
See 'GHC.Rename.Env.lookupGREInfo'.

A possible alternative design would be to change the AvailInfo datatype to also
store GREInfo. We currently don't do that, as this would mean that every time
an interface re-exports something it has to also provide its GREInfo, which
could lead to bloat.

Note [Forcing GREInfo]
~~~~~~~~~~~~~~~~~~~~~~
The GREInfo field of a GlobalRdrElt needs to be lazy, as explained in
Note [Retrieving the GREInfo from interfaces]. For imported things, this field
is usually a thunk which looks up the GREInfo in a type environment
(see GHC.Rename.Env.lookupGREInfo).

We thus need to be careful not to introduce space leaks: such thunks could end
up retaining old type environments, which would violate invariant (5) of
Note [GHC Heap Invariants] in GHC.Driver.Make. This can happen, for example,
when reloading in GHCi (see e.g. test T15369, which can trigger the ghci leak check
if we're not careful).

A naive approach is to simply deeply force the whole GlobalRdrEnv. However,
forcing the GREInfo thunks can force the loading of interface files which we
otherwise might not need to load, so it leads to wasted work.

Instead, whenever we are about to store the GlobalRdrEnv somewhere (such as
in ModDetails), we dehydrate it by stripping away the GREInfo field, turning it
into (). See 'forceGlobalRdrEnv' and its cousin 'hydrateGlobalRdrEnv',
as well as Note [IfGlobalRdrEnv] in GHC.Types.Name.Reader.

Search for references to this note in the code for illustration.
-}

-- | Information about a 'Name' that is pertinent to the renamer.
--
-- See Note [GREInfo]
data GREInfo
      -- | No particular information... e.g. a function
    = Vanilla
      -- | 'TyCon'
    | IAmTyCon    !(TyConFlavour Name)
      -- | 'ConLike'
    | IAmConLike  !ConInfo
      -- ^ The constructor fields.
      -- See Note [Local constructor info in the renamer].
      -- | Record field
    | IAmRecField !RecFieldInfo

    deriving Data

instance NFData GREInfo where
  rnf Vanilla = ()
  rnf (IAmTyCon tc) = rnf tc
  rnf (IAmConLike info) = rnf info
  rnf (IAmRecField info) = rnf info

plusGREInfo :: GREInfo -> GREInfo -> GREInfo
plusGREInfo Vanilla Vanilla = Vanilla
plusGREInfo (IAmTyCon {})    info2@(IAmTyCon {}) = info2
plusGREInfo (IAmConLike {})  info2@(IAmConLike {}) = info2
plusGREInfo (IAmRecField {}) info2@(IAmRecField {}) = info2
plusGREInfo info1 info2 = pprPanic "plusInfo" $
  vcat [ text "info1:" <+> ppr info1
       , text "info2:" <+> ppr info2 ]

instance Outputable GREInfo where
  ppr Vanilla = text "Vanilla"
  ppr (IAmTyCon flav)
    = text "TyCon" <+> ppr flav
  ppr (IAmConLike info)
    = text "ConLike" <+> ppr info
  ppr (IAmRecField info)
    = text "RecField" <+> ppr info

{-**********************************************************************
*                                                                      *
                      Constructor info
*                                                                      *
************************************************************************

Note [Local constructor info in the renamer]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
As explained in Note [GREInfo], information pertinent to the renamer is
stored using the GREInfo datatype. What information do we need about constructors?

Consider the following example:

  data T = T1 { x, y :: Int }
         | T2 { x :: Int }
         | T3
         | T4 Int Bool

We need to know:
* The fields of the data constructor, so that
  - We can complain if you say `T1 { v = 3 }`, where `v` is not a field of `T1`
    See the following call stack
    * GHC.Rename.Expr.rnExpr (RecordCon case)
    * GHC.Rename.Pat.rnHsRecFields
    * GHC.Rename.Env.lookupRecFieldOcc
  - Ditto if you pattern match on `T1 { v = x }`.
    See the following call stack
    * GHC.Rename.Pat.rnHsRecPatsAndThen
    * GHC.Rename.Pat.rnHsRecFields
    * GHC.Rename.Env.lookupRecFieldOcc
  - We can fill in the dots if you say `T1 {..}` in construction or pattern matching
    See GHC.Rename.Pat.rnHsRecFields.rn_dotdot

* Whether the contructor is nullary.
  We need to know this to accept `T2 {..}`, and `T3 {..}`, but reject `T4 {..}`,
  in both construction and pattern matching.
  See GHC.Rename.Pat.rnHsRecFields.rn_dotdot
  and Note [Nullary constructors and empty record wildcards]

Note [Nullary constructors and empty record wildcards]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A nullary constructor is one with no arguments.
For example, both `data T = MkT` and `data T = MkT {}` are nullary.

For consistency and TH convenience, it was agreed that a `{..}`
match or usage on nullary constructors would be accepted.
This is done as as per https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0496-empty-record-wildcards.rst
-}

-- | Information about the record fields of a constructor.
--
-- See Note [Local constructor info in the renamer]
data ConInfo
  = ConHasRecordFields (NonEmpty FieldLabel)
  | ConHasPositionalArgs
  | ConIsNullary
  deriving stock Eq
  deriving Data

instance NFData ConInfo where
  rnf ConIsNullary = ()
  rnf ConHasPositionalArgs = ()
  rnf (ConHasRecordFields flds) = rnf flds

mkConInfo :: Arity -> [FieldLabel] -> ConInfo
mkConInfo 0 _ = ConIsNullary
mkConInfo _ fields = maybe ConHasPositionalArgs ConHasRecordFields
                   $ NonEmpty.nonEmpty fields

conInfoFields :: ConInfo -> [FieldLabel]
conInfoFields (ConHasRecordFields fields) = NonEmpty.toList fields
conInfoFields ConHasPositionalArgs = []
conInfoFields ConIsNullary = []

instance Outputable ConInfo where
  ppr ConIsNullary = text "ConIsNullary"
  ppr ConHasPositionalArgs = text "ConHasPositionalArgs"
  ppr (ConHasRecordFields fieldLabels) =
    text "ConHasRecordFields" <+> braces (ppr fieldLabels)

-- | The 'Name' of a 'ConLike'.
--
-- Useful when we are in the renamer and don't yet have a full 'DataCon' or
-- 'PatSyn' to hand.
data ConLikeName
  = DataConName { conLikeName_Name :: !Name }
  | PatSynName  { conLikeName_Name :: !Name }
  deriving (Eq, Data)

instance Outputable ConLikeName where
  ppr = ppr . conLikeName_Name

instance Uniquable ConLikeName where
  getUnique = getUnique . conLikeName_Name

instance NFData ConLikeName where
  rnf = rnf . conLikeName_Name

{-**********************************************************************
*                                                                      *
                      Record field info
*                                                                      *
**********************************************************************-}

data RecFieldInfo
  = RecFieldInfo
      { recFieldLabel :: !FieldLabel
      , recFieldCons  :: !(UniqSet ConLikeName)
         -- ^ The constructors which have this field label.
         -- Always non-empty.
         --
         -- NB: these constructors will always share a single parent,
         -- as the field label disambiguates between parents in the presence
         -- of duplicate record fields.
      }
  deriving (Eq, Data)

instance NFData RecFieldInfo where
  rnf (RecFieldInfo lbl cons)
    = rnf lbl `seq` nonDetStrictFoldUniqSet deepseq () cons

instance Outputable RecFieldInfo where
  ppr (RecFieldInfo { recFieldLabel = fl, recFieldCons = cons })
    = text "RecFieldInfo" <+> braces
      (text "recFieldLabel:" <+> ppr fl <> comma
      <+> text "recFieldCons:" <+> pprWithCommas ppr (nonDetEltsUniqSet cons))