| ... |
... |
@@ -11,7 +11,7 @@ |
|
11
|
11
|
module GHC.Tc.Utils.Unify (
|
|
12
|
12
|
-- Full-blown subsumption
|
|
13
|
13
|
tcWrapResult, tcWrapResultO, tcWrapResultMono,
|
|
14
|
|
- tcSubType, tcSubTypeSigma, tcSubTypePat, tcSubTypeDS, tcSubTypeHoleFit,
|
|
|
14
|
+ tcSubType, tcSubTypeSigma, tcSubTypePat, tcSubTypeApp, tcSubTypeHoleFit,
|
|
15
|
15
|
addSubTypeCtxt,
|
|
16
|
16
|
tcSubTypeAmbiguity, tcSubMult,
|
|
17
|
17
|
checkConstraints, checkTvConstraints,
|
| ... |
... |
@@ -1488,24 +1488,62 @@ tcSubTypePat _ _ (Infer inf_res) ty_expected |
|
1488
|
1488
|
|
|
1489
|
1489
|
---------------
|
|
1490
|
1490
|
|
|
1491
|
|
--- | A subtype check that performs deep subsumption.
|
|
1492
|
|
--- See also 'tcSubTypeMono', for when no instantiation is required.
|
|
1493
|
|
-tcSubTypeDS :: HsExpr GhcTc -- ^ App head (for error messages only)
|
|
1494
|
|
- -> DeepSubsumptionDepth
|
|
1495
|
|
- -> HsExpr GhcRn
|
|
1496
|
|
- -> TcRhoType -- ^ Actual type; a rho-type, not a sigma-type
|
|
1497
|
|
- -> TcRhoType -- ^ Expected type
|
|
1498
|
|
- -- DeepSubsumption <=> when checking, this type
|
|
1499
|
|
- -- is deeply skolemised
|
|
1500
|
|
- -> TcM HsWrapper
|
|
1501
|
|
--- Only one call site, in GHC.Tc.Gen.App.checkResultTy
|
|
1502
|
|
-tcSubTypeDS tc_fun ds_depth rn_expr act_rho exp_rho
|
|
1503
|
|
- = do { wrap <- tc_sub_type_deep (Just tc_fun, Top) ds_depth
|
|
1504
|
|
- (unifyExprType rn_expr)
|
|
1505
|
|
- (exprCtOrigin rn_expr)
|
|
1506
|
|
- GenSigCtxt act_rho exp_rho
|
|
|
1491
|
+-- | Connect up the inferred type of an application with the expected type.
|
|
|
1492
|
+-- This is usually just a unification, but with deep subsumption there is more to do.
|
|
|
1493
|
+tcSubTypeApp :: HsExpr GhcRn
|
|
|
1494
|
+ -> HsExpr GhcTc -- Head
|
|
|
1495
|
+ -> TcRhoType -- Inferred type of the application; zonked to
|
|
|
1496
|
+ -- expose foralls, but maybe not /deeply/ instantiated
|
|
|
1497
|
+ -> ExpRhoType -- Expected type; this is deeply skolemised
|
|
|
1498
|
+ -> TcM HsWrapper
|
|
|
1499
|
+tcSubTypeApp rn_expr tc_fun app_res_rho (Infer inf_res)
|
|
|
1500
|
+ = do { ds_flag <- getDeepSubsumptionFlag_DataConHead tc_fun
|
|
|
1501
|
+ -- Why the "DataConHead" bit? See (IIR5) in
|
|
|
1502
|
+ -- Note [Instantiation of InferResult] in GHC.Tc.Utils.Unify.
|
|
|
1503
|
+ ; fillInferResult ds_flag (exprCtOrigin rn_expr) app_res_rho inf_res }
|
|
|
1504
|
+
|
|
|
1505
|
+tcSubTypeApp rn_expr tc_fun app_res_rho (Check exp_rho)
|
|
|
1506
|
+ = do { ds_flag <- getDeepSubsumptionFlag_DataConHead tc_fun
|
|
|
1507
|
+ ; traceTc "tcSubTypeApp {" $
|
|
|
1508
|
+ vcat [ text "tc_fun:" <+> ppr tc_fun
|
|
|
1509
|
+ , text "app_res_rho:" <+> ppr app_res_rho
|
|
|
1510
|
+ , text "exp_rho:" <+> ppr exp_rho
|
|
|
1511
|
+ , text "ds_flag:" <+> ppr ds_flag ]
|
|
|
1512
|
+ ; wrap <- case ds_flag of
|
|
|
1513
|
+ Shallow -- No deep subsumption
|
|
|
1514
|
+ -- app_res_rho and res_ty are both rho-types,
|
|
|
1515
|
+ -- so with simple subsumption we can just unify them
|
|
|
1516
|
+ -- No need to zonk; the unifier does that
|
|
|
1517
|
+ -> do { co <- unifyExprType rn_expr app_res_rho exp_rho
|
|
|
1518
|
+ ; return (mkWpCastN co) }
|
|
|
1519
|
+
|
|
|
1520
|
+ Deep ds_depth -- Deep subsumption is ON
|
|
|
1521
|
+ -- Even though both app_res_rho and res_ty are rho-types,
|
|
|
1522
|
+ -- they may have nested polymorphism, so if deep subsumption
|
|
|
1523
|
+ -- is on we must call tcSubType.
|
|
|
1524
|
+ -> tc_sub_type_deep (Just tc_fun, Top) ds_depth
|
|
|
1525
|
+ (unifyExprType rn_expr)
|
|
|
1526
|
+ (exprCtOrigin rn_expr)
|
|
|
1527
|
+ GenSigCtxt app_res_rho exp_rho
|
|
|
1528
|
+
|
|
|
1529
|
+ ; traceTc "tcSubTypeApp }" $
|
|
|
1530
|
+ vcat [ text "tc_fun:" <+> ppr tc_fun
|
|
|
1531
|
+ , text "wrap:" <+> ppr wrap ]
|
|
|
1532
|
+
|
|
1507
|
1533
|
; return (mkWpSubType wrap) }
|
|
1508
|
1534
|
|
|
|
1535
|
+-- | Variant of 'getDeepSubsumptionFlag' which enables a top-level subsumption
|
|
|
1536
|
+-- in order to implement the plan of Note [Typechecking data constructors].
|
|
|
1537
|
+getDeepSubsumptionFlag_DataConHead :: HsExpr GhcTc -> TcM DeepSubsumptionFlag
|
|
|
1538
|
+getDeepSubsumptionFlag_DataConHead app_head
|
|
|
1539
|
+ = do { user_ds <- xoptM LangExt.DeepSubsumption
|
|
|
1540
|
+ ; return $ if | user_ds
|
|
|
1541
|
+ -> Deep DeepSub
|
|
|
1542
|
+ | XExpr (ConLikeTc (RealDataCon {})) <- app_head
|
|
|
1543
|
+ -> Deep TopSub
|
|
|
1544
|
+ | otherwise
|
|
|
1545
|
+ -> Shallow }
|
|
|
1546
|
+
|
|
1509
|
1547
|
---------------
|
|
1510
|
1548
|
|
|
1511
|
1549
|
-- | Checks that the 'actual' type is more polymorphic than the 'expected' type.
|
| ... |
... |
@@ -2104,18 +2142,6 @@ getDeepSubsumptionFlag = |
|
2104
|
2142
|
else return Shallow
|
|
2105
|
2143
|
}
|
|
2106
|
2144
|
|
|
2107
|
|
--- | Variant of 'getDeepSubsumptionFlag' which enables a top-level subsumption
|
|
2108
|
|
--- in order to implement the plan of Note [Typechecking data constructors].
|
|
2109
|
|
-getDeepSubsumptionFlag_DataConHead :: HsExpr GhcTc -> TcM DeepSubsumptionFlag
|
|
2110
|
|
-getDeepSubsumptionFlag_DataConHead app_head
|
|
2111
|
|
- = do { user_ds <- xoptM LangExt.DeepSubsumption
|
|
2112
|
|
- ; return $ if | user_ds
|
|
2113
|
|
- -> Deep DeepSub
|
|
2114
|
|
- | XExpr (ConLikeTc (RealDataCon {})) <- app_head
|
|
2115
|
|
- -> Deep TopSub
|
|
2116
|
|
- | otherwise
|
|
2117
|
|
- -> Shallow }
|
|
2118
|
|
-
|
|
2119
|
2145
|
|
|
2120
|
2146
|
-- | 'tc_sub_type_deep' is where the actual work happens for deep subsumption.
|
|
2121
|
2147
|
--
|
| ... |
... |
@@ -2132,11 +2158,7 @@ tc_sub_type_deep :: HasDebugCallStack |
|
2132
|
2158
|
-> TcRhoType -- ^ Expected; deeply skolemised
|
|
2133
|
2159
|
-> TcM HsWrapper
|
|
2134
|
2160
|
tc_sub_type_deep fun_pos@(tc_fun, pos) ds_depth unify inst_orig ctxt ty_actual ty_expected
|
|
2135
|
|
- = assertPpr (isDeeplySkolemised ty_expected)
|
|
2136
|
|
- (vcat [ text "tc_sub_type_deep: expected type is not a deep rho type"
|
|
2137
|
|
- , text "ty_expected:" <+> ppr ty_expected
|
|
2138
|
|
- , text "ty_actual:" <+> ppr ty_actual
|
|
2139
|
|
- ]) $
|
|
|
2161
|
+ = assert_precondition $
|
|
2140
|
2162
|
do { traceTc "tc_sub_type_deep" $
|
|
2141
|
2163
|
vcat [ text "ty_actual =" <+> ppr ty_actual
|
|
2142
|
2164
|
, text "ty_expected =" <+> ppr ty_expected ]
|
| ... |
... |
@@ -2250,6 +2272,27 @@ tc_sub_type_deep fun_pos@(tc_fun, pos) ds_depth unify inst_orig ctxt ty_actual t |
|
2250
|
2272
|
where
|
|
2251
|
2273
|
given_orig = GivenOrigin (SigSkol GenSigCtxt exp_arg [])
|
|
2252
|
2274
|
|
|
|
2275
|
+ -- Assertion check.
|
|
|
2276
|
+ -- If DeepSubsumption is on (ds_depth = Deep DeepSub) then `exp_rho`
|
|
|
2277
|
+ -- should already be deeply skolemised; the assertion checks this
|
|
|
2278
|
+ -- But if DeepSubsumption is NOT on, but there is a data constructor at the
|
|
|
2279
|
+ -- head, we must still call `tc_sub_type_deep` (for the multiplicity arrows)
|
|
|
2280
|
+ -- Hence ds_flag = Deep TopSub, but `exp_rho` will only be /top-level/ skolemised
|
|
|
2281
|
+ -- So we can only check for top-level skolemisation (`isRhoTy`)
|
|
|
2282
|
+ -- Example of the latter (see #27210), with -XNoDeepSubsumption
|
|
|
2283
|
+ -- foo :: forall a. a -> forall b. b -> (a,b)
|
|
|
2284
|
+ -- foo = (,)
|
|
|
2285
|
+ -- We will only shallowly-skolemise the expected type
|
|
|
2286
|
+ assert_precondition = assertPpr ty_expected_is_ok $
|
|
|
2287
|
+ vcat [ text "tc_sub_type_deep: expected type is not a deep rho type"
|
|
|
2288
|
+ , text "ty_expected:" <+> ppr ty_expected
|
|
|
2289
|
+ , text "ty_actual:" <+> ppr ty_actual ]
|
|
|
2290
|
+ ty_expected_is_ok
|
|
|
2291
|
+ = case ds_depth of
|
|
|
2292
|
+ TopSub -> True
|
|
|
2293
|
+ DeepSub -> isDeeplySkolemised ty_expected
|
|
|
2294
|
+
|
|
|
2295
|
+
|
|
2253
|
2296
|
-- | Whether to do deep subsumption when recurring inside arguments.
|
|
2254
|
2297
|
recurInArgumentDSFlag :: DeepSubsumptionDepth -> DeepSubsumptionFlag
|
|
2255
|
2298
|
recurInArgumentDSFlag = \case
|
| ... |
... |
@@ -5145,5 +5188,3 @@ lookupCycleBreakerVar cbv (IS { inert_cycle_breakers = cbvs_stack }) |
|
5145
|
5188
|
= tyfam_app
|
|
5146
|
5189
|
| otherwise
|
|
5147
|
5190
|
= pprPanic "lookupCycleBreakerVar found an unbound cycle breaker" (ppr cbv $$ ppr cbvs_stack) |
|
5148
|
|
-
|
|
5149
|
|
--------------------------------------------------------------------------------- |