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

Commits:

5 changed files:

Changes:

  • changelog.d/T27731
    1
    +section: compiler
    
    2
    +synopsis: Fix a compiler crash after typechecking
    
    3
    +issues: #27731
    
    4
    +mrs: !16564
    
    5
    +description: {
    
    6
    +  The type checker was failing to report an error, even though it had found one,
    
    7
    +  due to over-zealous error suppression. That led to subsequent chaos. This
    
    8
    +  MR fixes it *and* simplifies the code.
    
    9
    +}

  • compiler/GHC/Tc/Errors.hs
    ... ... @@ -452,16 +452,6 @@ reportBadTelescope ctxt env (ForAllSkol telescope) skols
    452 452
     reportBadTelescope _ _ skol_info skols
    
    453 453
       = pprPanic "reportBadTelescope" (ppr skol_info $$ ppr skols)
    
    454 454
     
    
    455
    --- | Should we completely ignore this constraint in error reporting?
    
    456
    --- It *must* be the case that any constraint for which this returns True
    
    457
    --- somehow causes an error to be reported elsewhere.
    
    458
    --- See Note [Constraints to ignore].
    
    459
    -ignoreConstraint :: Ct -> Bool
    
    460
    -ignoreConstraint ct
    
    461
    -  = case ctOrigin ct of
    
    462
    -      AssocFamPatOrigin         -> True  -- See (CIG1)
    
    463
    -      _                         -> False
    
    464
    -
    
    465 455
     -- | Makes an error item from a constraint, calculating whether or not the item
    
    466 456
     -- should be suppressed. See Note [Wanteds rewrite Wanteds: rewriter-sets]
    
    467 457
     -- in GHC.Tc.Types.Constraint. Returns Nothing if we should just ignore
    
    ... ... @@ -473,34 +463,177 @@ mkErrorItem ct
    473 463
            ; return Nothing }   -- See Note [Constraints to ignore]
    
    474 464
     
    
    475 465
       | otherwise
    
    476
    -  = do { let loc = ctLoc ct
    
    477
    -             flav = ctFlavour ct
    
    466
    +  = do { let ev = ctEvidence ct
    
    467
    +
    
    468
    +             m_evdest =  case ev of
    
    469
    +                           CtGiven {}                               -> Nothing
    
    470
    +                           CtWanted (WantedCt { ctev_dest = dest }) -> Just dest
    
    478 471
     
    
    479
    -             -- For this `suppress` stuff see
    
    480
    -             -- Note [Wanteds rewrite Wanteds: rewriter-sets] in GHC.Tc.Types.Constraint
    
    481
    -             (suppress, m_evdest) = case ctEvidence ct of
    
    482
    -                     CtGiven {} -> (False, Nothing)
    
    483
    -                     CtWanted (WantedCt { ctev_rewriters = rws, ctev_dest = dest })
    
    484
    -                                -> (not (isEmptyCoHoleSet rws), Just dest)
    
    485 472
     
    
    486
    -       ; let m_reason = case ct of
    
    473
    +             m_reason = case ct of
    
    487 474
                     CIrredCan (IrredCt { ir_reason = reason }) -> Just reason
    
    488 475
                     _                                          -> Nothing
    
    489 476
     
    
    490
    -             insoluble_ct = insolubleCt ct
    
    491
    -
    
    492 477
            ; return $ Just $ EI { ei_pred      = ctPred ct
    
    493 478
                                 , ei_evdest    = m_evdest
    
    494
    -                            , ei_flavour   = flav
    
    495
    -                            , ei_loc       = loc
    
    479
    +                            , ei_flavour   = ctFlavour ct
    
    480
    +                            , ei_loc       = ctLoc ct
    
    496 481
                                 , ei_m_reason  = m_reason
    
    497
    -                            , ei_insoluble = insoluble_ct
    
    498
    -                            , ei_suppress  = suppress }}
    
    482
    +                            , ei_insoluble = insolubleCt ct
    
    483
    +                            , ei_suppress  = suppressCtError ev }}
    
    499 484
     
    
    500 485
     -- | Actually report this 'ErrorItem'.
    
    501 486
     unsuppressErrorItem :: ErrorItem -> ErrorItem
    
    502 487
     unsuppressErrorItem ei = ei { ei_suppress = False }
    
    503 488
     
    
    489
    +-- | Should we completely ignore this constraint in error reporting?
    
    490
    +-- It *must* be the case that any constraint for which this returns True
    
    491
    +-- somehow causes an error to be reported elsewhere.
    
    492
    +-- See Note [Constraints to ignore].
    
    493
    +ignoreConstraint :: Ct -> Bool
    
    494
    +ignoreConstraint ct
    
    495
    +  = case ctOrigin ct of
    
    496
    +      AssocFamPatOrigin         -> True  -- See (CIG1)
    
    497
    +      _                         -> False
    
    498
    +
    
    499
    +suppressCtError :: CtEvidence -> Bool
    
    500
    +-- See Note [Suppressing confusing errors]
    
    501
    +suppressCtError (CtGiven {})
    
    502
    +  = False
    
    503
    +suppressCtError (CtWanted (WantedCt { ctev_rewriters = rws, ctev_loc = loc }))
    
    504
    +  | not (isEmptyCoHoleSet rws)
    
    505
    +  = True  -- See (SCE1)
    
    506
    +
    
    507
    +  | isWantedSuperclassOrigin (ctLocOrigin loc)
    
    508
    +  = True  -- See (SCE2)
    
    509
    +
    
    510
    +  | otherwise
    
    511
    +  = False
    
    512
    +
    
    513
    +{- Note [Constraints to ignore]
    
    514
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    515
    +Some constraints are meant only to aid the solver by unification; a failure
    
    516
    +to solve them is not necessarily an error to report to the user. It is critical
    
    517
    +that compilation is aborted elsewhere if there are any ignored constraints here;
    
    518
    +they will remain unfilled, and might have been used to rewrite another constraint.
    
    519
    +
    
    520
    +Currently, the constraints to ignore are:
    
    521
    +
    
    522
    +(CIG1) Constraints generated in order to unify associated type instance parameters
    
    523
    +   with class parameters. Here are two illustrative examples:
    
    524
    +
    
    525
    +     class C (a :: k) where
    
    526
    +       type F (b :: k)
    
    527
    +
    
    528
    +     instance C True where
    
    529
    +       type F a = Int
    
    530
    +
    
    531
    +     instance C Left where
    
    532
    +       type F (Left :: a -> Either a b) = Bool
    
    533
    +
    
    534
    +   In the first instance, we want to infer that `a` has type Bool. So we emit
    
    535
    +   a constraint unifying kappa (the guessed type of `a`) with Bool. All is well.
    
    536
    +
    
    537
    +   In the second instance, we process the associated type instance only
    
    538
    +   after fixing the quantified type variables of the class instance. We thus
    
    539
    +   have skolems a1 and b1 such that the class instance is for (Left :: a1 -> Either a1 b1).
    
    540
    +   Unifying a1 and b1 with a and b in the type instance will fail, but harmlessly so.
    
    541
    +   checkConsistentFamInst checks for this, and will fail if anything has gone
    
    542
    +   awry. Really the equality constraints emitted are just meant as an aid, not
    
    543
    +   a requirement. This is test case T13972.
    
    544
    +
    
    545
    +   We detect this case by looking for an origin of AssocFamPatOrigin; constraints
    
    546
    +   with this origin are dropped entirely during error message reporting.
    
    547
    +
    
    548
    +   If there is any trouble, checkValidFamInst bleats, aborting compilation.
    
    549
    +
    
    550
    +(Note: Aug 25: this seems a rather tricky corner;
    
    551
    +               c.f. Note [Suppressing confusing errors])
    
    552
    +
    
    553
    +Note [Suppressing confusing errors]
    
    554
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    555
    +Certain errors we might encounter are potentially confusing to users.
    
    556
    +If there are any other errors to report, at all, we want to suppress these.
    
    557
    +We achieve this by setting the `ei_suppress` flag in the `ErrorItem`.
    
    558
    +
    
    559
    +Which errors should be suppressed?
    
    560
    +
    
    561
    +(SCE1) Non-empty rewriter sets.  See Note [Wanteds rewrite Wanteds: rewriter-sets]
    
    562
    +   in  GHC.Tc.Types.Constraint
    
    563
    +
    
    564
    +(SCE2) Superclasses of Wanteds.  These are generated only in case they trigger functional
    
    565
    +   dependencies.  If such a constraint is unsolved, then its "parent" constraint must
    
    566
    +   also be unsolved, and is much more informative to the user.  Example (#26255):
    
    567
    +        class (MinVersion <= F era) => Era era where { ... }
    
    568
    +        f :: forall era. EraFamily era -> IO ()
    
    569
    +        f = ..blah...   -- [W] Era era
    
    570
    +   Here we have simply omitted "Era era =>" from f's type.  But we'll end up with
    
    571
    +   /two/ Wanted constraints:
    
    572
    +        [W] d1 :  Era era
    
    573
    +        [W] d2 : MinVersion <= F era  -- Superclass of d1
    
    574
    +   We definitely want to report d1 and not d2!  Happily it's easy to filter out those
    
    575
    +   superclass-Wanteds, becuase their Origin betrays them.
    
    576
    +
    
    577
    +There are wrinkles
    
    578
    +
    
    579
    +(SCE3) In rare cases we may suppress /all/ errors. That is catastrophic: GHC proceeds
    
    580
    +  to desguar and optimise the program, even though it is full of type errors (#22702,
    
    581
    +  #22793), and/or we fail to bind evidence (#27731).
    
    582
    +
    
    583
    +  If this happens, Unless we are sure that an error will be reported some other way
    
    584
    +  (details in the defn of `tidy_items` in `reportWanteds) we just un-suppress the lot,
    
    585
    +  which is brutal but safe.  It's a rare case.
    
    586
    +
    
    587
    +   How can it happen that there are /all/ errors are suppressed?
    
    588
    +   * See test T18851 for an example of how it is (just, barely) possible for the
    
    589
    +     /only/ errors to be superclass-of-Wanted constraints.
    
    590
    +   * Similarly #27731, which also involves a superclass-of-Wanted:
    
    591
    +          class (a ~ F b) => Ren a b
    
    592
    +     If we have a [W] Ren a b, we'll emit the superclass [W] a ~ F b, which will
    
    593
    +     rewrite the original class constraint to [W] Ren (F b) b.  Now we have two
    
    594
    +     constraints: one has a non-empty rewriter set (SEC1) and one is a superclass of
    
    595
    +     a Wanted (SEC2).
    
    596
    +   * Also see Wrinkle (PER2) in Note [Prioritise Wanteds with empty
    
    597
    +     CoHoleSet] in GHC.Tc.Types.Constraint.
    
    598
    +
    
    599
    +Historical note.  We used to suppress errors arising from the interaction of two
    
    600
    +   fundep constraints.  But nowadays fundep constraints never "escape" into the main
    
    601
    +   solver and so never show up in error messages.  See (SOLVE-FD) in Note [Overview
    
    602
    +   of functional dependencies in type inference] in GHC.Tc.Solver.FunDeps.  So this
    
    603
    +   wrinkle is now just a historical note.
    
    604
    +
    
    605
    +   Errors which arise from the interaction of two Wanted fun-dep constraints.
    
    606
    +   Example:
    
    607
    +
    
    608
    +     class C a b | a -> b where
    
    609
    +       op :: a -> b -> b
    
    610
    +
    
    611
    +     foo _ = op True Nothing
    
    612
    +
    
    613
    +     bar _ = op False []
    
    614
    +
    
    615
    +   Here, we could infer
    
    616
    +     foo :: C Bool (Maybe a) => p -> Maybe a
    
    617
    +     bar :: C Bool [a]       => p -> [a]
    
    618
    +
    
    619
    +   (The unused arguments suppress the monomorphism restriction.) The problem
    
    620
    +   is that these types can't both be correct, as they violate the functional
    
    621
    +   dependency. Yet reporting an error here is awkward: we must
    
    622
    +   non-deterministically choose either foo or bar to reject. We thus want
    
    623
    +   to report this problem only when there is nothing else to report.
    
    624
    +   See typecheck/should_fail/T13506 for an example of when to suppress
    
    625
    +   the error. The case above is actually accepted, because foo and bar
    
    626
    +   are checked separately, and thus the two fundep constraints never
    
    627
    +   encounter each other. It is test case typecheck/should_compile/FunDepOrigin1.
    
    628
    +
    
    629
    +   This case applies only when both fundeps are *Wanted* fundeps; when
    
    630
    +   both are givens, the error represents unreachable code. For
    
    631
    +   a Given/Wanted case, see #9612.
    
    632
    +
    
    633
    +   End of historical note
    
    634
    +
    
    635
    +-}
    
    636
    +
    
    504 637
     ----------------------------------------------------------------
    
    505 638
     reportWanteds :: SolverReportErrCtxt -> TcLevel -> WantedConstraints -> TcM ()
    
    506 639
     reportWanteds ctxt tc_lvl wc@(WC { wc_simple = simples, wc_impl = implics
    
    ... ... @@ -508,21 +641,9 @@ reportWanteds ctxt tc_lvl wc@(WC { wc_simple = simples, wc_impl = implics
    508 641
       | isEmptyWC wc = traceTc "reportWanteds empty WC" empty
    
    509 642
       | otherwise
    
    510 643
       = do { tidy_items1 <- mapMaybeM mkErrorItem tidy_cts
    
    511
    -       ; traceTc "reportWanteds 1" (vcat [ text "Simples =" <+> ppr simples
    
    512
    -                                         , text "Suppress =" <+> ppr (cec_suppress ctxt)
    
    513
    -                                         , text "tidy_cts   =" <+> ppr tidy_cts
    
    514
    -                                         , text "tidy_items1 =" <+> ppr tidy_items1
    
    515
    -                                         , text "tidy_errs =" <+> ppr tidy_errs ])
    
    516 644
     
    
    517 645
              -- Catch an awkward (and probably rare) case in which /all/ errors are
    
    518
    -         -- suppressed: see Wrinkle (PER2) in Note [Prioritise Wanteds with empty
    
    519
    -         -- CoHoleSet] in GHC.Tc.Types.Constraint.
    
    520
    -         --
    
    521
    -         -- Unless we are sure that an error will be reported some other way
    
    522
    -         -- (details in the defn of tidy_items) un-suppress the lot. This makes
    
    523
    -         -- sure we don't forget to report an error at all, which is
    
    524
    -         -- catastrophic: GHC proceeds to desguar and optimise the program, even
    
    525
    -         -- though it is full of type errors (#22702, #22793)
    
    646
    +         -- suppressed: see (SCE3) in Note [Suppressing confusing errors]
    
    526 647
            ; errs_already <- ifErrsM (return True) (return False)
    
    527 648
            ; let tidy_items
    
    528 649
                    | not errs_already                     -- Have not already reported an error (perhaps
    
    ... ... @@ -530,9 +651,16 @@ reportWanteds ctxt tc_lvl wc@(WC { wc_simple = simples, wc_impl = implics
    530 651
                    , not (any ignoreConstraint simples)   -- No error is ignorable (is reported elsewhere)
    
    531 652
                    , all ei_suppress tidy_items1          -- All errors are suppressed
    
    532 653
                    = map unsuppressErrorItem tidy_items1
    
    654
    +
    
    533 655
                    | otherwise
    
    534 656
                    = tidy_items1
    
    535 657
     
    
    658
    +       ; traceTc "reportWanteds 1" (vcat [ text "Simples =" <+> ppr simples
    
    659
    +                                         , text "Suppress =" <+> ppr (cec_suppress ctxt)
    
    660
    +                                         , text "tidy_cts   =" <+> ppr tidy_cts
    
    661
    +                                         , text "tidy_items1 =" <+> ppr tidy_items1
    
    662
    +                                         , text "tidy_errs =" <+> ppr tidy_errs ])
    
    663
    +
    
    536 664
              -- First, deal with any out-of-scope errors:
    
    537 665
            ; let (out_of_scope, other_holes, not_conc_errs, mult_co_errs) = partition_errors tidy_errs
    
    538 666
                    -- don't suppress out-of-scope errors
    
    ... ... @@ -558,10 +686,7 @@ reportWanteds ctxt tc_lvl wc@(WC { wc_simple = simples, wc_impl = implics
    558 686
            -- See wrinkle (DME1) in Note [Coercion errors in tcSubMult] in GHC.Tc.Utils.Unify.
    
    559 687
            ; when (null simples) $ reportMultiplicityCoercionErrs ctxt_for_insols mult_co_errs
    
    560 688
     
    
    561
    -          -- See Note [Suppressing confusing errors]
    
    562
    -       ; let (suppressed_items, reportable_items) = partition suppressItem tidy_items
    
    563
    -       ; traceTc "reportWanteds suppressed:" (ppr suppressed_items)
    
    564
    -       ; (ctxt1, items1) <- tryReporters ctxt_for_insols report1 reportable_items
    
    689
    +       ; (ctxt1, items1) <- tryReporters ctxt_for_insols report1 tidy_items
    
    565 690
     
    
    566 691
              -- Now all the other constraints.  We suppress errors here if
    
    567 692
              -- any of the first batch failed, or if the enclosing context
    
    ... ... @@ -577,18 +702,7 @@ reportWanteds ctxt tc_lvl wc@(WC { wc_simple = simples, wc_impl = implics
    577 702
                 -- NB ctxt2: don't suppress inner insolubles if there's only a
    
    578 703
                 -- wanted insoluble here; but do suppress inner insolubles
    
    579 704
                 -- if there's a *given* insoluble here (= inaccessible code)
    
    580
    -
    
    581
    -         -- If there are no other errors to report, report suppressed errors.
    
    582
    -         -- See (SCE3) in Note [Suppressing confusing errors].
    
    583
    -         -- NB: with -fdefer-type-errors we might have reported warnings only from
    
    584
    -         -- reportable_items`, but we still want to suppress the `suppressed_items`.
    
    585
    -       ; when (null reportable_items) $
    
    586
    -         do { (_, more_leftovers) <- tryReporters ctxt_for_insols (report1++report2)
    
    587
    -                                                  suppressed_items
    
    588
    -                 -- ctxt_for_insols: the suppressed errors can be Int~Bool, which
    
    589
    -                 -- will have made the incoming `ctxt` be True; don't make that
    
    590
    -                 -- suppress the Int~Bool error!
    
    591
    -            ; massertPpr (null more_leftovers) (ppr more_leftovers) } }
    
    705
    +       }
    
    592 706
      where
    
    593 707
         env       = cec_tidy ctxt
    
    594 708
         tidy_cts  = bagToList (mapBag (tidyCt env)   simples)
    
    ... ... @@ -752,15 +866,6 @@ reportWanteds ctxt tc_lvl wc@(WC { wc_simple = simples, wc_impl = implics
    752 866
           = has_gadt_match implics
    
    753 867
     
    
    754 868
     ---------------
    
    755
    -suppressItem :: ErrorItem -> Bool
    
    756
    - -- See Note [Suppressing confusing errors]
    
    757
    -suppressItem item
    
    758
    -  | Wanted <- ei_flavour item
    
    759
    -  , let orig = errorItemOrigin item
    
    760
    -  = isWantedSuperclassOrigin orig       -- See (SCE1)
    
    761
    -  | otherwise
    
    762
    -  = False
    
    763
    -
    
    764 869
     isSkolemTy :: TcLevel -> Type -> Bool
    
    765 870
     -- The type is a skolem tyvar
    
    766 871
     isSkolemTy tc_lvl ty
    
    ... ... @@ -778,113 +883,8 @@ isTyFun_maybe ty = case tcSplitTyConApp_maybe ty of
    778 883
                           Just (tc,_) | isTypeFamilyTyCon tc -> Just tc
    
    779 884
                           _ -> Nothing
    
    780 885
     
    
    781
    -{- Note [Suppressing confusing errors]
    
    782
    -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    783
    -Certain errors we might encounter are potentially confusing to users.
    
    784
    -If there are any other errors to report, at all, we want to suppress these.
    
    785
    -
    
    786
    -Which errors should be suppressed?
    
    787
    -
    
    788
    -(SCE1) Superclasses of Wanteds.  These are generated only in case they trigger functional
    
    789
    -   dependencies.  If such a constraint is unsolved, then its "parent" constraint must
    
    790
    -   also be unsolved, and is much more informative to the user.  Example (#26255):
    
    791
    -        class (MinVersion <= F era) => Era era where { ... }
    
    792
    -        f :: forall era. EraFamily era -> IO ()
    
    793
    -        f = ..blah...   -- [W] Era era
    
    794
    -   Here we have simply omitted "Era era =>" from f's type.  But we'll end up with
    
    795
    -   /two/ Wanted constraints:
    
    796
    -        [W] d1 :  Era era
    
    797
    -        [W] d2 : MinVersion <= F era  -- Superclass of d1
    
    798
    -   We definitely want to report d1 and not d2!  Happily it's easy to filter out those
    
    799
    -   superclass-Wanteds, becuase their Origin betrays them.
    
    800
    -
    
    801
    -Historical (SCE2).  Fundep constraints never "escape" into the
    
    802
    -   main solver and so never show up in error messages.
    
    803
    -   See (SOLVE-FD) in Note [Overview of functional dependencies in type inference]
    
    804
    -   in GHC.Tc.Solver.FunDeps.  So this wrinkle is now just a historical note.
    
    805
    -
    
    806
    -   Errors which arise from the interaction of two Wanted fun-dep constraints.
    
    807
    -   Example:
    
    808
    -
    
    809
    -     class C a b | a -> b where
    
    810
    -       op :: a -> b -> b
    
    811
    -
    
    812
    -     foo _ = op True Nothing
    
    813
    -
    
    814
    -     bar _ = op False []
    
    815
    -
    
    816
    -   Here, we could infer
    
    817
    -     foo :: C Bool (Maybe a) => p -> Maybe a
    
    818
    -     bar :: C Bool [a]       => p -> [a]
    
    819
    -
    
    820
    -   (The unused arguments suppress the monomorphism restriction.) The problem
    
    821
    -   is that these types can't both be correct, as they violate the functional
    
    822
    -   dependency. Yet reporting an error here is awkward: we must
    
    823
    -   non-deterministically choose either foo or bar to reject. We thus want
    
    824
    -   to report this problem only when there is nothing else to report.
    
    825
    -   See typecheck/should_fail/T13506 for an example of when to suppress
    
    826
    -   the error. The case above is actually accepted, because foo and bar
    
    827
    -   are checked separately, and thus the two fundep constraints never
    
    828
    -   encounter each other. It is test case typecheck/should_compile/FunDepOrigin1.
    
    829
    -
    
    830
    -   This case applies only when both fundeps are *Wanted* fundeps; when
    
    831
    -   both are givens, the error represents unreachable code. For
    
    832
    -   a Given/Wanted case, see #9612.
    
    833
    -
    
    834
    -   End of historical (SCE2)
    
    835
    -
    
    836
    -(SCE3) How can it happen that there are /only/ suppressed errors?  See test T18851
    
    837
    -   for an example of how it is (just, barely) possible for the /only/ errors to
    
    838
    -   be superclass-of-Wanted constraints.
    
    839
    -
    
    840
    -Mechanism:
    
    841
    -
    
    842
    -We use the `suppress` function within reportWanteds to filter out these
    
    843
    -"suppress" cases, then report all other errors. After doing so, we return to these
    
    844
    -suppressed ones and report them only if there have been no errors so far.
    
    845
    -
    
    846
    -Note [Constraints to ignore]
    
    847
    -~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    848
    -Some constraints are meant only to aid the solver by unification; a failure
    
    849
    -to solve them is not necessarily an error to report to the user. It is critical
    
    850
    -that compilation is aborted elsewhere if there are any ignored constraints here;
    
    851
    -they will remain unfilled, and might have been used to rewrite another constraint.
    
    852
    -
    
    853
    -Currently, the constraints to ignore are:
    
    854
    -
    
    855
    -(CIG1) Constraints generated in order to unify associated type instance parameters
    
    856
    -   with class parameters. Here are two illustrative examples:
    
    857
    -
    
    858
    -     class C (a :: k) where
    
    859
    -       type F (b :: k)
    
    860
    -
    
    861
    -     instance C True where
    
    862
    -       type F a = Int
    
    863
    -
    
    864
    -     instance C Left where
    
    865
    -       type F (Left :: a -> Either a b) = Bool
    
    866
    -
    
    867
    -   In the first instance, we want to infer that `a` has type Bool. So we emit
    
    868
    -   a constraint unifying kappa (the guessed type of `a`) with Bool. All is well.
    
    869
    -
    
    870
    -   In the second instance, we process the associated type instance only
    
    871
    -   after fixing the quantified type variables of the class instance. We thus
    
    872
    -   have skolems a1 and b1 such that the class instance is for (Left :: a1 -> Either a1 b1).
    
    873
    -   Unifying a1 and b1 with a and b in the type instance will fail, but harmlessly so.
    
    874
    -   checkConsistentFamInst checks for this, and will fail if anything has gone
    
    875
    -   awry. Really the equality constraints emitted are just meant as an aid, not
    
    876
    -   a requirement. This is test case T13972.
    
    877
    -
    
    878
    -   We detect this case by looking for an origin of AssocFamPatOrigin; constraints
    
    879
    -   with this origin are dropped entirely during error message reporting.
    
    880
    -
    
    881
    -   If there is any trouble, checkValidFamInst bleats, aborting compilation.
    
    882
    -
    
    883
    -(Note: Aug 25: this seems a rather tricky corner;
    
    884
    -               c.f. Note [Suppressing confusing errors])
    
    885
    -
    
    886
    -Note [Implementation of Unsatisfiable constraints]
    
    887
    -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    886
    +{- Note [Implementation of Unsatisfiable constraints]
    
    887
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    888 888
     The Unsatisfiable constraint was introduced in GHC proposal #433 (https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0433-unsatisfiable.rst).
    
    889 889
     See Note [The Unsatisfiable constraint] in GHC.TypeError.
    
    890 890
     
    

  • testsuite/tests/typecheck/should_fail/T27731.hs
    1
    +{-# LANGUAGE ImplicitParams, TypeFamilies #-}
    
    2
    +
    
    3
    +module Bug where
    
    4
    +
    
    5
    +type family St (a :: k) :: *
    
    6
    +type family Ev (a :: k) :: * -> *
    
    7
    +
    
    8
    +data T1 a = C1 a
    
    9
    +data T2 h g = C2 (h ())
    
    10
    +
    
    11
    +class (h ~ Ev g, s ~ St g) => Ren s h g
    
    12
    +
    
    13
    +f1 ::
    
    14
    +  (s ~ St h, Ren s h g, ?settings :: settings)
    
    15
    +  => (g v -> g ()) -> g v -> g ()
    
    16
    +f1 form = (\_ a -> a) (C1 (f2 {-@g-})) form
    
    17
    +
    
    18
    +f2 :: forall g h s. ( s ~ St h, Ren s h g) => T2 h g
    
    19
    +f2 = error "urk"
    
    20
    +
    
    21
    +{- Call of f2
    
    22
    +
    
    23
    +[W] s ~ St h  -->  St g ~ St h -->  St g ~ St (Ev g)
    
    24
    +[W] Ren s h g
    
    25
    +[W] s ~ St g   -- Superclass of wanted
    
    26
    +[W] h ~ Ev g   -- Superclass of wanted
    
    27
    +-}
    
    28
    +
    
    29
    +{-
    
    30
    +f3 ::
    
    31
    +  T1 ()
    
    32
    +  -> (g v -> g ())
    
    33
    +  -> g v -> g ()
    
    34
    +f3 wd form = ((\_ a -> a) wd form)
    
    35
    +
    
    36
    +-}

  • testsuite/tests/typecheck/should_fail/T27731.stderr
    1
    +T27731.hs:16:28: [GHC-05617]
    
    2
    +    • Could not deduce ‘St (Ev g0) ~ St g0’
    
    3
    +        arising from a superclass required to satisfy ‘Ren
    
    4
    +                                                         (St (Ev g0)) (Ev g0) g0’,
    
    5
    +        arising from a use of ‘f2’
    
    6
    +      from the context: (s ~ St h, Ren s h g, ?settings::settings)
    
    7
    +        bound by the type signature for:
    
    8
    +                   f1 :: forall s (h :: * -> *) (g :: * -> *) settings v.
    
    9
    +                         (s ~ St h, Ren s h g, ?settings::settings) =>
    
    10
    +                         (g v -> g ()) -> g v -> g ()
    
    11
    +        at T27731.hs:(13,1)-(15,33)
    
    12
    +      Note: ‘St’ is a non-injective type family.
    
    13
    +      The type variable ‘g0’ is ambiguous
    
    14
    +    • In the first argument of ‘C1’, namely ‘(f2)’
    
    15
    +      In the first argument of ‘\ _ a -> a’, namely ‘(C1 (f2))’
    
    16
    +      In the expression: (\ _ a -> a) (C1 (f2)) form

  • testsuite/tests/typecheck/should_fail/all.T
    ... ... @@ -763,3 +763,4 @@ test('T26861', normal, compile_fail, [''])
    763 763
     test('T26862', normal, compile_fail, [''])
    
    764 764
     test('T27210', normal, compile_fail, [''])
    
    765 765
     test('T26532', normal, compile_fail, [''])
    
    766
    +test('T27731', normal, compile_fail, [''])