Simon Peyton Jones pushed to branch wip/ani/tc-expand at Glasgow Haskell Compiler / GHC

Commits:

8 changed files:

Changes:

  • compiler/GHC/Tc/Errors/Ppr.hs
    ... ... @@ -7709,12 +7709,8 @@ pprHsCtxt = \case
    7709 7709
       PatSigErrCtxt sig_ty res_ty ->
    
    7710 7710
         vcat [ hang (text "When checking that the pattern signature:")
    
    7711 7711
                   4 (ppr sig_ty)
    
    7712
    -         , nest 2 (hang (text "fits the type of its context:") 2 pp_res_ty) ]
    
    7713
    -    where
    
    7714
    -      -- Zonking will have turned Infer into Check
    
    7715
    -      pp_res_ty = case res_ty of
    
    7716
    -                    Check ty -> ppr ty
    
    7717
    -                    Infer ir -> text "OOPS" <+> ppr ir
    
    7712
    +         , nest 2 (hang (text "fits the type of its context:")
    
    7713
    +                      2 (ppr (getCheckExpType res_ty))) ]
    
    7718 7714
     
    
    7719 7715
       PatCtxt pat ->
    
    7720 7716
         hang (text "In the pattern:") 2 (ppr pat)
    
    ... ... @@ -7776,7 +7772,7 @@ pprHsCtxt = \case
    7776 7772
           full_herald = pprExpectedFunTyHerald herald
    
    7777 7773
                     <+> speakNOf n_vis_args_in_call (text "visible argument")
    
    7778 7774
                      -- What are "visible" arguments? See Note [Visibility and arity] in GHC.Types.Basic
    
    7779
    -  FunResCtxt fun n_val_args res_fun res_env n_fun n_env
    
    7775
    +  FunResCtxt fun n_val_args fun_res_ty env_ty
    
    7780 7776
         | -- Check for too few args
    
    7781 7777
           --  fun_tau = a -> b, res_tau = Int
    
    7782 7778
           n_fun > n_env
    
    ... ... @@ -7800,6 +7796,18 @@ pprHsCtxt = \case
    7800 7796
         -> empty
    
    7801 7797
           -- text "Debug" <+> vcat [ppr fun, ppr n_val_args, ppr res_fun, ppr res_env, ppr n_fun, ppr n_env]
    
    7802 7798
         where
    
    7799
    +      -- See Note [Splitting nested sigma types in mismatched
    
    7800
    +      --           function types]
    
    7801
    +      -- env_ty is an ExpRhoTy, but with simple subsumption it
    
    7802
    +      -- is not /deeply/ skolemised, so still use tcSplitNestedSigmaTys
    
    7803
    +
    
    7804
    +      (_,_,fun_tau)   = tcSplitNestedSigmaTys fun_res_ty
    
    7805
    +      (_, _, env_tau) = tcSplitNestedSigmaTys (getCheckExpType env_ty)
    
    7806
    +      (args_fun, res_fun) = tcSplitFunTys fun_tau
    
    7807
    +      (args_env, res_env) = tcSplitFunTys env_tau
    
    7808
    +      n_fun = length args_fun
    
    7809
    +      n_env = length args_env
    
    7810
    +
    
    7803 7811
           not_fun ty   -- ty is definitely not an arrow type,
    
    7804 7812
                        -- and cannot conceivably become one
    
    7805 7813
             = case tcSplitTyConApp_maybe ty of
    

  • compiler/GHC/Tc/Gen/App.hs
    ... ... @@ -354,6 +354,7 @@ tcApp :: HsExpr GhcRn
    354 354
           -> ExpRhoType   -- When checking, -XDeepSubsumption <=> deeply skolemised
    
    355 355
           -> TcM (HsExpr GhcTc)
    
    356 356
     -- See Note [tcApp: typechecking applications]
    
    357
    +-- INVARIANT: the expression is an application of some kind
    
    357 358
     tcApp rn_expr exp_res_ty
    
    358 359
       = do { -- Step 1: Split the application chain
    
    359 360
              ((rn_fun, fun_lspan), rn_args) <- splitHsApps rn_expr
    
    ... ... @@ -366,6 +367,8 @@ tcApp rn_expr exp_res_ty
    366 367
                     , text "rn_args:" <+> ppr rn_args ]
    
    367 368
     
    
    368 369
            -- Step 2: Infer the type of `fun`, the head of the application
    
    370
    +       -- NB: we are certain that there is at least one argument, so inferring is
    
    371
    +       --
    
    369 372
            ; (tc_fun, fun_sigma) <- tcInferExprSigma rn_fun
    
    370 373
            ; let tc_head = (tc_fun, fun_lspan)
    
    371 374
                  -- inst_final: top-instantiate the result type of the application,
    
    ... ... @@ -458,44 +461,9 @@ checkResultTy :: HsExpr GhcRn
    458 461
                                 --   expose foralls, but maybe not /deeply/ instantiated
    
    459 462
                   -> ExpRhoType -- Expected type; this is deeply skolemised
    
    460 463
                   -> TcM HsWrapper
    
    461
    -checkResultTy rn_expr (tc_fun, _) _ app_res_rho (Infer inf_res)
    
    462
    -  = do { ds_flag <- getDeepSubsumptionFlag_DataConHead tc_fun
    
    463
    -       ; fillInferResult ds_flag (exprCtOrigin rn_expr) app_res_rho inf_res
    
    464
    -       }
    
    465
    -
    
    466
    -checkResultTy rn_expr (tc_fun, fun_loc) inst_args app_res_rho (Check res_ty)
    
    467
    --- Unify with expected type from the context
    
    468
    --- See Note [Unify with expected type before typechecking arguments]
    
    469
    ---
    
    470
    --- Match up app_res_rho: the result type of rn_expr
    
    471
    ---     with res_ty:  the expected result type
    
    472
    - = perhaps_add_res_ty_ctxt $
    
    473
    -   do { ds_flag <- getDeepSubsumptionFlag_DataConHead tc_fun
    
    474
    -      ; traceTc "checkResultTy {" $
    
    475
    -          vcat [ text "tc_fun:" <+> ppr tc_fun
    
    476
    -               , text "app_res_rho:" <+> ppr app_res_rho
    
    477
    -               , text "res_ty:"  <+> ppr res_ty
    
    478
    -               , text "ds_flag:" <+> ppr ds_flag ]
    
    479
    -      ; case ds_flag of
    
    480
    -          Shallow -> -- No deep subsumption
    
    481
    -             -- app_res_rho and res_ty are both rho-types,
    
    482
    -             -- so with simple subsumption we can just unify them
    
    483
    -             -- No need to zonk; the unifier does that
    
    484
    -             do { co <- unifyExprType rn_expr app_res_rho res_ty
    
    485
    -                ; traceTc "checkResultTy 1 }" (ppr co)
    
    486
    -                ; return (mkWpCastN co) }
    
    487
    -
    
    488
    -          Deep ds_reason ->   -- Deep subsumption
    
    489
    -             -- Even though both app_res_rho and res_ty are rho-types,
    
    490
    -             -- they may have nested polymorphism, so if deep subsumption
    
    491
    -             -- is on we must call tcSubType.
    
    492
    -             do { wrap <- tcSubTypeDS tc_fun ds_reason rn_expr app_res_rho res_ty
    
    493
    -                ; traceTc "checkResultTy 2 }" $
    
    494
    -                   vcat [ text "app_res_rho:" <+> ppr app_res_rho
    
    495
    -                        , text "res_ty:" <+> ppr res_ty
    
    496
    -                        , text "wrap:" <+> ppr wrap
    
    497
    -                        ]
    
    498
    -                ; return wrap } }
    
    464
    +checkResultTy rn_expr (tc_fun, fun_loc) inst_args app_res_ty res_ty
    
    465
    +  = perhaps_add_res_ty_ctxt $
    
    466
    +    tcCheckAppResult rn_expr tc_fun app_res_ty res_ty
    
    499 467
       where
    
    500 468
         -- perhaps_add_res_ty_ctxt: Inside an expansion, the addFunResCtxt stuff is
    
    501 469
         -- more confusing than helpful because the function at the head isn't in
    
    ... ... @@ -505,7 +473,7 @@ checkResultTy rn_expr (tc_fun, fun_loc) inst_args app_res_rho (Check res_ty)
    505 473
           | isGeneratedSrcSpan fun_loc
    
    506 474
           = thing_inside
    
    507 475
           | otherwise
    
    508
    -      = addFunResCtxt tc_fun inst_args app_res_rho (mkCheckExpType res_ty) $
    
    476
    +      = addFunResCtxt tc_fun inst_args app_res_ty res_ty $
    
    509 477
             thing_inside
    
    510 478
     
    
    511 479
     ----------------
    
    ... ... @@ -1915,12 +1883,15 @@ quickLookArg1 pos app_lspan (fun, fun_lspan) larg@(L _ arg) sc_arg_ty@(Scaled _
    1915 1883
                                -- generated by calls in arg
    
    1916 1884
         do { ((rn_fun_arg, fun_lspan_arg), rn_args) <- splitHsApps arg
    
    1917 1885
     
    
    1918
    -       ; if tooComplicatedForQuickLook rn_fun_arg
    
    1886
    +         -- See Note [The head of an application]
    
    1887
    +       ; if not (headOkForQuickLook rn_fun_arg)
    
    1919 1888
              then skipQuickLook app_lspan larg sc_arg_ty
    
    1920 1889
              else
    
    1921 1890
     
    
    1922 1891
            -- Step 1: get the type of the head of the argument
    
    1923
    -    do { (fun_ue, (tc_fun_arg_head, fun_sigma_arg_head)) <- tcCollectingUsage $ tcInferExprSigma rn_fun_arg
    
    1892
    +    do { (fun_ue, (tc_fun_arg_head, fun_sigma_arg_head))
    
    1893
    +                  <- tcCollectingUsage $
    
    1894
    +                     tcInferExprSigma rn_fun_arg
    
    1924 1895
              -- tcCollectingUsage: the use of an Id at the head generates usage-info
    
    1925 1896
              -- See the call to `tcEmitBindingUsage` in `check_local_id`.  So we must
    
    1926 1897
              -- capture and save it in the `EValArgQL`.  See (QLA6) in
    
    ... ... @@ -2003,15 +1974,30 @@ mk_origin fun_lspan rn_fun
    2003 1974
            }
    
    2004 1975
     
    
    2005 1976
     
    
    2006
    -tooComplicatedForQuickLook :: HsExpr GhcRn -> Bool
    
    2007
    -tooComplicatedForQuickLook expr
    
    1977
    +headOkForQuickLook :: HsExpr GhcRn -> Bool
    
    1978
    +headOkForQuickLook expr
    
    2008 1979
       = case expr of
    
    2009
    -      HsVar {}              -> False
    
    2010
    -      ExprWithTySig {}      -> False
    
    2011
    -      XExpr (HsRecSelRn {}) -> False
    
    2012
    -      HsOverLit {}          -> False
    
    2013
    -      _ -> True  -- Too complicated
    
    1980
    +      HsVar {}              -> True
    
    1981
    +      ExprWithTySig {}      -> True
    
    1982
    +      XExpr (HsRecSelRn {}) -> True
    
    1983
    +      HsOverLit {}          -> True
    
    1984
    +      HsUntypedSplice {}    -> True   -- See #21077
    
    1985
    +      _ -> False  -- Too complicated
    
    1986
    +
    
    1987
    +{- Note [The head of an application]
    
    1988
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1989
    +Consider   (f $ (\x -> blah))
    
    1990
    +           where f :: (forall a. a->a) -> Int
    
    1991
    +We really, really need to push down that (forall a. a->a) to the \x.
    
    1992
    +
    
    1993
    +But when doing QuickLook on the second argument of ($) in `quickLookArg1`, namely
    
    1994
    +(\x -> blah), the `splitHsApps` will return an empty argument list, and a function
    
    1995
    +part of (\x -> blah).  We must NOT infer the type of the argument (\x -> blah),
    
    1996
    +via `tcInferExprSigma`, because that will give a monotype to `x`.
    
    2014 1997
     
    
    1998
    +Instead we give up. Which is exactly what the paper says; QuickLook only does something
    
    1999
    +when the head of the application is a variable.
    
    2000
    +-}
    
    2015 2001
     
    
    2016 2002
     {- *********************************************************************
    
    2017 2003
     *                                                                      *
    

  • compiler/GHC/Tc/Gen/Expr.hs
    ... ... @@ -304,41 +304,37 @@ tcExpr :: HsExpr GhcRn
    304 304
                            --                     is deeply skolemised
    
    305 305
            -> TcM (HsExpr GhcTc)
    
    306 306
     
    
    307
    +-- Deal with expansions
    
    308
    +tcExpr (XExpr (ExpandedThingRn hse)) res_ty = tcHsExpansion hse res_ty
    
    309
    +
    
    310
    +tcExpr e@(HsVar _ v_rn) res_ty
    
    311
    +  = do { (v_tc, sigma_ty) <- tcInferId v_rn
    
    312
    +       ; wrap <- tcCheckAppResult e v_tc sigma_ty res_ty
    
    313
    +       ; return (mkHsWrap wrap v_tc) }
    
    314
    +
    
    315
    +tcExpr e@(ExprWithTySig _ e_rn e_ty) res_ty
    
    316
    +  = do { (e_tc, sigma_ty) <- tcExprWithSig e_rn e_ty
    
    317
    +       ; wrap <- tcCheckAppResult e e_tc sigma_ty res_ty
    
    318
    +       ; return (mkHsWrap wrap e_tc) }
    
    319
    +
    
    320
    +tcExpr e@(XExpr(HsRecSelRn f)) res_ty
    
    321
    +  = do { (e_tc, sigma_ty) <- tcInferRecSelId f
    
    322
    +       ; wrap <- tcCheckAppResult e e_tc sigma_ty res_ty
    
    323
    +       ; return (mkHsWrap wrap e_tc) }
    
    324
    +
    
    307 325
     -- Use tcApp to typecheck applications, which are treated specially
    
    308 326
     -- by Quick Look.  Specifically:
    
    309
    ---   - HsVar           lone variables, to ensure that they can get an
    
    310
    ---                     impredicative instantiation (via Quick Look
    
    311
    ---                     driven by res_ty (in checking mode)).
    
    312 327
     --   - HsApp           value applications
    
    313 328
     --   - HsAppType       type applications
    
    314
    ---   - ExprWithTySig   (e :: type)
    
    315
    ---   - HsRecSel        overloaded record fields
    
    316
    ---   - ExpandedThingRn renamer/pre-typechecker expansions
    
    317 329
     --   - HsOpApp         operator applications
    
    318
    ---   - HsOverLit       overloaded literals
    
    319 330
     -- These constructors are the union of
    
    320 331
     --   - ones taken apart by GHC.Tc.Gen.Head.splitHsApps
    
    321 332
     --   - ones understood by GHC.Tc.Gen.Head.tcInferAppHead_maybe
    
    322 333
     -- See Note [Application chains and heads] in GHC.Tc.Gen.App
    
    323 334
     -- Se Note [Typechecking by expansion: overview]
    
    324
    -tcExpr e@(HsVar _ v_rn) res_ty
    
    325
    -  = do { (v_tc, sigma_ty) <- tcInferId v_rn
    
    326
    -       ; traceTc "tcExpr:HsVar" (ppr v_tc <+> dcolon <+> ppr sigma_ty $$ ppr res_ty)
    
    327
    -       ; tcWrapResult e v_tc sigma_ty res_ty }
    
    328
    -
    
    329
    -tcExpr e@(ExprWithTySig _ e_rn e_ty) res_ty
    
    330
    -  = do { (e_tc, sigma_ty) <- tcExprWithSig e_rn e_ty
    
    331
    -       ; tcWrapResult e e_tc sigma_ty res_ty }
    
    332
    -
    
    333
    -tcExpr e@(XExpr(HsRecSelRn f)) res_ty
    
    334
    -  = do { (e_tc, sigma_ty) <- tcInferRecSelId f
    
    335
    -       ; tcWrapResult e e_tc sigma_ty res_ty }
    
    336
    -
    
    337
    -tcExpr (XExpr (ExpandedThingRn hse)) res_ty = tcHsExpansion hse res_ty
    
    338
    -
    
    339
    -tcExpr e@(HsApp {})              res_ty = tcApp e res_ty
    
    340
    -tcExpr e@(OpApp {})              res_ty = tcApp e res_ty
    
    341
    -tcExpr e@(HsAppType {})          res_ty = tcApp e res_ty
    
    335
    +tcExpr e@(HsApp {})     res_ty = tcApp e res_ty
    
    336
    +tcExpr e@(HsAppType {}) res_ty = tcApp e res_ty
    
    337
    +tcExpr e@(OpApp {})     res_ty = tcApp e res_ty
    
    342 338
     
    
    343 339
     -- Typecheck an occurrence of an unbound Id
    
    344 340
     --
    
    ... ... @@ -488,7 +484,8 @@ tcExpr expr@(ExplicitTuple x tup_args boxity) res_ty
    488 484
     
    
    489 485
            ; traceTc "ExplicitTuple" (ppr act_res_ty $$ ppr res_ty)
    
    490 486
     
    
    491
    -       ; tcWrapResultMono expr expr' act_res_ty res_ty }
    
    487
    +       ; co <- tcSubTypeMono expr act_res_ty res_ty
    
    488
    +       ; return (mkHsWrapCo co expr') }
    
    492 489
     
    
    493 490
     tcExpr (ExplicitSum _ alt arity expr) res_ty
    
    494 491
       = do { let sum_tc = sumTyCon arity
    
    ... ... @@ -682,8 +679,6 @@ tcExpr expr@(RecordCon { rcon_con = L loc qcon@(WithUserRdr _ con_name)
    682 679
                                     , rcon_con = L loc con_like
    
    683 680
                                     , rcon_flds = rbinds' }
    
    684 681
     
    
    685
    -        ; ret <- tcWrapResultMono expr expr' actual_res_ty res_ty
    
    686
    -
    
    687 682
             -- Check for missing fields.  We do this after type-checking to get
    
    688 683
             -- better types in error messages (cf #18869).  For example:
    
    689 684
             --     data T a = MkT { x :: a, y :: a }
    
    ... ... @@ -694,7 +689,8 @@ tcExpr expr@(RecordCon { rcon_con = L loc qcon@(WithUserRdr _ con_name)
    694 689
             -- via a new `HoleSort`.  But that seems too much work.
    
    695 690
             ; checkMissingFields con_like rbinds arg_tys
    
    696 691
     
    
    697
    -        ; return ret }
    
    692
    +        ; co <- tcSubTypeMono expr actual_res_ty res_ty
    
    693
    +        ; return (mkHsWrapCo co expr') }
    
    698 694
       where
    
    699 695
         orig = OccurrenceOf con_name
    
    700 696
     
    
    ... ... @@ -721,14 +717,13 @@ tcExpr expr@(RecordUpd { rupd_expr = record_expr
    721 717
                    -- NB: it's important to use ds_res_ty and not res_ty here.
    
    722 718
                    -- Test case: T18802b.
    
    723 719
     
    
    724
    -             ; tcWrapResultMono expr expr' ds_res_ty res_ty
    
    720
    +             ; co <- tcSubTypeMono expr ds_res_ty res_ty
    
    725 721
                  -- We need to unify the result type of the expanded
    
    726 722
                  -- expression with the expected result type.
    
    727 723
                  --
    
    728 724
                  -- See Note [Unifying result types in tcRecordUpd].
    
    729 725
                  -- Test case: T10808.
    
    730
    -             }
    
    731
    -        }
    
    726
    +             ; return (mkHsWrapCo co expr') } }
    
    732 727
     
    
    733 728
     tcExpr e@(RecordUpd { rupd_flds = OverloadedRecUpdFields {}}) _
    
    734 729
       = pprPanic "tcExpr: unexpected overloaded-dot RecordUpd" $ ppr e
    

  • compiler/GHC/Tc/Gen/Head.hs
    ... ... @@ -21,8 +21,6 @@ module GHC.Tc.Gen.Head
    21 21
     
    
    22 22
            , pprArgInst, addFunResCtxt ) where
    
    23 23
     
    
    24
    -import {-# SOURCE #-} GHC.Tc.Gen.Splice( getUntypedSpliceBody )
    
    25
    -
    
    26 24
     import GHC.Prelude
    
    27 25
     import GHC.Hs
    
    28 26
     import GHC.Hs.Syn.Type
    
    ... ... @@ -63,7 +61,6 @@ import GHC.Builtin.Names
    63 61
     import GHC.Driver.DynFlags
    
    64 62
     import GHC.Utils.Misc
    
    65 63
     import GHC.Utils.Outputable as Outputable
    
    66
    -import GHC.Utils.Panic
    
    67 64
     
    
    68 65
     
    
    69 66
     {- *********************************************************************
    
    ... ... @@ -765,10 +762,11 @@ nonBidirectionalErr = TcRnPatSynNotBidirectional
    765 762
     
    
    766 763
     {- Note [Typechecking data constructors]
    
    767 764
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    768
    -As per Note [Polymorphisation of linear fields] in
    
    769
    -GHC.Core.Multiplicity, when we use a data constructor as a term, we want to
    
    770
    -consider its field to have polymorphic multiplicities. That is,
    
    771
    -Note [Data constructors are linear by default] says:
    
    765
    +As per Note [Polymorphisation of linear fields] in GHC.Core.Multiplicity,
    
    766
    +when we use a data constructor as a term, we want to consider its field to
    
    767
    +have polymorphic multiplicities.
    
    768
    +
    
    769
    +That is, Note [Data constructors are linear by default] says:
    
    772 770
     
    
    773 771
         Just :: a. a %1 -> Maybe a
    
    774 772
     
    
    ... ... @@ -934,7 +932,8 @@ See Note [-fno-code mode].
    934 932
     *                                                                      *
    
    935 933
     ********************************************************************* -}
    
    936 934
     
    
    937
    -addFunResCtxt :: HsExpr GhcTc -> [HsExprArg p]
    
    935
    +addFunResCtxt :: HasDebugCallStack
    
    936
    +              => HsExpr GhcTc -> [HsExprArg p]
    
    938 937
                   -> TcType -> ExpRhoType
    
    939 938
                   -> TcM a -> TcM a
    
    940 939
     -- When we have a mis-match in the return type of a function
    
    ... ... @@ -942,33 +941,10 @@ addFunResCtxt :: HsExpr GhcTc -> [HsExprArg p]
    942 941
     -- But not in generated code, where we don't want
    
    943 942
     -- to mention internal (rebindable syntax) function names
    
    944 943
     addFunResCtxt fun args fun_res_ty env_ty thing_inside
    
    945
    -  = do { env_tv  <- newFlexiTyVarTy liftedTypeKind
    
    946
    -       ; dumping <- doptM Opt_D_dump_tc_trace
    
    947
    -       ; msg <- mk_msg dumping env_tv
    
    948
    -       ; addErrCtxt msg thing_inside }
    
    944
    +  = addErrCtxt (FunResCtxt fun (count isValArg args) fun_res_ty env_ty) $
    
    945
    +    thing_inside
    
    949 946
           -- NB: use a landmark error context, so that an empty context
    
    950 947
           -- doesn't suppress some more useful context
    
    951
    -  where
    
    952
    -    mk_msg dumping env_tv
    
    953
    -      = do { mb_env_ty <- readExpType_maybe env_ty
    
    954
    -                     -- by the time the message is rendered, the ExpType
    
    955
    -                     -- will be filled in (except if we're debugging)
    
    956
    -           ; env'     <- case mb_env_ty of
    
    957
    -                           Just env_ty -> return env_ty
    
    958
    -                           Nothing     -> do { massert dumping; return env_tv }
    
    959
    -           ; let -- See Note [Splitting nested sigma types in mismatched
    
    960
    -                 --           function types]
    
    961
    -                 (_, _, fun_tau) = tcSplitNestedSigmaTys fun_res_ty
    
    962
    -                 (_, _, env_tau) = tcSplitNestedSigmaTys env'
    
    963
    -                     -- env_ty is an ExpRhoTy, but with simple subsumption it
    
    964
    -                     -- is not deeply skolemised, so still use tcSplitNestedSigmaTys
    
    965
    -                 (args_fun, res_fun) = tcSplitFunTys fun_tau
    
    966
    -                 (args_env, res_env) = tcSplitFunTys env_tau
    
    967
    -                 info =
    
    968
    -                  FunResCtxt fun (count isValArg args) res_fun res_env
    
    969
    -                    (length args_fun) (length args_env)
    
    970
    -           ; return info }
    
    971
    -
    
    972 948
     
    
    973 949
     {-
    
    974 950
     Note [Splitting nested sigma types in mismatched function types]
    

  • compiler/GHC/Tc/Types/ErrCtxt.hs
    ... ... @@ -251,7 +251,10 @@ data HsCtxt
    251 251
       -- | In the instance type signature of a class method.
    
    252 252
       | MethSigCtxt !Name !TcType !TcType
    
    253 253
       -- | In a pattern type signature.
    
    254
    +
    
    254 255
       | PatSigErrCtxt !TcType !ExpType
    
    256
    +     -- ExpType: see Note [ExpType in HsCtxt]
    
    257
    +
    
    255 258
       -- | In a pattern.
    
    256 259
       | PatCtxt !(Pat GhcRn)
    
    257 260
       -- | In a pattern synonym declaration.
    
    ... ... @@ -268,7 +271,10 @@ data HsCtxt
    268 271
       -- | In a function call.
    
    269 272
       | FunTysCtxt !ExpectedFunTyCtxt !Type !Int !Int
    
    270 273
       -- | In the result of a function call.
    
    271
    -  | FunResCtxt !(HsExpr GhcTc) !Int !Type !Type !Int !Int
    
    274
    +
    
    275
    +  | FunResCtxt !(HsExpr GhcTc) !Int !TcType !ExpType
    
    276
    +     -- ExpType: see Note [ExpType in HsCtxt]
    
    277
    +
    
    272 278
       -- | In the declaration of a type constructor.
    
    273 279
       | TyConDeclCtxt !Name !(TyConFlavour TyCon)
    
    274 280
       -- | In a type or data family instance (or default instance).
    
    ... ... @@ -377,3 +383,14 @@ isHsCtxtLandmark (DerivBindCtxt{}) = True
    377 383
     isHsCtxtLandmark (FunResCtxt{}) = True
    
    378 384
     isHsCtxtLandmark (VDQWarningCtxt{}) = True
    
    379 385
     isHsCtxtLandmark _ = False
    
    386
    +
    
    387
    +{- Note [ExpType in HsCtxt]
    
    388
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    389
    +A couple of HsCtxt constructors have ExpTypes in them.  When zonking the
    
    390
    +Infer{} case we read the hole, which should be filled in by now, and zonk
    
    391
    +that type.  Now we want to put it back: we use (Check ty') for this, so that
    
    392
    +clients of a zonked HsCtxt don't need to be monadic.
    
    393
    +
    
    394
    +Result: after zonking, these ExpTypes are always (Check ty).  It woudl be nice
    
    395
    +to guarantee this statically, but it's hard to do so.
    
    396
    +-}

  • compiler/GHC/Tc/Utils/TcType.hs
    ... ... @@ -28,7 +28,7 @@ module GHC.Tc.Utils.TcType (
    28 28
       ExpType(..), ExpKind, InferResult(..), InferInstFlag(..), InferFRRFlag(..),
    
    29 29
       ExpTypeFRR, ExpSigmaType, ExpSigmaTypeFRR,
    
    30 30
       ExpRhoType, ExpRhoTypeFRR,
    
    31
    -  mkCheckExpType,
    
    31
    +  mkCheckExpType, getCheckExpType,
    
    32 32
       checkingExpType_maybe, checkingExpType,
    
    33 33
     
    
    34 34
       ExpPatType(..), mkCheckExpFunPatTy, mkInvisExpPatType,
    
    ... ... @@ -492,6 +492,12 @@ instance Outputable InferResult where
    492 492
     mkCheckExpType :: TcType -> ExpType
    
    493 493
     mkCheckExpType = Check
    
    494 494
     
    
    495
    +getCheckExpType :: HasDebugCallStack => ExpType -> TcType
    
    496
    +-- Expect a (Check ty).
    
    497
    +-- See Note [ExpType in HsCtxt] in GHC.Tc.Types.ErrCtxt
    
    498
    +getCheckExpType (Check ty) = ty
    
    499
    +getCheckExpType (Infer ir) = pprPanic "getCheckExpType" (ppr ir)
    
    500
    +
    
    495 501
     -- | Returns the expected type when in checking mode.
    
    496 502
     checkingExpType_maybe :: ExpType -> Maybe TcType
    
    497 503
     checkingExpType_maybe (Check ty) = Just ty
    

  • compiler/GHC/Tc/Utils/Unify.hs
    ... ... @@ -10,10 +10,10 @@
    10 10
     -- | Type subsumption and unification
    
    11 11
     module GHC.Tc.Utils.Unify (
    
    12 12
       -- Full-blown subsumption
    
    13
    -  tcWrapResult, tcWrapResultO, tcWrapResultMono,
    
    14
    -  tcSubType, tcSubTypeSigma, tcSubTypePat, tcSubTypeDS, tcSubTypeHoleFit,
    
    15
    -  addSubTypeCtxt,
    
    16
    -  tcSubTypeAmbiguity, tcSubMult,
    
    13
    +  tcWrapResult, tcWrapResultO,
    
    14
    +  addSubTypeCtxt, tcCheckAppResult,
    
    15
    +  tcSubType, tcSubTypeSigma, tcSubTypePat, tcSubTypeHoleFit,
    
    16
    +  tcSubTypeAmbiguity, tcSubMult, tcSubTypeMono,
    
    17 17
       checkConstraints, checkTvConstraints,
    
    18 18
       buildImplicationFor, buildTvImplication, emitResidualTvConstraint,
    
    19 19
     
    
    ... ... @@ -99,13 +99,13 @@ import qualified GHC.LanguageExtensions as LangExt
    99 99
     
    
    100 100
     import GHC.Builtin.Types
    
    101 101
     import GHC.Types.Name
    
    102
    -import GHC.Types.Id( idType, isDataConId )
    
    102
    +import GHC.Types.Id( idType )
    
    103 103
     import GHC.Types.Var as Var
    
    104 104
     import GHC.Types.Var.Set
    
    105 105
     import GHC.Types.Var.Env
    
    106 106
     import GHC.Types.Basic
    
    107 107
     import GHC.Types.Unique.Set (nonDetEltsUniqSet)
    
    108
    -import GHC.Types.SrcLoc (unLoc, GenLocated (..))
    
    108
    +import GHC.Types.SrcLoc( GenLocated(..) )
    
    109 109
     
    
    110 110
     import GHC.Utils.Misc
    
    111 111
     import GHC.Utils.Outputable as Outputable
    
    ... ... @@ -1169,7 +1169,7 @@ fillInferResultNoInst act_res_ty (IR { ir_uniq = u
    1169 1169
     
    
    1170 1170
                          ; return final_co } }
    
    1171 1171
     
    
    1172
    -fillInferResult :: DeepSubsumptionFlag -> CtOrigin -> TcType -> InferResult -> TcM HsWrapper
    
    1172
    +fillInferResult :: DeepSubsumptionFlag -> CtOrigin -> TcSigmaType -> InferResult -> TcM HsWrapper
    
    1173 1173
     -- See Note [Instantiation of InferResult]
    
    1174 1174
     fillInferResult ds_flag ct_orig res_ty ires@(IR { ir_inst = iif })
    
    1175 1175
       = case iif of
    
    ... ... @@ -1425,18 +1425,29 @@ tcWrapResultO orig rn_expr expr actual_ty res_ty
    1425 1425
            ; wrap <- tcSubType orig GenSigCtxt (Just $ HsExprRnThing rn_expr) actual_ty res_ty
    
    1426 1426
            ; return (mkHsWrap wrap expr) }
    
    1427 1427
     
    
    1428
    --- | A version of 'tcWrapResult' to use when the actual type is a
    
    1429
    --- rho-type, so nothing to instantiate; just go straight to unify.
    
    1430
    --- It means we don't need to pass in a CtOrigin.
    
    1431
    -tcWrapResultMono :: HasDebugCallStack
    
    1432
    -                 => HsExpr GhcRn -> HsExpr GhcTc
    
    1433
    -                 -> TcRhoType   -- ^ Actual; a rho-type, not a sigma-type
    
    1434
    -                 -> ExpRhoType  -- ^ Expected
    
    1435
    -                 -> TcM (HsExpr GhcTc)
    
    1436
    -tcWrapResultMono rn_expr expr act_ty res_ty
    
    1437
    -  = do { co <- tcSubTypeMono rn_expr act_ty res_ty
    
    1438
    -       ; return (mkHsWrapCo co expr) }
    
    1428
    +tcCheckAppResult :: HsExpr GhcRn    -- The entire application (GhcRn)
    
    1429
    +                 -> HsExpr GhcTc    -- Head of the application (GhcTc)
    
    1430
    +                 -> TcSigmaType     -- Inferred type of the application; zonked to
    
    1431
    +                                    --   expose foralls, but maybe not /deeply/ instantiated
    
    1432
    +                 -> ExpRhoType      -- Expected type; this is deeply skolemised
    
    1433
    +                 -> TcM HsWrapper
    
    1434
    +-- Unify with expected type from the context
    
    1435
    +-- See Note [Unify with expected type before typechecking arguments]
    
    1436
    +--
    
    1437
    +-- Match up app_res_ty: the result type of rn_expr
    
    1438
    +--     with res_ty:  the expected result type
    
    1439
    +tcCheckAppResult rn_expr tc_fun app_res_ty exp_ty
    
    1440
    + = do { ds_flag <- getDeepSubsumptionFlag_DataConHead tc_fun
    
    1441
    +      ; traceTc "tcCheckResult" (ppr tc_fun $$ ppr ds_flag)
    
    1442
    +      ; let origin = exprCtOrigin rn_expr
    
    1443
    +      ; case exp_ty of
    
    1444
    +           Infer inf_res -> fillInferResult ds_flag origin app_res_ty inf_res
    
    1445
    +           Check res_ty  -> tcSubTypeDS (Just tc_fun) ds_flag
    
    1446
    +                                        (unifyExprType rn_expr)
    
    1447
    +                                        origin GenSigCtxt
    
    1448
    +                                        app_res_ty res_ty }
    
    1439 1449
     
    
    1450
    +------------------------
    
    1440 1451
     -- | A version of 'tcSubType' to use when the actual type is a rho-type,
    
    1441 1452
     -- so that no instantiation is needed.
    
    1442 1453
     tcSubTypeMono :: HasDebugCallStack
    
    ... ... @@ -1460,37 +1471,17 @@ tcSubTypePat :: CtOrigin -> UserTypeCtxt
    1460 1471
     --   to tcSubType
    
    1461 1472
     -- If wrap = tc_sub_type_et t1 t2
    
    1462 1473
     --    => wrap :: t1 ~~> t2
    
    1463
    -tcSubTypePat inst_orig ctxt (Check ty_actual) ty_expected
    
    1464
    -  = tc_sub_type unifyTypeET inst_orig ctxt ty_actual ty_expected
    
    1465
    -
    
    1466
    -tcSubTypePat _ _ (Infer inf_res) ty_expected
    
    1467
    -  = do { co <- fillInferResultNoInst ty_expected inf_res
    
    1468
    -               -- In patterns we do not instantatiate
    
    1469
    -
    
    1470
    -       ; return (mkWpCastN (mkSymCo co)) }
    
    1471
    -
    
    1472
    ----------------
    
    1474
    +tcSubTypePat inst_orig ctxt exp_actual ty_expected
    
    1475
    +  = do { ds_flag <- getDeepSubsumptionFlag
    
    1476
    +       ; case exp_actual of
    
    1477
    +            Infer inf_res -> do { co <- fillInferResultNoInst ty_expected inf_res
    
    1478
    +                                  -- NoInst: in patterns we do not instantatiate
    
    1479
    +                                ; return (mkWpCastN (mkSymCo co)) }
    
    1473 1480
     
    
    1474
    --- | A subtype check that performs deep subsumption.
    
    1475
    --- See also 'tcSubTypeMono', for when no instantiation is required.
    
    1476
    -tcSubTypeDS :: HsExpr GhcTc -- ^ App head (for error messages only)
    
    1477
    -            -> DeepSubsumptionDepth
    
    1478
    -            -> HsExpr GhcRn
    
    1479
    -            -> TcRhoType   -- ^ Actual type; a rho-type, not a sigma-type
    
    1480
    -            -> TcRhoType   -- ^ Expected type
    
    1481
    -                           -- DeepSubsumption <=> when checking, this type
    
    1482
    -                           --                     is deeply skolemised
    
    1483
    -            -> TcM HsWrapper
    
    1484
    --- Only one call site, in GHC.Tc.Gen.App.checkResultTy
    
    1485
    -tcSubTypeDS tc_fun ds_depth rn_expr act_rho exp_rho
    
    1486
    -  = do { wrap <- tc_sub_type_deep (Just tc_fun, Top) ds_depth
    
    1487
    -                   (unifyExprType rn_expr)
    
    1488
    -                   (exprCtOrigin rn_expr)
    
    1489
    -                   GenSigCtxt act_rho exp_rho
    
    1490
    -       ; return (mkWpSubType wrap) }
    
    1481
    +            Check ty_actual -> tcSubTypeDS Nothing ds_flag unifyTypeET
    
    1482
    +                                           inst_orig ctxt ty_actual ty_expected }
    
    1491 1483
     
    
    1492 1484
     ---------------
    
    1493
    -
    
    1494 1485
     -- | Checks that the 'actual' type is more polymorphic than the 'expected' type.
    
    1495 1486
     tcSubType :: CtOrigin          -- ^ Used when instantiating
    
    1496 1487
               -> UserTypeCtxt      -- ^ Used when skolemising
    
    ... ... @@ -1499,13 +1490,12 @@ tcSubType :: CtOrigin -- ^ Used when instantiating
    1499 1490
               -> ExpRhoType        -- ^ Expected type
    
    1500 1491
               -> TcM HsWrapper
    
    1501 1492
     tcSubType inst_orig ctxt m_thing ty_actual res_ty
    
    1502
    -  = case res_ty of
    
    1503
    -      Check ty_expected -> tc_sub_type (unifyType m_thing) inst_orig ctxt
    
    1504
    -                                       ty_actual ty_expected
    
    1493
    +  = do { ds_flag <- getDeepSubsumptionFlag
    
    1494
    +       ; case res_ty of
    
    1495
    +           Infer inf_res     -> fillInferResult ds_flag inst_orig ty_actual inf_res
    
    1496
    +           Check ty_expected -> tcSubTypeDS Nothing ds_flag (unifyType m_thing)
    
    1497
    +                                            inst_orig ctxt ty_actual ty_expected }
    
    1505 1498
     
    
    1506
    -      Infer inf_res ->
    
    1507
    -        do { ds_flag <- getDeepSubsumptionFlag
    
    1508
    -           ; fillInferResult ds_flag inst_orig ty_actual inf_res }
    
    1509 1499
     
    
    1510 1500
     ---------------
    
    1511 1501
     tcSubTypeSigma :: CtOrigin       -- where did the actual type arise / why are we
    
    ... ... @@ -1516,24 +1506,26 @@ tcSubTypeSigma :: CtOrigin -- where did the actual type arise / why are we
    1516 1506
     -- Checks that actual <= expected
    
    1517 1507
     -- Returns HsWrapper :: actual ~~> expected
    
    1518 1508
     tcSubTypeSigma orig ctxt ty_actual ty_expected
    
    1519
    -  = tc_sub_type (unifyType Nothing) orig ctxt ty_actual ty_expected
    
    1509
    +  = do { ds_flag <- getDeepSubsumptionFlag
    
    1510
    +       ; tcSubTypeDS Nothing ds_flag (unifyType Nothing)
    
    1511
    +                     orig ctxt ty_actual ty_expected }
    
    1520 1512
     
    
    1521 1513
     tcSubTypeHoleFit :: DeepSubsumptionFlag
    
    1522 1514
                      -> CtOrigin
    
    1523 1515
                      -> TcSigmaType  -- ^ Candidate expression type
    
    1524 1516
                      -> TcSigmaType  -- ^ Expected type (= hole type)
    
    1525 1517
                      -> TcM HsWrapper
    
    1526
    -tcSubTypeHoleFit ds_flag orig cand_ty hole_ty =
    
    1518
    +tcSubTypeHoleFit ds_flag orig cand_ty hole_ty
    
    1527 1519
        -- See Note [Deep subsumption in tcCheckHoleFit]
    
    1528
    -  tc_sub_type_ds (Nothing, Top) ds_flag (unifyType Nothing)
    
    1529
    -    orig (ExprSigCtxt NoRRC) cand_ty hole_ty
    
    1520
    +  = tcSubTypeDS Nothing ds_flag (unifyType Nothing)
    
    1521
    +                orig (ExprSigCtxt NoRRC) cand_ty hole_ty
    
    1530 1522
     
    
    1531 1523
     ---------------
    
    1532 1524
     tcSubTypeAmbiguity :: UserTypeCtxt   -- Where did this type arise
    
    1533 1525
                        -> TcSigmaType -> TcSigmaType -> TcM HsWrapper
    
    1534 1526
     -- See Note [Ambiguity check and deep subsumption]
    
    1535 1527
     tcSubTypeAmbiguity ctxt ty_actual ty_expected
    
    1536
    -  = tc_sub_type_ds (Nothing, Top)
    
    1528
    +  = tcSubTypeDS Nothing
    
    1537 1529
           Shallow
    
    1538 1530
           (unifyType Nothing)
    
    1539 1531
           (AmbiguityCheckOrigin ctxt)
    
    ... ... @@ -1550,25 +1542,19 @@ addSubTypeCtxt ty_actual ty_expected thing_inside
    1550 1542
           addErrCtxt (SubTypeCtxt ty_expected ty_actual) $
    
    1551 1543
             thing_inside
    
    1552 1544
     
    
    1553
    ----------------
    
    1554
    -tc_sub_type :: (TcType -> TcType -> TcM TcCoercionN)  -- How to unify
    
    1555
    -            -> CtOrigin       -- Used when instantiating
    
    1556
    -            -> UserTypeCtxt   -- Used when skolemising
    
    1557
    -            -> TcSigmaType    -- Actual; a sigma-type
    
    1558
    -            -> TcSigmaType    -- Expected; also a sigma-type
    
    1559
    -            -> TcM HsWrapper
    
    1560
    --- Checks that actual_ty is more polymorphic than expected_ty
    
    1561
    --- If wrap = tc_sub_type t1 t2
    
    1562
    ---    => wrap :: t1 ~~> t2
    
    1563
    ---
    
    1564
    --- The "how to unify argument" is always a call to `uType TypeLevel orig`,
    
    1565
    --- but with different ways of constructing the CtOrigin `orig` from
    
    1566
    --- the argument types and context.
    
    1567
    -
    
    1568 1545
     ----------------------
    
    1569
    -tc_sub_type unify inst_orig ctxt ty_actual ty_expected
    
    1570
    -  = do { ds_flag <- getDeepSubsumptionFlag
    
    1571
    -       ; wrap <- tc_sub_type_ds (Nothing, Top) ds_flag unify inst_orig ctxt ty_actual ty_expected
    
    1546
    +tcSubTypeDS :: Maybe (HsExpr GhcTc)
    
    1547
    +                -- ^ app head, and position in its type (for error messages only)
    
    1548
    +            -> DeepSubsumptionFlag
    
    1549
    +            -> (TcType -> TcType -> TcM TcCoercionN)
    
    1550
    +            -> CtOrigin -> UserTypeCtxt -> TcSigmaType
    
    1551
    +            -> TcSigmaType -> TcM HsWrapper
    
    1552
    +-- tcSubTypeDS is the main subsumption worker function
    
    1553
    +-- It takes an explicit DeepSubsumptionFlag
    
    1554
    +-- It adds the WpSubType tag; see Note [Deep subsumption and WpSubType]
    
    1555
    +tcSubTypeDS tc_fun ds_flag unify inst_orig ctxt ty_actual ty_expected
    
    1556
    +  = do { wrap <- tc_sub_type_ds (tc_fun, Top) ds_flag unify
    
    1557
    +                                inst_orig ctxt ty_actual ty_expected
    
    1572 1558
            ; return (mkWpSubType wrap) }
    
    1573 1559
     
    
    1574 1560
     ----------------------
    
    ... ... @@ -1578,8 +1564,6 @@ tc_sub_type_ds :: (Maybe (HsExpr GhcTc), Position p)
    1578 1564
                    -> (TcType -> TcType -> TcM TcCoercionN)
    
    1579 1565
                    -> CtOrigin -> UserTypeCtxt -> TcSigmaType
    
    1580 1566
                    -> TcSigmaType -> TcM HsWrapper
    
    1581
    --- tc_sub_type_ds is the main subsumption worker function
    
    1582
    --- It takes an explicit DeepSubsumptionFlag
    
    1583 1567
     tc_sub_type_ds pos ds_flag unify inst_orig ctxt ty_actual ty_expected
    
    1584 1568
       | definitely_poly ty_expected   -- See Note [Don't skolemise unnecessarily]
    
    1585 1569
       , isRhoTyDS ds_flag ty_actual
    
    ... ... @@ -1764,8 +1748,8 @@ The effects are in these main places:
    1764 1748
        signatures (e.g. f :: ty; f = e), we must deeply skolemise the type;
    
    1765 1749
        see the call to tcDeeplySkolemise in tcSkolemiseScoped.
    
    1766 1750
     
    
    1767
    -4. In GHC.Tc.Gen.App.tcApp we call tcSubTypeDS to match the result
    
    1768
    -   type. Without deep subsumption, tcSubTypeMono would be sufficent.
    
    1751
    +4. In GHC.Tc.Gen.App.tcApp we call tcCheckAppResult to check the result type
    
    1752
    +   This does a deep subsumption check when necessary.
    
    1769 1753
     
    
    1770 1754
     In all these cases note that the deep skolemisation must be done /first/.
    
    1771 1755
     Consider (1)
    
    ... ... @@ -2046,8 +2030,8 @@ instance Outputable DeepSubsumptionFlag where
    2046 2030
     
    
    2047 2031
     getDeepSubsumptionFlag :: TcM DeepSubsumptionFlag
    
    2048 2032
     getDeepSubsumptionFlag =
    
    2049
    -  do { ds <- xoptM LangExt.DeepSubsumption
    
    2050
    -     ; if ds
    
    2033
    +  do { user_ds <- xoptM LangExt.DeepSubsumption
    
    2034
    +     ; if user_ds
    
    2051 2035
            then return $ Deep DeepSub
    
    2052 2036
            else return Shallow
    
    2053 2037
          }
    
    ... ... @@ -2055,38 +2039,19 @@ getDeepSubsumptionFlag =
    2055 2039
     -- | Variant of 'getDeepSubsumptionFlag' which enables a top-level subsumption
    
    2056 2040
     -- in order to implement the plan of Note [Typechecking data constructors].
    
    2057 2041
     getDeepSubsumptionFlag_DataConHead :: HsExpr GhcTc -> TcM DeepSubsumptionFlag
    
    2058
    -getDeepSubsumptionFlag_DataConHead app_head =
    
    2059
    -  do { user_ds <- xoptM LangExt.DeepSubsumption
    
    2060
    -     ; traceTc "getDeepSubsumptionFlag_DataConHead" (ppr app_head)
    
    2061
    -     ; return $
    
    2062
    -         if | user_ds
    
    2063
    -            -> Deep DeepSub
    
    2064
    -            | otherwise
    
    2065
    -            -> go app_head
    
    2066
    -     }
    
    2042
    +getDeepSubsumptionFlag_DataConHead app_head
    
    2043
    +  = do { user_ds <- xoptM LangExt.DeepSubsumption
    
    2044
    +       ; return $ if | user_ds          -> Deep DeepSub
    
    2045
    +                     | dc_head app_head -> Deep TopSub
    
    2046
    +                     | otherwise        -> Shallow  }
    
    2067 2047
       where
    
    2068
    -    go :: HsExpr GhcTc -> DeepSubsumptionFlag
    
    2069
    -    go app_head
    
    2070
    -     | XExpr (ConLikeTc (RealDataCon {})) <- app_head
    
    2071
    -     = Deep TopSub
    
    2072
    -     | XExpr (ExpandedThingTc (HSE _ (L _ f))) <- app_head
    
    2073
    -     = go f
    
    2074
    -     | XExpr (WrapExpr _ f) <- app_head
    
    2075
    -     = go f
    
    2076
    -     | HsVar _ f <- app_head
    
    2077
    -     , isDataConId (unLoc f)
    
    2078
    -     = Deep TopSub
    
    2079
    -     | HsApp _ f _ <- app_head
    
    2080
    -     = go (unLoc f)
    
    2081
    -     | HsAppType _ f _ <- app_head
    
    2082
    -     = go (unLoc f)
    
    2083
    -     | OpApp _ _ f _ <- app_head
    
    2084
    -     = go (unLoc f)
    
    2085
    -     | HsPar _ f <- app_head
    
    2086
    -     = go (unLoc f)
    
    2087
    -     | otherwise
    
    2088
    -     = Shallow
    
    2089
    -
    
    2048
    +    dc_head (XExpr (ConLikeTc (RealDataCon {}))) = True
    
    2049
    +    dc_head (XExpr (WrapExpr _ f))  = dc_head f
    
    2050
    +    dc_head (HsApp _ (L _ f) _)     = dc_head f
    
    2051
    +    dc_head (HsAppType _ (L _ f) _) = dc_head f
    
    2052
    +    dc_head (OpApp _ _ (L _ f) _)   = dc_head f
    
    2053
    +    dc_head (HsPar _ (L _ f))       = dc_head f
    
    2054
    +    dc_head _ = False
    
    2090 2055
     
    
    2091 2056
     -- | 'tc_sub_type_deep' is where the actual work happens for deep subsumption.
    
    2092 2057
     --
    
    ... ... @@ -2347,7 +2312,8 @@ deeplyInstantiate :: CtOrigin -> TcType -> TcM (HsWrapper, Type)
    2347 2312
     -- Instantiate invisible foralls, even ones nested
    
    2348 2313
     -- (to the right) under arrows
    
    2349 2314
     deeplyInstantiate orig ty
    
    2350
    -  = go init_subst ty
    
    2315
    +  = traceTc "deeplyInstantiate" (ppr ty) >>
    
    2316
    +    go init_subst ty
    
    2351 2317
       where
    
    2352 2318
         init_subst = mkEmptySubst (mkInScopeSet (tyCoVarsOfType ty))
    
    2353 2319
     
    
    ... ... @@ -2356,10 +2322,10 @@ deeplyInstantiate orig ty
    2356 2322
           = do { let tvs = binderVars bndrs
    
    2357 2323
                   -- As per Note [Multiplicity in deep subsumption],
    
    2358 2324
                   -- we treat (a %1 -> b) as if it were forall m. a %m -> b.
    
    2359
    -           ; arg_tys <- traverse oneToMeta arg_tys
    
    2360 2325
                ; (subst', tvs') <- newMetaTyVarsX subst tvs
    
    2361 2326
                ; let arg_tys' = substScaledTys   subst' arg_tys
    
    2362 2327
                      theta'   = substTheta subst' theta
    
    2328
    +           ; arg_tys' <- traverse oneToMeta arg_tys'
    
    2363 2329
                ; ids1  <- newSysLocalIds (fsLit "di") arg_tys'
    
    2364 2330
                ; wrap1 <- instCall orig (mkTyVarTys tvs') theta'
    
    2365 2331
                ; (wrap2, rho2) <- go subst' rho
    
    ... ... @@ -2371,10 +2337,9 @@ deeplyInstantiate orig ty
    2371 2337
                ; return (idHsWrapper, ty') }
    
    2372 2338
     
    
    2373 2339
         oneToMeta :: Scaled TcType -> TcM (Scaled TcType)
    
    2374
    -    oneToMeta (Scaled OneTy ty)
    
    2375
    -      = do { mu <- newMultiplicityVar
    
    2376
    -           ; return $ Scaled mu ty }
    
    2377
    -    oneToMeta ty = return ty
    
    2340
    +    oneToMeta (Scaled OneTy ty) = do { mu <- newMultiplicityVar
    
    2341
    +                                     ; return $ Scaled mu ty }
    
    2342
    +    oneToMeta ty                = return ty
    
    2378 2343
     
    
    2379 2344
     
    
    2380 2345
     tcDeepSplitSigmaTy_maybe
    

  • compiler/GHC/Tc/Zonk/TcType.hs
    ... ... @@ -818,19 +818,26 @@ zonkTidyHsCtxt env e@(FunAppCtxt{}) = return (env, e)
    818 818
     zonkTidyHsCtxt env (FunTysCtxt ctxt ty i1 i2) = do
    
    819 819
       (env', ty') <- zonkTidyTcType env ty
    
    820 820
       return $ (env', FunTysCtxt ctxt ty' i1 i2)
    
    821
    -zonkTidyHsCtxt env (FunResCtxt e i1 ty1 ty2 i2 i3) = do
    
    822
    -  (env', ty1') <- zonkTidyTcType env ty1
    
    823
    -  (env', ty2') <- zonkTidyTcType env' ty2
    
    824
    -  return $ (env', FunResCtxt e i1 ty1' ty2' i2 i3)
    
    821
    +zonkTidyHsCtxt env (FunResCtxt e n ty1 env_ty) = do
    
    822
    +  (env', ty1')    <- zonkTidyTcType env ty1
    
    823
    +  (env', env_ty') <- zonkExpType env' env_ty
    
    824
    +  return $ (env', FunResCtxt e n ty1' env_ty')
    
    825 825
     zonkTidyHsCtxt env (PatSigErrCtxt sig_ty res_ty) = do
    
    826 826
       (env', sig_ty') <- zonkTidyTcType env sig_ty
    
    827
    -  (env', res_ty') <-
    
    828
    -    case res_ty of
    
    829
    -      Check ty -> zonkTidyTcType env' ty
    
    830
    -      Infer (IR {ir_ref = ref}) -> do -- inlining readExpTyp_maybe to avoid module dep loops
    
    831
    -        mb_ty <- liftIO $ readIORef ref
    
    832
    -        case mb_ty of
    
    833
    -          Nothing -> error "zonkTidyHsCtxt PatSigErrCtxt"
    
    834
    -          Just ty -> zonkTidyTcType env' ty
    
    835
    -  return (env', PatSigErrCtxt sig_ty' (Check res_ty'))
    
    827
    +  (env', res_ty') <- zonkExpType env' res_ty
    
    828
    +  return (env', PatSigErrCtxt sig_ty' res_ty')
    
    836 829
     zonkTidyHsCtxt env p = return (env, p)
    
    830
    +
    
    831
    +zonkExpType :: TidyEnv -> ExpType -> ZonkM (TidyEnv, ExpType)
    
    832
    +-- Zonk Infer{} to Check.  The hole should have been filled in by now
    
    833
    +zonkExpType env (Check ty)
    
    834
    +  = do { (env', ty') <- zonkTidyTcType env ty
    
    835
    +       ; return (env', Check ty') }
    
    836
    +zonkExpType env (Infer ir@(IR { ir_ref = ref }))
    
    837
    +  = do { -- inlining readExpTyp_maybe to avoid module dep loops
    
    838
    +       ; mb_ty <- liftIO $ readIORef ref
    
    839
    +       ; case mb_ty of
    
    840
    +            Nothing -> pprPanic "zonkTidyHsCtxt PatSigErrCtxt" (ppr ir)
    
    841
    +            Just ty -> do { (env', ty') <- zonkTidyTcType env ty
    
    842
    +                          ; return (env', Check ty') } }
    
    843
    +