In hole fits, don't show VTA for inferred variables (#16456)
[ghc.git] / compiler / typecheck / TcHoleErrors.hs
1 module TcHoleErrors ( findValidHoleFits, tcFilterHoleFits, HoleFit (..)
2 , HoleFitCandidate (..), tcCheckHoleFit, tcSubsumes
3 , withoutUnification ) where
4
5 import GhcPrelude
6
7 import TcRnTypes
8 import TcRnMonad
9 import TcMType
10 import TcEvidence
11 import TcType
12 import Type
13 import DataCon
14 import Name
15 import RdrName ( pprNameProvenance , GlobalRdrElt (..), globalRdrEnvElts )
16 import PrelNames ( gHC_ERR )
17 import Id
18 import VarSet
19 import VarEnv
20 import Bag
21 import ConLike ( ConLike(..) )
22 import Util
23 import TcEnv (tcLookup)
24 import Outputable
25 import DynFlags
26 import Maybes
27 import FV ( fvVarList, fvVarSet, unionFV, mkFVs, FV )
28
29 import Control.Arrow ( (&&&) )
30
31 import Control.Monad ( filterM, replicateM )
32 import Data.List ( partition, sort, sortOn, nubBy )
33 import Data.Graph ( graphFromEdges, topSort )
34 import Data.Function ( on )
35
36
37 import TcSimplify ( simpl_top, runTcSDeriveds )
38 import TcUnify ( tcSubType_NC )
39
40 import ExtractDocs ( extractDocs )
41 import qualified Data.Map as Map
42 import HsDoc ( HsDocString, unpackHDS, DeclDocMap(..) )
43 import HscTypes ( ModIface(..) )
44 import LoadIface ( loadInterfaceForNameMaybe )
45
46 import PrelInfo (knownKeyNames)
47
48
49 {-
50 Note [Valid hole fits include ...]
51 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
52 `findValidHoleFits` returns the "Valid hole fits include ..." message.
53 For example, look at the following definitions in a file called test.hs:
54
55 import Data.List (inits)
56
57 f :: [String]
58 f = _ "hello, world"
59
60 The hole in `f` would generate the message:
61
62 • Found hole: _ :: [Char] -> [String]
63 • In the expression: _
64 In the expression: _ "hello, world"
65 In an equation for ‘f’: f = _ "hello, world"
66 • Relevant bindings include f :: [String] (bound at test.hs:6:1)
67 Valid hole fits include
68 lines :: String -> [String]
69 (imported from ‘Prelude’ at mpt.hs:3:8-9
70 (and originally defined in ‘base-4.11.0.0:Data.OldList’))
71 words :: String -> [String]
72 (imported from ‘Prelude’ at mpt.hs:3:8-9
73 (and originally defined in ‘base-4.11.0.0:Data.OldList’))
74 inits :: forall a. [a] -> [[a]]
75 with inits @Char
76 (imported from ‘Data.List’ at mpt.hs:4:19-23
77 (and originally defined in ‘base-4.11.0.0:Data.OldList’))
78 repeat :: forall a. a -> [a]
79 with repeat @String
80 (imported from ‘Prelude’ at mpt.hs:3:8-9
81 (and originally defined in ‘GHC.List’))
82 fail :: forall (m :: * -> *). Monad m => forall a. String -> m a
83 with fail @[] @String
84 (imported from ‘Prelude’ at mpt.hs:3:8-9
85 (and originally defined in ‘GHC.Base’))
86 return :: forall (m :: * -> *). Monad m => forall a. a -> m a
87 with return @[] @String
88 (imported from ‘Prelude’ at mpt.hs:3:8-9
89 (and originally defined in ‘GHC.Base’))
90 pure :: forall (f :: * -> *). Applicative f => forall a. a -> f a
91 with pure @[] @String
92 (imported from ‘Prelude’ at mpt.hs:3:8-9
93 (and originally defined in ‘GHC.Base’))
94 read :: forall a. Read a => String -> a
95 with read @[String]
96 (imported from ‘Prelude’ at mpt.hs:3:8-9
97 (and originally defined in ‘Text.Read’))
98 mempty :: forall a. Monoid a => a
99 with mempty @([Char] -> [String])
100 (imported from ‘Prelude’ at mpt.hs:3:8-9
101 (and originally defined in ‘GHC.Base’))
102
103 Valid hole fits are found by checking top level identifiers and local bindings
104 in scope for whether their type can be instantiated to the the type of the hole.
105 Additionally, we also need to check whether all relevant constraints are solved
106 by choosing an identifier of that type as well, see Note [Relevant Constraints]
107
108 Since checking for subsumption results in the side-effect of type variables
109 being unified by the simplifier, we need to take care to restore them after
110 to being flexible type variables after we've checked for subsumption.
111 This is to avoid affecting the hole and later checks by prematurely having
112 unified one of the free unification variables.
113
114 When outputting, we sort the hole fits by the size of the types we'd need to
115 apply by type application to the type of the fit to to make it fit. This is done
116 in order to display "more relevant" suggestions first. Another option is to
117 sort by building a subsumption graph of fits, i.e. a graph of which fits subsume
118 what other fits, and then outputting those fits which are are subsumed by other
119 fits (i.e. those more specific than other fits) first. This results in the ones
120 "closest" to the type of the hole to be displayed first.
121
122 To help users understand how the suggested fit works, we also display the values
123 that the quantified type variables would take if that fit is used, like
124 `mempty @([Char] -> [String])` and `pure @[] @String` in the example above.
125 If -XTypeApplications is enabled, this can even be copied verbatim as a
126 replacement for the hole.
127
128
129 Note [Nested implications]
130 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
131
132 For the simplifier to be able to use any givens present in the enclosing
133 implications to solve relevant constraints, we nest the wanted subsumption
134 constraints and relevant constraints within the enclosing implications.
135
136 As an example, let's look at the following code:
137
138 f :: Show a => a -> String
139 f x = show _
140
141 The hole will result in the hole constraint:
142
143 [WD] __a1ph {0}:: a0_a1pd[tau:2] (CHoleCan: ExprHole(_))
144
145 Here the nested implications are just one level deep, namely:
146
147 [Implic {
148 TcLevel = 2
149 Skolems = a_a1pa[sk:2]
150 No-eqs = True
151 Status = Unsolved
152 Given = $dShow_a1pc :: Show a_a1pa[sk:2]
153 Wanted =
154 WC {wc_simple =
155 [WD] __a1ph {0}:: a_a1pd[tau:2] (CHoleCan: ExprHole(_))
156 [WD] $dShow_a1pe {0}:: Show a_a1pd[tau:2] (CDictCan(psc))}
157 Binds = EvBindsVar<a1pi>
158 Needed inner = []
159 Needed outer = []
160 the type signature for:
161 f :: forall a. Show a => a -> String }]
162
163 As we can see, the givens say that the information about the skolem
164 `a_a1pa[sk:2]` fulfills the Show constraint.
165
166 The simples are:
167
168 [[WD] __a1ph {0}:: a0_a1pd[tau:2] (CHoleCan: ExprHole(_)),
169 [WD] $dShow_a1pe {0}:: Show a0_a1pd[tau:2] (CNonCanonical)]
170
171 I.e. the hole `a0_a1pd[tau:2]` and the constraint that the type of the hole must
172 fulfill `Show a0_a1pd[tau:2])`.
173
174 So when we run the check, we need to make sure that the
175
176 [WD] $dShow_a1pe {0}:: Show a0_a1pd[tau:2] (CNonCanonical)
177
178 Constraint gets solved. When we now check for whether `x :: a0_a1pd[tau:2]` fits
179 the hole in `tcCheckHoleFit`, the call to `tcSubType` will end up writing the
180 meta type variable `a0_a1pd[tau:2] := a_a1pa[sk:2]`. By wrapping the wanted
181 constraints needed by tcSubType_NC and the relevant constraints (see
182 Note [Relevant Constraints] for more details) in the nested implications, we
183 can pass the information in the givens along to the simplifier. For our example,
184 we end up needing to check whether the following constraints are soluble.
185
186 WC {wc_impl =
187 Implic {
188 TcLevel = 2
189 Skolems = a_a1pa[sk:2]
190 No-eqs = True
191 Status = Unsolved
192 Given = $dShow_a1pc :: Show a_a1pa[sk:2]
193 Wanted =
194 WC {wc_simple =
195 [WD] $dShow_a1pe {0}:: Show a0_a1pd[tau:2] (CNonCanonical)}
196 Binds = EvBindsVar<a1pl>
197 Needed inner = []
198 Needed outer = []
199 the type signature for:
200 f :: forall a. Show a => a -> String }}
201
202 But since `a0_a1pd[tau:2] := a_a1pa[sk:2]` and we have from the nested
203 implications that Show a_a1pa[sk:2] is a given, this is trivial, and we end up
204 with a final WC of WC {}, confirming x :: a0_a1pd[tau:2] as a match.
205
206 To avoid side-effects on the nested implications, we create a new EvBindsVar so
207 that any changes to the ev binds during a check remains localised to that check.
208
209
210 Note [Valid refinement hole fits include ...]
211 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
212 When the `-frefinement-level-hole-fits=N` flag is given, we additionally look
213 for "valid refinement hole fits"", i.e. valid hole fits with up to N
214 additional holes in them.
215
216 With `-frefinement-level-hole-fits=0` (the default), GHC will find all
217 identifiers 'f' (top-level or nested) that will fit in the hole.
218
219 With `-frefinement-level-hole-fits=1`, GHC will additionally find all
220 applications 'f _' that will fit in the hole, where 'f' is an in-scope
221 identifier, applied to single argument. It will also report the type of the
222 needed argument (a new hole).
223
224 And similarly as the number of arguments increases
225
226 As an example, let's look at the following code:
227
228 f :: [Integer] -> Integer
229 f = _
230
231 with `-frefinement-level-hole-fits=1`, we'd get:
232
233 Valid refinement hole fits include
234
235 foldl1 (_ :: Integer -> Integer -> Integer)
236 with foldl1 @[] @Integer
237 where foldl1 :: forall (t :: * -> *).
238 Foldable t =>
239 forall a. (a -> a -> a) -> t a -> a
240 foldr1 (_ :: Integer -> Integer -> Integer)
241 with foldr1 @[] @Integer
242 where foldr1 :: forall (t :: * -> *).
243 Foldable t =>
244 forall a. (a -> a -> a) -> t a -> a
245 const (_ :: Integer)
246 with const @Integer @[Integer]
247 where const :: forall a b. a -> b -> a
248 ($) (_ :: [Integer] -> Integer)
249 with ($) @'GHC.Types.LiftedRep @[Integer] @Integer
250 where ($) :: forall a b. (a -> b) -> a -> b
251 fail (_ :: String)
252 with fail @((->) [Integer]) @Integer
253 where fail :: forall (m :: * -> *).
254 Monad m =>
255 forall a. String -> m a
256 return (_ :: Integer)
257 with return @((->) [Integer]) @Integer
258 where return :: forall (m :: * -> *). Monad m => forall a. a -> m a
259 (Some refinement hole fits suppressed;
260 use -fmax-refinement-hole-fits=N or -fno-max-refinement-hole-fits)
261
262 Which are hole fits with holes in them. This allows e.g. beginners to
263 discover the fold functions and similar, but also allows for advanced users
264 to figure out the valid functions in the Free monad, e.g.
265
266 instance Functor f => Monad (Free f) where
267 Pure a >>= f = f a
268 Free f >>= g = Free (fmap _a f)
269
270 Will output (with -frefinment-level-hole-fits=1):
271 Found hole: _a :: Free f a -> Free f b
272 Where: ‘a’, ‘b’ are rigid type variables bound by
273 the type signature for:
274 (>>=) :: forall a b. Free f a -> (a -> Free f b) -> Free f b
275 at fms.hs:25:12-14
276 ‘f’ is a rigid type variable bound by
277 ...
278 Relevant bindings include
279 g :: a -> Free f b (bound at fms.hs:27:16)
280 f :: f (Free f a) (bound at fms.hs:27:10)
281 (>>=) :: Free f a -> (a -> Free f b) -> Free f b
282 (bound at fms.hs:25:12)
283 ...
284 Valid refinement hole fits include
285 ...
286 (=<<) (_ :: a -> Free f b)
287 with (=<<) @(Free f) @a @b
288 where (=<<) :: forall (m :: * -> *) a b.
289 Monad m =>
290 (a -> m b) -> m a -> m b
291 (imported from ‘Prelude’ at fms.hs:5:18-22
292 (and originally defined in ‘GHC.Base’))
293 ...
294
295 Where `(=<<) _` is precisely the function we want (we ultimately want `>>= g`).
296
297 We find these refinement suggestions by considering hole fits that don't
298 fit the type of the hole, but ones that would fit if given an additional
299 argument. We do this by creating a new type variable with `newOpenFlexiTyVar`
300 (e.g. `t_a1/m[tau:1]`), and then considering hole fits of the type
301 `t_a1/m[tau:1] -> v` where `v` is the type of the hole.
302
303 Since the simplifier is free to unify this new type variable with any type, we
304 can discover any identifiers that would fit if given another identifier of a
305 suitable type. This is then generalized so that we can consider any number of
306 additional arguments by setting the `-frefinement-level-hole-fits` flag to any
307 number, and then considering hole fits like e.g. `foldl _ _` with two additional
308 arguments.
309
310 To make sure that the refinement hole fits are useful, we check that the types
311 of the additional holes have a concrete value and not just an invented type
312 variable. This eliminates suggestions such as `head (_ :: [t0 -> a]) (_ :: t0)`,
313 and limits the number of less than useful refinement hole fits.
314
315 Additionally, to further aid the user in their implementation, we show the
316 types of the holes the binding would have to be applied to in order to work.
317 In the free monad example above, this is demonstrated with
318 `(=<<) (_ :: a -> Free f b)`, which tells the user that the `(=<<)` needs to
319 be applied to an expression of type `a -> Free f b` in order to match.
320 If -XScopedTypeVariables is enabled, this hole fit can even be copied verbatim.
321
322
323 Note [Relevant Constraints]
324 ~~~~~~~~~~~~~~~~~~~
325
326 As highlighted by #14273, we need to check any relevant constraints as well
327 as checking for subsumption. Relevant constraints are the simple constraints
328 whose free unification variables are mentioned in the type of the hole.
329
330 In the simplest case, these are all non-hole constraints in the simples, such
331 as is the case in
332
333 f :: String
334 f = show _
335
336 Where the simples will be :
337
338 [[WD] __a1kz {0}:: a0_a1kv[tau:1] (CHoleCan: ExprHole(_)),
339 [WD] $dShow_a1kw {0}:: Show a0_a1kv[tau:1] (CNonCanonical)]
340
341 However, when there are multiple holes, we need to be more careful. As an
342 example, Let's take a look at the following code:
343
344 f :: Show a => a -> String
345 f x = show (_b (show _a))
346
347 Here there are two holes, `_a` and `_b`, and the simple constraints passed to
348 findValidHoleFits are:
349
350 [[WD] _a_a1pi {0}:: String
351 -> a0_a1pd[tau:2] (CHoleCan: ExprHole(_b)),
352 [WD] _b_a1ps {0}:: a1_a1po[tau:2] (CHoleCan: ExprHole(_a)),
353 [WD] $dShow_a1pe {0}:: Show a0_a1pd[tau:2] (CNonCanonical),
354 [WD] $dShow_a1pp {0}:: Show a1_a1po[tau:2] (CNonCanonical)]
355
356
357 Here we have the two hole constraints for `_a` and `_b`, but also additional
358 constraints that these holes must fulfill. When we are looking for a match for
359 the hole `_a`, we filter the simple constraints to the "Relevant constraints",
360 by throwing out all hole constraints and any constraints which do not mention
361 a variable mentioned in the type of the hole. For hole `_a`, we will then
362 only require that the `$dShow_a1pp` constraint is solved, since that is
363 the only non-hole constraint that mentions any free type variables mentioned in
364 the hole constraint for `_a`, namely `a_a1pd[tau:2]` , and similarly for the
365 hole `_b` we only require that the `$dShow_a1pe` constraint is solved.
366
367 Note [Leaking errors]
368 ~~~~~~~~~~~~~~~~~~~
369
370 When considering candidates, GHC believes that we're checking for validity in
371 actual source. However, As evidenced by #15321, #15007 and #15202, this can
372 cause bewildering error messages. The solution here is simple: if a candidate
373 would cause the type checker to error, it is not a valid hole fit, and thus it
374 is discarded.
375
376 -}
377
378
379 data HoleFitDispConfig = HFDC { showWrap :: Bool
380 , showWrapVars :: Bool
381 , showType :: Bool
382 , showProv :: Bool
383 , showMatches :: Bool }
384
385 debugHoleFitDispConfig :: HoleFitDispConfig
386 debugHoleFitDispConfig = HFDC True True True False False
387
388
389 -- We read the various -no-show-*-of-hole-fits flags
390 -- and set the display config accordingly.
391 getHoleFitDispConfig :: TcM HoleFitDispConfig
392 getHoleFitDispConfig
393 = do { sWrap <- goptM Opt_ShowTypeAppOfHoleFits
394 ; sWrapVars <- goptM Opt_ShowTypeAppVarsOfHoleFits
395 ; sType <- goptM Opt_ShowTypeOfHoleFits
396 ; sProv <- goptM Opt_ShowProvOfHoleFits
397 ; sMatc <- goptM Opt_ShowMatchesOfHoleFits
398 ; return HFDC{ showWrap = sWrap, showWrapVars = sWrapVars
399 , showProv = sProv, showType = sType
400 , showMatches = sMatc } }
401
402 -- Which sorting algorithm to use
403 data SortingAlg = NoSorting -- Do not sort the fits at all
404 | BySize -- Sort them by the size of the match
405 | BySubsumption -- Sort by full subsumption
406 deriving (Eq, Ord)
407
408 getSortingAlg :: TcM SortingAlg
409 getSortingAlg =
410 do { shouldSort <- goptM Opt_SortValidHoleFits
411 ; subsumSort <- goptM Opt_SortBySubsumHoleFits
412 ; sizeSort <- goptM Opt_SortBySizeHoleFits
413 -- We default to sizeSort unless it has been explicitly turned off
414 -- or subsumption sorting has been turned on.
415 ; return $ if not shouldSort
416 then NoSorting
417 else if subsumSort
418 then BySubsumption
419 else if sizeSort
420 then BySize
421 else NoSorting }
422
423
424 -- | HoleFitCandidates are passed to the filter and checked whether they can be
425 -- made to fit.
426 data HoleFitCandidate = IdHFCand Id -- An id, like locals.
427 | NameHFCand Name -- A name, like built-in syntax.
428 | GreHFCand GlobalRdrElt -- A global, like imported ids.
429 deriving (Eq)
430 instance Outputable HoleFitCandidate where
431 ppr = pprHoleFitCand
432
433 pprHoleFitCand :: HoleFitCandidate -> SDoc
434 pprHoleFitCand (IdHFCand id) = text "Id HFC: " <> ppr id
435 pprHoleFitCand (NameHFCand name) = text "Name HFC: " <> ppr name
436 pprHoleFitCand (GreHFCand gre) = text "Gre HFC: " <> ppr gre
437
438 instance HasOccName HoleFitCandidate where
439 occName hfc = case hfc of
440 IdHFCand id -> occName id
441 NameHFCand name -> occName name
442 GreHFCand gre -> occName (gre_name gre)
443
444 -- | HoleFit is the type we use for valid hole fits. It contains the
445 -- element that was checked, the Id of that element as found by `tcLookup`,
446 -- and the refinement level of the fit, which is the number of extra argument
447 -- holes that this fit uses (e.g. if hfRefLvl is 2, the fit is for `Id _ _`).
448 data HoleFit =
449 HoleFit { hfId :: Id -- The elements id in the TcM
450 , hfCand :: HoleFitCandidate -- The candidate that was checked.
451 , hfType :: TcType -- The type of the id, possibly zonked.
452 , hfRefLvl :: Int -- The number of holes in this fit.
453 , hfWrap :: [TcType] -- The wrapper for the match.
454 , hfMatches :: [TcType] -- What the refinement variables got matched
455 -- with, if anything
456 , hfDoc :: Maybe HsDocString } -- Documentation of this HoleFit, if
457 -- available.
458
459
460 hfName :: HoleFit -> Name
461 hfName hf = case hfCand hf of
462 IdHFCand id -> idName id
463 NameHFCand name -> name
464 GreHFCand gre -> gre_name gre
465
466 hfIsLcl :: HoleFit -> Bool
467 hfIsLcl hf = case hfCand hf of
468 IdHFCand _ -> True
469 NameHFCand _ -> False
470 GreHFCand gre -> gre_lcl gre
471
472 -- We define an Eq and Ord instance to be able to build a graph.
473 instance Eq HoleFit where
474 (==) = (==) `on` hfId
475
476 -- We compare HoleFits by their name instead of their Id, since we don't
477 -- want our tests to be affected by the non-determinism of `nonDetCmpVar`,
478 -- which is used to compare Ids. When comparing, we want HoleFits with a lower
479 -- refinement level to come first.
480 instance Ord HoleFit where
481 compare a b = cmp a b
482 where cmp = if hfRefLvl a == hfRefLvl b
483 then compare `on` hfName
484 else compare `on` hfRefLvl
485
486 instance Outputable HoleFit where
487 ppr = pprHoleFit debugHoleFitDispConfig
488
489 -- If enabled, we go through the fits and add any associated documentation,
490 -- by looking it up in the module or the environment (for local fits)
491 addDocs :: [HoleFit] -> TcM [HoleFit]
492 addDocs fits =
493 do { showDocs <- goptM Opt_ShowDocsOfHoleFits
494 ; if showDocs
495 then do { (_, DeclDocMap lclDocs, _) <- extractDocs <$> getGblEnv
496 ; mapM (upd lclDocs) fits }
497 else return fits }
498 where
499 msg = text "TcHoleErrors addDocs"
500 lookupInIface name (ModIface { mi_decl_docs = DeclDocMap dmap })
501 = Map.lookup name dmap
502 upd lclDocs fit =
503 let name = hfName fit in
504 do { doc <- if hfIsLcl fit
505 then pure (Map.lookup name lclDocs)
506 else do { mbIface <- loadInterfaceForNameMaybe msg name
507 ; return $ mbIface >>= lookupInIface name }
508 ; return $ fit {hfDoc = doc} }
509
510 -- For pretty printing hole fits, we display the name and type of the fit,
511 -- with added '_' to represent any extra arguments in case of a non-zero
512 -- refinement level.
513 pprHoleFit :: HoleFitDispConfig -> HoleFit -> SDoc
514 pprHoleFit (HFDC sWrp sWrpVars sTy sProv sMs) hf = hang display 2 provenance
515 where name = hfName hf
516 ty = hfType hf
517 matches = hfMatches hf
518 wrap = hfWrap hf
519 tyApp = sep $ zipWithEqual "pprHoleFit" pprArg vars wrap
520 where pprArg b arg = case binderArgFlag b of
521 Specified -> text "@" <> pprParendType arg
522 -- Do not print type application for inferred
523 -- variables (#16456)
524 Inferred -> empty
525 Required -> pprPanic "pprHoleFit: bad Required"
526 (ppr b <+> ppr arg)
527 tyAppVars = sep $ punctuate comma $
528 zipWithEqual "pprHoleFit" (\v t -> ppr (binderVar v) <+>
529 text "~" <+> pprParendType t)
530 vars wrap
531
532 vars = unwrapTypeVars ty
533 where
534 -- Attempts to get all the quantified type variables in a type,
535 -- e.g.
536 -- return :: forall (m :: * -> *) Monad m => (forall a . a -> m a)
537 -- into [m, a]
538 unwrapTypeVars :: Type -> [TyCoVarBinder]
539 unwrapTypeVars t = vars ++ case splitFunTy_maybe unforalled of
540 Just (_, unfunned) -> unwrapTypeVars unfunned
541 _ -> []
542 where (vars, unforalled) = splitForAllVarBndrs t
543 holeVs = sep $ map (parens . (text "_" <+> dcolon <+>) . ppr) matches
544 holeDisp = if sMs then holeVs
545 else sep $ replicate (length matches) $ text "_"
546 occDisp = pprPrefixOcc name
547 tyDisp = ppWhen sTy $ dcolon <+> ppr ty
548 has = not . null
549 wrapDisp = ppWhen (has wrap && (sWrp || sWrpVars))
550 $ text "with" <+> if sWrp || not sTy
551 then occDisp <+> tyApp
552 else tyAppVars
553 docs = case hfDoc hf of
554 Just d -> text "{-^" <>
555 (vcat . map text . lines . unpackHDS) d
556 <> text "-}"
557 _ -> empty
558 funcInfo = ppWhen (has matches && sTy) $
559 text "where" <+> occDisp <+> tyDisp
560 subDisp = occDisp <+> if has matches then holeDisp else tyDisp
561 display = subDisp $$ nest 2 (funcInfo $+$ docs $+$ wrapDisp)
562 provenance = ppWhen sProv $ parens $
563 case hfCand hf of
564 GreHFCand gre -> pprNameProvenance gre
565 _ -> text "bound at" <+> ppr (getSrcLoc name)
566
567 getLocalBindings :: TidyEnv -> Ct -> TcM [Id]
568 getLocalBindings tidy_orig ct
569 = do { (env1, _) <- zonkTidyOrigin tidy_orig (ctLocOrigin loc)
570 ; go env1 [] (removeBindingShadowing $ tcl_bndrs lcl_env) }
571 where
572 loc = ctEvLoc (ctEvidence ct)
573 lcl_env = ctLocEnv loc
574
575 go :: TidyEnv -> [Id] -> [TcBinder] -> TcM [Id]
576 go _ sofar [] = return (reverse sofar)
577 go env sofar (tc_bndr : tc_bndrs) =
578 case tc_bndr of
579 TcIdBndr id _ -> keep_it id
580 _ -> discard_it
581 where
582 discard_it = go env sofar tc_bndrs
583 keep_it id = go env (id:sofar) tc_bndrs
584
585
586
587 -- See Note [Valid hole fits include ...]
588 findValidHoleFits :: TidyEnv -- ^ The tidy_env for zonking
589 -> [Implication] -- ^ Enclosing implications for givens
590 -> [Ct]
591 -- ^ The unsolved simple constraints in the implication for
592 -- the hole.
593 -> Ct -- ^ The hole constraint itself
594 -> TcM (TidyEnv, SDoc)
595 findValidHoleFits tidy_env implics simples ct | isExprHoleCt ct =
596 do { rdr_env <- getGlobalRdrEnv
597 ; lclBinds <- getLocalBindings tidy_env ct
598 ; maxVSubs <- maxValidHoleFits <$> getDynFlags
599 ; hfdc <- getHoleFitDispConfig
600 ; sortingAlg <- getSortingAlg
601 ; let findVLimit = if sortingAlg > NoSorting then Nothing else maxVSubs
602 ; refLevel <- refLevelHoleFits <$> getDynFlags
603 ; traceTc "findingValidHoleFitsFor { " $ ppr ct
604 ; traceTc "hole_lvl is:" $ ppr hole_lvl
605 ; traceTc "implics are: " $ ppr implics
606 ; traceTc "simples are: " $ ppr simples
607 ; traceTc "locals are: " $ ppr lclBinds
608 ; let (lcl, gbl) = partition gre_lcl (globalRdrEnvElts rdr_env)
609 -- We remove binding shadowings here, but only for the local level.
610 -- this is so we e.g. suggest the global fmap from the Functor class
611 -- even though there is a local definition as well, such as in the
612 -- Free monad example.
613 locals = removeBindingShadowing $
614 map IdHFCand lclBinds ++ map GreHFCand lcl
615 globals = map GreHFCand gbl
616 syntax = map NameHFCand builtIns
617 to_check = locals ++ syntax ++ globals
618 ; (searchDiscards, subs) <-
619 tcFilterHoleFits findVLimit implics relevantCts (hole_ty, []) to_check
620 ; (tidy_env, tidy_subs) <- zonkSubs tidy_env subs
621 ; tidy_sorted_subs <- sortFits sortingAlg tidy_subs
622 ; let (pVDisc, limited_subs) = possiblyDiscard maxVSubs tidy_sorted_subs
623 vDiscards = pVDisc || searchDiscards
624 ; subs_with_docs <- addDocs limited_subs
625 ; let vMsg = ppUnless (null subs_with_docs) $
626 hang (text "Valid hole fits include") 2 $
627 vcat (map (pprHoleFit hfdc) subs_with_docs)
628 $$ ppWhen vDiscards subsDiscardMsg
629 -- Refinement hole fits. See Note [Valid refinement hole fits include ...]
630 ; (tidy_env, refMsg) <- if refLevel >= Just 0 then
631 do { maxRSubs <- maxRefHoleFits <$> getDynFlags
632 -- We can use from just, since we know that Nothing >= _ is False.
633 ; let refLvls = [1..(fromJust refLevel)]
634 -- We make a new refinement type for each level of refinement, where
635 -- the level of refinement indicates number of additional arguments
636 -- to allow.
637 ; ref_tys <- mapM mkRefTy refLvls
638 ; traceTc "ref_tys are" $ ppr ref_tys
639 ; let findRLimit = if sortingAlg > NoSorting then Nothing
640 else maxRSubs
641 ; refDs <- mapM (flip (tcFilterHoleFits findRLimit implics
642 relevantCts) to_check) ref_tys
643 ; (tidy_env, tidy_rsubs) <- zonkSubs tidy_env $ concatMap snd refDs
644 ; tidy_sorted_rsubs <- sortFits sortingAlg tidy_rsubs
645 -- For refinement substitutions we want matches
646 -- like id (_ :: t), head (_ :: [t]), asTypeOf (_ :: t),
647 -- and others in that vein to appear last, since these are
648 -- unlikely to be the most relevant fits.
649 ; (tidy_env, tidy_hole_ty) <- zonkTidyTcType tidy_env hole_ty
650 ; let hasExactApp = any (tcEqType tidy_hole_ty) . hfWrap
651 (exact, not_exact) = partition hasExactApp tidy_sorted_rsubs
652 (pRDisc, exact_last_rfits) =
653 possiblyDiscard maxRSubs $ not_exact ++ exact
654 rDiscards = pRDisc || any fst refDs
655 ; rsubs_with_docs <- addDocs exact_last_rfits
656 ; return (tidy_env,
657 ppUnless (null rsubs_with_docs) $
658 hang (text "Valid refinement hole fits include") 2 $
659 vcat (map (pprHoleFit hfdc) rsubs_with_docs)
660 $$ ppWhen rDiscards refSubsDiscardMsg) }
661 else return (tidy_env, empty)
662 ; traceTc "findingValidHoleFitsFor }" empty
663 ; return (tidy_env, vMsg $$ refMsg) }
664 where
665 -- We extract the type, the tcLevel and the types free variables
666 -- from from the constraint.
667 hole_ty :: TcPredType
668 hole_ty = ctPred ct
669 hole_fvs :: FV
670 hole_fvs = tyCoFVsOfType hole_ty
671 hole_lvl = ctLocLevel $ ctEvLoc $ ctEvidence ct
672
673 -- BuiltInSyntax names like (:) and []
674 builtIns :: [Name]
675 builtIns = filter isBuiltInSyntax knownKeyNames
676
677 -- We make a refinement type by adding a new type variable in front
678 -- of the type of t h hole, going from e.g. [Integer] -> Integer
679 -- to t_a1/m[tau:1] -> [Integer] -> Integer. This allows the simplifier
680 -- to unify the new type variable with any type, allowing us
681 -- to suggest a "refinement hole fit", like `(foldl1 _)` instead
682 -- of only concrete hole fits like `sum`.
683 mkRefTy :: Int -> TcM (TcType, [TcTyVar])
684 mkRefTy refLvl = (wrapWithVars &&& id) <$> newTyVars
685 where newTyVars = replicateM refLvl $ setLvl <$>
686 (newOpenTypeKind >>= newFlexiTyVar)
687 setLvl = flip setMetaTyVarTcLevel hole_lvl
688 wrapWithVars vars = mkVisFunTys (map mkTyVarTy vars) hole_ty
689
690 sortFits :: SortingAlg -- How we should sort the hole fits
691 -> [HoleFit] -- The subs to sort
692 -> TcM [HoleFit]
693 sortFits NoSorting subs = return subs
694 sortFits BySize subs
695 = (++) <$> sortBySize (sort lclFits)
696 <*> sortBySize (sort gblFits)
697 where (lclFits, gblFits) = span hfIsLcl subs
698
699 -- To sort by subsumption, we invoke the sortByGraph function, which
700 -- builds the subsumption graph for the fits and then sorts them using a
701 -- graph sort. Since we want locals to come first anyway, we can sort
702 -- them separately. The substitutions are already checked in local then
703 -- global order, so we can get away with using span here.
704 -- We use (<*>) to expose the parallelism, in case it becomes useful later.
705 sortFits BySubsumption subs
706 = (++) <$> sortByGraph (sort lclFits)
707 <*> sortByGraph (sort gblFits)
708 where (lclFits, gblFits) = span hfIsLcl subs
709
710 -- See Note [Relevant Constraints]
711 relevantCts :: [Ct]
712 relevantCts = if isEmptyVarSet (fvVarSet hole_fvs) then []
713 else filter isRelevant simples
714 where ctFreeVarSet :: Ct -> VarSet
715 ctFreeVarSet = fvVarSet . tyCoFVsOfType . ctPred
716 hole_fv_set = fvVarSet hole_fvs
717 anyFVMentioned :: Ct -> Bool
718 anyFVMentioned ct = not $ isEmptyVarSet $
719 ctFreeVarSet ct `intersectVarSet` hole_fv_set
720 -- We filter out those constraints that have no variables (since
721 -- they won't be solved by finding a type for the type variable
722 -- representing the hole) and also other holes, since we're not
723 -- trying to find hole fits for many holes at once.
724 isRelevant ct = not (isEmptyVarSet (ctFreeVarSet ct))
725 && anyFVMentioned ct
726 && not (isHoleCt ct)
727
728 -- We zonk the hole fits so that the output aligns with the rest
729 -- of the typed hole error message output.
730 zonkSubs :: TidyEnv -> [HoleFit] -> TcM (TidyEnv, [HoleFit])
731 zonkSubs = zonkSubs' []
732 where zonkSubs' zs env [] = return (env, reverse zs)
733 zonkSubs' zs env (hf:hfs) = do { (env', z) <- zonkSub env hf
734 ; zonkSubs' (z:zs) env' hfs }
735 zonkSub env hf@HoleFit{hfType = ty, hfMatches = m, hfWrap = wrp}
736 = do { (env, ty') <- zonkTidyTcType env ty
737 ; (env, m') <- zonkTidyTcTypes env m
738 ; (env, wrp') <- zonkTidyTcTypes env wrp
739 ; let zFit = hf {hfType = ty', hfMatches = m', hfWrap = wrp'}
740 ; return (env, zFit ) }
741
742 -- Based on the flags, we might possibly discard some or all the
743 -- fits we've found.
744 possiblyDiscard :: Maybe Int -> [HoleFit] -> (Bool, [HoleFit])
745 possiblyDiscard (Just max) fits = (fits `lengthExceeds` max, take max fits)
746 possiblyDiscard Nothing fits = (False, fits)
747
748 -- Sort by size uses as a measure for relevance the sizes of the
749 -- different types needed to instantiate the fit to the type of the hole.
750 -- This is much quicker than sorting by subsumption, and gives reasonable
751 -- results in most cases.
752 sortBySize :: [HoleFit] -> TcM [HoleFit]
753 sortBySize = return . sortOn sizeOfFit
754 where sizeOfFit :: HoleFit -> TypeSize
755 sizeOfFit = sizeTypes . nubBy tcEqType . hfWrap
756
757 -- Based on a suggestion by phadej on #ghc, we can sort the found fits
758 -- by constructing a subsumption graph, and then do a topological sort of
759 -- the graph. This makes the most specific types appear first, which are
760 -- probably those most relevant. This takes a lot of work (but results in
761 -- much more useful output), and can be disabled by
762 -- '-fno-sort-valid-hole-fits'.
763 sortByGraph :: [HoleFit] -> TcM [HoleFit]
764 sortByGraph fits = go [] fits
765 where tcSubsumesWCloning :: TcType -> TcType -> TcM Bool
766 tcSubsumesWCloning ht ty = withoutUnification fvs (tcSubsumes ht ty)
767 where fvs = tyCoFVsOfTypes [ht,ty]
768 go :: [(HoleFit, [HoleFit])] -> [HoleFit] -> TcM [HoleFit]
769 go sofar [] = do { traceTc "subsumptionGraph was" $ ppr sofar
770 ; return $ uncurry (++)
771 $ partition hfIsLcl topSorted }
772 where toV (hf, adjs) = (hf, hfId hf, map hfId adjs)
773 (graph, fromV, _) = graphFromEdges $ map toV sofar
774 topSorted = map ((\(h,_,_) -> h) . fromV) $ topSort graph
775 go sofar (hf:hfs) =
776 do { adjs <-
777 filterM (tcSubsumesWCloning (hfType hf) . hfType) fits
778 ; go ((hf, adjs):sofar) hfs }
779
780 -- We don't (as of yet) handle holes in types, only in expressions.
781 findValidHoleFits env _ _ _ = return (env, empty)
782
783
784 -- | tcFilterHoleFits filters the candidates by whether, given the implications
785 -- and the relevant constraints, they can be made to match the type by
786 -- running the type checker. Stops after finding limit matches.
787 tcFilterHoleFits :: Maybe Int
788 -- ^ How many we should output, if limited
789 -> [Implication]
790 -- ^ Enclosing implications for givens
791 -> [Ct]
792 -- ^ Any relevant unsolved simple constraints
793 -> (TcType, [TcTyVar])
794 -- ^ The type to check for fits and a list of refinement
795 -- variables (free type variables in the type) for emulating
796 -- additional holes.
797 -> [HoleFitCandidate]
798 -- ^ The candidates to check whether fit.
799 -> TcM (Bool, [HoleFit])
800 -- ^ We return whether or not we stopped due to hitting the limit
801 -- and the fits we found.
802 tcFilterHoleFits (Just 0) _ _ _ _ = return (False, []) -- Stop right away on 0
803 tcFilterHoleFits limit implics relevantCts ht@(hole_ty, _) candidates =
804 do { traceTc "checkingFitsFor {" $ ppr hole_ty
805 ; (discards, subs) <- go [] emptyVarSet limit ht candidates
806 ; traceTc "checkingFitsFor }" empty
807 ; return (discards, subs) }
808 where
809 hole_fvs :: FV
810 hole_fvs = tyCoFVsOfType hole_ty
811 -- Kickoff the checking of the elements.
812 -- We iterate over the elements, checking each one in turn for whether
813 -- it fits, and adding it to the results if it does.
814 go :: [HoleFit] -- What we've found so far.
815 -> VarSet -- Ids we've already checked
816 -> Maybe Int -- How many we're allowed to find, if limited
817 -> (TcType, [TcTyVar]) -- The type, and its refinement variables.
818 -> [HoleFitCandidate] -- The elements we've yet to check.
819 -> TcM (Bool, [HoleFit])
820 go subs _ _ _ [] = return (False, reverse subs)
821 go subs _ (Just 0) _ _ = return (True, reverse subs)
822 go subs seen maxleft ty (el:elts) =
823 -- See Note [Leaking errors]
824 tryTcDiscardingErrs discard_it $
825 do { traceTc "lookingUp" $ ppr el
826 ; maybeThing <- lookup el
827 ; case maybeThing of
828 Just id | not_trivial id ->
829 do { fits <- fitsHole ty (idType id)
830 ; case fits of
831 Just (wrp, matches) -> keep_it id wrp matches
832 _ -> discard_it }
833 _ -> discard_it }
834 where
835 -- We want to filter out undefined and the likes from GHC.Err
836 not_trivial id = nameModule_maybe (idName id) /= Just gHC_ERR
837
838 lookup :: HoleFitCandidate -> TcM (Maybe Id)
839 lookup (IdHFCand id) = return (Just id)
840 lookup hfc = do { thing <- tcLookup name
841 ; return $ case thing of
842 ATcId {tct_id = id} -> Just id
843 AGlobal (AnId id) -> Just id
844 AGlobal (AConLike (RealDataCon con)) ->
845 Just (dataConWrapId con)
846 _ -> Nothing }
847 where name = case hfc of
848 IdHFCand id -> idName id
849 GreHFCand gre -> gre_name gre
850 NameHFCand name -> name
851 discard_it = go subs seen maxleft ty elts
852 keep_it eid wrp ms = go (fit:subs) (extendVarSet seen eid)
853 ((\n -> n - 1) <$> maxleft) ty elts
854 where
855 fit = HoleFit { hfId = eid, hfCand = el, hfType = (idType eid)
856 , hfRefLvl = length (snd ty)
857 , hfWrap = wrp, hfMatches = ms
858 , hfDoc = Nothing }
859
860
861
862
863 unfoldWrapper :: HsWrapper -> [Type]
864 unfoldWrapper = reverse . unfWrp'
865 where unfWrp' (WpTyApp ty) = [ty]
866 unfWrp' (WpCompose w1 w2) = unfWrp' w1 ++ unfWrp' w2
867 unfWrp' _ = []
868
869
870 -- The real work happens here, where we invoke the type checker using
871 -- tcCheckHoleFit to see whether the given type fits the hole.
872 fitsHole :: (TcType, [TcTyVar]) -- The type of the hole wrapped with the
873 -- refinement variables created to simulate
874 -- additional holes (if any), and the list
875 -- of those variables (possibly empty).
876 -- As an example: If the actual type of the
877 -- hole (as specified by the hole
878 -- constraint CHoleExpr passed to
879 -- findValidHoleFits) is t and we want to
880 -- simulate N additional holes, h_ty will
881 -- be r_1 -> ... -> r_N -> t, and
882 -- ref_vars will be [r_1, ... , r_N].
883 -- In the base case with no additional
884 -- holes, h_ty will just be t and ref_vars
885 -- will be [].
886 -> TcType -- The type we're checking to whether it can be
887 -- instantiated to the type h_ty.
888 -> TcM (Maybe ([TcType], [TcType])) -- If it is not a match, we
889 -- return Nothing. Otherwise,
890 -- we Just return the list of
891 -- types that quantified type
892 -- variables in ty would take
893 -- if used in place of h_ty,
894 -- and the list types of any
895 -- additional holes simulated
896 -- with the refinement
897 -- variables in ref_vars.
898 fitsHole (h_ty, ref_vars) ty =
899 -- We wrap this with the withoutUnification to avoid having side-effects
900 -- beyond the check, but we rely on the side-effects when looking for
901 -- refinement hole fits, so we can't wrap the side-effects deeper than this.
902 withoutUnification fvs $
903 do { traceTc "checkingFitOf {" $ ppr ty
904 ; (fits, wrp) <- tcCheckHoleFit (listToBag relevantCts) implics h_ty ty
905 ; traceTc "Did it fit?" $ ppr fits
906 ; traceTc "wrap is: " $ ppr wrp
907 ; traceTc "checkingFitOf }" empty
908 ; z_wrp_tys <- zonkTcTypes (unfoldWrapper wrp)
909 -- We'd like to avoid refinement suggestions like `id _ _` or
910 -- `head _ _`, and only suggest refinements where our all phantom
911 -- variables got unified during the checking. This can be disabled
912 -- with the `-fabstract-refinement-hole-fits` flag.
913 -- Here we do the additional handling when there are refinement
914 -- variables, i.e. zonk them to read their final value to check for
915 -- abstract refinements, and to report what the type of the simulated
916 -- holes must be for this to be a match.
917 ; if fits
918 then if null ref_vars
919 then return (Just (z_wrp_tys, []))
920 else do { let -- To be concrete matches, matches have to
921 -- be more than just an invented type variable.
922 fvSet = fvVarSet fvs
923 notAbstract :: TcType -> Bool
924 notAbstract t = case getTyVar_maybe t of
925 Just tv -> tv `elemVarSet` fvSet
926 _ -> True
927 allConcrete = all notAbstract z_wrp_tys
928 ; z_vars <- zonkTcTyVars ref_vars
929 ; let z_mtvs = mapMaybe tcGetTyVar_maybe z_vars
930 ; allFilled <- not <$> anyM isFlexiTyVar z_mtvs
931 ; allowAbstract <- goptM Opt_AbstractRefHoleFits
932 ; if allowAbstract || (allFilled && allConcrete )
933 then return $ Just (z_wrp_tys, z_vars)
934 else return Nothing }
935 else return Nothing }
936 where fvs = mkFVs ref_vars `unionFV` hole_fvs `unionFV` tyCoFVsOfType ty
937
938
939 subsDiscardMsg :: SDoc
940 subsDiscardMsg =
941 text "(Some hole fits suppressed;" <+>
942 text "use -fmax-valid-hole-fits=N" <+>
943 text "or -fno-max-valid-hole-fits)"
944
945 refSubsDiscardMsg :: SDoc
946 refSubsDiscardMsg =
947 text "(Some refinement hole fits suppressed;" <+>
948 text "use -fmax-refinement-hole-fits=N" <+>
949 text "or -fno-max-refinement-hole-fits)"
950
951
952 -- | Checks whether a MetaTyVar is flexible or not.
953 isFlexiTyVar :: TcTyVar -> TcM Bool
954 isFlexiTyVar tv | isMetaTyVar tv = isFlexi <$> readMetaTyVar tv
955 isFlexiTyVar _ = return False
956
957 -- | Takes a list of free variables and restores any Flexi type variables in
958 -- free_vars after the action is run.
959 withoutUnification :: FV -> TcM a -> TcM a
960 withoutUnification free_vars action =
961 do { flexis <- filterM isFlexiTyVar fuvs
962 ; result <- action
963 -- Reset any mutated free variables
964 ; mapM_ restore flexis
965 ; return result }
966 where restore = flip writeTcRef Flexi . metaTyVarRef
967 fuvs = fvVarList free_vars
968
969 -- | Reports whether first type (ty_a) subsumes the second type (ty_b),
970 -- discarding any errors. Subsumption here means that the ty_b can fit into the
971 -- ty_a, i.e. `tcSubsumes a b == True` if b is a subtype of a.
972 tcSubsumes :: TcSigmaType -> TcSigmaType -> TcM Bool
973 tcSubsumes ty_a ty_b = fst <$> tcCheckHoleFit emptyBag [] ty_a ty_b
974
975
976 -- | A tcSubsumes which takes into account relevant constraints, to fix trac
977 -- #14273. This makes sure that when checking whether a type fits the hole,
978 -- the type has to be subsumed by type of the hole as well as fulfill all
979 -- constraints on the type of the hole.
980 -- Note: The simplifier may perform unification, so make sure to restore any
981 -- free type variables to avoid side-effects.
982 tcCheckHoleFit :: Cts -- ^ Any relevant Cts to the hole.
983 -> [Implication]
984 -- ^ The nested implications of the hole with the innermost
985 -- implication first.
986 -> TcSigmaType -- ^ The type of the hole.
987 -> TcSigmaType -- ^ The type to check whether fits.
988 -> TcM (Bool, HsWrapper)
989 -- ^ Whether it was a match, and the wrapper from hole_ty to ty.
990 tcCheckHoleFit _ _ hole_ty ty | hole_ty `eqType` ty
991 = return (True, idHsWrapper)
992 tcCheckHoleFit relevantCts implics hole_ty ty = discardErrs $
993 do { -- We wrap the subtype constraint in the implications to pass along the
994 -- givens, and so we must ensure that any nested implications and skolems
995 -- end up with the correct level. The implications are ordered so that
996 -- the innermost (the one with the highest level) is first, so it
997 -- suffices to get the level of the first one (or the current level, if
998 -- there are no implications involved).
999 innermost_lvl <- case implics of
1000 [] -> getTcLevel
1001 -- imp is the innermost implication
1002 (imp:_) -> return (ic_tclvl imp)
1003 ; (wrp, wanted) <- setTcLevel innermost_lvl $ captureConstraints $
1004 tcSubType_NC ExprSigCtxt ty hole_ty
1005 ; traceTc "Checking hole fit {" empty
1006 ; traceTc "wanteds are: " $ ppr wanted
1007 ; if isEmptyWC wanted && isEmptyBag relevantCts
1008 then traceTc "}" empty >> return (True, wrp)
1009 else do { fresh_binds <- newTcEvBinds
1010 -- The relevant constraints may contain HoleDests, so we must
1011 -- take care to clone them as well (to avoid #15370).
1012 ; cloned_relevants <- mapBagM cloneWanted relevantCts
1013 -- We wrap the WC in the nested implications, see
1014 -- Note [Nested Implications]
1015 ; let outermost_first = reverse implics
1016 setWC = setWCAndBinds fresh_binds
1017 -- We add the cloned relevants to the wanteds generated by
1018 -- the call to tcSubType_NC, see Note [Relevant Constraints]
1019 -- There's no need to clone the wanteds, because they are
1020 -- freshly generated by `tcSubtype_NC`.
1021 w_rel_cts = addSimples wanted cloned_relevants
1022 w_givens = foldr setWC w_rel_cts outermost_first
1023 ; traceTc "w_givens are: " $ ppr w_givens
1024 ; rem <- runTcSDeriveds $ simpl_top w_givens
1025 -- We don't want any insoluble or simple constraints left, but
1026 -- solved implications are ok (and neccessary for e.g. undefined)
1027 ; traceTc "rems was:" $ ppr rem
1028 ; traceTc "}" empty
1029 ; return (isSolvedWC rem, wrp) } }
1030 where
1031 setWCAndBinds :: EvBindsVar -- Fresh ev binds var.
1032 -> Implication -- The implication to put WC in.
1033 -> WantedConstraints -- The WC constraints to put implic.
1034 -> WantedConstraints -- The new constraints.
1035 setWCAndBinds binds imp wc
1036 = WC { wc_simple = emptyBag
1037 , wc_impl = unitBag $ imp { ic_wanted = wc , ic_binds = binds } }