+ sig_tys = case maybe_rhs_sig of { Just t -> [t]; Nothing -> [] }
+ ++ collectSigTysFromPats pats
+
+ tc_match expected_ty -- Any sig tyvars are in scope by now
+ = -- STEP 1: Typecheck the patterns
+ tcMatchPats pats expected_ty `thenTc` \ (rhs_ty, pats', lie_req1, ex_tvs, pat_bndrs, lie_avail) ->
+ let
+ xve2 = bagToList pat_bndrs
+ pat_ids = map snd xve2
+ in
+
+ -- STEP 2: Check that the remaining "expected type" is not a rank-2 type
+ -- If it is it'll mess up the unifier when checking the RHS
+ checkTc (isTauTy rhs_ty) lurkingRank2SigErr `thenTc_`
+
+ -- STEP 3: Unify with the rhs type signature if any
+ (case maybe_rhs_sig of
+ Nothing -> returnTc ()
+ Just sig -> tcHsSigType sig `thenTc` \ sig_ty ->
+
+ -- Check that the signature isn't a polymorphic one, which
+ -- we don't permit (at present, anyway)
+ checkTc (isTauTy sig_ty) (polyPatSig sig_ty) `thenTc_`
+ unifyTauTy rhs_ty sig_ty
+ ) `thenTc_`
+
+ -- STEP 4: Typecheck the guarded RHSs and the associated where clause
+ tcExtendLocalValEnv xve1 (tcExtendLocalValEnv xve2 (
+ tcGRHSs grhss rhs_ty ctxt
+ )) `thenTc` \ (grhss', lie_req2) ->
+
+ -- STEP 5: Check for existentially bound type variables
+ tcCheckExistentialPat pat_ids ex_tvs lie_avail
+ (lie_req1 `plusLIE` lie_req2)
+ rhs_ty `thenTc` \ (lie_req', ex_binds) ->
+
+ -- Phew! All done.
+ let
+ match' = Match [] pats' Nothing (glue_on Recursive ex_binds grhss')
+ in
+ returnTc (pat_ids, (match', lie_req'))
+
+ -- glue_on just avoids stupid dross
+glue_on _ EmptyMonoBinds grhss = grhss -- The common case
+glue_on is_rec mbinds (GRHSs grhss binds ty)
+ = GRHSs grhss (mkMonoBind mbinds [] is_rec `ThenBinds` binds) ty
+
+tcGRHSs :: RenamedGRHSs
+ -> TcType -> HsMatchContext
+ -> TcM (TcGRHSs, LIE)
+
+tcGRHSs (GRHSs grhss binds _) expected_ty ctxt
+ = tcBindsAndThen glue_on binds (tc_grhss grhss)
+ where
+ tc_grhss grhss
+ = mapAndUnzipTc tc_grhs grhss `thenTc` \ (grhss', lies) ->
+ returnTc (GRHSs grhss' EmptyBinds (Just expected_ty), plusLIEs lies)
+
+ tc_grhs (GRHS guarded locn)
+ = tcAddSrcLoc locn $
+ tcStmts ctxt (\ty -> ty, expected_ty) guarded `thenTc` \ (guarded', lie) ->
+ returnTc (GRHS guarded' locn, lie)
+
+
+tcCheckExistentialPat :: [TcId] -- Ids bound by this pattern
+ -> Bag TcTyVar -- Existentially quantified tyvars bound by pattern
+ -> LIE -- and context
+ -> LIE -- Required context
+ -> TcType -- and result type; vars in here must not escape
+ -> TcM (LIE, TcDictBinds) -- LIE to float out and dict bindings
+tcCheckExistentialPat ids ex_tvs lie_avail lie_req result_ty
+ | isEmptyBag ex_tvs && all not_overloaded ids
+ -- Short cut for case when there are no existentials
+ -- and no polymorphic overloaded variables
+ -- e.g. f :: (forall a. Ord a => a -> a) -> Int -> Int
+ -- f op x = ....
+ -- Here we must discharge op Methods
+ = ASSERT( isEmptyLIE lie_avail )
+ returnTc (lie_req, EmptyMonoBinds)
+
+ | otherwise
+ = tcExtendGlobalTyVars (tyVarsOfType result_ty) $
+ tcAddErrCtxtM (sigPatCtxt tv_list ids) $
+
+ -- In case there are any polymorpic, overloaded binders in the pattern
+ -- (which can happen in the case of rank-2 type signatures, or data constructors
+ -- with polymorphic arguments), we must do a bindInstsOfLocalFns here
+ bindInstsOfLocalFuns lie_req ids `thenTc` \ (lie1, inst_binds) ->
+
+ -- Deal with overloaded functions bound by the pattern
+ tcSimplifyCheck doc tv_list
+ (lieToList lie_avail) lie1 `thenTc` \ (lie2, dict_binds) ->
+ checkSigTyVars tv_list emptyVarSet `thenTc_`
+
+ returnTc (lie2, dict_binds `AndMonoBinds` inst_binds)
+ where
+ doc = text ("the existential context of a data constructor")
+ tv_list = bagToList ex_tvs
+ not_overloaded id = case splitSigmaTy (idType id) of
+ (_, theta, _) -> null theta