Simon Peyton Jones pushed to branch wip/ani/no-ds-flag-cache at Glasgow Haskell Compiler / GHC
Commits:
-
b5adc452
by Simon Peyton Jones at 2026-03-30T17:45:43+01:00
4 changed files:
- compiler/GHC/Tc/Gen/App.hs
- − compiler/GHC/Tc/Gen/App.hs-boot
- compiler/GHC/Tc/Gen/Head.hs
- compiler/GHC/Tc/Utils/Unify.hs
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
|
| 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) |
| ... | ... | @@ -445,8 +445,7 @@ tcInferAppHead (fun,fun_lspan) |
| 445 | 445 | do { mb_tc_fun <- tcInferAppHead_maybe fun
|
| 446 | 446 | ; case mb_tc_fun of
|
| 447 | 447 | Just (fun', fun_sigma) -> return (fun', fun_sigma)
|
| 448 | - Nothing -> tcInferExprSigma fun
|
|
| 449 | - |
|
| 448 | + Nothing -> tcInferExprSigma fun
|
|
| 450 | 449 | }
|
| 451 | 450 | |
| 452 | 451 | tcInferAppHead_maybe :: HsExpr GhcRn
|
| ... | ... | @@ -456,24 +455,11 @@ tcInferAppHead_maybe :: HsExpr GhcRn |
| 456 | 455 | -- XExpr's although complicated needs to be looked through, useful for QL things when
|
| 457 | 456 | -- the argument is an XExpr
|
| 458 | 457 | tcInferAppHead_maybe fun = case fun of
|
| 459 | - HsVar _ nm
|
|
| 460 | - -> Just <$> tcInferId nm
|
|
| 461 | - ExprWithTySig _ e hs_ty
|
|
| 462 | - -> Just <$>tcExprWithSig e hs_ty
|
|
| 463 | - HsOverLit _ lit
|
|
| 464 | - -> Just <$> tcInferOverLit lit
|
|
| 465 | - XExpr (HsRecSelRn f)
|
|
| 466 | - -> Just <$> tcInferRecSelId f
|
|
| 467 | - |
|
| 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
|
|
| 458 | + HsVar _ nm -> Just <$> tcInferId nm
|
|
| 459 | + ExprWithTySig _ e hs_ty -> Just <$>tcExprWithSig e hs_ty
|
|
| 460 | + HsOverLit _ lit -> Just <$> tcInferOverLit lit
|
|
| 461 | + XExpr (HsRecSelRn f) -> Just <$> tcInferRecSelId f
|
|
| 462 | + _ -> return Nothing
|
|
| 477 | 463 | |
| 478 | 464 | {- *********************************************************************
|
| 479 | 465 | * *
|
| ... | ... | @@ -1261,17 +1261,28 @@ we simply look to see if the hole is filled already. |
| 1261 | 1261 | But consider
|
| 1262 | 1262 | case x of
|
| 1263 | 1263 | True -> True
|
| 1264 | - False -> error "urk"
|
|
| 1264 | + False -> undefined
|
|
| 1265 | 1265 | and suppose we call `tcInferSigma` on this expression, so that the `ir_inst`
|
| 1266 | 1266 | field of the expected result type is `IIF_Sigma`. The danger is that we'll
|
| 1267 | 1267 | fill the hole with `Bool` (from the `True`) and then reject when we try to
|
| 1268 | -unify that with `forall a. a->a`, from the call to `error`.
|
|
| 1268 | +unify that with `forall a. a->a`, from the call to `undefined`.
|
|
| 1269 | + |
|
| 1270 | +Another example:
|
|
| 1271 | + case x of
|
|
| 1272 | + True -> (e1 :: forall a b. a->b)
|
|
| 1273 | + False -> (e3 :: forall b a. a->b)
|
|
| 1269 | 1274 | |
| 1270 | 1275 | To avoid this, we never infer a sigma-type from a multi-branch `case`. Instead
|
| 1271 | 1276 | we just zap the `IIF_Sigma` to `IIF_DeepRho` when walking inside the branches
|
| 1272 | 1277 | of multi-arm case-expression, or an if-expression. See calls to
|
| 1273 | 1278 | `adjustExpTypeForCaseBranches`.
|
| 1274 | 1279 | |
| 1280 | +This does mean that this would work:
|
|
| 1281 | + (let x = 77+55 in h x x) @Int
|
|
| 1282 | +where
|
|
| 1283 | + h :: Int -> Int -> forall a. a->a
|
|
| 1284 | +The `@Int` would instantiate the `forall a`.
|
|
| 1285 | + |
|
| 1275 | 1286 | Note that
|
| 1276 | 1287 | case e of
|
| 1277 | 1288 | True -> hr
|