Apoorv Ingle pushed to branch wip/spj-apporv-Oct24 at Glasgow Haskell Compiler / GHC

Commits:

5 changed files:

Changes:

  • compiler/GHC/Tc/Errors/Ppr.hs
    ... ... @@ -5328,6 +5328,7 @@ pprArising :: CtLoc -> SDoc
    5328 5328
     -- We've done special processing for TypeEq, KindEq, givens
    
    5329 5329
     pprArising ct_loc
    
    5330 5330
       | suppress_origin   = empty
    
    5331
    +  | in_generated_code = pprCtOrigin orig -- TODO ANI: maybe should go way
    
    5331 5332
       | otherwise         = pprCtOrigin orig
    
    5332 5333
       where
    
    5333 5334
         orig = ctLocOrigin ct_loc
    

  • compiler/GHC/Tc/Gen/App.hs
    1
    -
    
    2 1
     {-# LANGUAGE DataKinds           #-}
    
    3 2
     {-# LANGUAGE FlexibleContexts    #-}
    
    4 3
     {-# LANGUAGE GADTs               #-}
    
    ... ... @@ -189,8 +188,8 @@ tcExprSigma inst fun_orig rn_expr
    189 188
       = do { (fun@(rn_fun,fun_ctxt), rn_args) <- splitHsApps rn_expr
    
    190 189
            ; do_ql <- wantQuickLook rn_fun
    
    191 190
            ; (tc_fun, fun_sigma) <- tcInferAppHead fun
    
    192
    -       ; (inst_args, app_res_sigma) <- tcInstFun do_ql inst fun_orig (tc_fun, fun_ctxt) fun_sigma rn_args
    
    193
    -       ; tc_args <- tcValArgs do_ql inst_args
    
    191
    +       ; (inst_args, app_res_sigma) <- tcInstFun do_ql inst fun_orig (tc_fun, rn_fun, fun_ctxt) fun_sigma rn_args
    
    192
    +       ; tc_args <- tcValArgs do_ql rn_fun inst_args
    
    194 193
            ; let tc_expr = rebuildHsApps (tc_fun, fun_ctxt) tc_args
    
    195 194
            ; return (tc_expr, app_res_sigma) }
    
    196 195
     
    
    ... ... @@ -422,7 +421,7 @@ tcApp fun_orig rn_expr exp_res_ty
    422 421
                   , text "do_ql:" <+> ppr do_ql]
    
    423 422
     
    
    424 423
            ; (inst_args, app_res_rho)
    
    425
    -              <- tcInstFun do_ql True fun_orig (tc_fun, fun_ctxt) fun_sigma rn_args
    
    424
    +              <- tcInstFun do_ql True fun_orig (tc_fun, rn_fun, fun_ctxt) fun_sigma rn_args
    
    426 425
     
    
    427 426
            ; case do_ql of
    
    428 427
                 NoQL -> do { traceTc "tcApp:NoQL" (ppr rn_fun $$ ppr app_res_rho)
    
    ... ... @@ -433,7 +432,7 @@ tcApp fun_orig rn_expr exp_res_ty
    433 432
                                                        app_res_rho exp_res_ty
    
    434 433
     
    
    435 434
                              -- Step 4.2: typecheck the  arguments
    
    436
    -                       ; tc_args <- tcValArgs NoQL inst_args
    
    435
    +                       ; tc_args <- tcValArgs NoQL rn_fun inst_args
    
    437 436
                              -- Step 4.3: wrap up
    
    438 437
                            ; finishApp tc_head tc_args app_res_rho res_wrap }
    
    439 438
     
    
    ... ... @@ -443,7 +442,7 @@ tcApp fun_orig rn_expr exp_res_ty
    443 442
                            ; quickLookResultType app_res_rho exp_res_ty
    
    444 443
                              -- Step 5.2: typecheck the arguments, and monomorphise
    
    445 444
                              --           any un-unified instantiation variables
    
    446
    -                       ; tc_args <- tcValArgs DoQL inst_args
    
    445
    +                       ; tc_args <- tcValArgs DoQL rn_fun inst_args
    
    447 446
                              -- Step 5.3: typecheck the arguments
    
    448 447
                            ; app_res_rho <- liftZonkM $ zonkTcType app_res_rho
    
    449 448
                              -- Step 5.4: subsumption check against the expected type
    
    ... ... @@ -458,7 +457,7 @@ quickLookResultType :: TcRhoType -> ExpRhoType -> TcM ()
    458 457
     quickLookResultType app_res_rho (Check exp_rho) = qlUnify app_res_rho exp_rho
    
    459 458
     quickLookResultType  _           _              = return ()
    
    460 459
     
    
    461
    -finishApp :: (HsExpr GhcTc, AppCtxt) -> [HsExprArg 'TcpTc]
    
    460
    +finishApp :: (HsExpr GhcTc, SrcSpan) -> [HsExprArg 'TcpTc]
    
    462 461
               -> TcRhoType -> HsWrapper
    
    463 462
               -> TcM (HsExpr GhcTc)
    
    464 463
     -- Do final checks and wrap up the result
    
    ... ... @@ -473,7 +472,7 @@ finishApp tc_head@(tc_fun,_) tc_args app_res_rho res_wrap
    473 472
            ; return (mkHsWrap res_wrap res_expr) }
    
    474 473
     
    
    475 474
     checkResultTy :: HsExpr GhcRn
    
    476
    -              -> (HsExpr GhcTc, AppCtxt)  -- Head
    
    475
    +              -> (HsExpr GhcTc, SrcSpan)  -- Head
    
    477 476
                   -> [HsExprArg p]            -- Arguments, just error messages
    
    478 477
                   -> TcRhoType  -- Inferred type of the application; zonked to
    
    479 478
                                 --   expose foralls, but maybe not deeply instantiated
    
    ... ... @@ -522,33 +521,50 @@ checkResultTy rn_expr (tc_fun, fun_ctxt) inst_args app_res_rho (Check res_ty)
    522 521
         -- the source program; it was added by the renamer.  See
    
    523 522
         -- Note [Handling overloaded and rebindable constructs] in GHC.Rename.Expr
    
    524 523
         perhaps_add_res_ty_ctxt thing_inside
    
    525
    -      | insideExpansion fun_ctxt
    
    524
    +      | isGeneratedSrcSpan fun_ctxt
    
    526 525
           = thing_inside
    
    527 526
           | otherwise
    
    528 527
           = addFunResCtxt tc_fun inst_args app_res_rho (mkCheckExpType res_ty) $
    
    529 528
             thing_inside
    
    530 529
     
    
    531 530
     ----------------
    
    532
    -tcValArgs :: QLFlag -> [HsExprArg 'TcpInst] -> TcM [HsExprArg 'TcpTc]
    
    531
    +tcValArgs :: QLFlag -> HsExpr GhcRn -> [HsExprArg 'TcpInst] -> TcM [HsExprArg 'TcpTc]
    
    533 532
     -- Importantly, tcValArgs works left-to-right, so that by the time we
    
    534 533
     -- encounter an argument, we have monomorphised all the instantiation
    
    535 534
     -- variables that its type contains.  All that is left to do is an ordinary
    
    536 535
     -- zonkTcType.  See Note [Monomorphise instantiation variables].
    
    537
    -tcValArgs do_ql args = mapM (tcValArg do_ql) args
    
    536
    +tcValArgs do_ql fun args = go do_ql 0 args
    
    537
    +  where
    
    538
    +    go _ _ [] = return []
    
    539
    +    go do_ql pos (arg : args) =
    
    540
    +      do { arg' <- tcValArg do_ql pos' fun arg
    
    541
    +         ; args' <- go do_ql pos' args
    
    542
    +         ; return (arg' : args')
    
    543
    +         }
    
    544
    +      where
    
    545
    +    -- increment position if the argument is user written type or value argument
    
    546
    +        pos' | EValArg{} <- arg
    
    547
    +             = pos + 1
    
    548
    +             | EValArgQL{} <- arg
    
    549
    +             = pos + 1
    
    550
    +             | ETypeArg{ ea_ctxt = l } <- arg
    
    551
    +             , not (isGeneratedSrcSpan l) = pos + 1
    
    552
    +             | otherwise = pos
    
    538 553
     
    
    539
    -tcValArg :: QLFlag -> HsExprArg 'TcpInst    -- Actual argument
    
    554
    +
    
    555
    +tcValArg :: QLFlag -> Int -> HsExpr GhcRn -> HsExprArg 'TcpInst    -- Actual argument
    
    540 556
              -> TcM (HsExprArg 'TcpTc)          -- Resulting argument
    
    541
    -tcValArg _     (EPrag l p)         = return (EPrag l (tcExprPrag p))
    
    542
    -tcValArg _     (ETypeArg l hty ty) = return (ETypeArg l hty ty)
    
    543
    -tcValArg do_ql (EWrap (EHsWrap w)) = do { whenQL do_ql $ qlMonoHsWrapper w
    
    544
    -                                        ; return (EWrap (EHsWrap w)) }
    
    557
    +tcValArg _     _ _ (EPrag l p)         = return (EPrag l (tcExprPrag p))
    
    558
    +tcValArg _     _ _ (ETypeArg l hty ty) = return (ETypeArg l hty ty)
    
    559
    +tcValArg do_ql _ _ (EWrap (EHsWrap w)) = do { whenQL do_ql $ qlMonoHsWrapper w
    
    560
    +                                            ; return (EWrap (EHsWrap w)) }
    
    545 561
       -- qlMonoHsWrapper: see Note [Monomorphise instantiation variables]
    
    546
    -tcValArg _     (EWrap ew)          = return (EWrap ew)
    
    562
    +tcValArg _     _ _ (EWrap ew)          = return (EWrap ew)
    
    547 563
     
    
    548
    -tcValArg do_ql (EValArg { ea_ctxt   = ctxt
    
    549
    -                        , ea_arg    = larg@(L arg_loc arg)
    
    550
    -                        , ea_arg_ty = sc_arg_ty })
    
    551
    -  = addArgCtxt ctxt larg $
    
    564
    +tcValArg do_ql pos fun (EValArg { ea_ctxt   = ctxt
    
    565
    +                            , ea_arg    = larg@(L arg_loc arg)
    
    566
    +                            , ea_arg_ty = sc_arg_ty })
    
    567
    +  = addArgCtxt pos fun larg $
    
    552 568
         do { -- Crucial step: expose QL results before checking exp_arg_ty
    
    553 569
              -- So far as the paper is concerned, this step applies
    
    554 570
              -- the poly-substitution Theta, learned by QL, so that we
    
    ... ... @@ -577,23 +593,26 @@ tcValArg do_ql (EValArg { ea_ctxt = ctxt
    577 593
                              , ea_arg = L arg_loc arg'
    
    578 594
                              , ea_arg_ty = noExtField }) }
    
    579 595
     
    
    580
    -tcValArg _ (EValArgQL { eaql_wanted  = wanted
    
    596
    +tcValArg _ pos fun (EValArgQL { eaql_wanted  = wanted
    
    581 597
                           , eaql_ctxt    = ctxt
    
    582 598
                           , eaql_arg_ty  = sc_arg_ty
    
    583 599
                           , eaql_larg    = larg@(L arg_loc rn_expr)
    
    584 600
                           , eaql_tc_fun  = tc_head
    
    601
    +                      , eaql_rn_fun  = rn_fun
    
    585 602
                           , eaql_fun_ue  = head_ue
    
    586 603
                           , eaql_args    = inst_args
    
    587 604
                           , eaql_encl    = arg_influences_enclosing_call
    
    588 605
                           , eaql_res_rho = app_res_rho })
    
    589
    -  = addArgCtxt ctxt larg $
    
    606
    +  = addArgCtxt pos fun larg $
    
    590 607
         do { -- Expose QL results to tcSkolemise, as in EValArg case
    
    591 608
              Scaled mult exp_arg_ty <- liftZonkM $ zonkScaledTcType sc_arg_ty
    
    592 609
     
    
    593 610
            ; traceTc "tcEValArgQL {" (vcat [ text "app_res_rho:" <+> ppr app_res_rho
    
    594 611
                                            , text "exp_arg_ty:" <+> ppr exp_arg_ty
    
    595 612
                                            , text "args:" <+> ppr inst_args
    
    596
    -                                       , text "mult:" <+> ppr mult])
    
    613
    +                                       , text "mult:" <+> ppr mult
    
    614
    +                                       , text "fun" <+> ppr fun
    
    615
    +                                       , text "tc_head" <+> ppr tc_head])
    
    597 616
     
    
    598 617
            ; ds_flag <- getDeepSubsumptionFlag
    
    599 618
            ; (wrap, arg')
    
    ... ... @@ -611,7 +630,7 @@ tcValArg _ (EValArgQL { eaql_wanted = wanted
    611 630
                       ; unless arg_influences_enclosing_call $  -- Don't repeat
    
    612 631
                         qlUnify app_res_rho exp_arg_rho         -- the qlUnify
    
    613 632
     
    
    614
    -                  ; tc_args <- tcValArgs DoQL inst_args
    
    633
    +                  ; tc_args <- tcValArgs DoQL rn_fun inst_args
    
    615 634
                       ; app_res_rho <- liftZonkM $ zonkTcType app_res_rho
    
    616 635
                       ; res_wrap <- checkResultTy rn_expr tc_head inst_args
    
    617 636
                                                   app_res_rho (mkCheckExpType exp_arg_rho)
    
    ... ... @@ -656,14 +675,14 @@ tcInstFun :: QLFlag
    656 675
                         -- Otherwise we do eager instantiation; in Fig 5 of the paper
    
    657 676
                         --    |-inst returns a rho-type
    
    658 677
               -> CtOrigin
    
    659
    -          -> (HsExpr GhcTc, AppCtxt)
    
    678
    +          -> (HsExpr GhcTc, HsExpr GhcRn, SrcSpan)
    
    660 679
               -> TcSigmaType -> [HsExprArg 'TcpRn]
    
    661 680
               -> TcM ( [HsExprArg 'TcpInst]
    
    662 681
                      , TcSigmaType )
    
    663 682
     -- This crucial function implements the |-inst judgement in Fig 4, plus the
    
    664 683
     -- modification in Fig 5, of the QL paper:
    
    665 684
     -- "A quick look at impredicativity" (ICFP'20).
    
    666
    -tcInstFun do_ql inst_final fun_orig (tc_fun, fun_ctxt) fun_sigma rn_args
    
    685
    +tcInstFun do_ql inst_final fun_orig (tc_fun, rn_fun, fun_ctxt) fun_sigma rn_args
    
    667 686
       = do { traceTc "tcInstFun" (vcat [ text "origin" <+> ppr fun_orig
    
    668 687
                                        , text "tc_fun" <+> ppr tc_fun
    
    669 688
                                        , text "fun_sigma" <+> ppr fun_sigma
    
    ... ... @@ -845,7 +864,7 @@ tcInstFun do_ql inst_final fun_orig (tc_fun, fun_ctxt) fun_sigma rn_args
    845 864
                       (Just $ HsExprTcThing tc_fun)
    
    846 865
                       (n_val_args, fun_sigma) fun_ty
    
    847 866
     
    
    848
    -           ; arg' <- quickLookArg do_ql ctxt arg arg_ty
    
    867
    +           ; arg' <- quickLookArg do_ql pos ctxt rn_fun arg arg_ty
    
    849 868
                ; let acc' = arg' : addArgWrap wrap acc
    
    850 869
                ; go (pos+1) acc' res_ty rest_args }
    
    851 870
     
    
    ... ... @@ -889,7 +908,7 @@ looks_like_type_arg EValArg{ ea_arg = L _ e } =
    889 908
         _           -> False
    
    890 909
     looks_like_type_arg _ = False
    
    891 910
     
    
    892
    -addArgCtxt :: AppCtxt -> LHsExpr GhcRn
    
    911
    +addArgCtxt :: Int -> HsExpr GhcRn -> LHsExpr GhcRn
    
    893 912
                -> TcM a -> TcM a
    
    894 913
     -- There are 2 cases:
    
    895 914
     -- 1. In the normal case, we add an informative context
    
    ... ... @@ -901,20 +920,18 @@ addArgCtxt :: AppCtxt -> LHsExpr GhcRn
    901 920
     --   Unless the arg is also a generated thing, in which case do nothing.
    
    902 921
     --  See Note [Rebindable syntax and XXExprGhcRn] in GHC.Hs.Expr
    
    903 922
     --  See Note [Expanding HsDo with XXExprGhcRn] in GHC.Tc.Gen.Do
    
    904
    -addArgCtxt ctxt (L arg_loc arg) thing_inside
    
    923
    +addArgCtxt arg_no fun (L arg_loc arg) thing_inside
    
    905 924
       = do { in_generated_code <- inGeneratedCode
    
    906 925
            ; traceTc "addArgCtxt" (vcat [ text "generated:" <+> ppr in_generated_code
    
    907 926
                                         , text "arg: " <+> ppr arg
    
    908 927
                                         , text "arg_loc" <+> ppr arg_loc])
    
    909
    -       ; case ctxt of
    
    910
    -           VACall fun arg_no _ | not in_generated_code
    
    911
    -             -> do setSrcSpanA arg_loc                    $
    
    912
    -                     addErrCtxt (FunAppCtxt (FunAppCtxtExpr fun arg) arg_no) $
    
    913
    -                     thing_inside
    
    914
    -
    
    915
    -           _ -> setSrcSpanA arg_loc $
    
    916
    -                  addExprCtxt arg     $  -- Auto-suppressed if arg_loc is generated
    
    917
    -                  thing_inside }
    
    928
    +       ; if in_generated_code
    
    929
    +         then do setSrcSpanA arg_loc $
    
    930
    +                   addExprCtxt arg     $  -- Auto-suppressed if arg_loc is generated
    
    931
    +                   thing_inside
    
    932
    +         else do setSrcSpanA arg_loc                    $
    
    933
    +                   addErrCtxt (FunAppCtxt (FunAppCtxtExpr fun arg) arg_no) $
    
    934
    +                   thing_inside }
    
    918 935
     
    
    919 936
     {- *********************************************************************
    
    920 937
     *                                                                      *
    
    ... ... @@ -1690,21 +1707,21 @@ This turned out to be more subtle than I expected. Wrinkles:
    1690 1707
     
    
    1691 1708
     -}
    
    1692 1709
     
    
    1693
    -quickLookArg :: QLFlag -> AppCtxt
    
    1710
    +quickLookArg :: QLFlag -> Int -> SrcSpan -> HsExpr GhcRn
    
    1694 1711
                  -> LHsExpr GhcRn          -- ^ Argument
    
    1695 1712
                  -> Scaled TcSigmaTypeFRR  -- ^ Type expected by the function
    
    1696 1713
                  -> TcM (HsExprArg 'TcpInst)
    
    1697 1714
     -- See Note [Quick Look at value arguments]
    
    1698
    -quickLookArg NoQL ctxt larg orig_arg_ty
    
    1715
    +quickLookArg NoQL _ ctxt _ larg orig_arg_ty
    
    1699 1716
       = skipQuickLook ctxt larg orig_arg_ty
    
    1700
    -quickLookArg DoQL ctxt larg orig_arg_ty
    
    1717
    +quickLookArg DoQL pos ctxt fun larg orig_arg_ty
    
    1701 1718
       = do { is_rho <- tcIsDeepRho (scaledThing orig_arg_ty)
    
    1702 1719
            ; traceTc "qla" (ppr orig_arg_ty $$ ppr is_rho)
    
    1703 1720
            ; if not is_rho
    
    1704 1721
              then skipQuickLook ctxt larg orig_arg_ty
    
    1705
    -         else quickLookArg1 ctxt larg orig_arg_ty }
    
    1722
    +         else quickLookArg1 pos ctxt fun larg orig_arg_ty }
    
    1706 1723
     
    
    1707
    -skipQuickLook :: AppCtxt -> LHsExpr GhcRn -> Scaled TcRhoType
    
    1724
    +skipQuickLook :: SrcSpan -> LHsExpr GhcRn -> Scaled TcRhoType
    
    1708 1725
                   -> TcM (HsExprArg 'TcpInst)
    
    1709 1726
     skipQuickLook ctxt larg arg_ty
    
    1710 1727
       = return (EValArg { ea_ctxt   = ctxt
    
    ... ... @@ -1745,12 +1762,12 @@ isGuardedTy ty
    1745 1762
       | Just {} <- tcSplitAppTy_maybe ty        = True
    
    1746 1763
       | otherwise                               = False
    
    1747 1764
     
    
    1748
    -quickLookArg1 :: AppCtxt -> LHsExpr GhcRn
    
    1765
    +quickLookArg1 :: Int -> SrcSpan -> HsExpr GhcRn -> LHsExpr GhcRn
    
    1749 1766
                   -> Scaled TcRhoType  -- Deeply skolemised
    
    1750 1767
                   -> TcM (HsExprArg 'TcpInst)
    
    1751 1768
     -- quickLookArg1 implements the "QL Argument" judgement in Fig 5 of the paper
    
    1752
    -quickLookArg1 ctxt larg@(L _ arg) sc_arg_ty@(Scaled _ orig_arg_rho)
    
    1753
    -  = addArgCtxt ctxt larg $ -- Context needed for constraints
    
    1769
    +quickLookArg1 pos ctxt fun larg@(L _ arg) sc_arg_ty@(Scaled _ orig_arg_rho)
    
    1770
    +  = addArgCtxt pos fun larg $ -- Context needed for constraints
    
    1754 1771
                                -- generated by calls in arg
    
    1755 1772
         do { ((rn_fun, fun_ctxt), rn_args) <- splitHsApps arg
    
    1756 1773
     
    
    ... ... @@ -1776,7 +1793,7 @@ quickLookArg1 ctxt larg@(L _ arg) sc_arg_ty@(Scaled _ orig_arg_rho)
    1776 1793
            ; do_ql <- wantQuickLook rn_fun
    
    1777 1794
            ; ((inst_args, app_res_rho), wanted)
    
    1778 1795
                  <- captureConstraints $
    
    1779
    -                tcInstFun do_ql True (exprCtOrigin arg) (tc_fun, fun_ctxt) fun_sigma rn_args
    
    1796
    +                tcInstFun do_ql True (exprCtOrigin arg) (tc_fun, rn_fun, fun_ctxt) fun_sigma rn_args
    
    1780 1797
                     -- We must capture type-class and equality constraints here, but
    
    1781 1798
                     -- not equality constraints.  See (QLA6) in Note [Quick Look at
    
    1782 1799
                     -- value arguments]
    
    ... ... @@ -1812,6 +1829,7 @@ quickLookArg1 ctxt larg@(L _ arg) sc_arg_ty@(Scaled _ orig_arg_rho)
    1812 1829
                                , eaql_arg_ty  = sc_arg_ty
    
    1813 1830
                                , eaql_larg    = larg
    
    1814 1831
                                , eaql_tc_fun  = tc_head
    
    1832
    +                           , eaql_rn_fun  = rn_fun
    
    1815 1833
                                , eaql_fun_ue  = fun_ue
    
    1816 1834
                                , eaql_args    = inst_args
    
    1817 1835
                                , eaql_wanted  = wanted
    
    ... ... @@ -2187,7 +2205,7 @@ isTagToEnum :: HsExpr GhcTc -> Bool
    2187 2205
     isTagToEnum (HsVar _ (L _ fun_id)) = fun_id `hasKey` tagToEnumKey
    
    2188 2206
     isTagToEnum _ = False
    
    2189 2207
     
    
    2190
    -tcTagToEnum :: (HsExpr GhcTc, AppCtxt) -> [HsExprArg 'TcpTc]
    
    2208
    +tcTagToEnum :: (HsExpr GhcTc, SrcSpan) -> [HsExprArg 'TcpTc]
    
    2191 2209
                 -> TcRhoType
    
    2192 2210
                 -> TcM (HsExpr GhcTc)
    
    2193 2211
     -- tagToEnum# :: forall a. Int# -> a
    
    ... ... @@ -2314,7 +2332,7 @@ Wrinkle [Representation-polymorphic lambdas] in Note [Typechecking data construc
    2314 2332
     -- if the representation of its argument isn't known.
    
    2315 2333
     --
    
    2316 2334
     -- See Note [Eta-expanding rep-poly unlifted newtypes].
    
    2317
    -rejectRepPolyNewtypes :: (HsExpr GhcTc, AppCtxt)
    
    2335
    +rejectRepPolyNewtypes :: (HsExpr GhcTc, SrcSpan)
    
    2318 2336
                           -> TcRhoType
    
    2319 2337
                           -> TcM ()
    
    2320 2338
     rejectRepPolyNewtypes (fun,_) app_res_rho = case fun of
    

  • compiler/GHC/Tc/Gen/Expr.hs
    1
    -
    
    2 1
     {-# LANGUAGE DataKinds           #-}
    
    3 2
     {-# LANGUAGE FlexibleContexts    #-}
    
    4 3
     {-# LANGUAGE ScopedTypeVariables #-}
    
    ... ... @@ -905,7 +904,7 @@ tcSyntaxOpGen :: CtOrigin
    905 904
                   -> ([TcSigmaTypeFRR] -> [Mult] -> TcM a)
    
    906 905
                   -> TcM (a, SyntaxExprTc)
    
    907 906
     tcSyntaxOpGen orig (SyntaxExprRn op) arg_tys res_ty thing_inside
    
    908
    -  = do { (expr, sigma) <- tcInferAppHead (op, VACall op 0 noSrcSpan)
    
    907
    +  = do { (expr, sigma) <- tcInferAppHead (op, noSrcSpan)
    
    909 908
                  -- Ugh!! But all this code is scheduled for demolition anyway
    
    910 909
            ; traceTc "tcSyntaxOpGen" (ppr op $$ ppr expr $$ ppr sigma)
    
    911 910
            ; (result, expr_wrap, arg_wraps, res_wrap)
    

  • compiler/GHC/Tc/Gen/Head.hs
    1
    -
    
    2 1
     {-# LANGUAGE DataKinds           #-}
    
    3 2
     {-# LANGUAGE FlexibleContexts    #-}
    
    4 3
     {-# LANGUAGE GADTs               #-}
    
    ... ... @@ -17,7 +16,6 @@
    17 16
     
    
    18 17
     module GHC.Tc.Gen.Head
    
    19 18
            ( HsExprArg(..), TcPass(..), QLFlag(..), EWrap(..)
    
    20
    -       , AppCtxt(..), appCtxtLoc, insideExpansion, appCtxtExpr
    
    21 19
            , splitHsApps, rebuildHsApps
    
    22 20
            , addArgWrap, isHsValArg
    
    23 21
            , leadingValArgs, isVisibleArg
    
    ... ... @@ -167,18 +165,19 @@ data TcPass = TcpRn -- Arguments decomposed
    167 165
     data HsExprArg (p :: TcPass) where -- See Note [HsExprArg]
    
    168 166
     
    
    169 167
       -- Data constructor EValArg represents a value argument
    
    170
    -  EValArg :: { ea_ctxt   :: AppCtxt
    
    168
    +  EValArg :: { ea_ctxt   :: SrcSpan
    
    171 169
                  , ea_arg_ty :: !(XEVAType p)
    
    172 170
                  , ea_arg    :: LHsExpr (GhcPass (XPass p)) }
    
    173 171
               -> HsExprArg p
    
    174 172
     
    
    175 173
       -- Data constructor EValArgQL represents an argument that has been
    
    176 174
       -- partly-type-checked by Quick Look; see Note [EValArgQL]
    
    177
    -  EValArgQL :: { eaql_ctxt    :: AppCtxt
    
    175
    +  EValArgQL :: { eaql_ctxt    :: SrcSpan
    
    178 176
                    , eaql_arg_ty  :: Scaled TcSigmaType  -- Argument type expected by function
    
    179 177
                    , eaql_larg    :: LHsExpr GhcRn       -- Original application, for
    
    180 178
                                                          -- location and error msgs
    
    181
    -               , eaql_tc_fun  :: (HsExpr GhcTc, AppCtxt) -- Typechecked head
    
    179
    +               , eaql_rn_fun  :: HsExpr GhcRn  -- Head of the argument if it is an application
    
    180
    +               , eaql_tc_fun  :: (HsExpr GhcTc, SrcSpan) -- Typechecked head
    
    182 181
                    , eaql_fun_ue  :: UsageEnv -- Usage environment of the typechecked head (QLA5)
    
    183 182
                    , eaql_args    :: [HsExprArg 'TcpInst]    -- Args: instantiated, not typechecked
    
    184 183
                    , eaql_wanted  :: WantedConstraints
    
    ... ... @@ -187,12 +186,12 @@ data HsExprArg (p :: TcPass) where -- See Note [HsExprArg]
    187 186
                    , eaql_res_rho :: TcRhoType }           -- Result type of the application
    
    188 187
                 -> HsExprArg 'TcpInst  -- Only exists in TcpInst phase
    
    189 188
     
    
    190
    -  ETypeArg :: { ea_ctxt   :: AppCtxt
    
    189
    +  ETypeArg :: { ea_ctxt   :: SrcSpan
    
    191 190
                   , ea_hs_ty  :: LHsWcType GhcRn  -- The type arg
    
    192 191
                   , ea_ty_arg :: !(XETAType p) }  -- Kind-checked type arg
    
    193 192
                -> HsExprArg p
    
    194 193
     
    
    195
    -  EPrag :: AppCtxt -> (HsPragE (GhcPass (XPass p))) -> HsExprArg p
    
    194
    +  EPrag :: SrcSpan -> (HsPragE (GhcPass (XPass p))) -> HsExprArg p
    
    196 195
       EWrap :: EWrap                                    -> HsExprArg p
    
    197 196
     
    
    198 197
     type family XETAType (p :: TcPass) where  -- Type arguments
    
    ... ... @@ -205,70 +204,25 @@ type family XEVAType (p :: TcPass) where -- Value arguments
    205 204
     
    
    206 205
     data QLFlag = DoQL | NoQL
    
    207 206
     
    
    208
    -data EWrap = EPar    AppCtxt
    
    207
    +data EWrap = EPar    SrcSpan
    
    209 208
                | EExpand (HsExpr GhcRn)
    
    210 209
                | EHsWrap HsWrapper
    
    211 210
     
    
    212
    -data AppCtxt =
    
    213
    -  VACall
    
    214
    -     (HsExpr GhcRn) Int  -- In the third argument of function f
    
    215
    -     SrcSpan             -- The SrcSpan of the application (f e1 e2 e3)
    
    216
    -                         --    noSrcSpan if outermost; see Note [AppCtxt]
    
    217
    -
    
    218
    -
    
    219
    -{- Note [AppCtxt]
    
    220
    -~~~~~~~~~~~~~~~~~
    
    221
    -In a call (f e1 ... en), we pair up each argument with an AppCtxt. For
    
    222
    -example, the AppCtxt for e3 allows us to say
    
    223
    -    "In the third argument of `f`"
    
    224
    -See splitHsApps.
    
    225
    -
    
    226
    -To do this we must take a quick look into the expression to find the
    
    227
    -function at the head (`f` in this case) and how many arguments it
    
    228
    -has. That is what the funcion top_ctxt does.
    
    229
    -
    
    230
    -If the function part is an expansion, we don't want to look further.
    
    231
    -For example, with rebindable syntax the expression
    
    232
    -    (if e1 then e2 else e3) e4 e5
    
    233
    -might expand to
    
    234
    -    (ifThenElse e1 e2 e3) e4 e5
    
    235
    -For e4 we an AppCtxt that says "In the first argument of (if ...)",
    
    236
    -not "In the fourth argument of ifThenElse".  So top_ctxt stops
    
    237
    -at expansions.
    
    238
    -
    
    239
    -The SrcSpan in an AppCtxt describes the whole call.  We initialise
    
    240
    -it to noSrcSpan, because splitHsApps deals in HsExpr not LHsExpr, so
    
    241
    -we don't have a span for the whole call; and we use that noSrcSpan in
    
    242
    -GHC.Tc.Gen.App.tcInstFun (set_fun_ctxt) to avoid pushing "In the expression `f`"
    
    243
    -a second time.
    
    244
    --}
    
    245
    -
    
    246
    -appCtxtLoc :: AppCtxt -> SrcSpan
    
    247
    -appCtxtLoc (VACall _ _ l)    = l
    
    248
    -
    
    249
    -insideExpansion :: AppCtxt -> Bool
    
    250
    -insideExpansion ctxt  = isGeneratedSrcSpan (appCtxtLoc ctxt)
    
    251
    -
    
    252
    -appCtxtExpr :: AppCtxt -> HsExpr GhcRn
    
    253
    -appCtxtExpr (VACall e _ _) = e
    
    254 211
     
    
    255 212
     instance Outputable QLFlag where
    
    256 213
       ppr DoQL = text "DoQL"
    
    257 214
       ppr NoQL = text "NoQL"
    
    258 215
     
    
    259
    -instance Outputable AppCtxt where
    
    260
    -  ppr (VACall f n l)    = text "VACall" <+> int n <+> ppr f  <+> ppr l
    
    261
    -
    
    262 216
     type family XPass (p :: TcPass) where
    
    263 217
       XPass 'TcpRn   = 'Renamed
    
    264 218
       XPass 'TcpInst = 'Renamed
    
    265 219
       XPass 'TcpTc   = 'Typechecked
    
    266 220
     
    
    267
    -mkEValArg :: AppCtxt -> LHsExpr GhcRn -> HsExprArg 'TcpRn
    
    221
    +mkEValArg :: SrcSpan -> LHsExpr GhcRn -> HsExprArg 'TcpRn
    
    268 222
     mkEValArg ctxt e = EValArg { ea_arg = e, ea_ctxt = ctxt
    
    269 223
                                , ea_arg_ty = noExtField }
    
    270 224
     
    
    271
    -mkETypeArg :: AppCtxt -> LHsWcType GhcRn -> HsExprArg 'TcpRn
    
    225
    +mkETypeArg :: SrcSpan -> LHsWcType GhcRn -> HsExprArg 'TcpRn
    
    272 226
     mkETypeArg ctxt hs_ty =
    
    273 227
       ETypeArg { ea_ctxt = ctxt
    
    274 228
                , ea_hs_ty = hs_ty
    
    ... ... @@ -281,52 +235,38 @@ addArgWrap wrap args
    281 235
     
    
    282 236
     
    
    283 237
     splitHsApps :: HsExpr GhcRn
    
    284
    -            -> TcM ( (HsExpr GhcRn, AppCtxt)  -- Head
    
    238
    +            -> TcM ( (HsExpr GhcRn, SrcSpan)  -- Head
    
    285 239
                        , [HsExprArg 'TcpRn])      -- Args
    
    286 240
     -- See Note [splitHsApps].
    
    287 241
     --
    
    288 242
     -- This uses the TcM monad solely because we must run modFinalizers when looking
    
    289 243
     -- through HsUntypedSplices
    
    290 244
     -- (see Note [Looking through Template Haskell splices in splitHsApps]).
    
    291
    -splitHsApps e = go e (top_ctxt 0 e) []
    
    245
    +splitHsApps e = go e noSrcSpan []
    
    292 246
       where
    
    293
    -    top_ctxt :: Int -> HsExpr GhcRn -> AppCtxt
    
    294
    -    -- Always returns VACall fun n_val_args noSrcSpan
    
    295
    -    -- to initialise the argument splitting in 'go'
    
    296
    -    -- See Note [AppCtxt]
    
    297
    -    top_ctxt n (HsPar _ fun)        = top_lctxt n fun
    
    298
    -    top_ctxt n (HsPragE _ _ fun)    = top_lctxt n fun
    
    299
    -    top_ctxt n (HsAppType _ fun _)  = top_lctxt (n+1) fun
    
    300
    -    top_ctxt n (HsApp _ fun _)      = top_lctxt (n+1) fun
    
    301
    -    top_ctxt n (XExpr (PopErrCtxt fun)) = top_ctxt n fun
    
    302
    -    top_ctxt n other_fun            = VACall other_fun n noSrcSpan
    
    303
    -
    
    304
    -    top_lctxt :: Int -> LHsExpr GhcRn -> AppCtxt
    
    305
    -    top_lctxt n (L _ fun) = top_ctxt n fun
    
    306
    -
    
    307
    -    go :: HsExpr GhcRn -> AppCtxt -> [HsExprArg 'TcpRn]
    
    308
    -       -> TcM ((HsExpr GhcRn, AppCtxt), [HsExprArg 'TcpRn])
    
    309
    -    -- Modify the AppCtxt as we walk inwards, so it describes the next argument
    
    310
    -    go (HsPar _ (L l fun))           ctxt args = go fun (set l ctxt) (EWrap (EPar ctxt)     : args)
    
    311
    -    go (HsPragE _ p (L l fun))       ctxt args = go fun (set l ctxt) (EPrag      ctxt p     : args)
    
    312
    -    go (HsAppType _ (L l fun) ty)    ctxt args = go fun (dec l ctxt) (mkETypeArg ctxt ty    : args)
    
    313
    -    go (HsApp _ (L l fun) arg)       ctxt args = go fun (dec l ctxt) (mkEValArg  ctxt arg   : args)
    
    247
    +    go :: HsExpr GhcRn -> SrcSpan -> [HsExprArg 'TcpRn]
    
    248
    +       -> TcM ((HsExpr GhcRn, SrcSpan), [HsExprArg 'TcpRn])
    
    249
    +    -- Modify the SrcSpan as we walk inwards, so it describes the next argument
    
    250
    +    go (HsPar _ (L l fun))        ctxt args = go fun (locA l) (EWrap (EPar ctxt)     : args)
    
    251
    +    go (HsPragE _ p (L l fun))    ctxt args = go fun (locA l) (EPrag      ctxt p     : args)
    
    252
    +    go (HsAppType _ (L l fun) ty) ctxt args = go fun (locA l) (mkETypeArg ctxt ty    : args)
    
    253
    +    go (HsApp _ (L l fun) arg)    ctxt args = go fun (locA l) (mkEValArg  ctxt arg   : args)
    
    314 254
     
    
    315 255
         -- See Note [Looking through Template Haskell splices in splitHsApps]
    
    316
    -    go e@(HsUntypedSplice splice_res splice) ctxt args
    
    256
    +    go e@(HsUntypedSplice splice_res splice) _ args
    
    317 257
           = do { fun <- getUntypedSpliceBody splice_res
    
    318 258
                ; go fun ctxt' (EWrap (EExpand e) : args) }
    
    319 259
           where
    
    320
    -        ctxt' :: AppCtxt
    
    260
    +        ctxt' :: SrcSpan
    
    321 261
             ctxt' = case splice of
    
    322
    -            HsUntypedSpliceExpr _ (L l _) -> set l ctxt -- l :: SrcAnn AnnListItem
    
    323
    -            HsQuasiQuote _ _ (L l _)      -> set l ctxt -- l :: SrcAnn NoEpAnns
    
    262
    +            HsUntypedSpliceExpr _ (L l _) -> locA l -- l :: SrcAnn AnnListItem
    
    263
    +            HsQuasiQuote _ _ (L l _)      -> locA l -- l :: SrcAnn NoEpAnns
    
    324 264
     
    
    325 265
         -- See Note [Desugar OpApp in the typechecker]
    
    326 266
         go e@(OpApp _ arg1 (L l op) arg2) _ args
    
    327
    -      = pure ( (op, VACall op 0 (locA l))
    
    328
    -             ,   mkEValArg (VACall op 1 generatedSrcSpan) arg1
    
    329
    -               : mkEValArg (VACall op 2 generatedSrcSpan) arg2
    
    267
    +      = pure ( (op, locA l)
    
    268
    +             ,   mkEValArg generatedSrcSpan arg1
    
    269
    +               : mkEValArg generatedSrcSpan arg2
    
    330 270
                         -- generatedSrcSpan because this the span of the call,
    
    331 271
                         -- and its hard to say exactly what that is
    
    332 272
                    : EWrap (EExpand e)
    
    ... ... @@ -337,11 +277,6 @@ splitHsApps e = go e (top_ctxt 0 e) []
    337 277
     
    
    338 278
         go e ctxt args = pure ((e,ctxt), args)
    
    339 279
     
    
    340
    -    set :: EpAnn ann -> AppCtxt -> AppCtxt
    
    341
    -    set l (VACall f n _)          = VACall f n (locA l)
    
    342
    -
    
    343
    -    dec :: EpAnn ann -> AppCtxt -> AppCtxt
    
    344
    -    dec l (VACall f n _)          = VACall f (n-1) (locA l)
    
    345 280
     
    
    346 281
     -- | Rebuild an application: takes a type-checked application head
    
    347 282
     -- expression together with arguments in the form of typechecked 'HsExprArg's
    
    ... ... @@ -351,7 +286,7 @@ splitHsApps e = go e (top_ctxt 0 e) []
    351 286
     -- representation-polymorphic unlifted newtypes have been eta-expanded.
    
    352 287
     --
    
    353 288
     -- See Note [Eta-expanding rep-poly unlifted newtypes].
    
    354
    -rebuildHsApps :: (HsExpr GhcTc, AppCtxt)
    
    289
    +rebuildHsApps :: (HsExpr GhcTc, SrcSpan)
    
    355 290
                           -- ^ the function being applied
    
    356 291
                   -> [HsExprArg 'TcpTc]
    
    357 292
                           -- ^ the arguments to the function
    
    ... ... @@ -372,7 +307,7 @@ rebuildHsApps (fun, ctxt) (arg : args)
    372 307
           EWrap (EHsWrap wrap)
    
    373 308
             -> rebuildHsApps (mkHsWrap wrap fun, ctxt) args
    
    374 309
       where
    
    375
    -    lfun = L (noAnnSrcSpan $ appCtxtLoc ctxt) fun
    
    310
    +    lfun = L (noAnnSrcSpan ctxt) fun
    
    376 311
     
    
    377 312
     isHsValArg :: HsExprArg id -> Bool
    
    378 313
     isHsValArg (EValArg {}) = True
    
    ... ... @@ -505,7 +440,7 @@ Wrinkle (UTS1):
    505 440
     *                                                                      *
    
    506 441
     ********************************************************************* -}
    
    507 442
     
    
    508
    -tcInferAppHead :: (HsExpr GhcRn, AppCtxt)
    
    443
    +tcInferAppHead :: (HsExpr GhcRn, SrcSpan)
    
    509 444
                    -> TcM (HsExpr GhcTc, TcSigmaType)
    
    510 445
     -- Infer type of the head of an application
    
    511 446
     --   i.e. the 'f' in (f e1 ... en)
    
    ... ... @@ -545,13 +480,12 @@ tcInferAppHead_maybe fun =
    545 480
           HsOverLit _ lit             -> Just <$> tcInferOverLit lit
    
    546 481
           _                           -> return Nothing
    
    547 482
     
    
    548
    -addHeadCtxt :: AppCtxt -> TcM a -> TcM a
    
    549
    -addHeadCtxt fun_ctxt thing_inside
    
    483
    +addHeadCtxt :: SrcSpan -> TcM a -> TcM a --TODO ANI: Why not just setSrcSpan?
    
    484
    +addHeadCtxt fun_loc thing_inside
    
    550 485
       | not (isGoodSrcSpan fun_loc)       -- noSrcSpan => no arguments
    
    551 486
       = thing_inside                      -- => context is already set
    
    552 487
       | otherwise
    
    553 488
       = setSrcSpan fun_loc thing_inside
    
    554
    -  where fun_loc = appCtxtLoc fun_ctxt
    
    555 489
     
    
    556 490
     
    
    557 491
     {- *********************************************************************
    

  • compiler/GHC/Tc/Utils/Monad.hs
    ... ... @@ -1730,6 +1730,7 @@ mkErrCtxt env ctxts
    1730 1730
     --          else go dbg 0 env ctxts
    
    1731 1731
      = go False 0 env ctxts -- regular error ctx
    
    1732 1732
      where
    
    1733
    +   go :: Bool -> Int -> TidyEnv -> [ErrCtxt] -> TcM [ErrCtxtMsg]
    
    1733 1734
        go _ _ _   [] = return []
    
    1734 1735
        go dbg n env ((is_landmark, ctxt) : ctxts)
    
    1735 1736
          | is_landmark || n < mAX_CONTEXTS -- Too verbose || dbg