Apoorv Ingle pushed to branch wip/ani/better-expansion at Glasgow Haskell Compiler / GHC
Commits:
-
a091fcbb
by Apoorv Ingle at 2026-04-01T09:43:04-05:00
11 changed files:
- compiler/GHC/Tc/Gen/App.hs
- − compiler/GHC/Tc/Gen/App.hs-boot
- + compiler/GHC/Tc/Gen/Expand.hs
- compiler/GHC/Tc/Gen/Expr.hs
- compiler/GHC/Tc/Gen/Expr.hs-boot
- compiler/GHC/Tc/Gen/Head.hs
- compiler/GHC/Tc/Gen/Match.hs
- compiler/GHC/Tc/Utils/TcMType.hs
- compiler/GHC/Tc/Utils/Unify.hs
- compiler/ghc.cabal.in
- testsuite/tests/partial-sigs/should_compile/SplicesUsed.stderr
Changes:
| ... | ... | @@ -11,7 +11,6 @@ |
| 11 | 11 | |
| 12 | 12 | module GHC.Tc.Gen.App
|
| 13 | 13 | ( tcApp
|
| 14 | - , tcExprSigma
|
|
| 15 | 14 | , tcExprPrag ) where
|
| 16 | 15 | |
| 17 | 16 | import {-# SOURCE #-} GHC.Tc.Gen.Expr( tcPolyExpr )
|
| ... | ... | @@ -165,34 +164,6 @@ Note [Instantiation variables are short lived] |
| 165 | 164 | -}
|
| 166 | 165 | |
| 167 | 166 | |
| 168 | -{- *********************************************************************
|
|
| 169 | -* *
|
|
| 170 | - tcInferSigma
|
|
| 171 | -* *
|
|
| 172 | -********************************************************************* -}
|
|
| 173 | - |
|
| 174 | --- Very similar to tcApp, but returns a sigma (uninstantiated) type
|
|
| 175 | --- CAUTION: Any changes to tcApp should be reflected here
|
|
| 176 | --- cf. T19167. the head is an expanded expression applied to a type
|
|
| 177 | --- Caution: Currently we assume that the expression is compiler generated/expanded
|
|
| 178 | --- Because that is what T19167 test case expects.
|
|
| 179 | --- This function should go away after MR!15778 lands
|
|
| 180 | -tcExprSigma :: Bool -> CtOrigin -> HsExpr GhcRn -> TcM (HsExpr GhcTc, TcSigmaType)
|
|
| 181 | -tcExprSigma inst fun_orig rn_expr
|
|
| 182 | - = do { (fun@(rn_fun,fun_lspan), rn_args) <- splitHsApps rn_expr
|
|
| 183 | - ; do_ql <- wantQuickLook rn_fun
|
|
| 184 | - ; (tc_fun, fun_sigma) <- tcInferAppHead fun
|
|
| 185 | - ; inGenCode <- inGeneratedCode
|
|
| 186 | - ; traceTc "tcExprSigma" (vcat [ text "rn_expr:" <+> ppr rn_expr
|
|
| 187 | - , text "tc_fun" <+> ppr tc_fun
|
|
| 188 | - , text "inGeneratedCode:" <+> ppr inGenCode])
|
|
| 189 | - ; (inst_args, app_res_sigma) <- tcInstFun do_ql inst (fun_orig, rn_fun, fun_lspan)
|
|
| 190 | - tc_fun fun_sigma rn_args
|
|
| 191 | - ; tc_args <- tcValArgs do_ql (rn_fun, fun_lspan) inst_args
|
|
| 192 | - ; let tc_expr = rebuildHsApps (tc_fun, fun_lspan) tc_args
|
|
| 193 | - ; return (tc_expr, app_res_sigma) }
|
|
| 194 | - |
|
| 195 | - |
|
| 196 | 167 | {- *********************************************************************
|
| 197 | 168 | * *
|
| 198 | 169 | Typechecking n-ary applications
|
| ... | ... | @@ -379,24 +350,22 @@ Unify result type /before/ typechecking the args |
| 379 | 350 | |
| 380 | 351 | The latter is much better. That is why we call `checkResultTy` before tcValArgs.
|
| 381 | 352 | -}
|
| 382 | --- CAUTION: Any changes to tcApp should be reflected in tcExprSigma
|
|
| 383 | -tcApp :: HsExpr GhcRn
|
|
| 353 | + |
|
| 354 | +--------------------
|
|
| 355 | +tcApp :: HsExpr GhcRn -- The whole application
|
|
| 356 | + -> HsExpr GhcRn -> [HsExprArg 'TcpRn] -- Function and arguments
|
|
| 384 | 357 | -> ExpRhoType -- When checking, -XDeepSubsumption <=> deeply skolemised
|
| 385 | 358 | -> TcM (HsExpr GhcTc)
|
| 386 | 359 | -- See Note [tcApp: typechecking applications]
|
| 387 | -tcApp rn_expr exp_res_ty
|
|
| 388 | - = do { -- Step 1: Split the application chain
|
|
| 389 | - (fun@(rn_fun, fun_lspan), rn_args) <- splitHsApps rn_expr
|
|
| 390 | - ; inGenCode <- inGeneratedCode
|
|
| 360 | +tcApp rn_expr rn_fun rn_args exp_res_ty
|
|
| 361 | + = do { fun_lspan <- getFunSrcSpan rn_args
|
|
| 391 | 362 | ; traceTc "tcApp {" $
|
| 392 | - vcat [ text "generated? " <+> ppr inGenCode
|
|
| 393 | - , text "rn_expr:" <+> ppr rn_expr
|
|
| 394 | - , text "rn_fun:" <+> ppr rn_fun
|
|
| 363 | + vcat [ text "rn_fun:" <+> ppr rn_fun
|
|
| 395 | 364 | , text "fun_lspan:" <+> ppr fun_lspan
|
| 396 | 365 | , text "rn_args:" <+> ppr rn_args ]
|
| 397 | 366 | |
| 398 | 367 | -- Step 2: Infer the type of `fun`, the head of the application
|
| 399 | - ; (tc_fun, fun_sigma) <- tcInferAppHead fun
|
|
| 368 | + ; (tc_fun, fun_sigma) <- tcInferAppHead (rn_fun, fun_lspan)
|
|
| 400 | 369 | ; let tc_head = (tc_fun, fun_lspan)
|
| 401 | 370 | -- inst_final: top-instantiate the result type of the application,
|
| 402 | 371 | -- EXCEPT if we are trying to infer a sigma-type
|
| ... | ... | @@ -411,22 +380,12 @@ tcApp rn_expr exp_res_ty |
| 411 | 380 | -- Step 3.1: Instantiate the function type (taking a quick look at args)
|
| 412 | 381 | ; do_ql <- wantQuickLook rn_fun
|
| 413 | 382 | |
| 414 | - -- Setp 3.2 Set the correct origin to blame for the error message
|
|
| 415 | - -- What should be the origin for this function call?
|
|
| 416 | - -- If the head of the function is user written
|
|
| 417 | - -- then it can be used in the error message
|
|
| 418 | - -- If it is generated code location span, blame it on the
|
|
| 419 | - -- origin that can be retrived from the top of the error ctxt stack.
|
|
| 420 | - -- See Note [Error contexts in generated code]
|
|
| 421 | - ; fun_orig <- mk_origin fun_lspan rn_fun
|
|
| 422 | - |
|
| 423 | 383 | ; traceTc "tcApp:inferAppHead" $
|
| 424 | 384 | vcat [ text "tc_fun:" <+> ppr tc_fun
|
| 425 | 385 | , text "fun_sigma:" <+> ppr fun_sigma
|
| 426 | - , text "fun_origin" <+> ppr fun_orig
|
|
| 427 | 386 | , text "do_ql:" <+> ppr do_ql]
|
| 428 | 387 | ; (inst_args, app_res_rho)
|
| 429 | - <- tcInstFun do_ql inst_final (fun_orig, rn_fun, fun_lspan) tc_fun fun_sigma rn_args
|
|
| 388 | + <- tcInstFun do_ql inst_final (rn_fun, fun_lspan) tc_fun fun_sigma rn_args
|
|
| 430 | 389 | -- See (TCAPP1) and (TCAPP2) in
|
| 431 | 390 | -- Note [tcApp: typechecking applications]
|
| 432 | 391 | |
| ... | ... | @@ -440,7 +399,7 @@ tcApp rn_expr exp_res_ty |
| 440 | 399 | -- Step 4.2: typecheck the arguments
|
| 441 | 400 | ; tc_args <- tcValArgs NoQL (rn_fun, fun_lspan) inst_args
|
| 442 | 401 | -- Step 4.3: wrap up
|
| 443 | - ; finishApp tc_head tc_args app_res_rho res_wrap }
|
|
| 402 | + ; finishApp tc_fun tc_args app_res_rho res_wrap }
|
|
| 444 | 403 | |
| 445 | 404 | DoQL -> do { traceTc "tcApp:DoQL" (ppr rn_fun $$ ppr app_res_rho)
|
| 446 | 405 | |
| ... | ... | @@ -458,7 +417,7 @@ tcApp rn_expr exp_res_ty |
| 458 | 417 | ; res_wrap <- checkResultTy rn_expr tc_head inst_args
|
| 459 | 418 | app_res_rho exp_res_ty
|
| 460 | 419 | -- Step 5.5: wrap up
|
| 461 | - ; finishApp tc_head tc_args app_res_rho res_wrap } }
|
|
| 420 | + ; finishApp tc_fun tc_args app_res_rho res_wrap } }
|
|
| 462 | 421 | |
| 463 | 422 | quickLookResultType :: TcRhoType -> ExpRhoType -> TcM ()
|
| 464 | 423 | -- This function implements the shaded bit of rule APP-Downarrow in
|
| ... | ... | @@ -466,16 +425,16 @@ quickLookResultType :: TcRhoType -> ExpRhoType -> TcM () |
| 466 | 425 | quickLookResultType app_res_rho (Check exp_rho) = qlUnify app_res_rho exp_rho
|
| 467 | 426 | quickLookResultType _ _ = return ()
|
| 468 | 427 | |
| 469 | -finishApp :: (HsExpr GhcTc, SrcSpan) -> [HsExprArg 'TcpTc]
|
|
| 428 | +finishApp :: HsExpr GhcTc -> [HsExprArg 'TcpTc]
|
|
| 470 | 429 | -> TcRhoType -> HsWrapper
|
| 471 | 430 | -> TcM (HsExpr GhcTc)
|
| 472 | 431 | -- Do final checks and wrap up the result
|
| 473 | -finishApp tc_head@(tc_fun,_) tc_args app_res_rho res_wrap
|
|
| 432 | +finishApp tc_fun tc_args app_res_rho res_wrap
|
|
| 474 | 433 | = do {
|
| 475 | 434 | -- Reconstruct, with a horrible special case for tagToEnum#.
|
| 476 | 435 | res_expr <- if isTagToEnum tc_fun
|
| 477 | - then tcTagToEnum tc_head tc_args app_res_rho
|
|
| 478 | - else return (rebuildHsApps tc_head tc_args)
|
|
| 436 | + then tcTagToEnum tc_fun tc_args app_res_rho
|
|
| 437 | + else return (rebuildHsApps tc_fun tc_args)
|
|
| 479 | 438 | ; traceTc "End tcApp }" (ppr tc_fun)
|
| 480 | 439 | ; return (mkHsWrap res_wrap res_expr) }
|
| 481 | 440 | |
| ... | ... | @@ -488,11 +447,12 @@ checkResultTy :: HsExpr GhcRn |
| 488 | 447 | -- expose foralls, but maybe not /deeply/ instantiated
|
| 489 | 448 | -> ExpRhoType -- Expected type; this is deeply skolemised
|
| 490 | 449 | -> TcM HsWrapper
|
| 491 | -checkResultTy rn_expr (tc_fun, _) _ app_res_rho (Infer inf_res)
|
|
| 450 | +checkResultTy rn_expr (tc_fun,_) _ app_res_rho (Infer inf_res)
|
|
| 492 | 451 | = do { ds_flag <- getDeepSubsumptionFlag_DataConHead tc_fun
|
| 452 | + -- Why the "DataConHead" bit? See (IIR5) in
|
|
| 453 | + -- Note [Instantiation of InferResult] in GHC.Tc.Utils.Unify.
|
|
| 493 | 454 | ; fillInferResult ds_flag (exprCtOrigin rn_expr) app_res_rho inf_res }
|
| 494 | 455 | |
| 495 | - |
|
| 496 | 456 | checkResultTy rn_expr (tc_fun, fun_loc) inst_args app_res_rho (Check res_ty)
|
| 497 | 457 | -- Unify with expected type from the context
|
| 498 | 458 | -- See Note [Unify with expected type before typechecking arguments]
|
| ... | ... | @@ -561,7 +521,7 @@ tcValArgs do_ql (fun, fun_lspan) args = go do_ql 0 args |
| 561 | 521 | | EValArgQL{} <- arg
|
| 562 | 522 | = pos + 1
|
| 563 | 523 | | ETypeArg{ ea_loc_span = l } <- arg
|
| 564 | - , not (isGeneratedSrcSpan l)
|
|
| 524 | + , not (isGeneratedSrcSpan (locA l))
|
|
| 565 | 525 | = pos + 1
|
| 566 | 526 | | otherwise
|
| 567 | 527 | = pos
|
| ... | ... | @@ -618,7 +578,7 @@ tcValArg _ pos (fun, fun_lspan) (EValArgQL { |
| 618 | 578 | , eaql_loc_span = lspan
|
| 619 | 579 | , eaql_arg_ty = sc_arg_ty
|
| 620 | 580 | , eaql_larg = larg@(L arg_loc rn_expr)
|
| 621 | - , eaql_tc_fun = tc_head
|
|
| 581 | + , eaql_tc_fun = tc_head@(tc_fun,_)
|
|
| 622 | 582 | , eaql_rn_fun = rn_fun
|
| 623 | 583 | , eaql_fun_ue = head_ue
|
| 624 | 584 | , eaql_args = inst_args
|
| ... | ... | @@ -636,7 +596,8 @@ tcValArg _ pos (fun, fun_lspan) (EValArgQL { |
| 636 | 596 | , text "app_lspan" <+> ppr lspan
|
| 637 | 597 | , text "head_lspan" <+> ppr fun_lspan
|
| 638 | 598 | , text "tc_head" <+> ppr tc_head])
|
| 639 | - ; ds_flag <- getDeepSubsumptionFlag_DataConHead (fst tc_head)
|
|
| 599 | + ; ds_flag <- getDeepSubsumptionFlag
|
|
| 600 | + -- NB: whether to do deep /skolemisation/ is independent of data constructors
|
|
| 640 | 601 | ; (wrap, arg')
|
| 641 | 602 | <- tcScalingUsage mult $
|
| 642 | 603 | tcSkolemise ds_flag GenSigCtxt exp_arg_ty $ \ exp_arg_rho ->
|
| ... | ... | @@ -656,7 +617,7 @@ tcValArg _ pos (fun, fun_lspan) (EValArgQL { |
| 656 | 617 | ; app_res_rho <- liftZonkM $ zonkTcType app_res_rho
|
| 657 | 618 | ; res_wrap <- checkResultTy rn_expr tc_head inst_args
|
| 658 | 619 | app_res_rho (mkCheckExpType exp_arg_rho)
|
| 659 | - ; finishApp tc_head tc_args app_res_rho res_wrap }
|
|
| 620 | + ; finishApp tc_fun tc_args app_res_rho res_wrap }
|
|
| 660 | 621 | |
| 661 | 622 | ; traceTc "tcEValArgQL }" $
|
| 662 | 623 | vcat [ text "app_res_rho:" <+> ppr app_res_rho ]
|
| ... | ... | @@ -690,26 +651,48 @@ tcInstFun :: QLFlag |
| 690 | 651 | -- always return a rho-type (but not a deep-rho type)
|
| 691 | 652 | -- Generally speaking we pass in True; in Fig 5 of the paper
|
| 692 | 653 | -- |-inst returns a rho-type
|
| 693 | - -> (CtOrigin, HsExpr GhcRn, SrcSpan)
|
|
| 654 | + -> (HsExpr GhcRn, SrcSpan)
|
|
| 694 | 655 | -> HsExpr GhcTc
|
| 695 | 656 | -> TcSigmaType -> [HsExprArg 'TcpRn]
|
| 696 | 657 | -> TcM ( [HsExprArg 'TcpInst]
|
| 697 | 658 | , TcSigmaType ) -- Does not instantiate trailing invisible foralls
|
| 698 | --- This crucial function implements the |-inst judgement in Fig 4, plus the
|
|
| 699 | --- modification in Fig 5, of the QL paper:
|
|
| 659 | +-- This crucial function implements the |-inst judgement in Fig 4,
|
|
| 660 | +-- plus the modification in Fig 5, of the QL paper:
|
|
| 700 | 661 | -- "A quick look at impredicativity" (ICFP'20).
|
| 701 | -tcInstFun do_ql inst_final (fun_orig, rn_fun, fun_lspan) tc_fun fun_sigma rn_args
|
|
| 702 | - = do { traceTc "tcInstFun" (vcat [ text "origin" <+> ppr fun_orig
|
|
| 703 | - , text "tc_fun" <+> ppr tc_fun
|
|
| 662 | +tcInstFun do_ql inst_final rn_head@(_, fun_lspan) tc_fun fun_sigma rn_args
|
|
| 663 | + = do { traceTc "tcInstFun" (vcat [ text "tc_fun" <+> ppr tc_fun
|
|
| 664 | + , text "rn_fun" <+> ppr rn_head
|
|
| 704 | 665 | , text "fun_sigma" <+> ppr fun_sigma
|
| 705 | 666 | , text "args:" <+> ppr rn_args
|
| 706 | - , text "do_ql" <+> ppr do_ql
|
|
| 707 | - , text "ctx" <+> ppr fun_lspan])
|
|
| 708 | - ; res@(_, fun_ty) <- go 1 [] fun_sigma rn_args
|
|
| 667 | + , text "do_ql" <+> ppr do_ql])
|
|
| 668 | + ; fun_origin <- mk_origin rn_head
|
|
| 669 | + ; res@(_, fun_ty) <- go fun_origin 1 [] fun_sigma rn_args
|
|
| 709 | 670 | ; traceTc "tcInstFun:ret" (ppr fun_ty)
|
| 710 | 671 | ; return res
|
| 711 | 672 | }
|
| 712 | 673 | where
|
| 674 | + -- What should be the origin for this function call?
|
|
| 675 | + -- If the head of the function is user written
|
|
| 676 | + -- then it can be used in the error message
|
|
| 677 | + -- If it is generated code location span, blame it on the
|
|
| 678 | + -- origin that can be retrived from the top of the error ctxt stack.
|
|
| 679 | + -- See Note [Error contexts in generated code]
|
|
| 680 | + mk_origin :: (HsExpr GhcRn, SrcSpan) -- The head of the application chain and its location
|
|
| 681 | + -> TcM CtOrigin
|
|
| 682 | + mk_origin (rn_fun, fun_lspan)
|
|
| 683 | + | not (isGeneratedSrcSpan fun_lspan)
|
|
| 684 | + = return $ exprCtOrigin rn_fun
|
|
| 685 | + |
|
| 686 | + | otherwise -- If the location is generated, the best we can do is to
|
|
| 687 | + -- approximate by looking on top of the error message stack
|
|
| 688 | + = do { err_ctxt_stack <- getErrCtxt
|
|
| 689 | + ; let hs_ctxt = case err_ctxt_stack of
|
|
| 690 | + (c:_) -> c
|
|
| 691 | + [] -> pprPanic "mk_origin" (ppr rn_fun)
|
|
| 692 | + ; traceTc "mk_origin" (pprHsCtxt hs_ctxt)
|
|
| 693 | + ; return $ hsCtxtCtOrigin hs_ctxt
|
|
| 694 | + }
|
|
| 695 | + |
|
| 713 | 696 | -- These are the type variables which must be instantiated to concrete
|
| 714 | 697 | -- types. See Note [Representation-polymorphic Ids with no binding]
|
| 715 | 698 | -- in GHC.Tc.Utils.Concrete
|
| ... | ... | @@ -741,34 +724,35 @@ tcInstFun do_ql inst_final (fun_orig, rn_fun, fun_lspan) tc_fun fun_sigma rn_arg |
| 741 | 724 | inst_fun _ = isInferredForAllTyFlag
|
| 742 | 725 | |
| 743 | 726 | -----------
|
| 744 | - go, go1 :: Int -- Value-argument position of next arg
|
|
| 727 | + go, go1 :: CtOrigin -- Of the function
|
|
| 728 | + -> Int -- Value-argument position of next arg
|
|
| 745 | 729 | -> [HsExprArg 'TcpInst] -- Accumulator, reversed
|
| 746 | 730 | -> TcSigmaType -> [HsExprArg 'TcpRn]
|
| 747 | 731 | -> TcM ([HsExprArg 'TcpInst], TcSigmaType)
|
| 748 | 732 | |
| 749 | 733 | -- go: If fun_ty=kappa, look it up in Theta
|
| 750 | - go pos acc fun_ty args
|
|
| 734 | + go fun_orig pos acc fun_ty args
|
|
| 751 | 735 | | Just kappa <- getTyVar_maybe fun_ty
|
| 752 | 736 | , isQLInstTyVar kappa
|
| 753 | 737 | = do { cts <- readMetaTyVar kappa
|
| 754 | 738 | ; case cts of
|
| 755 | - Indirect fun_ty' -> go pos acc fun_ty' args
|
|
| 756 | - Flexi -> go1 pos acc fun_ty args }
|
|
| 739 | + Indirect fun_ty' -> go fun_orig pos acc fun_ty' args
|
|
| 740 | + Flexi -> go1 fun_orig pos acc fun_ty args }
|
|
| 757 | 741 | | otherwise
|
| 758 | - = go1 pos acc fun_ty args
|
|
| 742 | + = go1 fun_orig pos acc fun_ty args
|
|
| 759 | 743 | |
| 760 | 744 | -- go1: fun_ty is not filled-in instantiation variable
|
| 761 | 745 | -- ('go' dealt with that case)
|
| 762 | 746 | |
| 763 | 747 | -- Handle out-of-scope functions gracefully
|
| 764 | - go1 pos acc fun_ty (arg : rest_args)
|
|
| 748 | + go1 fun_orig pos acc fun_ty (arg : rest_args)
|
|
| 765 | 749 | | fun_is_out_of_scope, looks_like_type_arg arg -- See Note [VTA for out-of-scope functions]
|
| 766 | - = go pos acc fun_ty rest_args
|
|
| 750 | + = go fun_orig pos acc fun_ty rest_args
|
|
| 767 | 751 | |
| 768 | 752 | -- Rule IALL from Fig 4 of the QL paper; applies even if args = []
|
| 769 | 753 | -- Instantiate invisible foralls and dictionaries.
|
| 770 | 754 | -- c.f. GHC.Tc.Utils.Instantiate.topInstantiate
|
| 771 | - go1 pos acc fun_ty args
|
|
| 755 | + go1 fun_orig pos acc fun_ty args
|
|
| 772 | 756 | | (tvs, body1) <- tcSplitSomeForAllTyVars (inst_fun args) fun_ty
|
| 773 | 757 | , (theta, body2) <- if inst_fun args Inferred
|
| 774 | 758 | then tcSplitPhiTy body1
|
| ... | ... | @@ -797,12 +781,12 @@ tcInstFun do_ql inst_final (fun_orig, rn_fun, fun_lspan) tc_fun fun_sigma rn_arg |
| 797 | 781 | -- argument of (#,#) to @LiftedRep, but want to rule out the
|
| 798 | 782 | -- second instantiation @r.
|
| 799 | 783 | |
| 800 | - ; go pos (addArgWrap wrap acc) fun_rho args }
|
|
| 784 | + ; go fun_orig pos (addArgWrap wrap acc) fun_rho args }
|
|
| 801 | 785 | -- Going around again means we deal easily with
|
| 802 | 786 | -- nested forall a. Eq a => forall b. Show b => blah
|
| 803 | 787 | |
| 804 | 788 | -- Rule IRESULT from Fig 4 of the QL paper; no more arguments
|
| 805 | - go1 _pos acc fun_ty []
|
|
| 789 | + go1 _fun_orig _pos acc fun_ty []
|
|
| 806 | 790 | | XExpr (ConLikeTc (RealDataCon dc)) <- tc_fun
|
| 807 | 791 | , isNewDataCon dc
|
| 808 | 792 | , [Scaled _ orig_arg_ty] <- dataConOrigArgTys dc
|
| ... | ... | @@ -822,30 +806,30 @@ tcInstFun do_ql inst_final (fun_orig, rn_fun, fun_lspan) tc_fun fun_sigma rn_arg |
| 822 | 806 | = return (reverse acc, fun_ty)
|
| 823 | 807 | |
| 824 | 808 | -- Rule ITVDQ from the GHC Proposal #281
|
| 825 | - go1 pos acc fun_ty ((EValArg { ea_arg = arg }) : rest_args)
|
|
| 809 | + go1 fun_orig pos acc fun_ty ((EValArg { ea_arg = arg }) : rest_args)
|
|
| 826 | 810 | | Just (tvb, body) <- tcSplitForAllTyVarBinder_maybe fun_ty
|
| 827 | 811 | = assertPpr (binderFlag tvb == Required) (ppr fun_ty $$ ppr arg) $
|
| 828 | 812 | -- Any invisible binders have been instantiated by IALL above,
|
| 829 | 813 | -- so this forall must be visible (i.e. Required)
|
| 830 | 814 | do { (ty_arg, inst_body) <- tcVDQ fun_conc_tvs (tvb, body) arg
|
| 831 | 815 | ; let wrap = mkWpTyApps [ty_arg]
|
| 832 | - ; go (pos+1) (addArgWrap wrap acc) inst_body rest_args }
|
|
| 816 | + ; go fun_orig (pos+1) (addArgWrap wrap acc) inst_body rest_args }
|
|
| 833 | 817 | |
| 834 | - go1 pos acc fun_ty (EWrap w : args)
|
|
| 835 | - = go1 pos (EWrap w : acc) fun_ty args
|
|
| 818 | + go1 fun_orig pos acc fun_ty (EWrap w : args)
|
|
| 819 | + = go1 fun_orig pos (EWrap w : acc) fun_ty args
|
|
| 836 | 820 | |
| 837 | - go1 pos acc fun_ty (EPrag sp prag : args)
|
|
| 838 | - = go1 pos (EPrag sp prag : acc) fun_ty args
|
|
| 821 | + go1 fun_orig pos acc fun_ty (EPrag sp prag : args)
|
|
| 822 | + = go1 fun_orig pos (EPrag sp prag : acc) fun_ty args
|
|
| 839 | 823 | |
| 840 | 824 | -- Rule ITYARG from Fig 4 of the QL paper
|
| 841 | - go1 pos acc fun_ty ( ETypeArg { ea_loc_span = ctxt, ea_hs_ty = hs_ty }
|
|
| 842 | - : rest_args )
|
|
| 825 | + go1 fun_orig pos acc fun_ty ( ETypeArg { ea_loc_span = ctxt, ea_hs_ty = hs_ty }
|
|
| 826 | + : rest_args )
|
|
| 843 | 827 | = do { (ty_arg, inst_ty) <- tcVTA fun_conc_tvs fun_ty hs_ty
|
| 844 | 828 | ; let arg' = ETypeArg { ea_loc_span = ctxt, ea_hs_ty = hs_ty, ea_ty_arg = ty_arg }
|
| 845 | - ; go pos (arg' : acc) inst_ty rest_args }
|
|
| 829 | + ; go fun_orig pos (arg' : acc) inst_ty rest_args }
|
|
| 846 | 830 | |
| 847 | 831 | -- Rule IVAR from Fig 4 of the QL paper:
|
| 848 | - go1 pos acc fun_ty args@(EValArg {} : _)
|
|
| 832 | + go1 fun_orig pos acc fun_ty args@(EValArg {} : _)
|
|
| 849 | 833 | | Just kappa <- getTyVar_maybe fun_ty
|
| 850 | 834 | , isQLInstTyVar kappa
|
| 851 | 835 | = -- Function type was of form f :: forall a b. t1 -> t2 -> b
|
| ... | ... | @@ -861,7 +845,7 @@ tcInstFun do_ql inst_final (fun_orig, rn_fun, fun_lspan) tc_fun fun_sigma rn_arg |
| 861 | 845 | -- - We must be sure to actually update the variable right now,
|
| 862 | 846 | -- not defer in any way, because this is a QL instantiation variable.
|
| 863 | 847 | -- It's easier just to do the job directly here.
|
| 864 | - do { arg_tys <- zipWithM new_arg_ty (leadingValArgs args) [pos..]
|
|
| 848 | + do { arg_tys <- zipWithM (new_arg_ty fun_orig) (leadingValArgs args) [pos..]
|
|
| 865 | 849 | ; res_ty <- newOpenFlexiTyVarTyQL do_ql TauTv
|
| 866 | 850 | ; let fun_ty' = mkScaledFunTys arg_tys res_ty
|
| 867 | 851 | |
| ... | ... | @@ -877,12 +861,12 @@ tcInstFun do_ql inst_final (fun_orig, rn_fun, fun_lspan) tc_fun fun_sigma rn_arg |
| 877 | 861 | -- Then fun_ty :: kk, fun_ty' :: Type, kind_co :: Type ~ kk
|
| 878 | 862 | -- co_wrap :: (fun_ty' |> kind_co) ~ fun_ty'
|
| 879 | 863 | |
| 880 | - ; go pos acc' fun_ty' args }
|
|
| 864 | + ; go fun_orig pos acc' fun_ty' args }
|
|
| 881 | 865 | |
| 882 | 866 | -- Rule IARG from Fig 4 of the QL paper:
|
| 883 | - go1 pos acc fun_ty
|
|
| 867 | + go1 fun_orig pos acc fun_ty
|
|
| 884 | 868 | (EValArg { ea_arg = arg, ea_loc_span = ctxt } : rest_args)
|
| 885 | - = do { let herald = mk_herald tc_fun (unLoc arg)
|
|
| 869 | + = do { let herald = mk_herald fun_orig tc_fun (unLoc arg)
|
|
| 886 | 870 | ; (fun_co, arg_ty, res_ty) <-
|
| 887 | 871 | -- NB: matchActualFunTy does the rep-poly check.
|
| 888 | 872 | -- For example, suppose we have f :: forall r (a::TYPE r). a -> Int
|
| ... | ... | @@ -894,16 +878,15 @@ tcInstFun do_ql inst_final (fun_orig, rn_fun, fun_lspan) tc_fun fun_sigma rn_arg |
| 894 | 878 | matchActualFunTy herald
|
| 895 | 879 | (Just $ HsExprTcThing tc_fun)
|
| 896 | 880 | (n_val_args, fun_sigma) fun_ty
|
| 897 | - ; ds_flag <- getDeepSubsumptionFlag_DataConHead tc_fun
|
|
| 898 | - ; arg' <- quickLookArg ds_flag do_ql pos ctxt (rn_fun, fun_lspan) arg arg_ty
|
|
| 881 | + ; arg' <- quickLookArg do_ql pos ctxt rn_head arg arg_ty
|
|
| 899 | 882 | ; let acc' = arg' : addArgWrap (mkWpCastN fun_co) acc
|
| 900 | - ; go (pos+1) acc' res_ty rest_args }
|
|
| 883 | + ; go fun_orig (pos+1) acc' res_ty rest_args }
|
|
| 901 | 884 | |
| 902 | - new_arg_ty :: LHsExpr GhcRn -> Int -> TcM (Scaled TcType)
|
|
| 885 | + new_arg_ty :: CtOrigin -> LHsExpr GhcRn -> Int -> TcM (Scaled TcType)
|
|
| 903 | 886 | -- Make a fresh nus for each argument in rule IVAR
|
| 904 | - new_arg_ty (L _ arg) i
|
|
| 887 | + new_arg_ty fun_orig (L _ arg) i
|
|
| 905 | 888 | = do { arg_nu <- newArgTyVarTyQL do_ql $
|
| 906 | - FRRExpectedFunTy (mk_herald tc_fun arg) i
|
|
| 889 | + FRRExpectedFunTy (mk_herald fun_orig tc_fun arg) i
|
|
| 907 | 890 | -- Following matchActualFunTy, we create nu_i :: TYPE kappa_i[conc],
|
| 908 | 891 | -- thereby ensuring that the arguments have concrete runtime representations
|
| 909 | 892 | |
| ... | ... | @@ -913,12 +896,13 @@ tcInstFun do_ql inst_final (fun_orig, rn_fun, fun_lspan) tc_fun fun_sigma rn_arg |
| 913 | 896 | |
| 914 | 897 | ; return (mkScaled mult_ty arg_nu) }
|
| 915 | 898 | |
| 916 | - mk_herald :: HsExpr GhcTc -> HsExpr GhcRn -> ExpectedFunTyCtxt
|
|
| 917 | - mk_herald tc_fun arg
|
|
| 899 | + mk_herald :: CtOrigin -> HsExpr GhcTc -> HsExpr GhcRn -> ExpectedFunTyCtxt
|
|
| 900 | + mk_herald fun_orig tc_fun arg
|
|
| 918 | 901 | = case fun_orig of
|
| 919 | 902 | DoStmtOrigin -> ExpectedFunTySyntaxOp DoStmtOrigin tc_fun
|
| 920 | 903 | _ -> ExpectedFunTyArg (HsExprTcThing tc_fun) arg
|
| 921 | 904 | |
| 905 | + |
|
| 922 | 906 | -- Is the argument supposed to instantiate a forall?
|
| 923 | 907 | --
|
| 924 | 908 | -- In other words, given a function application `fn arg`,
|
| ... | ... | @@ -1883,23 +1867,23 @@ This turned out to be more subtle than I expected. Wrinkles: |
| 1883 | 1867 | |
| 1884 | 1868 | -}
|
| 1885 | 1869 | |
| 1886 | -quickLookArg :: DeepSubsumptionFlag -> QLFlag -> Int
|
|
| 1887 | - -> SrcSpan -- ^ location span of the whole application
|
|
| 1870 | +quickLookArg :: QLFlag -> Int
|
|
| 1871 | + -> HsExprLoc -- ^ location span of the whole application
|
|
| 1888 | 1872 | -> (HsExpr GhcRn, SrcSpan) -- ^ Head of the application chain and its source span
|
| 1889 | 1873 | -> LHsExpr GhcRn -- ^ Argument
|
| 1890 | 1874 | -> Scaled TcSigmaTypeFRR -- ^ Type expected by the function
|
| 1891 | 1875 | -> TcM (HsExprArg 'TcpInst)
|
| 1892 | 1876 | -- See Note [Quick Look at value arguments]
|
| 1893 | -quickLookArg _ NoQL _ app_lspan _ larg orig_arg_ty
|
|
| 1877 | +quickLookArg NoQL _ app_lspan _ larg orig_arg_ty
|
|
| 1894 | 1878 | = skipQuickLook app_lspan larg orig_arg_ty
|
| 1895 | -quickLookArg ds_flag DoQL pos app_lspan fun_and_lspan larg orig_arg_ty
|
|
| 1896 | - = do { is_rho <- tcIsDeepRho ds_flag (scaledThing orig_arg_ty)
|
|
| 1879 | +quickLookArg DoQL pos app_lspan fun_and_lspan larg orig_arg_ty
|
|
| 1880 | + = do { is_rho <- qlArgHasRhoType (scaledThing orig_arg_ty)
|
|
| 1897 | 1881 | ; traceTc "qla" (ppr orig_arg_ty $$ ppr is_rho)
|
| 1898 | 1882 | ; if not is_rho
|
| 1899 | 1883 | then skipQuickLook app_lspan larg orig_arg_ty
|
| 1900 | 1884 | else quickLookArg1 pos app_lspan fun_and_lspan larg orig_arg_ty }
|
| 1901 | 1885 | |
| 1902 | -skipQuickLook :: SrcSpan -> LHsExpr GhcRn -> Scaled TcRhoType
|
|
| 1886 | +skipQuickLook :: HsExprLoc -> LHsExpr GhcRn -> Scaled TcRhoType
|
|
| 1903 | 1887 | -> TcM (HsExprArg 'TcpInst)
|
| 1904 | 1888 | skipQuickLook app_lspan larg arg_ty
|
| 1905 | 1889 | = return (EValArg { ea_loc_span = app_lspan
|
| ... | ... | @@ -1910,13 +1894,26 @@ whenQL :: QLFlag -> ZonkM () -> TcM () |
| 1910 | 1894 | whenQL DoQL thing_inside = liftZonkM thing_inside
|
| 1911 | 1895 | whenQL NoQL _ = return ()
|
| 1912 | 1896 | |
| 1913 | -tcIsDeepRho :: DeepSubsumptionFlag -> TcType -> TcM Bool
|
|
| 1914 | --- This top-level zonk step, which is the reason we need a local 'go' loop,
|
|
| 1915 | --- is subtle. See Section 9 of the QL paper
|
|
| 1897 | +qlArgHasRhoType :: TcType -> TcM Bool
|
|
| 1898 | +-- `qlArgHasRhoType` checks that the expected argument type in rule
|
|
| 1899 | +-- App-lightning-bolt (Fig 5 in the paper) is indeed a rho-type.
|
|
| 1900 | +--
|
|
| 1901 | +-- It must apply the current QL substitution, so it any QLInstTyVar that it
|
|
| 1902 | +-- comes across. Why? See Section 5.7 in the paper; argument order matters.
|
|
| 1903 | +--
|
|
| 1904 | +-- What if we find an /un-filled/ QLInstVar? We treat this as a rho-type
|
|
| 1905 | +-- even though a later argument might force it to be sigma-type. See
|
|
| 1906 | +-- Section 9 in the paper.
|
|
| 1907 | +--
|
|
| 1908 | +-- With -XDeepSubsunption we need a /deep/ rho-type.
|
|
| 1909 | +-- (We don't need getDeepSubsumptionFlag_DataConHead here because this
|
|
| 1910 | +-- is only about QuickLook.)
|
|
| 1916 | 1911 | |
| 1917 | -tcIsDeepRho ds_flag = go
|
|
| 1912 | +qlArgHasRhoType ty
|
|
| 1913 | + = do { ds_flag <- getDeepSubsumptionFlag
|
|
| 1914 | + ; go ds_flag ty }
|
|
| 1918 | 1915 | where
|
| 1919 | - go ty
|
|
| 1916 | + go ds_flag ty
|
|
| 1920 | 1917 | | isSigmaTy ty
|
| 1921 | 1918 | = return False
|
| 1922 | 1919 | |
| ... | ... | @@ -1924,12 +1921,12 @@ tcIsDeepRho ds_flag = go |
| 1924 | 1921 | , isQLInstTyVar kappa
|
| 1925 | 1922 | = do { info <- readMetaTyVar kappa
|
| 1926 | 1923 | ; case info of
|
| 1927 | - Indirect arg_ty' -> go arg_ty'
|
|
| 1924 | + Indirect arg_ty' -> go ds_flag arg_ty'
|
|
| 1928 | 1925 | Flexi -> return True }
|
| 1929 | 1926 | |
| 1930 | 1927 | | Deep {} <- ds_flag
|
| 1931 | 1928 | , Just (_, res_ty) <- tcSplitFunTy_maybe ty
|
| 1932 | - = go res_ty
|
|
| 1929 | + = go ds_flag res_ty
|
|
| 1933 | 1930 | |
| 1934 | 1931 | | otherwise
|
| 1935 | 1932 | = return True
|
| ... | ... | @@ -1940,14 +1937,20 @@ isGuardedTy ty |
| 1940 | 1937 | | Just {} <- tcSplitAppTy_maybe ty = True
|
| 1941 | 1938 | | otherwise = False
|
| 1942 | 1939 | |
| 1943 | -quickLookArg1 :: Int -> SrcSpan -> (HsExpr GhcRn, SrcSpan) -> LHsExpr GhcRn
|
|
| 1940 | +quickLookArg1 :: Int -> HsExprLoc -> (HsExpr GhcRn, SrcSpan) -> LHsExpr GhcRn
|
|
| 1944 | 1941 | -> Scaled TcRhoType -- Deeply skolemised
|
| 1945 | 1942 | -> TcM (HsExprArg 'TcpInst)
|
| 1946 | 1943 | -- quickLookArg1 implements the "QL Argument" judgement in Fig 5 of the paper
|
| 1947 | 1944 | quickLookArg1 pos app_lspan (fun, fun_lspan) larg@(L _ arg) sc_arg_ty@(Scaled _ orig_arg_rho)
|
| 1948 | 1945 | = addArgCtxt pos (fun, fun_lspan) larg $ -- Context needed for constraints
|
| 1949 | - -- generated by calls in arg
|
|
| 1950 | - do { ((rn_fun_arg, fun_lspan_arg), rn_args) <- splitHsApps arg
|
|
| 1946 | + -- generated by calls in arg
|
|
| 1947 | + do { traceTc "qla1" (ppr arg)
|
|
| 1948 | + |
|
| 1949 | + ; (rn_fun_arg, rn_args) <- splitHsApps arg
|
|
| 1950 | + |
|
| 1951 | + ; traceTc "qla2" (ppr arg)
|
|
| 1952 | + |
|
| 1953 | + ; fun_lspan_arg <- getFunSrcSpan rn_args
|
|
| 1951 | 1954 | |
| 1952 | 1955 | -- Step 1: get the type of the head of the argument
|
| 1953 | 1956 | ; (fun_ue, mb_fun_ty) <- tcCollectingUsage $ tcInferAppHead_maybe rn_fun_arg
|
| ... | ... | @@ -1970,17 +1973,15 @@ quickLookArg1 pos app_lspan (fun, fun_lspan) larg@(L _ arg) sc_arg_ty@(Scaled _ |
| 1970 | 1973 | do { let arg_tc_head = (tc_fun_arg_head, fun_lspan_arg)
|
| 1971 | 1974 | ; do_ql <- wantQuickLook rn_fun_arg
|
| 1972 | 1975 | |
| 1973 | - ; arg_orig <- mk_origin fun_lspan_arg rn_fun_arg
|
|
| 1974 | 1976 | ; ((inst_args, app_res_rho), wanted)
|
| 1975 | 1977 | <- captureConstraints $
|
| 1976 | - tcInstFun do_ql True (arg_orig, rn_fun_arg, fun_lspan_arg) tc_fun_arg_head fun_sigma_arg_head rn_args
|
|
| 1978 | + tcInstFun do_ql True (rn_fun_arg, fun_lspan_arg) tc_fun_arg_head fun_sigma_arg_head rn_args
|
|
| 1977 | 1979 | -- We must capture type-class and equality constraints here, but
|
| 1978 | 1980 | -- not usage information. See (QLA6) in Note [Quick Look at
|
| 1979 | 1981 | -- value arguments]
|
| 1980 | 1982 | |
| 1981 | 1983 | ; traceTc "quickLookArg 2" $
|
| 1982 | 1984 | vcat [ text "arg:" <+> ppr arg
|
| 1983 | - , text "orig:" <+> ppr arg_orig
|
|
| 1984 | 1985 | , text "orig_arg_rho:" <+> ppr orig_arg_rho
|
| 1985 | 1986 | , text "app_res_rho:" <+> ppr app_res_rho ]
|
| 1986 | 1987 | |
| ... | ... | @@ -2018,24 +2019,6 @@ quickLookArg1 pos app_lspan (fun, fun_lspan) larg@(L _ arg) sc_arg_ty@(Scaled _ |
| 2018 | 2019 | , eaql_res_rho = app_res_rho }) }}}
|
| 2019 | 2020 | |
| 2020 | 2021 | |
| 2021 | -mk_origin :: SrcSpan -- SrcSpan of the function
|
|
| 2022 | - -> HsExpr GhcRn -- The head of the expression application chain
|
|
| 2023 | - -> TcM CtOrigin
|
|
| 2024 | -mk_origin fun_lspan rn_fun
|
|
| 2025 | - | not (isGeneratedSrcSpan fun_lspan)
|
|
| 2026 | - = return $ exprCtOrigin rn_fun
|
|
| 2027 | - |
|
| 2028 | - | otherwise -- If the location is generated, the best we can do is to
|
|
| 2029 | - -- approximate by looking on top of the error message stack
|
|
| 2030 | - = do { err_ctxt_stack <- getErrCtxt
|
|
| 2031 | - ; let hs_ctxt = case err_ctxt_stack of
|
|
| 2032 | - (c:_) -> c
|
|
| 2033 | - [] -> pprPanic "mk_origin" (ppr rn_fun)
|
|
| 2034 | - ; traceTc "mk_origin" (pprHsCtxt hs_ctxt)
|
|
| 2035 | - ; return $ hsCtxtCtOrigin hs_ctxt
|
|
| 2036 | - }
|
|
| 2037 | - |
|
| 2038 | - |
|
| 2039 | 2022 | {- *********************************************************************
|
| 2040 | 2023 | * *
|
| 2041 | 2024 | Folding over instantiation variables
|
| ... | ... | @@ -2437,12 +2420,11 @@ isTagToEnum :: HsExpr GhcTc -> Bool |
| 2437 | 2420 | isTagToEnum (HsVar _ (L _ fun_id)) = fun_id `hasKey` tagToEnumKey
|
| 2438 | 2421 | isTagToEnum _ = False
|
| 2439 | 2422 | |
| 2440 | -tcTagToEnum :: (HsExpr GhcTc, SrcSpan) -> [HsExprArg 'TcpTc]
|
|
| 2441 | - -> TcRhoType
|
|
| 2423 | +tcTagToEnum :: HsExpr GhcTc -> [HsExprArg 'TcpTc] -> TcRhoType
|
|
| 2442 | 2424 | -> TcM (HsExpr GhcTc)
|
| 2443 | 2425 | -- tagToEnum# :: forall a. Int# -> a
|
| 2444 | 2426 | -- See Note [tagToEnum#] Urgh!
|
| 2445 | -tcTagToEnum (tc_fun, fun_lspan) tc_args res_ty
|
|
| 2427 | +tcTagToEnum tc_fun tc_args res_ty
|
|
| 2446 | 2428 | | [val_arg] <- dropWhile (not . isHsValArg) tc_args
|
| 2447 | 2429 | = do { res_ty <- liftZonkM $ zonkTcType res_ty
|
| 2448 | 2430 | |
| ... | ... | @@ -2464,14 +2446,14 @@ tcTagToEnum (tc_fun, fun_lspan) tc_args res_ty |
| 2464 | 2446 | ; let rep_ty = mkTyConApp rep_tc rep_args
|
| 2465 | 2447 | tc_fun' = mkHsWrap (WpTyApp rep_ty) tc_fun
|
| 2466 | 2448 | df_wrap = mkWpCastR (mkSymCo coi)
|
| 2467 | - tc_expr = rebuildHsApps (tc_fun', fun_lspan) [val_arg]
|
|
| 2449 | + tc_expr = rebuildHsApps tc_fun' [val_arg]
|
|
| 2468 | 2450 | ; return (mkHsWrap df_wrap tc_expr) }}}}}
|
| 2469 | 2451 | |
| 2470 | 2452 | | otherwise
|
| 2471 | 2453 | = failWithTc TcRnTagToEnumMissingValArg
|
| 2472 | 2454 | |
| 2473 | 2455 | where
|
| 2474 | - vanilla_result = return (rebuildHsApps (tc_fun, fun_lspan) tc_args)
|
|
| 2456 | + vanilla_result = return (rebuildHsApps tc_fun tc_args)
|
|
| 2475 | 2457 | |
| 2476 | 2458 | check_enumeration ty' tc
|
| 2477 | 2459 | | -- isTypeDataTyCon: see wrinkle (W1) in
|
| 1 | -module GHC.Tc.Gen.App where
|
|
| 2 | - |
|
| 3 | -import GHC.Hs ( HsExpr )
|
|
| 4 | -import GHC.Tc.Types ( TcM )
|
|
| 5 | -import GHC.Tc.Types.Origin ( CtOrigin )
|
|
| 6 | -import GHC.Tc.Utils.TcType ( TcSigmaType )
|
|
| 7 | -import GHC.Hs.Extension ( GhcRn, GhcTc )
|
|
| 8 | - |
|
| 9 | - |
|
| 10 | -import GHC.Prelude (Bool)
|
|
| 11 | - |
|
| 12 | -tcExprSigma :: Bool -> CtOrigin -> HsExpr GhcRn -> TcM (HsExpr GhcTc, TcSigmaType) |
| 1 | +{-# LANGUAGE TypeFamilies #-}
|
|
| 2 | + |
|
| 3 | +{-
|
|
| 4 | +(c) The University of Glasgow 2006
|
|
| 5 | +(c) The GRASP/AQUA Project, Glasgow University, 1992-1998
|
|
| 6 | +-}
|
|
| 7 | + |
|
| 8 | +module GHC.Tc.Gen.Expand( tcExpand ) where
|
|
| 9 | + |
|
| 10 | +import GHC.Prelude
|
|
| 11 | + |
|
| 12 | +import {-# SOURCE #-} GHC.Tc.Gen.Splice( getUntypedSpliceBody )
|
|
| 13 | + |
|
| 14 | +import GHC.Hs
|
|
| 15 | + |
|
| 16 | +import GHC.Tc.Utils.Monad
|
|
| 17 | +import GHC.Tc.Types.ErrCtxt
|
|
| 18 | + |
|
| 19 | +import GHC.Rename.Utils
|
|
| 20 | + |
|
| 21 | +{- Note [Typechecking by expansion: overview]
|
|
| 22 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 23 | +For many constructs, rather than typechecking the user-written code
|
|
| 24 | +directly, it's much easier to
|
|
| 25 | + * Expand (or desugar) the code to something simpler
|
|
| 26 | + * Typecheck that simpler expression
|
|
| 27 | + |
|
| 28 | +Example: Typechecking the do expression. The typechecker looks (somewhat) like this:
|
|
| 29 | + |
|
| 30 | + tcExpr e@(HsDo _ stmts) rho = do { hse <- expandDoStmts stmts
|
|
| 31 | + ; tcHsExpansion hse rho }
|
|
| 32 | + |
|
| 33 | +The `expandDoStmts` replaces the HsDo { x <- e1; return x }
|
|
| 34 | +with something like
|
|
| 35 | + HSE { hse_ctxt = ExprCtxt e
|
|
| 36 | + , hse_exp = e1 >>= \ x -> x }
|
|
| 37 | +and we then typecheck the expression `e1 >>= \ x -> x`
|
|
| 38 | + |
|
| 39 | +See also Note [Handling overloaded and rebindable constructs]
|
|
| 40 | + and Note [Doing XXExprGhcRn in the Renamer vs Typechecker]
|
|
| 41 | + |
|
| 42 | +The Big Question is how to ensure that error messages mention
|
|
| 43 | +only user-written source code, and never talk about the expanded code.
|
|
| 44 | +The rest of this Note explains how that is done.
|
|
| 45 | + |
|
| 46 | +* The expansion process typically takes a user written thing
|
|
| 47 | + L lspan ue
|
|
| 48 | + and returns
|
|
| 49 | + L lspan (XExpr (ExpandedThingRn (HSE { hse_ctxt = ue
|
|
| 50 | + , hse_exp = ee } ))
|
|
| 51 | + where `ee` is the expansion of the user written thing `ue`
|
|
| 52 | + |
|
| 53 | +* The type checker context has 3 key fields that describe the context:
|
|
| 54 | + TcLclCtxt { tcl_loc :: RealSrcSpan
|
|
| 55 | + , tcl_in_gen_code :: Bool
|
|
| 56 | + , tcl_err_ctxt :: ErrCtxtStack
|
|
| 57 | + , ... }
|
|
| 58 | + Note `tcl_loc` always points to a real place in the source code,
|
|
| 59 | + hence `RealSrcSpan`.
|
|
| 60 | + |
|
| 61 | + The `tcl_err_ctxt` is a stack of contexts, each saying something
|
|
| 62 | + like "In the expression: x+y" or "In second argument of `$` namely 'r { x=2 }'"
|
|
| 63 | + |
|
| 64 | + The `tcl_in_gen_code` is a boolean that keeps track of whether
|
|
| 65 | + the current expression being typechecked is compiler generated
|
|
| 66 | + or user generated.
|
|
| 67 | + |
|
| 68 | + INVARIANT: `tcl_loc` and `tcl_in_gen_code` are modified only in `setSrcSpan`.
|
|
| 69 | + |
|
| 70 | +* Now, when
|
|
| 71 | + tcMonoLExpr :: LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
|
|
| 72 | + gets a located expression, it does 3 things:
|
|
| 73 | + (a) Calls `setSrcSpanA` to set the ambient source-code location
|
|
| 74 | + (b) Calls `addExprCtxt` to push a suitable `HsCtxt` on top of the `tcl_err_ctxt`.
|
|
| 75 | + (c) Calls `tcExpr` to typecheck the expression.
|
|
| 76 | + |
|
| 77 | +* In these calls, if the `span` is generated (see `isGeneratedSrcSpan`), then
|
|
| 78 | + - `setSrcSpanA` sets `tcl_in_gen_code` to `True`, and leaves `tcl_loc` unchanged
|
|
| 79 | + - `addExprCtxt` is a no-op if `tcl_in_gen_code` is True
|
|
| 80 | + The result is that `tcl_loc` has the span from the innermost /user/ tree node;
|
|
| 81 | + and the ErrCtxtStack in `tcl_err_ctxt` only has contexts arisign from user code.
|
|
| 82 | + |
|
| 83 | +* Note that inside an expansion we have sub-expressions from the original program.
|
|
| 84 | + As soon as we enter one of those, identified by a /user/ span, `setSrcSpanA` will
|
|
| 85 | + sets the `tcl_loc` to reflect that span, and switch off `tcl_in_gen_code`. Nice!
|
|
| 86 | +-}
|
|
| 87 | + |
|
| 88 | +---------------
|
|
| 89 | +tcExpand :: HsExpr GhcRn -> TcM (Maybe (HsExpansion GhcRn))
|
|
| 90 | +tcExpand e@(OpApp _ arg1 op arg2)
|
|
| 91 | + = return $ Just $
|
|
| 92 | + HSE { hse_ctxt = ExprCtxt e
|
|
| 93 | + , hse_exp = foldl ap op [arg1,arg2] }
|
|
| 94 | + where
|
|
| 95 | + ap f a = wrapGenSpan (HsApp noExtField f a)
|
|
| 96 | + |
|
| 97 | +tcExpand (XExpr (ExpandedThingRn hse))
|
|
| 98 | + = return (Just hse)
|
|
| 99 | + |
|
| 100 | +tcExpand e@(HsUntypedSplice splice_res _)
|
|
| 101 | +-- See Note [Looking through Template Haskell splices in splitHsApps]
|
|
| 102 | + = do { fun <- getUntypedSpliceBody splice_res
|
|
| 103 | + ; return $ Just $
|
|
| 104 | + HSE { hse_ctxt = ExprCtxt e
|
|
| 105 | + , hse_exp = wrapGenSpan fun } }
|
|
| 106 | + |
|
| 107 | +tcExpand _ = return Nothing |
| ... | ... | @@ -13,7 +13,7 @@ |
| 13 | 13 | module GHC.Tc.Gen.Expr
|
| 14 | 14 | ( tcCheckPolyExpr, tcCheckPolyExprNC,
|
| 15 | 15 | tcCheckMonoExpr, tcCheckMonoExprNC,
|
| 16 | - tcInferExpr, tcInferSigma,
|
|
| 16 | + tcInferExpr, tcInferSigma, tcInferExprSigma,
|
|
| 17 | 17 | tcInferRho, tcInferRhoNC,
|
| 18 | 18 | tcMonoLExpr, tcMonoLExprNC,
|
| 19 | 19 | tcInferRhoFRR, tcInferRhoFRRNC,
|
| ... | ... | @@ -30,10 +30,10 @@ import {-# SOURCE #-} GHC.Tc.Gen.Splice |
| 30 | 30 | |
| 31 | 31 | import GHC.Hs
|
| 32 | 32 | import GHC.Hs.Syn.Type
|
| 33 | - |
|
| 34 | 33 | import GHC.Rename.Utils
|
| 35 | 34 | import GHC.Rename.Env ( addUsedGRE, getUpdFieldLbls )
|
| 36 | 35 | |
| 36 | +import GHC.Tc.Gen.Expand( tcExpand )
|
|
| 37 | 37 | import GHC.Tc.Gen.App
|
| 38 | 38 | import GHC.Tc.Gen.Head
|
| 39 | 39 | import GHC.Tc.Gen.Do
|
| ... | ... | @@ -237,6 +237,9 @@ tcPolyExprCheck expr res_ty |
| 237 | 237 | tcInferSigma :: LHsExpr GhcRn -> TcM (LHsExpr GhcTc, TcSigmaType)
|
| 238 | 238 | tcInferSigma = tcInferExpr IIF_Sigma
|
| 239 | 239 | |
| 240 | +tcInferExprSigma :: HsExpr GhcRn -> TcM (HsExpr GhcTc, TcSigmaType)
|
|
| 241 | +tcInferExprSigma e = runInfer IIF_Sigma IFRR_Any (tcExpr e)
|
|
| 242 | + |
|
| 240 | 243 | tcInferRho, tcInferRhoNC :: LHsExpr GhcRn -> TcM (LHsExpr GhcTc, TcRhoType)
|
| 241 | 244 | -- Infer a *rho*-type. The return type is always instantiated.
|
| 242 | 245 | tcInferRho = tcInferExpr IIF_DeepRho
|
| ... | ... | @@ -291,6 +294,12 @@ tcMonoLExprNC (L loc expr) res_ty |
| 291 | 294 | do { expr' <- tcExpr expr res_ty
|
| 292 | 295 | ; return (L loc expr') }
|
| 293 | 296 | |
| 297 | +---------------
|
|
| 298 | +tcCollectApp :: HsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
|
|
| 299 | +tcCollectApp the_app res_ty
|
|
| 300 | + = do { (fun, args) <- splitHsApps the_app
|
|
| 301 | + ; tcApp the_app fun args res_ty }
|
|
| 302 | + |
|
| 294 | 303 | ---------------
|
| 295 | 304 | tcExpr :: HsExpr GhcRn
|
| 296 | 305 | -> ExpRhoType -- DeepSubsumption <=> when checking, this type
|
| ... | ... | @@ -312,19 +321,11 @@ tcExpr :: HsExpr GhcRn |
| 312 | 321 | -- - ones taken apart by GHC.Tc.Gen.Head.splitHsApps
|
| 313 | 322 | -- - ones understood by GHC.Tc.Gen.Head.tcInferAppHead_maybe
|
| 314 | 323 | -- See Note [Application chains and heads] in GHC.Tc.Gen.App
|
| 315 | -tcExpr e@(HsVar {}) res_ty = tcApp e res_ty
|
|
| 316 | -tcExpr e@(HsApp {}) res_ty = tcApp e res_ty
|
|
| 317 | -tcExpr e@(OpApp {}) res_ty = tcApp e res_ty
|
|
| 318 | -tcExpr e@(HsAppType {}) res_ty = tcApp e res_ty
|
|
| 319 | -tcExpr e@(ExprWithTySig {}) res_ty = tcApp e res_ty
|
|
| 320 | -tcExpr e@(XExpr (HsRecSelRn{})) res_ty = tcApp e res_ty
|
|
| 321 | - |
|
| 322 | --- Renamer expanded expressions (eg. Right/Left sections)
|
|
| 323 | --- or tcExpr expanded expressions (eg. Do statements and Record updates)
|
|
| 324 | --- are type checked using tcHsExpansion.
|
|
| 325 | --- See Note [Typechecking by expansion: overview]
|
|
| 326 | -tcExpr (XExpr (ExpandedThingRn hse)) res_ty = tcHsExpansion hse res_ty
|
|
| 327 | - |
|
| 324 | +tcExpr e@(HsVar {}) res_ty = tcApp e e [] res_ty
|
|
| 325 | +tcExpr e@(ExprWithTySig {}) res_ty = tcApp e e [] res_ty
|
|
| 326 | +tcExpr e@(XExpr (HsRecSelRn{})) res_ty = tcApp e e [] res_ty
|
|
| 327 | +tcExpr e@(HsAppType {}) res_ty = tcCollectApp e res_ty
|
|
| 328 | +tcExpr e@(HsApp {}) res_ty = tcCollectApp e res_ty
|
|
| 328 | 329 | |
| 329 | 330 | -- Typecheck an occurrence of an unbound Id
|
| 330 | 331 | --
|
| ... | ... | @@ -392,7 +393,7 @@ tcExpr e@(HsOverLit _ lit) res_ty |
| 392 | 393 | -- See Note [Short cut for overloaded literals] in GHC.Tc.Utils.TcMType
|
| 393 | 394 | ; case mb_res of
|
| 394 | 395 | Just lit' -> return (HsOverLit noExtField lit')
|
| 395 | - Nothing -> tcApp e res_ty }
|
|
| 396 | + Nothing -> tcApp e e [] res_ty }
|
|
| 396 | 397 | -- Why go via tcApp? See Note [Typechecking overloaded literals]
|
| 397 | 398 | |
| 398 | 399 | {- Note [Typechecking overloaded literals]
|
| ... | ... | @@ -530,8 +531,9 @@ tcExpr (HsCase ctxt scrut matches) res_ty |
| 530 | 531 | |
| 531 | 532 | tcExpr (HsIf x pred b1 b2) res_ty
|
| 532 | 533 | = do { pred' <- tcCheckMonoExpr pred boolTy
|
| 533 | - ; (u1,b1') <- tcCollectingUsage $ tcMonoLExpr b1 res_ty
|
|
| 534 | - ; (u2,b2') <- tcCollectingUsage $ tcMonoLExpr b2 res_ty
|
|
| 534 | + ; let res_ty' = adjustExpTypeForCaseBranches res_ty [b1,b2]
|
|
| 535 | + ; (u1,b1') <- tcCollectingUsage $ tcMonoLExpr b1 res_ty'
|
|
| 536 | + ; (u2,b2') <- tcCollectingUsage $ tcMonoLExpr b2 res_ty'
|
|
| 535 | 537 | ; tcEmitBindingUsage (supUE u1 u2)
|
| 536 | 538 | ; return (HsIf x pred' b1' b2') }
|
| 537 | 539 | |
| ... | ... | @@ -730,19 +732,6 @@ tcExpr e@(RecordUpd { rupd_flds = OverloadedRecUpdFields {}}) _ |
| 730 | 732 | tcExpr (ArithSeq _ witness seq) res_ty
|
| 731 | 733 | = tcArithSeq witness seq res_ty
|
| 732 | 734 | |
| 733 | -{-
|
|
| 734 | -************************************************************************
|
|
| 735 | -* *
|
|
| 736 | - Record dot syntax
|
|
| 737 | -* *
|
|
| 738 | -************************************************************************
|
|
| 739 | --}
|
|
| 740 | - |
|
| 741 | --- These terms have been replaced by their expanded expressions in the renamer. See
|
|
| 742 | --- Note [Overview of record dot syntax].
|
|
| 743 | -tcExpr (HsGetField _ _ _) _ = panic "GHC.Tc.Gen.Expr: tcExpr: HsGetField: Not implemented"
|
|
| 744 | -tcExpr (HsProjection _ _) _ = panic "GHC.Tc.Gen.Expr: tcExpr: HsProjection: Not implemented"
|
|
| 745 | - |
|
| 746 | 735 | {-
|
| 747 | 736 | ************************************************************************
|
| 748 | 737 | * *
|
| ... | ... | @@ -755,17 +744,7 @@ tcExpr (HsProjection _ _) _ = panic "GHC.Tc.Gen.Expr: tcExpr: HsProjection: Not |
| 755 | 744 | -- See Note [Delaying modFinalizers in untyped splices] in GHC.Rename.Splice.
|
| 756 | 745 | tcExpr (HsTypedSplice ext splice) res_ty = tcTypedSplice ext splice res_ty
|
| 757 | 746 | tcExpr e@(HsTypedBracket _ext body) res_ty = tcTypedBracket e body res_ty
|
| 758 | - |
|
| 759 | 747 | tcExpr e@(HsUntypedBracket ps body) res_ty = tcUntypedBracket e body ps res_ty
|
| 760 | -tcExpr (HsUntypedSplice splice _) res_ty
|
|
| 761 | - -- Since `tcApp` deals with `HsUntypedSplice` (in `splitHsApps`), you might
|
|
| 762 | - -- wonder why we don't delegate to `tcApp` as we do for `HsVar`, etc.
|
|
| 763 | - -- (See the initial block of equations for `tcExpr`.) But we can't do this
|
|
| 764 | - -- for `HsUntypedSplice`; to see why, read Wrinkle (UTS1) in
|
|
| 765 | - -- Note [Looking through Template Haskell splices in splitHsApps] in
|
|
| 766 | - -- GHC.Tc.Gen.Head.
|
|
| 767 | - = do { expr <- getUntypedSpliceBody splice
|
|
| 768 | - ; tcExpr expr res_ty }
|
|
| 769 | 748 | |
| 770 | 749 | {-
|
| 771 | 750 | ************************************************************************
|
| ... | ... | @@ -775,10 +754,12 @@ tcExpr (HsUntypedSplice splice _) res_ty |
| 775 | 754 | ************************************************************************
|
| 776 | 755 | -}
|
| 777 | 756 | |
| 778 | -tcExpr (HsOverLabel {}) ty = pprPanic "tcExpr:HsOverLabel" (ppr ty)
|
|
| 779 | -tcExpr (SectionL {}) ty = pprPanic "tcExpr:SectionL" (ppr ty)
|
|
| 780 | -tcExpr (SectionR {}) ty = pprPanic "tcExpr:SectionR" (ppr ty)
|
|
| 781 | - |
|
| 757 | +-- See Note [Typechecking by expansion: overview]
|
|
| 758 | +tcExpr e res_ty
|
|
| 759 | + = do { mb_hse <- tcExpand e
|
|
| 760 | + ; case mb_hse of
|
|
| 761 | + Just hse -> tcHsExpansion hse res_ty
|
|
| 762 | + Nothing -> pprPanic "tcExpr: unhandled case:" (ppr e) }
|
|
| 782 | 763 | |
| 783 | 764 | {-
|
| 784 | 765 | ************************************************************************
|
| ... | ... | @@ -788,73 +769,6 @@ tcExpr (SectionR {}) ty = pprPanic "tcExpr:SectionR" (ppr ty) |
| 788 | 769 | ************************************************************************
|
| 789 | 770 | -}
|
| 790 | 771 | |
| 791 | -{- Note [Typechecking by expansion: overview]
|
|
| 792 | -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 793 | -For many constructs, rather than typechecking the user-written code
|
|
| 794 | -directly, it's much easier to
|
|
| 795 | - * Expand (or desugar) the code to something simpler
|
|
| 796 | - * Typecheck that simpler expression
|
|
| 797 | - |
|
| 798 | -Example: Typechecking the do expression. The typechecker looks (somewhat) like this:
|
|
| 799 | - |
|
| 800 | - tcExpr e@(HsDo _ stmts) rho = do { hse <- expandDoStmts stmts
|
|
| 801 | - ; tcHsExpansion hse rho }
|
|
| 802 | - |
|
| 803 | -The `expandDoStmts` replaces the HsDo { x <- e1; return x }
|
|
| 804 | -with something like
|
|
| 805 | - HSE { hse_ctxt = ExprCtxt e
|
|
| 806 | - , hse_exp = e1 >>= \ x -> x }
|
|
| 807 | -and we then typecheck the expression `e1 >>= \ x -> x`
|
|
| 808 | - |
|
| 809 | -See also Note [Handling overloaded and rebindable constructs]
|
|
| 810 | - and Note [Doing XXExprGhcRn in the Renamer vs Typechecker]
|
|
| 811 | - |
|
| 812 | -The Big Question is how to ensure that error messages mention
|
|
| 813 | -only user-written source code, and never talk about the expanded code.
|
|
| 814 | -The rest of this Note explains how that is done.
|
|
| 815 | - |
|
| 816 | -* The expansion process typically takes a user written thing
|
|
| 817 | - L lspan ue
|
|
| 818 | - and returns
|
|
| 819 | - L lspan (XExpr (ExpandedThingRn (HSE { hse_ctxt = ue
|
|
| 820 | - , hse_exp = ee } ))
|
|
| 821 | - where `ee` is the expansion of the user written thing `ue`
|
|
| 822 | - |
|
| 823 | -* The type checker context has 3 key fields that describe the context:
|
|
| 824 | - TcLclCtxt { tcl_loc :: RealSrcSpan
|
|
| 825 | - , tcl_in_gen_code :: Bool
|
|
| 826 | - , tcl_err_ctxt :: ErrCtxtStack
|
|
| 827 | - , ... }
|
|
| 828 | - Note `tcl_loc` always points to a real place in the source code,
|
|
| 829 | - hence `RealSrcSpan`.
|
|
| 830 | - |
|
| 831 | - The `tcl_err_ctxt` is a stack of contexts, each saying something
|
|
| 832 | - like "In the expression: x+y" or "In second argument of `$` namely 'r { x=2 }'"
|
|
| 833 | - |
|
| 834 | - The `tcl_in_gen_code` is a boolean that keeps track of whether
|
|
| 835 | - the current expression being typechecked is compiler generated
|
|
| 836 | - or user generated.
|
|
| 837 | - |
|
| 838 | - INVARIANT: `tcl_loc` and `tcl_in_gen_code` are modified only in `setSrcSpan`.
|
|
| 839 | - |
|
| 840 | -* Now, when
|
|
| 841 | - tcMonoLExpr :: LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
|
|
| 842 | - gets a located expression, it does 3 things:
|
|
| 843 | - (a) Calls `setSrcSpanA` to set the ambient source-code location
|
|
| 844 | - (b) Calls `addExprCtxt` to push a suitable `HsCtxt` on top of the `tcl_err_ctxt`.
|
|
| 845 | - (c) Calls `tcExpr` to typecheck the expression.
|
|
| 846 | - |
|
| 847 | -* In these calls, if the `span` is generated (see `isGeneratedSrcSpan`), then
|
|
| 848 | - - `setSrcSpanA` sets `tcl_in_gen_code` to `True`, and leaves `tcl_loc` unchanged
|
|
| 849 | - - `addExprCtxt` is a no-op if `tcl_in_gen_code` is True
|
|
| 850 | - The result is that `tcl_loc` has the span from the innermost /user/ tree node;
|
|
| 851 | - and the ErrCtxtStack in `tcl_err_ctxt` only has contexts arisign from user code.
|
|
| 852 | - |
|
| 853 | -* Note that inside an expansion we have sub-expressions from the original program.
|
|
| 854 | - As soon as we enter one of those, identified by a /user/ span, `setSrcSpanA` will
|
|
| 855 | - sets the `tcl_loc` to reflect that span, and switch off `tcl_in_gen_code`. Nice!
|
|
| 856 | --}
|
|
| 857 | - |
|
| 858 | 772 | tcHsExpansion :: HsExpansion GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
|
| 859 | 773 | tcHsExpansion (HSE { hse_ctxt = o, hse_exp = e }) res_ty
|
| 860 | 774 | = do { e' <- tcMonoLExpr e res_ty
|
| ... | ... | @@ -35,6 +35,8 @@ tcInferRho, tcInferRhoNC :: |
| 35 | 35 | tcInferRhoFRR, tcInferRhoFRRNC ::
|
| 36 | 36 | FixedRuntimeRepContext -> LHsExpr GhcRn -> TcM (LHsExpr GhcTc, TcRhoType)
|
| 37 | 37 | |
| 38 | +tcInferExprSigma :: HsExpr GhcRn -> TcM (HsExpr GhcTc, TcSigmaType)
|
|
| 39 | + |
|
| 38 | 40 | tcInferExpr :: InferInstFlag -> LHsExpr GhcRn -> TcM (LHsExpr GhcTc, TcType)
|
| 39 | 41 | |
| 40 | 42 | tcSyntaxOp :: CtOrigin
|
| ... | ... | @@ -9,9 +9,9 @@ |
| 9 | 9 | -}
|
| 10 | 10 | |
| 11 | 11 | module GHC.Tc.Gen.Head
|
| 12 | - ( HsExprArg(..), TcPass(..), QLFlag(..), EWrap(..)
|
|
| 12 | + ( HsExprArg(..), HsExprLoc, TcPass(..), QLFlag(..), EWrap(..)
|
|
| 13 | 13 | , splitHsApps, rebuildHsApps
|
| 14 | - , addArgWrap, isHsValArg
|
|
| 14 | + , addArgWrap, isHsValArg, getFunSrcSpan
|
|
| 15 | 15 | , leadingValArgs, isVisibleArg, getDeepSubsumptionFlag_DataConHead
|
| 16 | 16 | |
| 17 | 17 | , tcInferAppHead, tcInferAppHead_maybe
|
| ... | ... | @@ -22,16 +22,13 @@ module GHC.Tc.Gen.Head |
| 22 | 22 | , pprArgInst, addFunResCtxt ) where
|
| 23 | 23 | |
| 24 | 24 | import {-# SOURCE #-} GHC.Tc.Gen.Expr( tcExpr, tcCheckPolyExprNC, tcPolyLExprSig )
|
| 25 | -import {-# SOURCE #-} GHC.Tc.Gen.Splice( getUntypedSpliceBody )
|
|
| 26 | -import {-# SOURCE #-} GHC.Tc.Gen.App( tcExprSigma )
|
|
| 27 | 25 | |
| 28 | 26 | import GHC.Prelude
|
| 29 | 27 | import GHC.Hs
|
| 30 | 28 | import GHC.Hs.Syn.Type
|
| 31 | 29 | |
| 32 | -import GHC.Rename.Utils (mkExpandedTc, mkExpandedExprTc)
|
|
| 33 | - |
|
| 34 | 30 | import GHC.Tc.Gen.HsType
|
| 31 | +import GHC.Tc.Gen.Expand( tcExpand )
|
|
| 35 | 32 | import GHC.Tc.Gen.Bind( chooseInferredQuantifiers )
|
| 36 | 33 | import GHC.Tc.Gen.Sig( tcUserTypeSig, tcInstSig )
|
| 37 | 34 | import GHC.Tc.TyCl.PatSyn( patSynBuilderOcc )
|
| ... | ... | @@ -86,7 +83,7 @@ import GHC.Data.Maybe |
| 86 | 83 | The data type HsExprArg :: TcPass -> Type
|
| 87 | 84 | is a very local type, used only within this module and GHC.Tc.Gen.App
|
| 88 | 85 | |
| 89 | -* It's really a zipper for an application chain
|
|
| 86 | +* It's just a bog-standard zipper for an application chain
|
|
| 90 | 87 | See Note [Application chains and heads] in GHC.Tc.Gen.App for
|
| 91 | 88 | what an "application chain" is.
|
| 92 | 89 | |
| ... | ... | @@ -147,6 +144,8 @@ takes apart either an HsApp, or an infix OpApp, returning |
| 147 | 144 | * We do not look through expanded expressions (except PopErrCtxt.)
|
| 148 | 145 | -}
|
| 149 | 146 | |
| 147 | +type HsExprLoc = EpAnn AnnListItem -- The location attached to a HsExpr
|
|
| 148 | + |
|
| 150 | 149 | data TcPass = TcpRn -- Arguments decomposed
|
| 151 | 150 | | TcpInst -- Function instantiated
|
| 152 | 151 | | TcpTc -- Typechecked
|
| ... | ... | @@ -154,34 +153,34 @@ data TcPass = TcpRn -- Arguments decomposed |
| 154 | 153 | data HsExprArg (p :: TcPass) where -- See Note [HsExprArg]
|
| 155 | 154 | |
| 156 | 155 | -- Data constructor EValArg represents a value argument
|
| 157 | - EValArg :: { ea_loc_span :: SrcSpan
|
|
| 158 | - , ea_arg_ty :: !(XEVAType p)
|
|
| 159 | - , ea_arg :: LHsExpr (GhcPass (XPass p)) }
|
|
| 156 | + EValArg :: { ea_loc_span :: HsExprLoc
|
|
| 157 | + , ea_arg_ty :: !(XEVAType p)
|
|
| 158 | + , ea_arg :: LHsExpr (GhcPass (XPass p)) }
|
|
| 160 | 159 | -> HsExprArg p
|
| 161 | 160 | |
| 162 | 161 | -- Data constructor EValArgQL represents an argument that has been
|
| 163 | 162 | -- partly-type-checked by Quick Look; see Note [EValArgQL]
|
| 164 | - EValArgQL :: { eaql_loc_span :: SrcSpan
|
|
| 165 | - , eaql_arg_ty :: Scaled TcSigmaType -- Argument type expected by function
|
|
| 166 | - , eaql_larg :: LHsExpr GhcRn -- Original application, for
|
|
| 167 | - -- location and error msgs
|
|
| 168 | - , eaql_rn_fun :: HsExpr GhcRn -- Head of the argument if it is an application
|
|
| 169 | - , eaql_tc_fun :: (HsExpr GhcTc, SrcSpan) -- Typechecked head and its location span
|
|
| 170 | - , eaql_fun_ue :: UsageEnv -- Usage environment of the typechecked head (QLA5)
|
|
| 171 | - , eaql_args :: [HsExprArg 'TcpInst] -- Args: instantiated, not typechecked
|
|
| 172 | - , eaql_wanted :: WantedConstraints
|
|
| 173 | - , eaql_encl :: Bool -- True <=> we have already qlUnified
|
|
| 174 | - -- eaql_arg_ty and eaql_res_rho
|
|
| 175 | - , eaql_res_rho :: TcRhoType } -- Result type of the application
|
|
| 163 | + EValArgQL :: { eaql_loc_span :: HsExprLoc
|
|
| 164 | + , eaql_arg_ty :: Scaled TcSigmaType -- Argument type expected by function
|
|
| 165 | + , eaql_larg :: LHsExpr GhcRn -- Original application, for
|
|
| 166 | + -- location and error msgs
|
|
| 167 | + , eaql_rn_fun :: HsExpr GhcRn -- Head of the argument if it is an application
|
|
| 168 | + , eaql_tc_fun :: (HsExpr GhcTc, SrcSpan) -- Typechecked head and its location span
|
|
| 169 | + , eaql_fun_ue :: UsageEnv -- Usage environment of the typechecked head (QLA5)
|
|
| 170 | + , eaql_args :: [HsExprArg 'TcpInst] -- Args: instantiated, not typechecked
|
|
| 171 | + , eaql_wanted :: WantedConstraints
|
|
| 172 | + , eaql_encl :: Bool -- True <=> we have already qlUnified
|
|
| 173 | + -- eaql_arg_ty and eaql_res_rho
|
|
| 174 | + , eaql_res_rho :: TcRhoType } -- Result type of the application
|
|
| 176 | 175 | -> HsExprArg 'TcpInst -- Only exists in TcpInst phase
|
| 177 | 176 | |
| 178 | - ETypeArg :: { ea_loc_span :: SrcSpan
|
|
| 179 | - , ea_hs_ty :: LHsWcType GhcRn -- The type arg
|
|
| 180 | - , ea_ty_arg :: !(XETAType p) } -- Kind-checked type arg
|
|
| 177 | + ETypeArg :: { ea_loc_span :: HsExprLoc
|
|
| 178 | + , ea_hs_ty :: LHsWcType GhcRn -- The type arg
|
|
| 179 | + , ea_ty_arg :: !(XETAType p) } -- Kind-checked type arg
|
|
| 181 | 180 | -> HsExprArg p
|
| 182 | 181 | |
| 183 | - EPrag :: SrcSpan -> (HsPragE (GhcPass (XPass p))) -> HsExprArg p
|
|
| 184 | - EWrap :: EWrap -> HsExprArg p
|
|
| 182 | + EPrag :: HsExprLoc -> (HsPragE (GhcPass (XPass p))) -> HsExprArg p
|
|
| 183 | + EWrap :: EWrap -> HsExprArg p
|
|
| 185 | 184 | |
| 186 | 185 | type family XETAType (p :: TcPass) where -- Type arguments
|
| 187 | 186 | XETAType 'TcpRn = NoExtField
|
| ... | ... | @@ -193,8 +192,8 @@ type family XEVAType (p :: TcPass) where -- Value arguments |
| 193 | 192 | |
| 194 | 193 | data QLFlag = DoQL | NoQL
|
| 195 | 194 | |
| 196 | -data EWrap = EPar SrcSpan
|
|
| 197 | - | EExpand (HsExpr GhcRn)
|
|
| 195 | +data EWrap = EPar HsExprLoc
|
|
| 196 | + | EExpand HsExprLoc HsCtxt
|
|
| 198 | 197 | | EHsWrap HsWrapper
|
| 199 | 198 | |
| 200 | 199 | |
| ... | ... | @@ -207,11 +206,11 @@ type family XPass (p :: TcPass) where |
| 207 | 206 | XPass 'TcpInst = 'Renamed
|
| 208 | 207 | XPass 'TcpTc = 'Typechecked
|
| 209 | 208 | |
| 210 | -mkEValArg :: SrcSpan -> LHsExpr GhcRn -> HsExprArg 'TcpRn
|
|
| 209 | +mkEValArg :: HsExprLoc -> LHsExpr GhcRn -> HsExprArg 'TcpRn
|
|
| 211 | 210 | mkEValArg src_loc e = EValArg { ea_arg = e, ea_loc_span = src_loc
|
| 212 | 211 | , ea_arg_ty = noExtField }
|
| 213 | 212 | |
| 214 | -mkETypeArg :: SrcSpan -> LHsWcType GhcRn -> HsExprArg 'TcpRn
|
|
| 213 | +mkETypeArg :: HsExprLoc -> LHsWcType GhcRn -> HsExprArg 'TcpRn
|
|
| 215 | 214 | mkETypeArg src_loc hs_ty =
|
| 216 | 215 | ETypeArg { ea_loc_span = src_loc
|
| 217 | 216 | , ea_hs_ty = hs_ty
|
| ... | ... | @@ -223,74 +222,17 @@ addArgWrap wrap args |
| 223 | 222 | | otherwise = EWrap (EHsWrap wrap) : args
|
| 224 | 223 | |
| 225 | 224 | |
| 226 | -splitHsApps :: HsExpr GhcRn
|
|
| 227 | - -> TcM ( (HsExpr GhcRn, SrcSpan) -- Head
|
|
| 228 | - , [HsExprArg 'TcpRn]) -- Args
|
|
| 229 | --- See Note [splitHsApps].
|
|
| 230 | ---
|
|
| 231 | --- This uses the TcM monad solely because we must run modFinalizers when looking
|
|
| 232 | --- through HsUntypedSplices
|
|
| 233 | --- (see Note [Looking through Template Haskell splices in splitHsApps]).
|
|
| 234 | -splitHsApps e = go e noSrcSpan []
|
|
| 235 | - where
|
|
| 236 | - go :: HsExpr GhcRn -> SrcSpan -> [HsExprArg 'TcpRn]
|
|
| 237 | - -> TcM ((HsExpr GhcRn, SrcSpan), [HsExprArg 'TcpRn])
|
|
| 238 | - -- Modify the SrcSpan as we walk inwards, so it describes the next argument
|
|
| 239 | - go (HsPar _ (L l fun)) lspan args = go fun (locA l) (EWrap (EPar lspan) : args)
|
|
| 240 | - go (HsPragE _ p (L l fun)) lspan args = go fun (locA l) (EPrag lspan p : args)
|
|
| 241 | - go (HsAppType _ (L l fun) ty) lspan args = go fun (locA l) (mkETypeArg lspan ty : args)
|
|
| 242 | - go (HsApp _ (L l fun) arg) lspan args = go fun (locA l) (mkEValArg lspan arg : args)
|
|
| 243 | - |
|
| 244 | - -- See Note [Looking through Template Haskell splices in splitHsApps]
|
|
| 245 | - go e@(HsUntypedSplice splice_res splice) _ args
|
|
| 246 | - = do { fun <- getUntypedSpliceBody splice_res
|
|
| 247 | - ; go fun lspan' (EWrap (EExpand e) : args) }
|
|
| 248 | - where
|
|
| 249 | - lspan' :: SrcSpan
|
|
| 250 | - lspan' = case splice of
|
|
| 251 | - HsUntypedSpliceExpr _ (L l _) -> locA l -- l :: SrcAnn AnnListItem
|
|
| 252 | - HsQuasiQuote _ _ (L l _) -> locA l -- l :: SrcAnn NoEpAnns
|
|
| 253 | - (XUntypedSplice (HsImplicitLiftSplice _ _ _ (L l _))) -> locA l
|
|
| 254 | - |
|
| 255 | - -- See Note [Desugar OpApp in the typechecker]
|
|
| 256 | - go e@(OpApp _ arg1 (L l op) arg2) _ args
|
|
| 257 | - = pure ( (op, locA l)
|
|
| 258 | - , mkEValArg noSrcSpan arg1
|
|
| 259 | - : mkEValArg noSrcSpan arg2
|
|
| 260 | - -- noSrcSpan because this the span of the call,
|
|
| 261 | - -- and its hard to say exactly what that is
|
|
| 262 | - : EWrap (EExpand e)
|
|
| 263 | - : args )
|
|
| 264 | - |
|
| 265 | - go e lspan args = pure ((e, lspan), args)
|
|
| 266 | - |
|
| 267 | - |
|
| 268 | --- | Rebuild an application: takes a type-checked application head
|
|
| 269 | --- expression together with arguments in the form of typechecked 'HsExprArg's
|
|
| 270 | --- and returns a typechecked application of the head to the arguments.
|
|
| 271 | -rebuildHsApps :: (HsExpr GhcTc, SrcSpan)
|
|
| 272 | - -- ^ the function being applied
|
|
| 273 | - -> [HsExprArg 'TcpTc]
|
|
| 274 | - -- ^ the arguments to the function
|
|
| 275 | - -> HsExpr GhcTc
|
|
| 276 | -rebuildHsApps (fun, _) [] = fun
|
|
| 277 | -rebuildHsApps (fun, sloc) (arg : args)
|
|
| 278 | - = case arg of
|
|
| 279 | - EValArg { ea_arg = arg, ea_loc_span = sloc' }
|
|
| 280 | - -> rebuildHsApps (HsApp noExtField lfun arg, sloc') args
|
|
| 281 | - ETypeArg { ea_hs_ty = hs_ty, ea_ty_arg = ty, ea_loc_span = sloc' }
|
|
| 282 | - -> rebuildHsApps (HsAppType ty lfun hs_ty, sloc') args
|
|
| 283 | - EPrag sloc' p
|
|
| 284 | - -> rebuildHsApps (HsPragE noExtField p lfun, sloc') args
|
|
| 285 | - EWrap (EPar sloc')
|
|
| 286 | - -> rebuildHsApps (gHsPar lfun, sloc') args
|
|
| 287 | - EWrap (EExpand o)
|
|
| 288 | - -> rebuildHsApps (mkExpandedExprTc o fun, sloc) args
|
|
| 289 | - EWrap (EHsWrap wrap)
|
|
| 290 | - -> rebuildHsApps (mkHsWrap wrap fun, sloc) args
|
|
| 291 | - where
|
|
| 292 | - lfun = L (noAnnSrcSpan sloc) fun
|
|
| 225 | +--------------------
|
|
| 226 | +getFunSrcSpan :: [HsExprArg 'TcpRn] -> TcM SrcSpan
|
|
| 227 | +getFunSrcSpan [] = getSrcSpanM
|
|
| 228 | +getFunSrcSpan (ETypeArg { ea_loc_span = l } : _) = return (locA l)
|
|
| 229 | +getFunSrcSpan (EValArg { ea_loc_span = l } : _) = return (locA l)
|
|
| 230 | +getFunSrcSpan (EPrag l _ : _) = return (locA l)
|
|
| 231 | +getFunSrcSpan (EWrap (EPar l) : _) = return (locA l)
|
|
| 232 | +getFunSrcSpan (EWrap (EExpand l _) : _) = return (locA l)
|
|
| 233 | +getFunSrcSpan (EWrap (EHsWrap {}) : args) = getFunSrcSpan args
|
|
| 293 | 234 | |
| 235 | +--------------------
|
|
| 294 | 236 | isHsValArg :: HsExprArg id -> Bool
|
| 295 | 237 | isHsValArg (EValArg {}) = True
|
| 296 | 238 | isHsValArg _ = False
|
| ... | ... | @@ -334,13 +276,60 @@ pprArgInst (EValArgQL { eaql_tc_fun = fun, eaql_args = args, eaql_res_rho = ty}) |
| 334 | 276 | 2 (vcat [ vcat (map pprArgInst args), text "ea_ql_ty:" <+> ppr ty ])
|
| 335 | 277 | |
| 336 | 278 | instance Outputable EWrap where
|
| 337 | - ppr (EPar _) = text "EPar"
|
|
| 338 | - ppr (EHsWrap w) = text "EHsWrap" <+> ppr w
|
|
| 339 | - ppr (EExpand orig) = text "EExpand" <+> ppr orig
|
|
| 279 | + ppr (EPar _) = text "EPar"
|
|
| 280 | + ppr (EHsWrap w) = text "EHsWrap" <+> ppr w
|
|
| 281 | + ppr (EExpand _ _) = text "EExpand" -- No Outputable instance for HsCtxt yet
|
|
| 282 | + |
|
| 283 | + |
|
| 284 | + |
|
| 285 | +{- *********************************************************************
|
|
| 286 | +* *
|
|
| 287 | + Splitting and rebuilding
|
|
| 288 | +* *
|
|
| 289 | +********************************************************************* -}
|
|
| 290 | + |
|
| 291 | +splitHsApps :: HsExpr GhcRn -> TcM (HsExpr GhcRn, [HsExprArg 'TcpRn])
|
|
| 292 | +splitHsApps e = go e []
|
|
| 293 | + where
|
|
| 294 | + go (HsPar _ (L l fun)) args = go fun (EWrap (EPar l) : args)
|
|
| 295 | + go (HsPragE _ p (L l fun)) args = go fun (EPrag l p : args)
|
|
| 296 | + go (HsAppType _ (L l fun) ty) args = go fun (mkETypeArg l ty : args)
|
|
| 297 | + go (HsApp _ (L l fun) arg) args = go fun (mkEValArg l arg : args)
|
|
| 298 | + go fun args = do { mb_hse <- tcExpand fun
|
|
| 299 | + ; case mb_hse of
|
|
| 300 | + Just (HSE { hse_ctxt = orig, hse_exp = L l fun' })
|
|
| 301 | + -> go fun' (EWrap (EExpand l orig) : args)
|
|
| 302 | + Nothing
|
|
| 303 | + -> return (fun, args) }
|
|
| 304 | + |
|
| 305 | +-- | Rebuild an application: takes a type-checked application head
|
|
| 306 | +-- expression together with arguments in the form of typechecked 'HsExprArg's
|
|
| 307 | +-- and returns a typechecked application of the head to the arguments.
|
|
| 308 | +rebuildHsApps :: HsExpr GhcTc
|
|
| 309 | + -- ^ the function being applied
|
|
| 310 | + -> [HsExprArg 'TcpTc]
|
|
| 311 | + -- ^ the arguments to the function
|
|
| 312 | + -> HsExpr GhcTc
|
|
| 313 | +rebuildHsApps fun [] = fun
|
|
| 314 | +rebuildHsApps fun (arg : args)
|
|
| 315 | + = case arg of
|
|
| 316 | + EValArg { ea_arg = arg, ea_loc_span = l }
|
|
| 317 | + -> rebuildHsApps (HsApp noExtField (L l fun) arg) args
|
|
| 318 | + ETypeArg { ea_hs_ty = hs_ty, ea_ty_arg = ty, ea_loc_span = l }
|
|
| 319 | + -> rebuildHsApps (HsAppType ty (L l fun) hs_ty) args
|
|
| 320 | + EPrag l p
|
|
| 321 | + -> rebuildHsApps (HsPragE noExtField p (L l fun)) args
|
|
| 322 | + EWrap (EPar l)
|
|
| 323 | + -> rebuildHsApps (HsPar noExtField (L l fun)) args
|
|
| 324 | + EWrap (EExpand l o)
|
|
| 325 | + -> rebuildHsApps (XExpr (ExpandedThingTc (HSE o (L l fun)))) args
|
|
| 326 | + EWrap (EHsWrap wrap)
|
|
| 327 | + -> rebuildHsApps (mkHsWrap wrap fun) args
|
|
| 328 | + |
|
| 340 | 329 | |
| 341 | 330 | {- Note [Desugar OpApp in the typechecker]
|
| 342 | 331 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 343 | -Operator sections are desugared in the renamer; see GHC.Rename.Expr
|
|
| 332 | +pOperator sections are desugared in the renamer; see GHC.Rename.Expr
|
|
| 344 | 333 | Note [Handling overloaded and rebindable constructs].
|
| 345 | 334 | But for reasons explained there, we rename OpApp to OpApp. Then,
|
| 346 | 335 | here in the typechecker, we desugar it to a use of ExpandedThingRn.
|
| ... | ... | @@ -401,6 +390,8 @@ handling splices and quasiquotes has already been performed by the renamer by |
| 401 | 390 | the time we get to `splitHsApps`.
|
| 402 | 391 | |
| 403 | 392 | Wrinkle (UTS1):
|
| 393 | +*** TODO *** put this somewhere else
|
|
| 394 | + |
|
| 404 | 395 | `tcExpr` has a separate case for `HsUntypedSplice`s that do /not/ occur at the
|
| 405 | 396 | head of an application. This is important to handle programs like this one:
|
| 406 | 397 | |
| ... | ... | @@ -446,9 +437,7 @@ tcInferAppHead (fun,fun_lspan) |
| 446 | 437 | do { mb_tc_fun <- tcInferAppHead_maybe fun
|
| 447 | 438 | ; case mb_tc_fun of
|
| 448 | 439 | Just (fun', fun_sigma) -> return (fun', fun_sigma)
|
| 449 | - Nothing -> runInferRho (tcExpr fun)
|
|
| 450 | - |
|
| 451 | - }
|
|
| 440 | + Nothing -> runInferRho (tcExpr fun) }
|
|
| 452 | 441 | |
| 453 | 442 | tcInferAppHead_maybe :: HsExpr GhcRn
|
| 454 | 443 | -> TcM (Maybe (HsExpr GhcTc, TcSigmaType))
|
| ... | ... | @@ -457,23 +446,11 @@ tcInferAppHead_maybe :: HsExpr GhcRn |
| 457 | 446 | -- XExpr's although complicated needs to be looked through, useful for QL things when
|
| 458 | 447 | -- the argument is an XExpr
|
| 459 | 448 | tcInferAppHead_maybe fun = case fun of
|
| 460 | - HsVar _ nm
|
|
| 461 | - -> Just <$> tcInferId nm
|
|
| 462 | - ExprWithTySig _ e hs_ty
|
|
| 463 | - -> Just <$>tcExprWithSig e hs_ty
|
|
| 464 | - HsOverLit _ lit
|
|
| 465 | - -> Just <$> tcInferOverLit lit
|
|
| 466 | - XExpr (HsRecSelRn f)
|
|
| 467 | - -> Just <$> tcInferRecSelId f
|
|
| 468 | - XExpr (ExpandedThingRn (HSE o (L loc e)))
|
|
| 469 | - -> setSrcSpan (locA loc) $ Just <$>
|
|
| 470 | - do { (e', ty) <- tcExprSigma False (hsCtxtCtOrigin o) e
|
|
| 471 | - ; return (mkExpandedTc o (L loc e'), ty) }
|
|
| 472 | - -- We do not want to instantiate the type of the head as there may be
|
|
| 473 | - -- visible type applications in the argument.
|
|
| 474 | - -- c.f. T19167
|
|
| 475 | - _
|
|
| 476 | - -> return Nothing
|
|
| 449 | + HsVar _ nm -> Just <$> tcInferId nm
|
|
| 450 | + ExprWithTySig _ e hs_ty -> Just <$> tcExprWithSig e hs_ty
|
|
| 451 | + HsOverLit _ lit -> Just <$> tcInferOverLit lit
|
|
| 452 | + XExpr (HsRecSelRn f) -> Just <$> tcInferRecSelId f
|
|
| 453 | + _ -> return Nothing
|
|
| 477 | 454 | |
| 478 | 455 | {- *********************************************************************
|
| 479 | 456 | * *
|
| ... | ... | @@ -219,10 +219,10 @@ tcMatches :: (AnnoBody body, Outputable (body GhcTc)) |
| 219 | 219 | -> MatchGroup GhcRn (LocatedA (body GhcRn))
|
| 220 | 220 | -> TcM (MatchGroup GhcTc (LocatedA (body GhcTc)))
|
| 221 | 221 | |
| 222 | -tcMatches ctxt tc_body pat_tys rhs_ty (MG { mg_alts = L l matches
|
|
| 222 | +tcMatches ctxt tc_body pat_tys exp_ty (MG { mg_alts = L l matches
|
|
| 223 | 223 | , mg_ext = origin })
|
| 224 | 224 | | null matches -- Deal with case e of {}
|
| 225 | - -- Since there are no branches, no one else will fill in rhs_ty
|
|
| 225 | + -- Since there are no branches, no one else will fill in exp_ty
|
|
| 226 | 226 | -- when in inference mode, so we must do it ourselves,
|
| 227 | 227 | -- here, using expTypeToType
|
| 228 | 228 | = do { tcEmitBindingUsage bottomUE
|
| ... | ... | @@ -233,17 +233,19 @@ tcMatches ctxt tc_body pat_tys rhs_ty (MG { mg_alts = L l matches |
| 233 | 233 | [ExpForAllPatTy tvb] -> failWithTc $ TcRnEmptyCase ctxt (EmptyCaseForall tvb)
|
| 234 | 234 | [] -> panic "tcMatches: no arguments in EmptyCase"
|
| 235 | 235 | _t1:(_t2:_ts) -> panic "tcMatches: multiple arguments in EmptyCase"
|
| 236 | - ; rhs_ty <- expTypeToType rhs_ty
|
|
| 236 | + ; rhs_ty <- expTypeToType exp_ty
|
|
| 237 | 237 | ; return (MG { mg_alts = L l []
|
| 238 | 238 | , mg_ext = MatchGroupTc [pat_ty] rhs_ty origin
|
| 239 | 239 | }) }
|
| 240 | 240 | |
| 241 | 241 | | otherwise
|
| 242 | - = do { umatches <- mapM (tcCollectingUsage . tcMatch tc_body pat_tys rhs_ty) matches
|
|
| 243 | - ; let (usages, matches') = unzip umatches
|
|
| 242 | + = do { let exp_ty' = adjustExpTypeForCaseBranches exp_ty matches
|
|
| 243 | + tc_match match = tcCollectingUsage $
|
|
| 244 | + tcMatch tc_body pat_tys exp_ty' match
|
|
| 245 | + ; (usages, matches') <- mapAndUnzipM tc_match matches
|
|
| 244 | 246 | ; tcEmitBindingUsage $ supUEs usages
|
| 245 | 247 | ; pat_tys <- mapM readScaledExpType (filter_out_forall_pat_tys pat_tys)
|
| 246 | - ; rhs_ty <- readExpType rhs_ty
|
|
| 248 | + ; rhs_ty <- readExpType exp_ty
|
|
| 247 | 249 | ; traceTc "tcMatches" (ppr matches' $$ ppr pat_tys $$ ppr rhs_ty)
|
| 248 | 250 | ; return (MG { mg_alts = L l matches'
|
| 249 | 251 | , mg_ext = MatchGroupTc pat_tys rhs_ty origin
|
| ... | ... | @@ -63,7 +63,7 @@ module GHC.Tc.Utils.TcMType ( |
| 63 | 63 | mkCheckExpType, newInferExpType, newInferExpTypeFRR,
|
| 64 | 64 | runInfer, runInferRho, runInferSigma, runInferKind, runInferRhoFRR, runInferSigmaFRR,
|
| 65 | 65 | readExpType, readExpType_maybe, readScaledExpType,
|
| 66 | - expTypeToType, scaledExpTypeToType,
|
|
| 66 | + expTypeToType, scaledExpTypeToType, adjustExpTypeForCaseBranches,
|
|
| 67 | 67 | checkingExpType_maybe, checkingExpType,
|
| 68 | 68 | inferResultToType, ensureMonoType, promoteTcType,
|
| 69 | 69 | |
| ... | ... | @@ -499,6 +499,17 @@ inferResultToType (IR { ir_uniq = u, ir_lvl = tc_lvl |
| 499 | 499 | ; let conc_orig = ConcreteFRR $ FixedRuntimeRepOrigin tau frr
|
| 500 | 500 | ; return tau }
|
| 501 | 501 | |
| 502 | +adjustExpTypeForCaseBranches :: ExpRhoType -> [branch] -> ExpRhoType
|
|
| 503 | +-- See Note [fillInferResult: multiple branches]
|
|
| 504 | +adjustExpTypeForCaseBranches exp_ty branches
|
|
| 505 | + = case exp_ty of
|
|
| 506 | + Infer ir | IR { ir_inst = IIF_Sigma } <- ir
|
|
| 507 | + , branches `lengthAtLeast` 2
|
|
| 508 | + -> Infer (ir { ir_inst = IIF_DeepRho })
|
|
| 509 | + | otherwise
|
|
| 510 | + -> exp_ty
|
|
| 511 | + Check {} -> exp_ty
|
|
| 512 | + |
|
| 502 | 513 | {- Note [inferResultToType]
|
| 503 | 514 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| 504 | 515 | expTypeToType and inferResultType convert an InferResult to a monotype.
|
| ... | ... | @@ -99,13 +99,12 @@ 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 (..))
|
|
| 109 | 108 | |
| 110 | 109 | import GHC.Utils.Misc
|
| 111 | 110 | import GHC.Utils.Outputable as Outputable
|
| ... | ... | @@ -426,7 +425,7 @@ Some examples: |
| 426 | 425 | |
| 427 | 426 | tcSkolemiseGeneral
|
| 428 | 427 | :: HasDebugCallStack
|
| 429 | - => DeepSubsumptionFlag
|
|
| 428 | + => DeepSubsumptionFlag -- Ignores the DeepSubsumptionDepth
|
|
| 430 | 429 | -> UserTypeCtxt
|
| 431 | 430 | -> TcType -> TcType -- top_ty and expected_ty
|
| 432 | 431 | -- Here, top_ty is the type we started to skolemise; used only in SigSkol
|
| ... | ... | @@ -1169,7 +1168,7 @@ fillInferResultNoInst act_res_ty (IR { ir_uniq = u |
| 1169 | 1168 | |
| 1170 | 1169 | ; return final_co } }
|
| 1171 | 1170 | |
| 1172 | -fillInferResult :: DeepSubsumptionFlag -> CtOrigin -> TcType -> InferResult -> TcM HsWrapper
|
|
| 1171 | +fillInferResult :: DeepSubsumptionFlag -> CtOrigin -> TcSigmaType -> InferResult -> TcM HsWrapper
|
|
| 1173 | 1172 | -- See Note [Instantiation of InferResult]
|
| 1174 | 1173 | fillInferResult ds_flag ct_orig res_ty ires@(IR { ir_inst = iif })
|
| 1175 | 1174 | = case iif of
|
| ... | ... | @@ -1203,7 +1202,7 @@ There are two things to worry about: |
| 1203 | 1202 | T1 -> e1
|
| 1204 | 1203 | T2 -> e2
|
| 1205 | 1204 | |
| 1206 | -Our typing rules are:
|
|
| 1205 | +In general our typing rules are:
|
|
| 1207 | 1206 | |
| 1208 | 1207 | * The RHS of a existential or GADT alternative must always be a
|
| 1209 | 1208 | monotype, regardless of the number of alternatives.
|
| ... | ... | @@ -1218,17 +1217,13 @@ Our typing rules are: |
| 1218 | 1217 | We use choice (2) in that Section.
|
| 1219 | 1218 | (GHC 8.10 and earlier used choice (1).)
|
| 1220 | 1219 | |
| 1221 | - But note that
|
|
| 1222 | - case e of
|
|
| 1223 | - True -> hr
|
|
| 1224 | - False -> \x -> hr x
|
|
| 1225 | - will fail, because we still /infer/ both branches, so the \x will get
|
|
| 1226 | - a (monotype) unification variable, which will fail to unify with
|
|
| 1227 | - (forall a. a->a)
|
|
| 1220 | +Note [fillInferResult: GADTs and existentials]
|
|
| 1221 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 1222 | +We can detect the GADT/existential situation, case (1) of Note [fillInferResult],
|
|
| 1223 | +by seeing that the current TcLevel is greater than that stored in ir_lvl of the
|
|
| 1224 | +Infer ExpType. We bump the level whenever we go past a GADT/existential match.
|
|
| 1228 | 1225 | |
| 1229 | -For (1) we can detect the GADT/existential situation by seeing that
|
|
| 1230 | -the current TcLevel is greater than that stored in ir_lvl of the Infer
|
|
| 1231 | -ExpType. We bump the level whenever we go past a GADT/existential match.
|
|
| 1226 | +We insist that the RHS has a monotype, regardless of the number of alternatives.
|
|
| 1232 | 1227 | |
| 1233 | 1228 | Then, before filling the hole use promoteTcType to promote the type
|
| 1234 | 1229 | to the outer ir_lvl. promoteTcType does this
|
| ... | ... | @@ -1239,11 +1234,6 @@ That forces the type to be a monotype (since unification variables can |
| 1239 | 1234 | only unify with monotypes); and catches skolem-escapes because the
|
| 1240 | 1235 | alpha is untouchable until the equality floats out.
|
| 1241 | 1236 | |
| 1242 | -For (2), we simply look to see if the hole is filled already.
|
|
| 1243 | - - if not, we promote (as above) and fill the hole
|
|
| 1244 | - - if it is filled, we simply unify with the type that is
|
|
| 1245 | - already there
|
|
| 1246 | - |
|
| 1247 | 1237 | (FIR1) There is one wrinkle. Suppose we have
|
| 1248 | 1238 | case e of
|
| 1249 | 1239 | T1 -> e1 :: (forall a. a->a) -> Int
|
| ... | ... | @@ -1258,7 +1248,47 @@ For (2), we simply look to see if the hole is filled already. |
| 1258 | 1248 | So if we check G2 second, we still want to emit a constraint that restricts
|
| 1259 | 1249 | the RHS to be a monotype. This is done by ensureMonoType, and it works
|
| 1260 | 1250 | by simply generating a constraint (alpha ~ ty), where alpha is a fresh
|
| 1261 | -unification variable. We discard the evidence.
|
|
| 1251 | + unification variable. We discard the evidence.
|
|
| 1252 | + |
|
| 1253 | +Note [fillInferResult: multiple branches]
|
|
| 1254 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
| 1255 | +If there are multiple case branches, case (2) of Note [fillInferResult]
|
|
| 1256 | +we simply look to see if the hole is filled already.
|
|
| 1257 | + - if not, we promote (as above) and fill the hole
|
|
| 1258 | + - if it is filled, we simply unify with the type that is already there
|
|
| 1259 | + |
|
| 1260 | +But consider
|
|
| 1261 | + case x of
|
|
| 1262 | + True -> True
|
|
| 1263 | + False -> undefined
|
|
| 1264 | +and suppose we call `tcInferSigma` on this expression, so that the `ir_inst`
|
|
| 1265 | +field of the expected result type is `IIF_Sigma`. The danger is that we'll
|
|
| 1266 | +fill the hole with `Bool` (from the `True`) and then reject when we try to
|
|
| 1267 | +unify that with `forall a. a->a`, from the call to `undefined`.
|
|
| 1268 | + |
|
| 1269 | +Another example:
|
|
| 1270 | + case x of
|
|
| 1271 | + True -> (e1 :: forall a b. a->b)
|
|
| 1272 | + False -> (e3 :: forall b a. a->b)
|
|
| 1273 | + |
|
| 1274 | +To avoid this, we never infer a sigma-type from a multi-branch `case`. Instead
|
|
| 1275 | +we just zap the `IIF_Sigma` to `IIF_DeepRho` when walking inside the branches
|
|
| 1276 | +of multi-arm case-expression, or an if-expression. See calls to
|
|
| 1277 | +`adjustExpTypeForCaseBranches`.
|
|
| 1278 | + |
|
| 1279 | +This does mean that this would work:
|
|
| 1280 | + (let x = 77+55 in h x x) @Int
|
|
| 1281 | +where
|
|
| 1282 | + h :: Int -> Int -> forall a. a->a
|
|
| 1283 | +The `@Int` would instantiate the `forall a`.
|
|
| 1284 | + |
|
| 1285 | +Note that
|
|
| 1286 | + case e of
|
|
| 1287 | + True -> hr
|
|
| 1288 | + False -> \x -> hr x
|
|
| 1289 | + where hr :: (forall a. a->a) -> Int
|
|
| 1290 | +will fail, because we still /infer/ both branches, so the \x will get a
|
|
| 1291 | +(monotype) unification variable, which will fail to unify with (forall a. a->a)
|
|
| 1262 | 1292 | |
| 1263 | 1293 | Note [Instantiation of InferResult]
|
| 1264 | 1294 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| ... | ... | @@ -1274,7 +1304,7 @@ Usually this field is `IIF_DeepRho` meaning "return a (possibly deep) rho-type". |
| 1274 | 1304 | Why is this the common case? See #17173 for discussion. Here are some examples
|
| 1275 | 1305 | of why:
|
| 1276 | 1306 | |
| 1277 | -1. Consider
|
|
| 1307 | +(IIR1) Consider
|
|
| 1278 | 1308 | f x = (*)
|
| 1279 | 1309 | We want to instantiate the type of (*) before returning, else we
|
| 1280 | 1310 | will infer the type
|
| ... | ... | @@ -1286,21 +1316,46 @@ of why: |
| 1286 | 1316 | instantiating. This could perhaps be worked around, but it may be
|
| 1287 | 1317 | hard to know even when instantiation should happen.
|
| 1288 | 1318 | |
| 1289 | -2. Another reason. Consider
|
|
| 1319 | +(IIR2) Another reason. Consider
|
|
| 1290 | 1320 | f :: (?x :: Int) => a -> a
|
| 1291 | 1321 | g y = let ?x = 3::Int in f
|
| 1292 | 1322 | Here want to instantiate f's type so that the ?x::Int constraint
|
| 1293 | 1323 | gets discharged by the enclosing implicit-parameter binding.
|
| 1294 | 1324 | |
| 1295 | -3. Suppose one defines plus = (+). If we instantiate lazily, we will
|
|
| 1325 | +(IIR3) Suppose one defines plus = (+). If we instantiate lazily, we will
|
|
| 1296 | 1326 | infer plus :: forall a. Num a => a -> a -> a. However, the monomorphism
|
| 1297 | 1327 | restriction compels us to infer
|
| 1298 | 1328 | plus :: Integer -> Integer -> Integer
|
| 1299 | 1329 | (or similar monotype). Indeed, the only way to know whether to apply
|
| 1300 | 1330 | the monomorphism restriction at all is to instantiate
|
| 1301 | 1331 | |
| 1302 | -HOWEVER, not always! Here are places where we want `IIF_Sigma` meaning
|
|
| 1303 | -"return a sigma-type":
|
|
| 1332 | +(IIR4) When -XDeepSubsumption is on, we /deeply/ instantiate. Why isn't
|
|
| 1333 | + top-instantiation enough? Answer: to accept the following program (T26225b) with
|
|
| 1334 | + -XDeepSubsumption, we need to deeply instantiate when inferring in checkResultTy:
|
|
| 1335 | + |
|
| 1336 | + f :: Int -> (forall a. a->a)
|
|
| 1337 | + g :: Int -> Bool -> Bool
|
|
| 1338 | + |
|
| 1339 | + test b = case b of
|
|
| 1340 | + True -> f
|
|
| 1341 | + False -> g
|
|
| 1342 | + |
|
| 1343 | + If we don't deeply instantiate in the branches of the case expression, we will
|
|
| 1344 | + try to unify the type of `f` with that of `g`, which fails. If we instead
|
|
| 1345 | + deeply instantiate `f`, we will fill the `InferResult` with `Int -> alpha -> alpha`
|
|
| 1346 | + which then successfully unifies with the type of `g` when we come to fill the
|
|
| 1347 | + `InferResult` hole a second time for the second case branch.
|
|
| 1348 | + |
|
| 1349 | +(IIR5) When inferring, even /without/ -XDeepSubsumption, we must deeply instantiate
|
|
| 1350 | + the types of data constructors. E.g
|
|
| 1351 | + data T = MkT Int int
|
|
| 1352 | + f = MkT 3
|
|
| 1353 | + We must infer MkT 3 :: Int ->{mu} T (fresh mu)
|
|
| 1354 | + and not MkT 3 :: Int ->{one} T
|
|
| 1355 | + See Note [Typechecking data constructors] in GHC.Tc.Gen.Head
|
|
| 1356 | + Hence the use of `getDeepSubsumptionFlag_DataConHead` in `checkResultTy`.
|
|
| 1357 | + |
|
| 1358 | +HOWEVER, `ir_inst` is not always `IIF_DeepRho`! Here are places when it isn't:
|
|
| 1304 | 1359 | |
| 1305 | 1360 | * IIF_Sigma: In GHC.Tc.Module.tcRnExpr, which implements GHCi's :type
|
| 1306 | 1361 | command, we want to return a completely uninstantiated type.
|
| ... | ... | @@ -1316,23 +1371,6 @@ HOWEVER, not always! Here are places where we want `IIF_Sigma` meaning |
| 1316 | 1371 | but /not/ deeply instantiate (#26331). See Note [View patterns and polymorphism]
|
| 1317 | 1372 | in GHC.Tc.Gen.Pat. This the only place we use IIF_ShallowRho.
|
| 1318 | 1373 | |
| 1319 | -Why do we want to deeply instantiate, ever? Why isn't top-instantiation enough?
|
|
| 1320 | -Answer: to accept the following program (T26225b) with -XDeepSubsumption, we
|
|
| 1321 | -need to deeply instantiate when inferring in checkResultTy:
|
|
| 1322 | - |
|
| 1323 | - f :: Int -> (forall a. a->a)
|
|
| 1324 | - g :: Int -> Bool -> Bool
|
|
| 1325 | - |
|
| 1326 | - test b =
|
|
| 1327 | - case b of
|
|
| 1328 | - True -> f
|
|
| 1329 | - False -> g
|
|
| 1330 | - |
|
| 1331 | -If we don't deeply instantiate in the branches of the case expression, we will
|
|
| 1332 | -try to unify the type of 'f' with that of 'g', which fails. If we instead
|
|
| 1333 | -deeply instantiate 'f', we will fill the 'InferResult' with 'Int -> alpha -> alpha'
|
|
| 1334 | -which then successfully unifies with the type of 'g' when we come to fill the
|
|
| 1335 | -'InferResult' hole a second time for the second case branch.
|
|
| 1336 | 1374 | -}
|
| 1337 | 1375 | |
| 1338 | 1376 | {-
|
| ... | ... | @@ -2068,24 +2106,14 @@ getDeepSubsumptionFlag = |
| 2068 | 2106 | -- | Variant of 'getDeepSubsumptionFlag' which enables a top-level subsumption
|
| 2069 | 2107 | -- in order to implement the plan of Note [Typechecking data constructors].
|
| 2070 | 2108 | getDeepSubsumptionFlag_DataConHead :: HsExpr GhcTc -> TcM DeepSubsumptionFlag
|
| 2071 | -getDeepSubsumptionFlag_DataConHead app_head =
|
|
| 2072 | - do { user_ds <- xoptM LangExt.DeepSubsumption
|
|
| 2073 | - ; traceTc "getDeepSubsumptionFlag_DataConHead" (ppr app_head)
|
|
| 2074 | - ; return $
|
|
| 2075 | - if | user_ds
|
|
| 2076 | - -> Deep DeepSub
|
|
| 2077 | - | otherwise
|
|
| 2078 | - -> go app_head
|
|
| 2079 | - }
|
|
| 2109 | +getDeepSubsumptionFlag_DataConHead app_head
|
|
| 2110 | + = do { user_ds <- xoptM LangExt.DeepSubsumption
|
|
| 2111 | + ; return $ if | user_ds -> Deep DeepSub
|
|
| 2112 | + | dc_head app_head -> Deep TopSub
|
|
| 2113 | + | otherwise -> Shallow }
|
|
| 2080 | 2114 | where
|
| 2081 | - go :: HsExpr GhcTc -> DeepSubsumptionFlag
|
|
| 2082 | - go (XExpr (ConLikeTc (RealDataCon {}))) = Deep TopSub
|
|
| 2083 | - go (XExpr (ExpandedThingTc (HSE _ (L _ f)))) = go f
|
|
| 2084 | - go (XExpr (WrapExpr _ f)) = go f
|
|
| 2085 | - go (HsApp _ f _) = go (unLoc f)
|
|
| 2086 | - go (HsAppType _ f _) = go (unLoc f)
|
|
| 2087 | - go _ = Shallow
|
|
| 2088 | - |
|
| 2115 | + dc_head (XExpr (ConLikeTc (RealDataCon {}))) = True
|
|
| 2116 | + dc_head _ = False
|
|
| 2089 | 2117 | |
| 2090 | 2118 | -- | 'tc_sub_type_deep' is where the actual work happens for deep subsumption.
|
| 2091 | 2119 | --
|
| ... | ... | @@ -832,6 +832,7 @@ Library |
| 832 | 832 | GHC.Tc.Gen.Bind
|
| 833 | 833 | GHC.Tc.Gen.Default
|
| 834 | 834 | GHC.Tc.Gen.Do
|
| 835 | + GHC.Tc.Gen.Expand
|
|
| 835 | 836 | GHC.Tc.Gen.Export
|
| 836 | 837 | GHC.Tc.Gen.Expr
|
| 837 | 838 | GHC.Tc.Gen.Foreign
|
| 1 | 1 | [1 of 2] Compiling Splices ( Splices.hs, Splices.o )
|
| 2 | 2 | [2 of 2] Compiling SplicesUsed ( SplicesUsed.hs, SplicesUsed.o )
|
| 3 | - |
|
| 4 | 3 | SplicesUsed.hs:7:15: warning: [GHC-88464] [-Wpartial-type-signatures (in -Wdefault)]
|
| 5 | 4 | • Found type wildcard ‘_’ standing for ‘Maybe Bool’
|
| 6 | 5 | • In the type signature: maybeBool :: _
|
| ... | ... | @@ -21,8 +20,7 @@ SplicesUsed.hs:8:26: warning: [GHC-88464] [-Wpartial-type-signatures (in -Wdefau |
| 21 | 20 | • Found type wildcard ‘_’ standing for ‘Bool’
|
| 22 | 21 | • In the first argument of ‘Maybe’, namely ‘_’
|
| 23 | 22 | In an expression type signature: Maybe _
|
| 24 | - In the first argument of ‘id :: _a -> _a’, namely
|
|
| 25 | - ‘(Just True :: Maybe _)’
|
|
| 23 | + In the expression: Just True :: Maybe _
|
|
| 26 | 24 | • Relevant bindings include
|
| 27 | 25 | maybeBool :: Maybe Bool (bound at SplicesUsed.hs:8:1)
|
| 28 | 26 | |
| ... | ... | @@ -78,3 +76,4 @@ SplicesUsed.hs:18:2: warning: [GHC-88464] [-Wpartial-type-signatures (in -Wdefau |
| 78 | 76 | the inferred type of bar :: Bool -> w -> (Bool, w)
|
| 79 | 77 | at SplicesUsed.hs:18:2-11
|
| 80 | 78 | • In the type signature: bar :: _a -> _b -> (_a, _b)
|
| 79 | + |