Simon Peyton Jones pushed to branch wip/T25992 at Glasgow Haskell Compiler / GHC

Commits:

6 changed files:

Changes:

  • compiler/GHC/Tc/Errors.hs
    ... ... @@ -399,13 +399,6 @@ warnRedundantConstraints ctxt env info redundant_evs
    399 399
      | null redundant_evs
    
    400 400
      = return ()
    
    401 401
     
    
    402
    - -- Do not report redundant constraints for quantified constraints
    
    403
    - -- See (RC4) in Note [Tracking redundant constraints]
    
    404
    - -- Fortunately it is easy to spot implications constraints that arise
    
    405
    - -- from quantified constraints, from their SkolInfo
    
    406
    - | InstSkol (IsQC {}) _ <- info
    
    407
    - = return ()
    
    408
    -
    
    409 402
      | SigSkol user_ctxt _ _ <- info
    
    410 403
      -- When dealing with a user-written type signature,
    
    411 404
      -- we want to add "In the type signature for f".
    

  • compiler/GHC/Tc/Solver/Default.hs
    ... ... @@ -275,7 +275,7 @@ solveImplicationUsingUnsatGiven :: (EvVar, Type) -> Implication -> TcS Implicati
    275 275
     solveImplicationUsingUnsatGiven
    
    276 276
       unsat_given@(given_ev,_)
    
    277 277
       impl@(Implic { ic_wanted = wtd, ic_tclvl = tclvl, ic_binds = ev_binds_var
    
    278
    -               , ic_need_pruned = inner })
    
    278
    +               , ic_need_implic = inner })
    
    279 279
       | isCoEvBindsVar ev_binds_var
    
    280 280
       -- We can't use Unsatisfiable evidence in kinds.
    
    281 281
       -- See Note [Coercion evidence only] in GHC.Tc.Types.Evidence.
    
    ... ... @@ -284,7 +284,7 @@ solveImplicationUsingUnsatGiven
    284 284
       = do { wcs <- nestImplicTcS ev_binds_var tclvl $ go_wc wtd
    
    285 285
            ; setImplicationStatus $
    
    286 286
              impl { ic_wanted = wcs
    
    287
    -              , ic_need_pruned = inner `extendEvNeedSet` given_ev } }
    
    287
    +              , ic_need_implic = inner `extendEvNeedSet` given_ev } }
    
    288 288
                     -- Record that the Given is needed; I'm not certain why
    
    289 289
       where
    
    290 290
         go_wc :: WantedConstraints -> TcS WantedConstraints
    

  • compiler/GHC/Tc/Solver/InertSet.hs
    ... ... @@ -2089,18 +2089,8 @@ solveOneFromTheOther.
    2089 2089
            (a) If both are GivenSCOrigin, choose the one that is unblocked if possible
    
    2090 2090
                according to Note [Solving superclass constraints] in GHC.Tc.TyCl.Instance.
    
    2091 2091
     
    
    2092
    -       (b) Prefer constraints that are not superclass selections. Example:
    
    2093
    -
    
    2094
    -             f :: (Eq a, Ord a) => a -> Bool
    
    2095
    -             f x = x == x
    
    2096
    -
    
    2097
    -           Eager superclass expansion gives us two [G] Eq a constraints. We
    
    2098
    -           want to keep the one from the user-written Eq a, not the superclass
    
    2099
    -           selection. This means we report the Ord a as redundant with
    
    2100
    -           -Wredundant-constraints, not the Eq a.
    
    2101
    -
    
    2102
    -           Getting this wrong was #20602. See also
    
    2103
    -           Note [Tracking redundant constraints] in GHC.Tc.Solver.
    
    2092
    +       (b) Prefer constraints that are not superclass selections. See
    
    2093
    +           (TRC3) in Note [Tracking redundant constraints] in GHC.Tc.Solver.
    
    2104 2094
     
    
    2105 2095
            (c) If both are GivenSCOrigin, chooose the one with the shallower
    
    2106 2096
                superclass-selection depth, in the hope of identifying more correct
    

  • compiler/GHC/Tc/Solver/Solve.hs
    ... ... @@ -354,84 +354,42 @@ solveImplication imp@(Implic { ic_tclvl = tclvl
    354 354
     ----------------------
    
    355 355
     setImplicationStatus :: Implication -> TcS Implication
    
    356 356
     -- Finalise the implication returned from solveImplication,
    
    357
    --- setting the ic_status field
    
    357
    +--   * Set the ic_status field
    
    358
    +--   * Prune unnecessary evidence bindings
    
    359
    +--   * Prune unnecessary child implications
    
    358 360
     -- Precondition: the ic_status field is not already IC_Solved
    
    359
    --- Return Nothing if we can discard the implication altogether
    
    360 361
     setImplicationStatus implic@(Implic { ic_status = old_status
    
    361 362
                                         , ic_info   = info
    
    362 363
                                         , ic_wanted = wc })
    
    363
    - | assertPpr (not (isSolvedStatus old_status)) (ppr info) $
    
    364
    + = assertPpr (not (isSolvedStatus old_status)) (ppr info) $
    
    364 365
        -- Precondition: we only set the status if it is not already solved
    
    365
    -   not (isSolvedWC wc)
    
    366
    - = do { traceTcS "setImplicationStatus(not-all-solved) {" (ppr implic)
    
    366
    +   do { traceTcS "setImplicationStatus {" (ppr implic)
    
    367 367
     
    
    368
    -      ; let new_status | insolubleWC wc = IC_Insoluble
    
    369
    -                       | otherwise      = IC_Unsolved
    
    370
    -            new_implic = pruneImplications (implic { ic_status = new_status })
    
    371
    -
    
    372
    -      ; traceTcS "setImplicationStatus(not-all-solved) }" (ppr new_implic)
    
    373
    -
    
    374
    -      ; return new_implic }
    
    375
    -
    
    376
    - | otherwise
    
    377
    - = do { traceTcS "setImplicationStatus(solved) {" (ppr implic)
    
    368
    +      ; let solved = isSolvedWC wc
    
    369
    +      ; new_implic    <- neededEvVars implic
    
    370
    +      ; bad_telescope <- if solved then checkBadTelescope implic
    
    371
    +                                   else return False
    
    378 372
     
    
    379
    -      ; (dead_givens, implic) <- neededEvVars implic
    
    380
    -
    
    381
    -      ; bad_telescope <- checkBadTelescope implic
    
    373
    +      ; let new_status | insolubleWC wc = IC_Insoluble
    
    374
    +                       | not solved     = IC_Unsolved
    
    375
    +                       | bad_telescope  = IC_BadTelescope
    
    376
    +                       | otherwise      = IC_Solved { ics_dead = dead_givens }
    
    377
    +            dead_givens  = findRedundantGivens new_implic
    
    382 378
     
    
    383
    -      ; let final_status
    
    384
    -              | bad_telescope = IC_BadTelescope
    
    385
    -              | otherwise     = IC_Solved { ics_dead = dead_givens }
    
    386
    -            final_implic = pruneImplications (implic { ic_status = final_status })
    
    379
    +            final_implic = new_implic { ic_status = new_status }
    
    387 380
     
    
    388
    -      ; traceTcS "setImplicationStatus(solved) }" (ppr final_implic)
    
    381
    +      ; traceTcS "setImplicationStatus }" (ppr final_implic)
    
    389 382
           ; return final_implic }
    
    390 383
     
    
    391
    -pruneImplications :: Implication -> Implication
    
    392
    --- We have now taken account of the `needs_outer` variables of these
    
    393
    --- implications, so we can drop any that are no longer necessary
    
    394
    -pruneImplications implic@(Implic { ic_wanted      = wc
    
    395
    -                                 , ic_need_pruned = old_needs })
    
    396
    -  = implic { ic_need_pruned = new_needs
    
    397
    -           , ic_wanted      = wc { wc_impl = new_implics } }
    
    398
    -                              -- Do not prune holes; these should be reported
    
    399
    -  where
    
    400
    -    (new_needs, new_implics) = foldr do_one (old_needs, emptyBag) (wc_impl wc)
    
    401
    -
    
    402
    -    do_one :: Implication -> (EvNeedSet, Bag Implication) -> (EvNeedSet, Bag Implication)
    
    403
    -    do_one implic (ens, implics)
    
    404
    -      | keep_me implic = (ens, implic `consBag` implics)
    
    405
    -      | otherwise      = (add_needs ens implic, implics)
    
    406
    -
    
    407
    -    keep_me :: Implication -> Bool
    
    408
    -    keep_me (Implic { ic_status = status, ic_wanted = wanted })
    
    409
    -      | IC_Solved { ics_dead = dead_givens } <- status -- Fully solved
    
    410
    -      , null dead_givens                               -- No redundant givens to report
    
    411
    -      , isEmptyBag (wc_impl wanted)                    -- No children that might have things to report
    
    412
    -      = False
    
    413
    -      | otherwise
    
    414
    -      = True        -- Otherwise, keep it
    
    415
    -
    
    416
    -    add_needs :: EvNeedSet -> Implication -> EvNeedSet
    
    417
    -    -- For a default-method implication, add all its needed vars to ens_dms
    
    418
    -    -- For anything else, just propagate
    
    419
    -    add_needs (ENS { ens_dms = dms, ens_fvs = fvs })
    
    420
    -              (Implic { ic_need_outer = ENS { ens_dms = dms1, ens_fvs = fvs1 }
    
    421
    -                      , ic_info = info })
    
    422
    -      | is_dm_skol info = ENS { ens_dms = dms `unionVarSet` dms1 `unionVarSet` fvs1
    
    423
    -                              , ens_fvs = fvs }
    
    424
    -      | otherwise       = ENS { ens_dms = dms `unionVarSet` dms1
    
    425
    -                              , ens_fvs = fvs `unionVarSet` fvs1 }
    
    426
    -
    
    427
    -findUnnecessaryGivens :: SkolemInfoAnon -> VarSet -> [EvVar] -> [EvVar]
    
    428
    -findUnnecessaryGivens info need_inner givens
    
    384
    +findRedundantGivens :: Implication -> [EvVar]
    
    385
    +findRedundantGivens (Implic { ic_info = info, ic_need = need, ic_given = givens })
    
    429 386
       | not (warnRedundantGivens info)   -- Don't report redundant constraints at all
    
    430
    -  = []
    
    387
    +  = []                    -- See (TRC4) of Note [Tracking redundant constraints]
    
    431 388
     
    
    432 389
       | not (null unused_givens)         -- Some givens are literally unused
    
    433 390
       = unused_givens
    
    434 391
     
    
    392
    +  -- Only try this if unused_givens is empty: see (TRC2a)
    
    435 393
       | otherwise                       -- All givens are used, but some might
    
    436 394
       = redundant_givens                -- still be redundant e.g. (Eq a, Ord a)
    
    437 395
     
    
    ... ... @@ -441,11 +399,13 @@ findUnnecessaryGivens info need_inner givens
    441 399
     
    
    442 400
         unused_givens = filterOut is_used givens
    
    443 401
     
    
    402
    +    needed_givens_ignoring_default_methods = ens_fvs need
    
    444 403
         is_used given =  is_type_error given
    
    445
    -                  || given `elemVarSet` need_inner
    
    404
    +                  || given `elemVarSet` needed_givens_ignoring_default_methods
    
    446 405
                       || (in_instance_decl && is_improving (idType given))
    
    447 406
     
    
    448
    -    minimal_givens = mkMinimalBySCs evVarPred givens
    
    407
    +    minimal_givens = mkMinimalBySCs evVarPred givens  -- See (TRC2)
    
    408
    +
    
    449 409
         is_minimal = (`elemVarSet` mkVarSet minimal_givens)
    
    450 410
         redundant_givens
    
    451 411
           | in_instance_decl = []
    
    ... ... @@ -457,6 +417,26 @@ findUnnecessaryGivens info need_inner givens
    457 417
         is_improving pred -- (transSuperClasses p) does not include p
    
    458 418
           = any isImprovementPred (pred : transSuperClasses pred)
    
    459 419
     
    
    420
    +warnRedundantGivens :: SkolemInfoAnon -> Bool
    
    421
    +warnRedundantGivens (SigSkol ctxt _ _)
    
    422
    +  = case ctxt of
    
    423
    +       FunSigCtxt _ rrc -> reportRedundantConstraints rrc
    
    424
    +       ExprSigCtxt rrc  -> reportRedundantConstraints rrc
    
    425
    +       _                -> False
    
    426
    +
    
    427
    +warnRedundantGivens (InstSkol from _)
    
    428
    + -- Do not report redundant constraints for quantified constraints
    
    429
    + -- See (TRC4) in Note [Tracking redundant constraints]
    
    430
    + -- Fortunately it is easy to spot implications constraints that arise
    
    431
    + -- from quantified constraints, from their SkolInfo
    
    432
    + = case from of
    
    433
    +      IsQC {}      -> False
    
    434
    +      IsClsInst {} -> True
    
    435
    +
    
    436
    +  -- To think about: do we want to report redundant givens for
    
    437
    +  -- pattern synonyms, PatSynSigSkol? c.f #9953, comment:21.
    
    438
    +warnRedundantGivens _ = False
    
    439
    +
    
    460 440
     {- Note [Redundant constraints in instance decls]
    
    461 441
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    462 442
     Instance declarations are special in two ways:
    
    ... ... @@ -508,21 +488,11 @@ checkBadTelescope (Implic { ic_info = info
    508 488
           | otherwise
    
    509 489
           = go (later_skols `extendVarSet` one_skol) earlier_skols
    
    510 490
     
    
    511
    -warnRedundantGivens :: SkolemInfoAnon -> Bool
    
    512
    -warnRedundantGivens (SigSkol ctxt _ _)
    
    513
    -  = case ctxt of
    
    514
    -       FunSigCtxt _ rrc -> reportRedundantConstraints rrc
    
    515
    -       ExprSigCtxt rrc  -> reportRedundantConstraints rrc
    
    516
    -       _                -> False
    
    517
    -
    
    518
    -  -- To think about: do we want to report redundant givens for
    
    519
    -  -- pattern synonyms, PatSynSigSkol? c.f #9953, comment:21.
    
    520
    -warnRedundantGivens (InstSkol {}) = True
    
    521
    -warnRedundantGivens _             = False
    
    522
    -
    
    523
    -neededEvVars :: Implication -> TcS ([EvVar], Implication)
    
    491
    +neededEvVars :: Implication -> TcS Implication
    
    524 492
     -- Find all the evidence variables that are "needed",
    
    493
    +-- /and/ delete dead evidence bindings
    
    494
    +-- /and/ delete unnecessary child implications
    
    495
    +--
    
    525 496
     --   See Note [Tracking redundant constraints]
    
    526 497
     --   See Note [Delete dead Given evidence bindings]
    
    527 498
     --
    
    ... ... @@ -539,78 +509,89 @@ neededEvVars :: Implication -> TcS ([EvVar], Implication)
    539 509
     --
    
    540 510
     --   - Prune out all Given bindings that are not needed
    
    541 511
     --
    
    542
    ---   - From the 'needed' set, delete ev_bndrs, the binders of the
    
    543
    ---     evidence bindings, to give the final needed variables
    
    544
    ---
    
    545
    -neededEvVars implic@(Implic { ic_given = givens
    
    546
    -                            , ic_info  = info
    
    547
    -                            , ic_binds = ev_binds_var
    
    548
    -                            , ic_wanted = WC { wc_impl = implics }
    
    549
    -                            , ic_need_pruned = need_pruned })
    
    512
    +--   - Prune out all child implications that are no longer useful
    
    513
    +
    
    514
    +neededEvVars implic@(Implic { ic_info        = info
    
    515
    +                            , ic_binds       = ev_binds_var
    
    516
    +                            , ic_wanted      = old_wanted
    
    517
    +                            , ic_need_implic = old_need_implic    -- See (TRC1)
    
    518
    +                    })
    
    519
    + | WC { wc_impl = old_implics } <- old_wanted
    
    550 520
      = do { ev_binds <- TcS.getTcEvBindsMap ev_binds_var
    
    551 521
           ; tcvs     <- TcS.getTcEvTyCoVars ev_binds_var
    
    552 522
     
    
    553
    -      ; let -- Get the variables needed by the implications
    
    554
    -            ENS { ens_dms = implic_dm_seeds, ens_fvs = implic_other_seeds }
    
    555
    -              = foldr add_implic_seeds need_pruned implics
    
    523
    +      ; let -- Augment `need_implic` (see (TRC1)) with the variables needed by the implications
    
    524
    +            new_need_implic@(ENS { ens_dms = dm_seeds, ens_fvs = other_seeds })
    
    525
    +                = foldr add_implic old_need_implic old_implics
    
    556 526
     
    
    557 527
                 -- Get the variables needed by the solved bindings
    
    528
    +            -- (It's OK to use a non-deterministic fold here
    
    529
    +            --  because add_wanted is commutative.)
    
    558 530
                 seeds_w = nonDetStrictFoldEvBindMap add_wanted tcvs ev_binds
    
    559
    -                        -- `seeds_w` are the vars mentioned by all the solved Wanted bindings
    
    560
    -                        -- (It's OK to use a non-deterministic fold here
    
    561
    -                        --  because add_wanted is commutative.)
    
    562 531
     
    
    563
    -            need_ignoring_dms = findNeededGivenEvVars ev_binds (implic_other_seeds `unionVarSet` seeds_w)
    
    564
    -            need_from_dms     = findNeededGivenEvVars ev_binds implic_dm_seeds
    
    532
    +            need_ignoring_dms = findNeededGivenEvVars ev_binds (other_seeds `unionVarSet` seeds_w)
    
    533
    +            need_from_dms     = findNeededGivenEvVars ev_binds dm_seeds
    
    565 534
                 need_full         = need_ignoring_dms `unionVarSet` need_from_dms
    
    566
    -            live_ev_binds = filterEvBindMap (needed_ev_bind need_full) ev_binds
    
    567 535
     
    
    536
    +            -- `new_need`: the Givens from outer scopes that are used in this implication
    
    537
    +            need | is_dm_skol info = ENS { ens_dms = trim ev_binds need_full
    
    538
    +                                         , ens_fvs = emptyVarSet }
    
    539
    +                 | otherwise       = ENS { ens_dms = trim ev_binds need_from_dms
    
    540
    +                                         , ens_fvs = trim ev_binds need_ignoring_dms }
    
    541
    +
    
    542
    +            -- `new_implics`: we have now taken account of the `ic_need` variables
    
    543
    +            -- of `old_implics`, so we can drop any that are no longer necessary
    
    544
    +            pruned_implics = filterBag keep_me old_implics
    
    545
    +            pruned_wanted  = old_wanted { wc_impl = pruned_implics }
    
    546
    +                          -- Do not prune holes; these should be reported
    
    547
    +
    
    548
    +      -- Delete dead Given evidence bindings
    
    549
    +      -- See Note [Delete dead Given evidence bindings]
    
    550
    +      ; let live_ev_binds = filterEvBindMap (needed_ev_bind need_full) ev_binds
    
    568 551
           ; TcS.setTcEvBindsMap ev_binds_var live_ev_binds
    
    569
    -           -- See Note [Delete dead Given evidence bindings]
    
    570
    -
    
    571
    -      ; let -- `dead_givens` are the Givens from this implication that are unused
    
    572
    -            dead_givens = findUnnecessaryGivens info need_ignoring_dms givens
    
    573 552
     
    
    574
    -            -- `need_outer` are the Givens from outer scopes that are used in this implication
    
    575
    -            need_outer
    
    576
    -              | is_dm_skol info = ENS { ens_dms = trim live_ev_binds need_full
    
    577
    -                                      , ens_fvs = emptyVarSet }
    
    578
    -              | otherwise       = ENS { ens_dms = trim live_ev_binds need_from_dms
    
    579
    -                                      , ens_fvs = trim live_ev_binds need_ignoring_dms }
    
    580 553
           ; traceTcS "neededEvVars" $
    
    581
    -        vcat [ text "old need_pruned:" <+> ppr need_pruned
    
    554
    +        vcat [ text "old_need_implic:" <+> ppr old_need_implic
    
    555
    +             , text "new_need_implic:" <+> ppr new_need_implic
    
    582 556
                  , text "tcvs:" <+> ppr tcvs
    
    583 557
                  , text "need_ignoring_dms:" <+> ppr need_ignoring_dms
    
    584 558
                  , text "need_from_dms:"     <+> ppr need_from_dms
    
    585
    -             , text "need_outer:" <+> ppr need_outer
    
    586
    -             , text "dead_givens:" <+> ppr dead_givens
    
    559
    +             , text "need:" <+> ppr need
    
    587 560
                  , text "ev_binds:" <+> ppr ev_binds
    
    588 561
                  , text "live_ev_binds:" <+> ppr live_ev_binds ]
    
    589
    -
    
    590
    -      ; return ( dead_givens
    
    591
    -               , implic { ic_need_outer = need_outer }) }
    
    562
    +      ; return (implic { ic_need        = need
    
    563
    +                       , ic_need_implic = new_need_implic
    
    564
    +                       , ic_wanted      = pruned_wanted }) }
    
    592 565
      where
    
    593
    -   trim :: EvBindMap -> VarSet -> VarSet
    
    594
    -   -- Delete variables bound by Givens or bindings
    
    595
    -   trim live_ev_binds needs = (needs `varSetMinusEvBindMap` live_ev_binds)
    
    596
    -                              `delVarSetList` givens
    
    566
    +    trim :: EvBindMap -> VarSet -> VarSet
    
    567
    +    -- Delete variables bound by Givens or bindings
    
    568
    +    trim ev_binds needs = needs `varSetMinusEvBindMap` ev_binds
    
    597 569
     
    
    598
    -   add_implic_seeds :: Implication -> EvNeedSet -> EvNeedSet
    
    599
    -   add_implic_seeds (Implic { ic_need_outer = needs }) acc
    
    600
    -      = needs `unionEvNeedSet` acc
    
    570
    +    add_implic :: Implication -> EvNeedSet -> EvNeedSet
    
    571
    +    add_implic (Implic { ic_given = givens, ic_need = need }) acc
    
    572
    +       = (need `delGivensFromEvNeedSet` givens) `unionEvNeedSet` acc
    
    601 573
     
    
    602
    -   needed_ev_bind needed (EvBind { eb_lhs = ev_var, eb_info = info })
    
    603
    -     | EvBindGiven{} <- info = ev_var `elemVarSet` needed
    
    604
    -     | otherwise = True   -- Keep all wanted bindings
    
    574
    +    needed_ev_bind needed (EvBind { eb_lhs = ev_var, eb_info = info })
    
    575
    +      | EvBindGiven{} <- info = ev_var `elemVarSet` needed
    
    576
    +      | otherwise             = True   -- Keep all wanted bindings
    
    605 577
     
    
    606
    -   add_wanted :: EvBind -> VarSet -> VarSet
    
    607
    -   add_wanted (EvBind { eb_info = info, eb_rhs = rhs }) needs
    
    608
    -     | EvBindGiven{} <- info = needs  -- Add the rhs vars of the Wanted bindings only
    
    609
    -     | otherwise = evVarsOfTerm rhs `unionVarSet` needs
    
    578
    +    add_wanted :: EvBind -> VarSet -> VarSet
    
    579
    +    add_wanted (EvBind { eb_info = info, eb_rhs = rhs }) needs
    
    580
    +      | EvBindGiven{} <- info = needs  -- Add the rhs vars of the Wanted bindings only
    
    581
    +      | otherwise = evVarsOfTerm rhs `unionVarSet` needs
    
    610 582
     
    
    611
    -is_dm_skol :: SkolemInfoAnon -> Bool
    
    612
    -is_dm_skol (MethSkol _ is_dm) = is_dm
    
    613
    -is_dm_skol _                  = False
    
    583
    +    keep_me :: Implication -> Bool
    
    584
    +    keep_me (Implic { ic_status = status, ic_wanted = wanted })
    
    585
    +      | IC_Solved { ics_dead = dead_givens } <- status -- Fully solved
    
    586
    +      , null dead_givens                               -- No redundant givens to report
    
    587
    +      , isEmptyBag (wc_impl wanted)                    -- No children that might have things to report
    
    588
    +      = False
    
    589
    +      | otherwise
    
    590
    +      = True        -- Otherwise, keep it
    
    591
    +
    
    592
    +    is_dm_skol :: SkolemInfoAnon -> Bool
    
    593
    +    is_dm_skol (MethSkol _ is_dm) = is_dm
    
    594
    +    is_dm_skol _                  = False
    
    614 595
     
    
    615 596
     findNeededGivenEvVars :: EvBindMap -> VarSet -> VarSet
    
    616 597
     -- Find all the Given evidence needed by seeds,
    
    ... ... @@ -752,133 +733,82 @@ in GHC.Tc.Gen.HsType.
    752 733
     
    
    753 734
     Note [Tracking redundant constraints]
    
    754 735
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    755
    -With Opt_WarnRedundantConstraints, GHC can report which
    
    756
    -constraints of a type signature (or instance declaration) are
    
    757
    -redundant, and can be omitted.  Here is an overview of how it
    
    758
    -works.
    
    759
    -
    
    760
    -This is all tested in typecheck/should_compile/T20602 (among
    
    761
    -others).
    
    762
    -
    
    763
    ------ What is a redundant constraint?
    
    764
    -
    
    765
    -* The things that can be redundant are precisely the Given
    
    766
    -  constraints of an implication.
    
    736
    +With Opt_WarnRedundantConstraints, GHC can report which constraints of a type
    
    737
    +signature (or instance declaration) are redundant, and can be omitted.  Here is
    
    738
    +an overview of how it works.
    
    767 739
     
    
    768
    -* A constraint can be redundant in two different ways:
    
    769
    -  a) It is not needed by the Wanted constraints covered by the
    
    770
    -     implication E.g.
    
    771
    -       f :: Eq a => a -> Bool
    
    772
    -       f x = True  -- Equality not used
    
    773
    -  b) It is implied by other givens.  E.g.
    
    774
    -       f :: (Eq a, Ord a)     => blah   -- Eq a unnecessary
    
    775
    -       g :: (Eq a, a~b, Eq b) => blah   -- Either Eq a or Eq b unnecessary
    
    776
    -
    
    777
    -*  To find (a) we need to know which evidence bindings are 'wanted';
    
    778
    -   hence the eb_is_given field on an EvBind.
    
    779
    -
    
    780
    -*  To find (b), we use mkMinimalBySCs on the Givens to see if any
    
    781
    -   are unnecessary.
    
    740
    +This is all tested in typecheck/should_compile/T20602 (among others).
    
    782 741
     
    
    783 742
     ----- How tracking works
    
    784 743
     
    
    785
    -(RC1) When two Givens are the same, we drop the evidence for the one
    
    786
    -  that requires more superclass selectors. This is done
    
    787
    -  according to 2(c) of Note [Replacement vs keeping] in GHC.Tc.Solver.InertSet.
    
    788
    -
    
    789
    -(RC2) The ic_need fields of an Implic records in-scope (given) evidence
    
    790
    -  variables bound by the context, that were needed to solve this
    
    791
    -  implication (so far).  See the declaration of Implication.
    
    744
    +* We maintain the `ic_need` field in an implication:
    
    745
    +     ic_need: the set of Given evidence variables that are needed somewhere
    
    746
    +              in this implication; and are bound either by this implication
    
    747
    +              or by an enclosing one.
    
    792 748
     
    
    793
    -(RC3) setImplicationStatus:
    
    749
    +* `setImplicationStatus` does all the work:
    
    794 750
       When the constraint solver finishes solving all the wanteds in
    
    795 751
       an implication, it sets its status to IC_Solved
    
    796 752
     
    
    797
    -  - The ics_dead field, of IC_Solved, records the subset of this
    
    798
    -    implication's ic_given that are redundant (not needed).
    
    799
    -
    
    800
    -  - We compute which evidence variables are needed by an implication
    
    801
    -    in setImplicationStatus.  A variable is needed if
    
    802
    -    a) it is free in the RHS of a Wanted EvBind,
    
    803
    -    b) it is free in the RHS of an EvBind whose LHS is needed, or
    
    804
    -    c) it is in the ics_need of a nested implication.
    
    805
    -
    
    806
    -  - After computing which variables are needed, we then look at the
    
    807
    -    remaining variables for internal redundancies. This is case (b)
    
    808
    -    from above. This is also done in setImplicationStatus.
    
    809
    -    Note that we only look for case (b) if case (a) shows up empty,
    
    810
    -    as exemplified below.
    
    811
    -
    
    812
    -  - We need to be careful not to discard an implication
    
    813
    -    prematurely, even one that is fully solved, because we might
    
    814
    -    thereby forget which variables it needs, and hence wrongly
    
    815
    -    report a constraint as redundant.  But we can discard it once
    
    816
    -    its free vars have been incorporated into its parent; or if it
    
    817
    -    simply has no free vars. This careful discarding is also
    
    818
    -    handled in setImplicationStatus.
    
    819
    -
    
    820
    -(RC4) We do not want to report redundant constraints for implications
    
    821
    -  that come from quantified constraints.  Example #23323:
    
    822
    -     data T a
    
    823
    -     instance Show (T a) where ...  -- No context!
    
    824
    -     foo :: forall f c. (forall a. c a => Show (f a)) => Proxy c -> f Int -> Int
    
    825
    -     bar = foo @T @Eq
    
    826
    -
    
    827
    -  The call to `foo` gives us
    
    828
    -    [W] d : (forall a. Eq a => Show (T a))
    
    829
    -  To solve this, GHC.Tc.Solver.Solve.solveForAll makes an implication constraint:
    
    830
    -    forall a. Eq a =>  [W] ds : Show (T a)
    
    831
    -  and because of the degnerate instance for `Show (T a)`, we don't need the `Eq a`
    
    832
    -  constraint.  But we don't want to report it as redundant!
    
    833
    -
    
    834
    -(RC5) Consider this (#25992), where `op2` has a default method
    
    835
    -        class C a where { op1, op2 :: a -> a
    
    836
    -                        ; op2 = op1 . op1 }
    
    837
    -        instance C a => C [a] where
    
    838
    -          op1 x = x
    
    839
    -
    
    840
    -  Plainly the (C a) constraint is unused; but the expanded decl will
    
    841
    -  look like
    
    842
    -        $dmop2 :: C a => a -> a
    
    843
    -        $dmop2 = op1 . op2
    
    844
    -
    
    845
    -        instance C a = C [a] b
    
    753
    +  - `neededEvVars`: computes which evidence variables are needed by an
    
    754
    +    implication in `setImplicationStatus`.  A variable is needed if
    
    846 755
     
    
    847
    -*** INCOMPLETE TODO ***
    
    756
    +      a) It is in the ic_need field of this implication, computed in
    
    757
    +         a previous call to `setImplicationStatus`; see (TRC1)
    
    848 758
     
    
    759
    +      b) It is in the ics_need of a nested implication; see `add_implic`
    
    760
    +         in `neededEvVars`
    
    849 761
     
    
    850
    -* Examples:
    
    851
    -
    
    852
    -    f, g, h :: (Eq a, Ord a) => a -> Bool
    
    853
    -    f x = x == x
    
    854
    -    g x = x > x
    
    855
    -    h x = x == x && x > x
    
    762
    +      c) It is free in the RHS of any /Wanted/ EvBind; each such binding
    
    763
    +         solves a Wanted, so we want them all.  See `add_wanted` in
    
    764
    +         `neededEvVars`
    
    856 765
     
    
    857
    -    All three will discover that they have two [G] Eq a constraints:
    
    858
    -    one as given and one extracted from the Ord a constraint. They will
    
    859
    -    both discard the latter, as noted above and in
    
    860
    -    Note [Replacement vs keeping] in GHC.Tc.Solver.InertSet.
    
    766
    +      d) It is free in the RHS of a /Given/ EvBind whose LHS is needed:
    
    767
    +         see `findNeededGivenEvVars` called from `neededEvVars`.
    
    861 768
     
    
    862
    -    The body of f uses the [G] Eq a, but not the [G] Ord a. It will
    
    863
    -    report a redundant Ord a using the logic for case (a).
    
    864
    -
    
    865
    -    The body of g uses the [G] Ord a, but not the [G] Eq a. It will
    
    866
    -    report a redundant Eq a using the logic for case (a).
    
    867
    -
    
    868
    -    The body of h uses both [G] Ord a and [G] Eq a. Case (a) will
    
    869
    -    thus come up with nothing redundant. But then, the case (b)
    
    870
    -    check will discover that Eq a is redundant and report this.
    
    871
    -
    
    872
    -    If we did case (b) even when case (a) reports something, then
    
    873
    -    we would report both constraints as redundant for f, which is
    
    874
    -    terrible.
    
    875
    -
    
    876
    ------ Reporting redundant constraints
    
    769
    +  - Next, if the final status is IC_Solved, `setImplicationStatus` uses
    
    770
    +    `findRedunantGivens` to decide which of this implicaion's Givens are redundant.
    
    877 771
     
    
    878 772
     * GHC.Tc.Errors does the actual warning, in warnRedundantConstraints.
    
    879 773
     
    
    880
    -* We don't report redundant givens for *every* implication; only
    
    881
    -  for those which reply True to GHC.Tc.Solver.warnRedundantGivens:
    
    774
    +
    
    775
    +Wrinkles:
    
    776
    +
    
    777
    +(TRC1) `pruneImplications` drops any sub-implications of an Implication
    
    778
    +  that are irrelevant for error reporting:
    
    779
    +      - no unsolved wanteds
    
    780
    +      - no sub-implications
    
    781
    +      - no redundant givens to report
    
    782
    +  But in doing so we must not lose track of the variables that those implications
    
    783
    +  needed!  So we do not recompute `ic_need` from scratch each time; rather, we
    
    784
    +  simply grow it -- see the use of `old_need` in `neededEvVars`.
    
    785
    +
    
    786
    +  Starting from `old_needs` also means that the transitive closure algorithm in
    
    787
    +  `findNeededGivenEvVars` will terminate faster
    
    788
    +
    
    789
    +(TRC2) A Given can be redundant because it is implied by other Givens
    
    790
    +         f :: (Eq a, Ord a)     => blah   -- Eq a unnecessary
    
    791
    +         g :: (Eq a, a~b, Eq b) => blah   -- Either Eq a or Eq b unnecessary
    
    792
    +   We nail this by using `mkMinimalBySCs` in `findRedundantGivens`.
    
    793
    +   (TRC2a) But NOTE that we only attempt this mkMinimalBySCs stuff if all Givens
    
    794
    +   used by evidence bindings.  Example:
    
    795
    +      f :: (Eq a, Ord a) => a -> Bool
    
    796
    +      f x = x == x
    
    797
    +   We report (Ord a) as unused because it is. But we must not also report (Eq a)
    
    798
    +   as unused because it is a superclass of Ord!
    
    799
    +
    
    800
    +(TRC3) When two Givens are the same, prefer one that does not involve superclass
    
    801
    +  selection, or more generally has shallower superclass-selection depth:
    
    802
    +  see 2(b,c) in Note [Replacement vs keeping] in GHC.Tc.Solver.InertSet.
    
    803
    +    e.g        f :: (Eq a, Ord a) => a -> Bool
    
    804
    +               f x = x == x
    
    805
    +  Eager superclass expansion gives us two [G] Eq a constraints. We want to keep
    
    806
    +  the one from the user-written Eq a, not the superclass selection. This means
    
    807
    +  we report the Ord a as redundant with -Wredundant-constraints, not the Eq a.
    
    808
    +  Getting this wrong was #20602.
    
    809
    +
    
    810
    +(TRC4) We don't compute redundant givens for *every* implication; only
    
    811
    +  for those which reply True to `warnRedundantGivens`:
    
    882 812
     
    
    883 813
        - For example, in a class declaration, the default method *can*
    
    884 814
          use the class constraint, but it certainly doesn't *have* to,
    
    ... ... @@ -897,9 +827,68 @@ others).
    897 827
             - GHC.Tc.Gen.Bind.tcSpecPrag
    
    898 828
             - GHC.Tc.Gen.Bind.tcTySig
    
    899 829
     
    
    900
    -  This decision is taken in setImplicationStatus, rather than GHC.Tc.Errors
    
    901
    -  so that we can discard implication constraints that we don't need.
    
    902
    -  So ics_dead consists only of the *reportable* redundant givens.
    
    830
    +   - We do not want to report redundant constraints for implications
    
    831
    +     that come from quantified constraints.  Example #23323:
    
    832
    +        data T a
    
    833
    +        instance Show (T a) where ...  -- No context!
    
    834
    +        foo :: forall f c. (forall a. c a => Show (f a)) => Proxy c -> f Int -> Int
    
    835
    +        bar = foo @T @Eq
    
    836
    +
    
    837
    +     The call to `foo` gives us
    
    838
    +       [W] d : (forall a. Eq a => Show (T a))
    
    839
    +     To solve this, GHC.Tc.Solver.Solve.solveForAll makes an implication constraint:
    
    840
    +       forall a. Eq a =>  [W] ds : Show (T a)
    
    841
    +     and because of the degnerate instance for `Show (T a)`, we don't need the `Eq a`
    
    842
    +     constraint.  But we don't want to report it as redundant!
    
    843
    +
    
    844
    +(TRC5) Consider this (#25992), where `op2` has a default method
    
    845
    +        class C a where { op1, op2 :: a -> a
    
    846
    +                        ; op2 = op1 . op1 }
    
    847
    +        instance C a => C [a] where
    
    848
    +          op1 x = x
    
    849
    +
    
    850
    +  Plainly the (C a) constraint is unused; but the expanded decl will look like
    
    851
    +        $dmop2 :: C a => a -> a
    
    852
    +        $dmop2 = op1 . op2
    
    853
    +
    
    854
    +        $fCList :: forall a. C a => C [a]
    
    855
    +        $fCList @a (d::C a) = MkC (\(x:a).x) ($dmop2 @a d)
    
    856
    +
    
    857
    +   Notice that `d` gets passed to `$dmop`: it is "needed".  But it's only
    
    858
    +   /really/ needed if some /other/ method (in this case `op1`) uses it.
    
    859
    +
    
    860
    +   So, rather than one set of "needed Givens" we use `EvNeedSet` to track a /pair/
    
    861
    +   of sets:
    
    862
    +      ens_dms: needed /only/ by default-method calls
    
    863
    +      ens_fvs: needed by something other than a default-method call
    
    864
    +   It's a bit of a palaver, but not really difficult.
    
    865
    +   All the works is in `neededEvVars`.
    
    866
    +
    
    867
    +
    
    868
    +
    
    869
    +----- Reporting redundant constraints
    
    870
    +
    
    871
    +
    
    872
    +----- Examples
    
    873
    +
    
    874
    +    f, g, h :: (Eq a, Ord a) => a -> Bool
    
    875
    +    f x = x == x
    
    876
    +    g x = x > x
    
    877
    +    h x = x == x && x > x
    
    878
    +
    
    879
    +    All of f,g,h will discover that they have two [G] Eq a constraints: one as
    
    880
    +    given and one extracted from the Ord a constraint. They will both discard
    
    881
    +    the latter; see (TRC3).
    
    882
    +
    
    883
    +    The body of f uses the [G] Eq a, but not the [G] Ord a. It will report a
    
    884
    +    redundant Ord a.
    
    885
    +
    
    886
    +    The body of g uses the [G] Ord a, but not the [G] Eq a. It will report a
    
    887
    +    redundant Eq a.
    
    888
    +
    
    889
    +    The body of h uses both [G] Ord a and [G] Eq a; each is used in a solved
    
    890
    +    Wanted evidence binding.  But (TRC2) kicks in and discovers the Eq a
    
    891
    +    is redundant.
    
    903 892
     
    
    904 893
     ----- Shortcomings
    
    905 894
     
    

  • compiler/GHC/Tc/Types/Constraint.hs
    ... ... @@ -67,7 +67,7 @@ module GHC.Tc.Types.Constraint (
    67 67
             ImplicStatus(..), isInsolubleStatus, isSolvedStatus,
    
    68 68
             UserGiven, getUserGivensFromImplics,
    
    69 69
             HasGivenEqs(..), checkImplicationInvariants,
    
    70
    -        EvNeedSet(..), emptyEvNeedSet, unionEvNeedSet, extendEvNeedSet,
    
    70
    +        EvNeedSet(..), emptyEvNeedSet, unionEvNeedSet, extendEvNeedSet, delGivensFromEvNeedSet,
    
    71 71
     
    
    72 72
             -- CtLocEnv
    
    73 73
             CtLocEnv(..), setCtLocEnvLoc, setCtLocEnvLvl, getCtLocEnvLoc, getCtLocEnvLvl, ctLocEnvInGeneratedCode,
    
    ... ... @@ -1459,14 +1459,17 @@ data Implication
    1459 1459
     
    
    1460 1460
           -- The ic_need fields keep track of which Given evidence
    
    1461 1461
           -- is used by this implication or its children
    
    1462
    -      -- NB: including stuff used by nested implications that have since
    
    1463
    -      --     been discarded
    
    1464
    -      -- See Note [Needed evidence variables]
    
    1465
    -      -- and (RC2) in Note [Tracking redundant constraints]a
    
    1466
    -      ic_need_outer  :: EvNeedSet,  -- Includes only the free Given evidence
    
    1467
    -                                    -- i.e. after deleting (a) ic_givens (b) binders of ic_binds
    
    1468
    -      ic_need_pruned :: EvNeedSet,  -- Union of the ic_need_outer EvNeedSets of implications that
    
    1469
    -                                    -- have been pruned from wc_impl.ic_wanted
    
    1462
    +      -- See Note [Tracking redundant constraints]
    
    1463
    +      -- NB: including stuff used by fully-solved nested implications that have
    
    1464
    +      --      since been discarded
    
    1465
    +      ic_need  :: EvNeedSet,        -- Includes needed Given evidence
    
    1466
    +                                    --   /after/ deleting the binders of ic_binds, but
    
    1467
    +                                    --   /before/ deleting ic_givens
    
    1468
    +
    
    1469
    +      ic_need_implic :: EvNeedSet,  -- Union of of the ic_need of all implications in ic_wanted
    
    1470
    +                                    -- /including/ any fully-solved implications that have been
    
    1471
    +                                    -- discarded.  This discarding is why we need to keep this
    
    1472
    +                                    -- field in the first place.
    
    1470 1473
     
    
    1471 1474
           ic_status   :: ImplicStatus
    
    1472 1475
         }
    
    ... ... @@ -1486,6 +1489,11 @@ unionEvNeedSet (ENS { ens_dms = dm1, ens_fvs = fv1 })
    1486 1489
     extendEvNeedSet :: EvNeedSet -> Var -> EvNeedSet
    
    1487 1490
     extendEvNeedSet ens@(ENS { ens_fvs = fvs }) v = ens { ens_fvs = fvs `extendVarSet` v }
    
    1488 1491
     
    
    1492
    +delGivensFromEvNeedSet :: EvNeedSet -> [Var] -> EvNeedSet
    
    1493
    +delGivensFromEvNeedSet (ENS { ens_dms = dms, ens_fvs = fvs }) givens
    
    1494
    +  = ENS { ens_dms = dms `delVarSetList` givens
    
    1495
    +        , ens_fvs = fvs `delVarSetList` givens }
    
    1496
    +
    
    1489 1497
     implicationPrototype :: CtLocEnv -> Implication
    
    1490 1498
     implicationPrototype ct_loc_env
    
    1491 1499
        = Implic { -- These fields must be initialised
    
    ... ... @@ -1494,15 +1502,17 @@ implicationPrototype ct_loc_env
    1494 1502
                 , ic_info       = panic "newImplic:info"
    
    1495 1503
                 , ic_warn_inaccessible = panic "newImplic:warn_inaccessible"
    
    1496 1504
     
    
    1497
    -            , ic_env        = ct_loc_env
    
    1505
    +              -- Given by caller
    
    1506
    +            , ic_env = ct_loc_env
    
    1507
    +
    
    1498 1508
                   -- The rest have sensible default values
    
    1499
    -            , ic_skols      = []
    
    1500
    -            , ic_given      = []
    
    1501
    -            , ic_wanted     = emptyWC
    
    1502
    -            , ic_given_eqs  = MaybeGivenEqs
    
    1503
    -            , ic_status     = IC_Unsolved
    
    1504
    -            , ic_need_pruned = emptyEvNeedSet
    
    1505
    -            , ic_need_outer  = emptyEvNeedSet }
    
    1509
    +            , ic_skols       = []
    
    1510
    +            , ic_given       = []
    
    1511
    +            , ic_wanted      = emptyWC
    
    1512
    +            , ic_given_eqs   = MaybeGivenEqs
    
    1513
    +            , ic_status      = IC_Unsolved
    
    1514
    +            , ic_need        = emptyEvNeedSet
    
    1515
    +            , ic_need_implic = emptyEvNeedSet }
    
    1506 1516
     
    
    1507 1517
     data ImplicStatus
    
    1508 1518
       = IC_Solved     -- All wanteds in the tree are solved, all the way down
    
    ... ... @@ -1578,7 +1588,7 @@ instance Outputable Implication where
    1578 1588
                   , ic_given = given, ic_given_eqs = given_eqs
    
    1579 1589
                   , ic_wanted = wanted, ic_status = status
    
    1580 1590
                   , ic_binds = binds
    
    1581
    -              , ic_need_pruned = need_pruned, ic_need_outer = need_out
    
    1591
    +              , ic_need = need, ic_need_implic = need_implic
    
    1582 1592
                   , ic_info = info })
    
    1583 1593
        = hang (text "Implic" <+> lbrace)
    
    1584 1594
             2 (sep [ text "TcLevel =" <+> ppr tclvl
    
    ... ... @@ -1588,8 +1598,8 @@ instance Outputable Implication where
    1588 1598
                    , hang (text "Given =")  2 (pprEvVars given)
    
    1589 1599
                    , hang (text "Wanted =") 2 (ppr wanted)
    
    1590 1600
                    , text "Binds =" <+> ppr binds
    
    1591
    -               , whenPprDebug (text "Needed pruned =" <+> ppr need_pruned)
    
    1592
    -               , whenPprDebug (text "Needed outer =" <+> ppr need_out)
    
    1601
    +               , text "need =" <+> ppr need
    
    1602
    +               , text "need_implic =" <+> ppr need_implic
    
    1593 1603
                    , pprSkolInfo info ] <+> rbrace)
    
    1594 1604
     
    
    1595 1605
     instance Outputable EvNeedSet where
    
    ... ... @@ -1684,18 +1694,6 @@ all at once, creating one implication constraint for the lot:
    1684 1694
       implication. TL;DR: an explicit forall should generate an implication
    
    1685 1695
       quantified only over those explicitly quantified variables.
    
    1686 1696
     
    
    1687
    -Note [Needed evidence variables]
    
    1688
    -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1689
    -Th ic_need_evs field holds the free vars of ic_binds, and all the
    
    1690
    -ic_binds in nested implications.
    
    1691
    -
    
    1692
    -  * Main purpose: if one of the ic_givens is not mentioned in here, it
    
    1693
    -    is redundant.
    
    1694
    -
    
    1695
    -  * solveImplication may drop an implication altogether if it has no
    
    1696
    -    remaining 'wanteds'. But we still track the free vars of its
    
    1697
    -    evidence binds, even though it has now disappeared.
    
    1698
    -
    
    1699 1697
     Note [Shadowing in a constraint]
    
    1700 1698
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1701 1699
     We assume NO SHADOWING in a constraint.  Specifically
    

  • compiler/GHC/Tc/Types/Evidence.hs
    ... ... @@ -50,27 +50,30 @@ module GHC.Tc.Types.Evidence (
    50 50
     
    
    51 51
     import GHC.Prelude
    
    52 52
     
    
    53
    -import GHC.Types.Unique.DFM
    
    54
    -import GHC.Types.Unique.FM
    
    55
    -import GHC.Types.Var
    
    56
    -import GHC.Types.Id( idScaledType )
    
    53
    +import GHC.Tc.Utils.TcType
    
    54
    +
    
    55
    +import GHC.Core
    
    57 56
     import GHC.Core.Coercion.Axiom
    
    58 57
     import GHC.Core.Coercion
    
    59 58
     import GHC.Core.Ppr ()   -- Instance OutputableBndr TyVar
    
    60
    -import GHC.Tc.Utils.TcType
    
    59
    +import GHC.Core.Predicate
    
    61 60
     import GHC.Core.Type
    
    62 61
     import GHC.Core.TyCon
    
    63 62
     import GHC.Core.DataCon ( DataCon, dataConWrapId )
    
    64
    -import GHC.Builtin.Names
    
    63
    +import GHC.Core.Class (Class, classSCSelId )
    
    64
    +import GHC.Core.FVs   ( exprSomeFreeVars )
    
    65
    +import GHC.Core.InstEnv ( CanonicalEvidence(..) )
    
    66
    +
    
    67
    +import GHC.Types.Unique.DFM
    
    68
    +import GHC.Types.Unique.FM
    
    69
    +import GHC.Types.Var
    
    70
    +import GHC.Types.Name( isInternalName )
    
    71
    +import GHC.Types.Id( idScaledType )
    
    65 72
     import GHC.Types.Var.Env
    
    66 73
     import GHC.Types.Var.Set
    
    67
    -import GHC.Core.Predicate
    
    68 74
     import GHC.Types.Basic
    
    69 75
     
    
    70
    -import GHC.Core
    
    71
    -import GHC.Core.Class (Class, classSCSelId )
    
    72
    -import GHC.Core.FVs   ( exprSomeFreeVars )
    
    73
    -import GHC.Core.InstEnv ( CanonicalEvidence(..) )
    
    76
    +import GHC.Builtin.Names
    
    74 77
     
    
    75 78
     import GHC.Utils.Misc
    
    76 79
     import GHC.Utils.Panic
    
    ... ... @@ -865,8 +868,13 @@ evTermCoercion tm = case evTermCoercion_maybe tm of
    865 868
     *                                                                      *
    
    866 869
     ********************************************************************* -}
    
    867 870
     
    
    871
    +relevantEvVar :: Var -> Bool
    
    872
    +-- Just returns /local/ free evidence variables; i.e ones with Internal Names
    
    873
    +-- Top-level ones (DFuns, dictionary selectors and the like) don't count
    
    874
    +relevantEvVar v = isInternalName (varName v)
    
    875
    +
    
    868 876
     evVarsOfTerm :: EvTerm -> VarSet
    
    869
    -evVarsOfTerm (EvExpr e)         = exprSomeFreeVars isEvVar e
    
    877
    +evVarsOfTerm (EvExpr e)         = exprSomeFreeVars relevantEvVar e
    
    870 878
     evVarsOfTerm (EvTypeable _ ev)  = evVarsOfTypeable ev
    
    871 879
     evVarsOfTerm (EvFun {})         = emptyVarSet -- See Note [Free vars of EvFun]
    
    872 880