- do_bind expected_record_ty (field_label, rhs_pat, pun_flag)
- = tcLookupGlobalValue field_label `thenNF_Tc` \ sel_id ->
- tcInstId sel_id `thenNF_Tc` \ (_, _, tau) ->
-
- -- Record selectors all have type
- -- forall a1..an. T a1 .. an -> tau
- ASSERT( maybeToBool (getFunTy_maybe tau) )
- let
- -- Selector must have type RecordType -> FieldType
- Just (record_ty, field_ty) = getFunTy_maybe tau
- in
- tcAddErrCtxt (recordLabel field_label) (
- unifyTauTy expected_record_ty record_ty
- ) `thenTc_`
- tcPat rhs_pat `thenTc` \ (rhs_pat', lie, rhs_ty) ->
- tcAddErrCtxt (recordRhs field_label rhs_pat) (
- unifyTauTy field_ty rhs_ty
- ) `thenTc_`
- returnTc ((sel_id, rhs_pat', pun_flag), lie)
+ tc_fields field_tys []
+ = returnTc ([], emptyLIE, emptyBag, emptyBag, emptyLIE)
+
+ tc_fields field_tys ((field_label, rhs_pat, pun_flag) : rpats)
+ = tc_fields field_tys rpats `thenTc` \ (rpats', lie_req1, tvs1, ids1, lie_avail1) ->
+
+ (case [ty | (f,ty) <- field_tys, f == field_label] of
+
+ -- No matching field; chances are this field label comes from some
+ -- other record type (or maybe none). As well as reporting an
+ -- error we still want to typecheck the pattern, principally to
+ -- make sure that all the variables it binds are put into the
+ -- environment, else the type checker crashes later:
+ -- f (R { foo = (a,b) }) = a+b
+ -- If foo isn't one of R's fields, we don't want to crash when
+ -- typechecking the "a+b".
+ [] -> addErrTc (badFieldCon name field_label) `thenNF_Tc_`
+ newTyVarTy liftedTypeKind `thenNF_Tc_`
+ returnTc (error "Bogus selector Id", pat_ty)
+
+ -- The normal case, when the field comes from the right constructor
+ (pat_ty : extras) ->
+ ASSERT( null extras )
+ tcLookupGlobalId field_label `thenNF_Tc` \ sel_id ->
+ returnTc (sel_id, pat_ty)
+ ) `thenTc` \ (sel_id, pat_ty) ->
+
+ tcPat tc_bndr rhs_pat pat_ty `thenTc` \ (rhs_pat', lie_req2, tvs2, ids2, lie_avail2) ->
+
+ returnTc ((sel_id, rhs_pat', pun_flag) : rpats',
+ lie_req1 `plusLIE` lie_req2,
+ tvs1 `unionBags` tvs2,
+ ids1 `unionBags` ids2,
+ lie_avail1 `plusLIE` lie_avail2)