| ... |
... |
@@ -329,7 +329,7 @@ tcPatBndr penv@(PE { pe_ctxt = LetPat { pc_lvl = bind_lvl |
|
329
|
329
|
-- Note [Typechecking pattern bindings] in GHC.Tc.Gen.Bind
|
|
330
|
330
|
|
|
331
|
331
|
| Just bndr_id <- sig_fn bndr_name -- There is a signature
|
|
332
|
|
- = do { wrap <- tc_sub_type penv (scaledThing exp_pat_ty) (idType bndr_id)
|
|
|
332
|
+ = do { wrap <- tcSubTypePat_GenSigCtxt penv (scaledThing exp_pat_ty) (idType bndr_id)
|
|
333
|
333
|
-- See Note [Subsumption check at pattern variables]
|
|
334
|
334
|
; traceTc "tcPatBndr(sig)" (ppr bndr_id $$ ppr (idType bndr_id) $$ ppr exp_pat_ty)
|
|
335
|
335
|
; return (wrap, bndr_id) }
|
| ... |
... |
@@ -376,10 +376,12 @@ newLetBndr LetLclBndr name w ty |
|
376
|
376
|
newLetBndr (LetGblBndr prags) name w ty
|
|
377
|
377
|
= addInlinePrags (mkLocalId name w ty) (lookupPragEnv prags name)
|
|
378
|
378
|
|
|
379
|
|
-tc_sub_type :: PatEnv -> ExpSigmaType -> TcSigmaType -> TcM HsWrapper
|
|
380
|
|
--- tcSubTypeET with the UserTypeCtxt specialised to GenSigCtxt
|
|
381
|
|
--- Used during typechecking patterns
|
|
382
|
|
-tc_sub_type penv t1 t2 = tcSubTypePat (pe_orig penv) GenSigCtxt t1 t2
|
|
|
379
|
+-- | A version of 'tcSubTypePat' specialised to 'GenSigCtxt'.
|
|
|
380
|
+--
|
|
|
381
|
+-- Used during typechecking of patterns.
|
|
|
382
|
+tcSubTypePat_GenSigCtxt :: PatEnv -> ExpSigmaType -> TcSigmaType -> TcM HsWrapper
|
|
|
383
|
+tcSubTypePat_GenSigCtxt penv t1 t2 =
|
|
|
384
|
+ tcSubTypePat (pe_orig penv) GenSigCtxt t1 t2
|
|
383
|
385
|
|
|
384
|
386
|
{- Note [Subsumption check at pattern variables]
|
|
385
|
387
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| ... |
... |
@@ -618,111 +620,123 @@ tc_pat :: Scaled ExpSigmaTypeFRR |
|
618
|
620
|
-> Checker (Pat GhcRn) (Pat GhcTc)
|
|
619
|
621
|
-- ^ Translated pattern
|
|
620
|
622
|
|
|
621
|
|
-tc_pat pat_ty penv ps_pat thing_inside = case ps_pat of
|
|
622
|
|
-
|
|
623
|
|
- VarPat x (L l name) -> do
|
|
624
|
|
- { (wrap, id) <- tcPatBndr penv name pat_ty
|
|
625
|
|
- ; res <- tcCheckUsage name (scaledMult pat_ty) $
|
|
626
|
|
- tcExtendIdEnv1 name id thing_inside
|
|
627
|
|
- ; pat_ty <- readExpType (scaledThing pat_ty)
|
|
628
|
|
- ; return (mkHsWrapPat wrap (VarPat x (L l id)) pat_ty, res) }
|
|
629
|
|
-
|
|
630
|
|
- ParPat x pat -> do
|
|
631
|
|
- { (pat', res) <- tc_lpat pat_ty penv pat thing_inside
|
|
632
|
|
- ; return (ParPat x pat', res) }
|
|
633
|
|
-
|
|
634
|
|
- BangPat x pat -> do
|
|
635
|
|
- { (pat', res) <- tc_lpat pat_ty penv pat thing_inside
|
|
636
|
|
- ; return (BangPat x pat', res) }
|
|
637
|
|
-
|
|
638
|
|
- OrPat _ pats -> do -- See Note [Implementation of OrPatterns], Typechecker (1)
|
|
639
|
|
- { let pats_list = NE.toList pats
|
|
640
|
|
- ; (pats_list', (res, pat_ct)) <- tc_lpats (map (const pat_ty) pats_list) penv pats_list (captureConstraints thing_inside)
|
|
641
|
|
- ; let pats' = NE.fromList pats_list' -- tc_lpats preserves non-emptiness
|
|
642
|
|
- ; emitConstraints pat_ct
|
|
643
|
|
- -- captureConstraints/extendConstraints:
|
|
644
|
|
- -- like in Note [Hopping the LIE in lazy patterns]
|
|
645
|
|
- ; pat_ty <- expTypeToType (scaledThing pat_ty)
|
|
646
|
|
- ; return (OrPat pat_ty pats', res) }
|
|
647
|
|
-
|
|
648
|
|
- LazyPat x pat -> do
|
|
649
|
|
- { checkManyPattern LazyPatternReason (noLocA ps_pat) pat_ty
|
|
650
|
|
- ; (pat', (res, pat_ct))
|
|
651
|
|
- <- tc_lpat pat_ty (makeLazy penv) pat $
|
|
652
|
|
- captureConstraints thing_inside
|
|
653
|
|
- -- Ignore refined penv', revert to penv
|
|
654
|
|
-
|
|
655
|
|
- ; emitConstraints pat_ct
|
|
656
|
|
- -- captureConstraints/extendConstraints:
|
|
657
|
|
- -- see Note [Hopping the LIE in lazy patterns]
|
|
658
|
|
-
|
|
659
|
|
- -- Check that the expected pattern type is itself lifted
|
|
660
|
|
- ; pat_ty <- readExpType (scaledThing pat_ty)
|
|
661
|
|
- ; _ <- unifyType Nothing (typeKind pat_ty) liftedTypeKind
|
|
662
|
|
-
|
|
663
|
|
- ; return ((LazyPat x pat'), res) }
|
|
664
|
|
-
|
|
665
|
|
- WildPat _ -> do
|
|
666
|
|
- { checkManyPattern OtherPatternReason (noLocA ps_pat) pat_ty
|
|
667
|
|
- ; res <- thing_inside
|
|
668
|
|
- ; pat_ty <- expTypeToType (scaledThing pat_ty)
|
|
669
|
|
- ; return (WildPat pat_ty, res) }
|
|
670
|
|
-
|
|
671
|
|
- AsPat x (L nm_loc name) pat -> do
|
|
672
|
|
- { checkManyPattern OtherPatternReason (noLocA ps_pat) pat_ty
|
|
673
|
|
- ; (wrap, bndr_id) <- setSrcSpanA nm_loc (tcPatBndr penv name pat_ty)
|
|
674
|
|
- ; (pat', res) <- tcExtendIdEnv1 name bndr_id $
|
|
675
|
|
- tc_lpat (pat_ty `scaledSet`(mkCheckExpType $ idType bndr_id))
|
|
676
|
|
- penv pat thing_inside
|
|
677
|
|
- -- NB: if we do inference on:
|
|
678
|
|
- -- \ (y@(x::forall a. a->a)) = e
|
|
679
|
|
- -- we'll fail. The as-pattern infers a monotype for 'y', which then
|
|
680
|
|
- -- fails to unify with the polymorphic type for 'x'. This could
|
|
681
|
|
- -- perhaps be fixed, but only with a bit more work.
|
|
682
|
|
- --
|
|
683
|
|
- -- If you fix it, don't forget the bindInstsOfPatIds!
|
|
684
|
|
- ; pat_ty <- readExpType (scaledThing pat_ty)
|
|
685
|
|
- ; return (mkHsWrapPat wrap (AsPat x (L nm_loc bndr_id) pat') pat_ty, res) }
|
|
686
|
|
-
|
|
687
|
|
- ViewPat _ expr pat -> do
|
|
688
|
|
- { checkManyPattern ViewPatternReason (noLocA ps_pat) pat_ty
|
|
689
|
|
- --
|
|
690
|
|
- -- It should be possible to have view patterns at linear (or otherwise
|
|
691
|
|
- -- non-Many) multiplicity. But it is not clear at the moment what
|
|
692
|
|
- -- restriction need to be put in place, if any, for linear view
|
|
693
|
|
- -- patterns to desugar to type-correct Core.
|
|
694
|
|
-
|
|
695
|
|
- ; (expr', expr_rho) <- tcInferExpr IIF_ShallowRho expr
|
|
696
|
|
- -- IIF_ShallowRho: do not perform deep instantiation, regardless of
|
|
697
|
|
- -- DeepSubsumption (Note [View patterns and polymorphism])
|
|
698
|
|
- -- But we must do top-instantiation to expose the arrow to matchActualFunTy
|
|
699
|
|
-
|
|
700
|
|
- -- Expression must be a function
|
|
701
|
|
- ; let herald = ExpectedFunTyViewPat $ unLoc expr
|
|
702
|
|
- ; (expr_co1, Scaled _mult inf_arg_ty, inf_res_sigma)
|
|
703
|
|
- <- matchActualFunTy herald (Just . HsExprRnThing $ unLoc expr) (1,expr_rho) expr_rho
|
|
704
|
|
- -- See Note [View patterns and polymorphism]
|
|
705
|
|
- -- expr_wrap1 :: expr_rho "->" (inf_arg_ty -> inf_res_sigma)
|
|
706
|
|
-
|
|
707
|
|
- -- Check that overall pattern is more polymorphic than arg type
|
|
708
|
|
- ; expr_wrap2 <- tc_sub_type penv (scaledThing pat_ty) inf_arg_ty
|
|
709
|
|
- -- expr_wrap2 :: pat_ty "->" inf_arg_ty
|
|
710
|
|
-
|
|
711
|
|
- -- Pattern must have inf_res_sigma
|
|
712
|
|
- ; (pat', res) <- tc_lpat (pat_ty `scaledSet` mkCheckExpType inf_res_sigma) penv pat thing_inside
|
|
713
|
|
-
|
|
714
|
|
- ; let Scaled w h_pat_ty = pat_ty
|
|
715
|
|
- ; pat_ty <- readExpType h_pat_ty
|
|
716
|
|
- ; let expr_wrap2' = mkWpFun expr_wrap2 idHsWrapper
|
|
717
|
|
- (Scaled w pat_ty) inf_res_sigma
|
|
718
|
|
- -- expr_wrap2' :: (inf_arg_ty -> inf_res_sigma) "->"
|
|
719
|
|
- -- (pat_ty -> inf_res_sigma)
|
|
720
|
|
- -- NB: pat_ty comes from matchActualFunTy, so it has a
|
|
721
|
|
- -- fixed RuntimeRep, as needed to call mkWpFun.
|
|
722
|
|
-
|
|
723
|
|
- expr_wrap = expr_wrap2' <.> mkWpCastN expr_co1
|
|
724
|
|
-
|
|
725
|
|
- ; return $ (ViewPat pat_ty (mkLHsWrap expr_wrap expr') pat', res) }
|
|
|
623
|
+tc_pat scaled_exp_pat_ty@(Scaled w_pat exp_pat_ty) penv ps_pat thing_inside =
|
|
|
624
|
+
|
|
|
625
|
+ case ps_pat of
|
|
|
626
|
+
|
|
|
627
|
+ VarPat x (L l name) -> do
|
|
|
628
|
+ { (wrap, id) <- tcPatBndr penv name scaled_exp_pat_ty
|
|
|
629
|
+ ; res <- tcCheckUsage name w_pat $ tcExtendIdEnv1 name id thing_inside
|
|
|
630
|
+ ; pat_ty <- readExpType exp_pat_ty
|
|
|
631
|
+ ; return (mkHsWrapPat wrap (VarPat x (L l id)) pat_ty, res) }
|
|
|
632
|
+
|
|
|
633
|
+ ParPat x pat -> do
|
|
|
634
|
+ { (pat', res) <- tc_lpat scaled_exp_pat_ty penv pat thing_inside
|
|
|
635
|
+ ; return (ParPat x pat', res) }
|
|
|
636
|
+
|
|
|
637
|
+ BangPat x pat -> do
|
|
|
638
|
+ { (pat', res) <- tc_lpat scaled_exp_pat_ty penv pat thing_inside
|
|
|
639
|
+ ; return (BangPat x pat', res) }
|
|
|
640
|
+
|
|
|
641
|
+ OrPat _ pats -> do -- See Note [Implementation of OrPatterns], Typechecker (1)
|
|
|
642
|
+ { let pats_list = NE.toList pats
|
|
|
643
|
+ pat_exp_tys = map (const scaled_exp_pat_ty) pats_list
|
|
|
644
|
+ ; (pats_list', (res, pat_ct)) <- tc_lpats pat_exp_tys penv pats_list (captureConstraints thing_inside)
|
|
|
645
|
+ ; let pats' = NE.fromList pats_list' -- tc_lpats preserves non-emptiness
|
|
|
646
|
+ ; emitConstraints pat_ct
|
|
|
647
|
+ -- captureConstraints/extendConstraints:
|
|
|
648
|
+ -- like in Note [Hopping the LIE in lazy patterns]
|
|
|
649
|
+ ; pat_ty <- expTypeToType exp_pat_ty
|
|
|
650
|
+ ; return (OrPat pat_ty pats', res) }
|
|
|
651
|
+
|
|
|
652
|
+ LazyPat x pat -> do
|
|
|
653
|
+ { checkManyPattern LazyPatternReason (noLocA ps_pat) scaled_exp_pat_ty
|
|
|
654
|
+ ; (pat', (res, pat_ct))
|
|
|
655
|
+ <- tc_lpat scaled_exp_pat_ty (makeLazy penv) pat $
|
|
|
656
|
+ captureConstraints thing_inside
|
|
|
657
|
+ -- Ignore refined penv', revert to penv
|
|
|
658
|
+
|
|
|
659
|
+ ; emitConstraints pat_ct
|
|
|
660
|
+ -- captureConstraints/extendConstraints:
|
|
|
661
|
+ -- see Note [Hopping the LIE in lazy patterns]
|
|
|
662
|
+
|
|
|
663
|
+ -- Check that the expected pattern type is itself lifted
|
|
|
664
|
+ ; pat_ty <- readExpType exp_pat_ty
|
|
|
665
|
+ ; _ <- unifyType Nothing (typeKind pat_ty) liftedTypeKind
|
|
|
666
|
+
|
|
|
667
|
+ ; return ((LazyPat x pat'), res) }
|
|
|
668
|
+
|
|
|
669
|
+ WildPat _ -> do
|
|
|
670
|
+ { checkManyPattern OtherPatternReason (noLocA ps_pat) scaled_exp_pat_ty
|
|
|
671
|
+ ; res <- thing_inside
|
|
|
672
|
+ ; pat_ty <- expTypeToType exp_pat_ty
|
|
|
673
|
+ ; return (WildPat pat_ty, res) }
|
|
|
674
|
+
|
|
|
675
|
+ AsPat x (L nm_loc name) pat -> do
|
|
|
676
|
+ { checkManyPattern OtherPatternReason (noLocA ps_pat) scaled_exp_pat_ty
|
|
|
677
|
+ ; (wrap, bndr_id) <- setSrcSpanA nm_loc (tcPatBndr penv name scaled_exp_pat_ty)
|
|
|
678
|
+ ; (pat', res) <- tcExtendIdEnv1 name bndr_id $
|
|
|
679
|
+ tc_lpat (Scaled w_pat (mkCheckExpType $ idType bndr_id))
|
|
|
680
|
+ penv pat thing_inside
|
|
|
681
|
+ -- NB: if we do inference on:
|
|
|
682
|
+ -- \ (y@(x::forall a. a->a)) = e
|
|
|
683
|
+ -- we'll fail. The as-pattern infers a monotype for 'y', which then
|
|
|
684
|
+ -- fails to unify with the polymorphic type for 'x'. This could
|
|
|
685
|
+ -- perhaps be fixed, but only with a bit more work.
|
|
|
686
|
+ --
|
|
|
687
|
+ -- If you fix it, don't forget the bindInstsOfPatIds!
|
|
|
688
|
+ ; pat_ty <- readExpType exp_pat_ty
|
|
|
689
|
+ ; return (mkHsWrapPat wrap (AsPat x (L nm_loc bndr_id) pat') pat_ty, res) }
|
|
|
690
|
+
|
|
|
691
|
+ ViewPat _ view_expr inner_pat -> do
|
|
|
692
|
+
|
|
|
693
|
+ -- The pattern is a view pattern, 'pat = (view_expr -> inner_pat)'.
|
|
|
694
|
+ -- First infer the type of 'view_expr'; the overall type of the pattern
|
|
|
695
|
+ -- is the argument type of 'view_expr', and the inner pattern type is
|
|
|
696
|
+ -- checked against the result type of 'view_expr'.
|
|
|
697
|
+
|
|
|
698
|
+ { checkManyPattern ViewPatternReason (noLocA ps_pat) scaled_exp_pat_ty
|
|
|
699
|
+ -- It should be possible to have view patterns at linear (or otherwise
|
|
|
700
|
+ -- non-Many) multiplicity. But it is not clear at the moment what
|
|
|
701
|
+ -- restrictions need to be put in place, if any, for linear view
|
|
|
702
|
+ -- patterns to desugar to type-correct Core.
|
|
|
703
|
+
|
|
|
704
|
+ -- Infer the type of 'view_expr'.
|
|
|
705
|
+ ; (view_expr', view_expr_rho) <- tcInferExpr IIF_ShallowRho view_expr
|
|
|
706
|
+ -- IIF_ShallowRho: do not perform deep instantiation, regardless of
|
|
|
707
|
+ -- DeepSubsumption (Note [View patterns and polymorphism])
|
|
|
708
|
+ -- But we must do top-instantiation to expose the arrow to matchActualFunTy
|
|
|
709
|
+
|
|
|
710
|
+ -- 'view_expr' must be a function; expose its argument/result types
|
|
|
711
|
+ -- using 'matchActualFunTy'.
|
|
|
712
|
+ ; let herald = ExpectedFunTyViewPat $ unLoc view_expr
|
|
|
713
|
+ ; (view_expr_co1, Scaled _mult view_arg_ty, view_res_ty)
|
|
|
714
|
+ <- matchActualFunTy herald (Just . HsExprRnThing $ unLoc view_expr)
|
|
|
715
|
+ (1, view_expr_rho) view_expr_rho
|
|
|
716
|
+ -- See Note [View patterns and polymorphism]
|
|
|
717
|
+ -- view_expr_co1 :: view_expr_rho ~~> (view_arg_ty -> view_res_ty)
|
|
|
718
|
+
|
|
|
719
|
+ -- Check that the overall pattern's type is more polymorphic than
|
|
|
720
|
+ -- the view function argument type.
|
|
|
721
|
+ ; view_expr_wrap2 <- tcSubTypePat_GenSigCtxt penv exp_pat_ty view_arg_ty
|
|
|
722
|
+ -- view_expr_wrap2 :: pat_ty ~~> view_arg_ty
|
|
|
723
|
+
|
|
|
724
|
+ -- The inner pattern must have type 'view_res_ty'.
|
|
|
725
|
+ ; (inner_pat', res) <- tc_lpat (Scaled w_pat (mkCheckExpType view_res_ty)) penv inner_pat thing_inside
|
|
|
726
|
+
|
|
|
727
|
+ ; pat_ty <- readExpType exp_pat_ty
|
|
|
728
|
+ ; let view_expr_wrap2' =
|
|
|
729
|
+ mkWpFun view_expr_wrap2 idHsWrapper
|
|
|
730
|
+ (Scaled w_pat pat_ty) view_res_ty
|
|
|
731
|
+ -- view_expr_wrap2' :: (view_arg_ty -> view_res_ty)
|
|
|
732
|
+ -- ~~> (pat_ty -> view_res_ty)
|
|
|
733
|
+ -- This satisfies WpFun-FRR-INVARIANT:
|
|
|
734
|
+ -- 'view_arg_ty' was returned by matchActualFunTy, hence FRR
|
|
|
735
|
+ -- 'pat_ty' was passed in and is an 'ExpSigmaTypeFRR'
|
|
|
736
|
+
|
|
|
737
|
+ view_expr_wrap = view_expr_wrap2' <.> mkWpCastN view_expr_co1
|
|
|
738
|
+
|
|
|
739
|
+ ; return $ (ViewPat pat_ty (mkLHsWrap view_expr_wrap view_expr') inner_pat', res) }
|
|
726
|
740
|
|
|
727
|
741
|
{- Note [View patterns and polymorphism]
|
|
728
|
742
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
| ... |
... |
@@ -748,93 +762,91 @@ Another example is #26331. |
|
748
|
762
|
|
|
749
|
763
|
-- Type signatures in patterns
|
|
750
|
764
|
-- See Note [Pattern coercions] below
|
|
751
|
|
- SigPat _ pat sig_ty -> do
|
|
752
|
|
- { (inner_ty, tv_binds, wcs, wrap) <- tcPatSig (inPatBind penv)
|
|
753
|
|
- sig_ty (scaledThing pat_ty)
|
|
754
|
|
- -- Using tcExtendNameTyVarEnv is appropriate here
|
|
755
|
|
- -- because we're not really bringing fresh tyvars into scope.
|
|
756
|
|
- -- We're *naming* existing tyvars. Note that it is OK for a tyvar
|
|
757
|
|
- -- from an outer scope to mention one of these tyvars in its kind.
|
|
758
|
|
- ; (pat', res) <- tcExtendNameTyVarEnv wcs $
|
|
759
|
|
- tcExtendNameTyVarEnv tv_binds $
|
|
760
|
|
- tc_lpat (pat_ty `scaledSet` mkCheckExpType inner_ty) penv pat thing_inside
|
|
761
|
|
- ; pat_ty <- readExpType (scaledThing pat_ty)
|
|
762
|
|
- ; return (mkHsWrapPat wrap (SigPat inner_ty pat' sig_ty) pat_ty, res) }
|
|
|
765
|
+ SigPat _ pat sig_ty -> do
|
|
|
766
|
+ { (inner_ty, tv_binds, wcs, wrap) <-
|
|
|
767
|
+ tcPatSig (inPatBind penv) sig_ty exp_pat_ty
|
|
|
768
|
+ -- Using tcExtendNameTyVarEnv is appropriate here
|
|
|
769
|
+ -- because we're not really bringing fresh tyvars into scope.
|
|
|
770
|
+ -- We're *naming* existing tyvars. Note that it is OK for a tyvar
|
|
|
771
|
+ -- from an outer scope to mention one of these tyvars in its kind.
|
|
|
772
|
+ ; (pat', res) <- tcExtendNameTyVarEnv wcs $
|
|
|
773
|
+ tcExtendNameTyVarEnv tv_binds $
|
|
|
774
|
+ tc_lpat (Scaled w_pat $ mkCheckExpType inner_ty) penv pat thing_inside
|
|
|
775
|
+ ; pat_ty <- readExpType exp_pat_ty
|
|
|
776
|
+ ; return (mkHsWrapPat wrap (SigPat inner_ty pat' sig_ty) pat_ty, res) }
|
|
763
|
777
|
|
|
764
|
778
|
------------------------
|
|
765
|
779
|
-- Lists, tuples, arrays
|
|
766
|
780
|
|
|
767
|
781
|
-- Necessarily a built-in list pattern, not an overloaded list pattern.
|
|
768
|
782
|
-- See Note [Desugaring overloaded list patterns].
|
|
769
|
|
- ListPat _ pats -> do
|
|
770
|
|
- { (coi, elt_ty) <- matchExpectedPatTy matchExpectedListTy penv (scaledThing pat_ty)
|
|
771
|
|
- ; (pats', res) <- tcMultiple (tc_lpat (pat_ty `scaledSet` mkCheckExpType elt_ty))
|
|
772
|
|
- penv pats thing_inside
|
|
773
|
|
- ; pat_ty <- readExpType (scaledThing pat_ty)
|
|
774
|
|
- ; return (mkHsWrapPat coi
|
|
775
|
|
- (ListPat elt_ty pats') pat_ty, res) }
|
|
776
|
|
-
|
|
777
|
|
- TuplePat _ pats boxity -> do
|
|
778
|
|
- { let arity = length pats
|
|
779
|
|
- tc = tupleTyCon boxity arity
|
|
780
|
|
- -- NB: tupleTyCon does not flatten 1-tuples
|
|
781
|
|
- -- See Note [Don't flatten tuples from HsSyn] in GHC.Core.Make
|
|
782
|
|
- ; checkTupSize arity
|
|
783
|
|
- ; (coi, arg_tys) <- matchExpectedPatTy (matchExpectedTyConApp tc)
|
|
784
|
|
- penv (scaledThing pat_ty)
|
|
785
|
|
- -- Unboxed tuples have RuntimeRep vars, which we discard:
|
|
786
|
|
- -- See Note [Unboxed tuple RuntimeRep vars] in GHC.Core.TyCon
|
|
787
|
|
- ; let con_arg_tys = case boxity of Unboxed -> drop arity arg_tys
|
|
788
|
|
- Boxed -> arg_tys
|
|
789
|
|
- ; (pats', res) <- tc_lpats (map (scaledSet pat_ty . mkCheckExpType) con_arg_tys)
|
|
|
783
|
+ ListPat _ pats -> do
|
|
|
784
|
+ { (coi, elt_ty) <- matchExpectedPatTy matchExpectedListTy penv exp_pat_ty
|
|
|
785
|
+ ; (pats', res) <- tcMultiple (tc_lpat (Scaled w_pat $ mkCheckExpType elt_ty))
|
|
790
|
786
|
penv pats thing_inside
|
|
791
|
|
-
|
|
792
|
|
- ; dflags <- getDynFlags
|
|
793
|
|
-
|
|
794
|
|
- -- Under flag control turn a pattern (x,y,z) into ~(x,y,z)
|
|
795
|
|
- -- so that we can experiment with lazy tuple-matching.
|
|
796
|
|
- -- This is a pretty odd place to make the switch, but
|
|
797
|
|
- -- it was easy to do.
|
|
798
|
|
- ; let
|
|
799
|
|
- unmangled_result = TuplePat con_arg_tys pats' boxity
|
|
800
|
|
- -- pat_ty /= pat_ty iff coi /= IdCo
|
|
801
|
|
- possibly_mangled_result
|
|
802
|
|
- | gopt Opt_IrrefutableTuples dflags &&
|
|
803
|
|
- isBoxed boxity = LazyPat noExtField (noLocA unmangled_result)
|
|
804
|
|
- | otherwise = unmangled_result
|
|
805
|
|
-
|
|
806
|
|
- ; pat_ty <- readExpType (scaledThing pat_ty)
|
|
807
|
|
- ; massert (con_arg_tys `equalLength` pats) -- Syntactically enforced
|
|
808
|
|
- ; return (mkHsWrapPat coi possibly_mangled_result pat_ty, res)
|
|
809
|
|
- }
|
|
810
|
|
-
|
|
811
|
|
- SumPat _ pat alt arity -> do
|
|
812
|
|
- { let tc = sumTyCon arity
|
|
813
|
|
- ; (coi, arg_tys) <- matchExpectedPatTy (matchExpectedTyConApp tc)
|
|
814
|
|
- penv (scaledThing pat_ty)
|
|
815
|
|
- ; -- Drop levity vars, we don't care about them here
|
|
816
|
|
- let con_arg_tys = drop arity arg_tys
|
|
817
|
|
- ; (pat', res) <- tc_lpat (pat_ty `scaledSet` mkCheckExpType (con_arg_tys `getNth` (alt - 1)))
|
|
818
|
|
- penv pat thing_inside
|
|
819
|
|
- ; pat_ty <- readExpType (scaledThing pat_ty)
|
|
820
|
|
- ; return (mkHsWrapPat coi (SumPat con_arg_tys pat' alt arity) pat_ty
|
|
821
|
|
- , res)
|
|
822
|
|
- }
|
|
|
787
|
+ ; pat_ty <- readExpType exp_pat_ty
|
|
|
788
|
+ ; return (mkHsWrapPat coi
|
|
|
789
|
+ (ListPat elt_ty pats') pat_ty, res) }
|
|
|
790
|
+
|
|
|
791
|
+ TuplePat _ pats boxity -> do
|
|
|
792
|
+ { let arity = length pats
|
|
|
793
|
+ tc = tupleTyCon boxity arity
|
|
|
794
|
+ -- NB: tupleTyCon does not flatten 1-tuples
|
|
|
795
|
+ -- See Note [Don't flatten tuples from HsSyn] in GHC.Core.Make
|
|
|
796
|
+ ; checkTupSize arity
|
|
|
797
|
+ ; (coi, arg_tys) <- matchExpectedPatTy (matchExpectedTyConApp tc) penv exp_pat_ty
|
|
|
798
|
+ -- Unboxed tuples have RuntimeRep vars, which we discard:
|
|
|
799
|
+ -- See Note [Unboxed tuple RuntimeRep vars] in GHC.Core.TyCon
|
|
|
800
|
+ ; let con_arg_tys = case boxity of Unboxed -> drop arity arg_tys
|
|
|
801
|
+ Boxed -> arg_tys
|
|
|
802
|
+ ; (pats', res) <- tc_lpats (map (Scaled w_pat . mkCheckExpType) con_arg_tys)
|
|
|
803
|
+ penv pats thing_inside
|
|
|
804
|
+
|
|
|
805
|
+ ; dflags <- getDynFlags
|
|
|
806
|
+
|
|
|
807
|
+ -- Under flag control turn a pattern (x,y,z) into ~(x,y,z)
|
|
|
808
|
+ -- so that we can experiment with lazy tuple-matching.
|
|
|
809
|
+ -- This is a pretty odd place to make the switch, but
|
|
|
810
|
+ -- it was easy to do.
|
|
|
811
|
+ ; let
|
|
|
812
|
+ unmangled_result = TuplePat con_arg_tys pats' boxity
|
|
|
813
|
+ -- pat_ty /= pat_ty iff coi /= IdCo
|
|
|
814
|
+ possibly_mangled_result
|
|
|
815
|
+ | gopt Opt_IrrefutableTuples dflags &&
|
|
|
816
|
+ isBoxed boxity = LazyPat noExtField (noLocA unmangled_result)
|
|
|
817
|
+ | otherwise = unmangled_result
|
|
|
818
|
+
|
|
|
819
|
+ ; pat_ty <- readExpType exp_pat_ty
|
|
|
820
|
+ ; massert (con_arg_tys `equalLength` pats) -- Syntactically enforced
|
|
|
821
|
+ ; return (mkHsWrapPat coi possibly_mangled_result pat_ty, res)
|
|
|
822
|
+ }
|
|
|
823
|
+
|
|
|
824
|
+ SumPat _ pat alt arity -> do
|
|
|
825
|
+ { let tc = sumTyCon arity
|
|
|
826
|
+ ; (coi, arg_tys) <- matchExpectedPatTy (matchExpectedTyConApp tc) penv exp_pat_ty
|
|
|
827
|
+ ; -- Drop levity vars, we don't care about them here
|
|
|
828
|
+ let con_arg_tys = drop arity arg_tys
|
|
|
829
|
+ ; (pat', res) <- tc_lpat (Scaled w_pat $ mkCheckExpType (con_arg_tys `getNth` (alt - 1)))
|
|
|
830
|
+ penv pat thing_inside
|
|
|
831
|
+ ; pat_ty <- readExpType exp_pat_ty
|
|
|
832
|
+ ; return (mkHsWrapPat coi (SumPat con_arg_tys pat' alt arity) pat_ty
|
|
|
833
|
+ , res)
|
|
|
834
|
+ }
|
|
823
|
835
|
|
|
824
|
836
|
------------------------
|
|
825
|
837
|
-- Data constructors
|
|
826
|
|
- ConPat _ con arg_pats ->
|
|
827
|
|
- tcConPat penv con pat_ty arg_pats thing_inside
|
|
|
838
|
+ ConPat _ con arg_pats ->
|
|
|
839
|
+ tcConPat penv con scaled_exp_pat_ty arg_pats thing_inside
|
|
828
|
840
|
|
|
829
|
841
|
------------------------
|
|
830
|
842
|
-- Literal patterns
|
|
831
|
|
- LitPat x simple_lit -> do
|
|
832
|
|
- { let lit_ty = hsLitType simple_lit
|
|
833
|
|
- ; wrap <- tc_sub_type penv (scaledThing pat_ty) lit_ty
|
|
834
|
|
- ; res <- thing_inside
|
|
835
|
|
- ; pat_ty <- readExpType (scaledThing pat_ty)
|
|
836
|
|
- ; return ( mkHsWrapPat wrap (LitPat x (convertLit simple_lit)) pat_ty
|
|
837
|
|
- , res) }
|
|
|
843
|
+ LitPat x simple_lit -> do
|
|
|
844
|
+ { let lit_ty = hsLitType simple_lit
|
|
|
845
|
+ ; wrap <- tcSubTypePat_GenSigCtxt penv exp_pat_ty lit_ty
|
|
|
846
|
+ ; res <- thing_inside
|
|
|
847
|
+ ; pat_ty <- readExpType exp_pat_ty
|
|
|
848
|
+ ; return ( mkHsWrapPat wrap (LitPat x (convertLit simple_lit)) pat_ty
|
|
|
849
|
+ , res) }
|
|
838
|
850
|
|
|
839
|
851
|
------------------------
|
|
840
|
852
|
-- Overloaded patterns: n, and n+k
|
| ... |
... |
@@ -854,31 +866,31 @@ Another example is #26331. |
|
854
|
866
|
-- where lit_ty is the type of the overloaded literal 5.
|
|
855
|
867
|
--
|
|
856
|
868
|
-- When there is no negation, neg_lit_ty and lit_ty are the same
|
|
857
|
|
- NPat _ (L l over_lit) mb_neg eq -> do
|
|
858
|
|
- { checkManyPattern OtherPatternReason (noLocA ps_pat) pat_ty
|
|
859
|
|
- -- It may be possible to refine linear pattern so that they work in
|
|
860
|
|
- -- linear environments. But it is not clear how useful this is.
|
|
861
|
|
- ; let orig = LiteralOrigin over_lit
|
|
862
|
|
- ; ((lit', mb_neg'), eq')
|
|
863
|
|
- <- tcSyntaxOp orig eq [SynType (scaledThing pat_ty), SynAny]
|
|
864
|
|
- (mkCheckExpType boolTy) $
|
|
865
|
|
- \ [neg_lit_ty] _ ->
|
|
866
|
|
- let new_over_lit lit_ty = newOverloadedLit over_lit
|
|
867
|
|
- (mkCheckExpType lit_ty)
|
|
868
|
|
- in case mb_neg of
|
|
869
|
|
- Nothing -> (, Nothing) <$> new_over_lit neg_lit_ty
|
|
870
|
|
- Just neg -> -- Negative literal
|
|
871
|
|
- -- The 'negate' is re-mappable syntax
|
|
872
|
|
- second Just <$>
|
|
873
|
|
- (tcSyntaxOp orig neg [SynRho] (mkCheckExpType neg_lit_ty) $
|
|
874
|
|
- \ [lit_ty] _ -> new_over_lit lit_ty)
|
|
875
|
|
- -- applied to a closed literal: linearity doesn't matter as
|
|
876
|
|
- -- literals are typed in an empty environment, hence have
|
|
877
|
|
- -- all multiplicities.
|
|
878
|
|
-
|
|
879
|
|
- ; res <- thing_inside
|
|
880
|
|
- ; pat_ty <- readExpType (scaledThing pat_ty)
|
|
881
|
|
- ; return (NPat pat_ty (L l lit') mb_neg' eq', res) }
|
|
|
869
|
+ NPat _ (L l over_lit) mb_neg eq -> do
|
|
|
870
|
+ { checkManyPattern OtherPatternReason (noLocA ps_pat) scaled_exp_pat_ty
|
|
|
871
|
+ -- It may be possible to refine linear pattern so that they work in
|
|
|
872
|
+ -- linear environments. But it is not clear how useful this is.
|
|
|
873
|
+ ; let orig = LiteralOrigin over_lit
|
|
|
874
|
+ ; ((lit', mb_neg'), eq')
|
|
|
875
|
+ <- tcSyntaxOp orig eq [SynType exp_pat_ty, SynAny]
|
|
|
876
|
+ (mkCheckExpType boolTy) $
|
|
|
877
|
+ \ [neg_lit_ty] _ ->
|
|
|
878
|
+ let new_over_lit lit_ty = newOverloadedLit over_lit
|
|
|
879
|
+ (mkCheckExpType lit_ty)
|
|
|
880
|
+ in case mb_neg of
|
|
|
881
|
+ Nothing -> (, Nothing) <$> new_over_lit neg_lit_ty
|
|
|
882
|
+ Just neg -> -- Negative literal
|
|
|
883
|
+ -- The 'negate' is re-mappable syntax
|
|
|
884
|
+ second Just <$>
|
|
|
885
|
+ (tcSyntaxOp orig neg [SynRho] (mkCheckExpType neg_lit_ty) $
|
|
|
886
|
+ \ [lit_ty] _ -> new_over_lit lit_ty)
|
|
|
887
|
+ -- applied to a closed literal: linearity doesn't matter as
|
|
|
888
|
+ -- literals are typed in an empty environment, hence have
|
|
|
889
|
+ -- all multiplicities.
|
|
|
890
|
+
|
|
|
891
|
+ ; res <- thing_inside
|
|
|
892
|
+ ; pat_ty <- readExpType exp_pat_ty
|
|
|
893
|
+ ; return (NPat pat_ty (L l lit') mb_neg' eq', res) }
|
|
882
|
894
|
|
|
883
|
895
|
{-
|
|
884
|
896
|
Note [NPlusK patterns]
|
| ... |
... |
@@ -904,68 +916,67 @@ AST is used for the subtraction operation. |
|
904
|
916
|
-}
|
|
905
|
917
|
|
|
906
|
918
|
-- See Note [NPlusK patterns]
|
|
907
|
|
- NPlusKPat _ (L nm_loc name)
|
|
908
|
|
- (L loc lit) _ ge minus -> do
|
|
909
|
|
- { checkManyPattern OtherPatternReason (noLocA ps_pat) pat_ty
|
|
910
|
|
- ; let pat_exp_ty = scaledThing pat_ty
|
|
911
|
|
- orig = LiteralOrigin lit
|
|
912
|
|
- ; (lit1', ge')
|
|
913
|
|
- <- tcSyntaxOp orig ge [SynType pat_exp_ty, SynRho]
|
|
914
|
|
- (mkCheckExpType boolTy) $
|
|
915
|
|
- \ [lit1_ty] _ ->
|
|
916
|
|
- newOverloadedLit lit (mkCheckExpType lit1_ty)
|
|
917
|
|
- ; ((lit2', minus_wrap, bndr_id), minus')
|
|
918
|
|
- <- tcSyntaxOpGen orig minus [SynType pat_exp_ty, SynRho] SynAny $
|
|
919
|
|
- \ [lit2_ty, var_ty] _ ->
|
|
920
|
|
- do { lit2' <- newOverloadedLit lit (mkCheckExpType lit2_ty)
|
|
921
|
|
- ; (wrap, bndr_id) <- setSrcSpanA nm_loc $
|
|
922
|
|
- tcPatBndr penv name (unrestricted $ mkCheckExpType var_ty)
|
|
923
|
|
- -- co :: var_ty ~ idType bndr_id
|
|
924
|
|
-
|
|
925
|
|
- -- minus_wrap is applicable to minus'
|
|
926
|
|
- ; return (lit2', wrap, bndr_id) }
|
|
927
|
|
-
|
|
928
|
|
- ; pat_ty <- readExpType pat_exp_ty
|
|
929
|
|
-
|
|
930
|
|
- -- The Report says that n+k patterns must be in Integral
|
|
931
|
|
- -- but it's silly to insist on this in the RebindableSyntax case
|
|
932
|
|
- ; unlessM (xoptM LangExt.RebindableSyntax) $
|
|
933
|
|
- do { icls <- tcLookupClass integralClassName
|
|
934
|
|
- ; instStupidTheta orig [mkClassPred icls [pat_ty]] }
|
|
935
|
|
-
|
|
936
|
|
- ; res <- tcExtendIdEnv1 name bndr_id thing_inside
|
|
937
|
|
-
|
|
938
|
|
- ; let minus'' = case minus' of
|
|
939
|
|
- NoSyntaxExprTc -> pprPanic "tc_pat NoSyntaxExprTc" (ppr minus')
|
|
940
|
|
- -- this should be statically avoidable
|
|
941
|
|
- -- Case (3) from Note [NoSyntaxExpr] in "GHC.Hs.Expr"
|
|
942
|
|
- SyntaxExprTc { syn_expr = minus'_expr
|
|
943
|
|
- , syn_arg_wraps = minus'_arg_wraps
|
|
944
|
|
- , syn_res_wrap = minus'_res_wrap }
|
|
945
|
|
- -> SyntaxExprTc { syn_expr = minus'_expr
|
|
946
|
|
- , syn_arg_wraps = minus'_arg_wraps
|
|
947
|
|
- , syn_res_wrap = minus_wrap <.> minus'_res_wrap }
|
|
948
|
|
- -- Oy. This should really be a record update, but
|
|
949
|
|
- -- we get warnings if we try. #17783
|
|
950
|
|
- pat' = NPlusKPat pat_ty (L nm_loc bndr_id) (L loc lit1') lit2'
|
|
951
|
|
- ge' minus''
|
|
952
|
|
- ; return (pat', res) }
|
|
|
919
|
+ NPlusKPat _ (L nm_loc name)
|
|
|
920
|
+ (L loc lit) _ ge minus -> do
|
|
|
921
|
+ { checkManyPattern OtherPatternReason (noLocA ps_pat) scaled_exp_pat_ty
|
|
|
922
|
+ ; let orig = LiteralOrigin lit
|
|
|
923
|
+ ; (lit1', ge')
|
|
|
924
|
+ <- tcSyntaxOp orig ge [SynType exp_pat_ty, SynRho]
|
|
|
925
|
+ (mkCheckExpType boolTy) $
|
|
|
926
|
+ \ [lit1_ty] _ ->
|
|
|
927
|
+ newOverloadedLit lit (mkCheckExpType lit1_ty)
|
|
|
928
|
+ ; ((lit2', minus_wrap, bndr_id), minus')
|
|
|
929
|
+ <- tcSyntaxOpGen orig minus [SynType exp_pat_ty, SynRho] SynAny $
|
|
|
930
|
+ \ [lit2_ty, var_ty] _ ->
|
|
|
931
|
+ do { lit2' <- newOverloadedLit lit (mkCheckExpType lit2_ty)
|
|
|
932
|
+ ; (wrap, bndr_id) <- setSrcSpanA nm_loc $
|
|
|
933
|
+ tcPatBndr penv name (unrestricted $ mkCheckExpType var_ty)
|
|
|
934
|
+ -- co :: var_ty ~ idType bndr_id
|
|
|
935
|
+
|
|
|
936
|
+ -- minus_wrap is applicable to minus'
|
|
|
937
|
+ ; return (lit2', wrap, bndr_id) }
|
|
|
938
|
+
|
|
|
939
|
+ ; pat_ty <- readExpType exp_pat_ty
|
|
|
940
|
+
|
|
|
941
|
+ -- The Report says that n+k patterns must be in Integral
|
|
|
942
|
+ -- but it's silly to insist on this in the RebindableSyntax case
|
|
|
943
|
+ ; unlessM (xoptM LangExt.RebindableSyntax) $
|
|
|
944
|
+ do { icls <- tcLookupClass integralClassName
|
|
|
945
|
+ ; instStupidTheta orig [mkClassPred icls [pat_ty]] }
|
|
|
946
|
+
|
|
|
947
|
+ ; res <- tcExtendIdEnv1 name bndr_id thing_inside
|
|
|
948
|
+
|
|
|
949
|
+ ; let minus'' = case minus' of
|
|
|
950
|
+ NoSyntaxExprTc -> pprPanic "tc_pat NoSyntaxExprTc" (ppr minus')
|
|
|
951
|
+ -- this should be statically avoidable
|
|
|
952
|
+ -- Case (3) from Note [NoSyntaxExpr] in "GHC.Hs.Expr"
|
|
|
953
|
+ SyntaxExprTc { syn_expr = minus'_expr
|
|
|
954
|
+ , syn_arg_wraps = minus'_arg_wraps
|
|
|
955
|
+ , syn_res_wrap = minus'_res_wrap }
|
|
|
956
|
+ -> SyntaxExprTc { syn_expr = minus'_expr
|
|
|
957
|
+ , syn_arg_wraps = minus'_arg_wraps
|
|
|
958
|
+ , syn_res_wrap = minus_wrap <.> minus'_res_wrap }
|
|
|
959
|
+ -- Oy. This should really be a record update, but
|
|
|
960
|
+ -- we get warnings if we try. #17783
|
|
|
961
|
+ pat' = NPlusKPat pat_ty (L nm_loc bndr_id) (L loc lit1') lit2'
|
|
|
962
|
+ ge' minus''
|
|
|
963
|
+ ; return (pat', res) }
|
|
953
|
964
|
|
|
954
|
965
|
-- Here we get rid of it and add the finalizers to the global environment.
|
|
955
|
966
|
-- See Note [Delaying modFinalizers in untyped splices] in GHC.Rename.Splice.
|
|
956
|
|
- SplicePat (HsUntypedSpliceTop mod_finalizers pat) _ -> do
|
|
|
967
|
+ SplicePat (HsUntypedSpliceTop mod_finalizers pat) _ -> do
|
|
957
|
968
|
{ addModFinalizersWithLclEnv mod_finalizers
|
|
958
|
|
- ; tc_pat pat_ty penv pat thing_inside }
|
|
|
969
|
+ ; tc_pat scaled_exp_pat_ty penv pat thing_inside }
|
|
959
|
970
|
|
|
960
|
|
- SplicePat (HsUntypedSpliceNested _) _ -> panic "tc_pat: nested splice in splice pat"
|
|
|
971
|
+ SplicePat (HsUntypedSpliceNested _) _ -> panic "tc_pat: nested splice in splice pat"
|
|
961
|
972
|
|
|
962
|
|
- EmbTyPat _ _ -> failWith TcRnIllegalTypePattern
|
|
|
973
|
+ EmbTyPat _ _ -> failWith TcRnIllegalTypePattern
|
|
963
|
974
|
|
|
964
|
|
- InvisPat _ _ -> panic "tc_pat: invisible pattern appears recursively in the pattern"
|
|
|
975
|
+ InvisPat _ _ -> panic "tc_pat: invisible pattern appears recursively in the pattern"
|
|
965
|
976
|
|
|
966
|
|
- XPat (HsPatExpanded lpat rpat) -> do
|
|
967
|
|
- { (rpat', res) <- tc_pat pat_ty penv rpat thing_inside
|
|
968
|
|
- ; return (XPat $ ExpansionPat lpat rpat', res) }
|
|
|
977
|
+ XPat (HsPatExpanded lpat rpat) -> do
|
|
|
978
|
+ { (rpat', res) <- tc_pat scaled_exp_pat_ty penv rpat thing_inside
|
|
|
979
|
+ ; return (XPat $ ExpansionPat lpat rpat', res) }
|
|
969
|
980
|
|
|
970
|
981
|
{-
|
|
971
|
982
|
Note [Hopping the LIE in lazy patterns]
|
| ... |
... |
@@ -1295,7 +1306,7 @@ tcPatSynPat (L con_span con_name) pat_syn pat_ty penv arg_pats thing_inside |
|
1295
|
1306
|
|
|
1296
|
1307
|
; (univ_ty_args, ex_ty_args, val_arg_pats) <- splitConTyArgs con_like arg_pats
|
|
1297
|
1308
|
|
|
1298
|
|
- ; wrap <- tc_sub_type penv (scaledThing pat_ty) ty'
|
|
|
1309
|
+ ; wrap <- tcSubTypePat_GenSigCtxt penv (scaledThing pat_ty) ty'
|
|
1299
|
1310
|
|
|
1300
|
1311
|
; traceTc "tcPatSynPat" $
|
|
1301
|
1312
|
vcat [ text "Pat syn:" <+> ppr pat_syn
|
| ... |
... |
@@ -1405,8 +1416,9 @@ matchExpectedConTy :: PatEnv |
|
1405
|
1416
|
-- In the case of a data family, this would
|
|
1406
|
1417
|
-- mention the /family/ TyCon
|
|
1407
|
1418
|
-> TcM (HsWrapper, [TcSigmaType])
|
|
1408
|
|
--- See Note [Matching constructor patterns]
|
|
1409
|
|
--- Returns a wrapper : pat_ty "->" T ty1 ... tyn
|
|
|
1419
|
+-- ^ See Note [Matching constructor patterns]
|
|
|
1420
|
+--
|
|
|
1421
|
+-- Returns a wrapper : pat_ty ~~> T ty1 ... tyn
|
|
1410
|
1422
|
matchExpectedConTy (PE { pe_orig = orig }) data_tc exp_pat_ty
|
|
1411
|
1423
|
| Just (fam_tc, fam_args, co_tc) <- tyConFamInstSig_maybe data_tc
|
|
1412
|
1424
|
-- Comments refer to Note [Matching constructor patterns]
|