Simon Peyton Jones pushed to branch wip/T24464 at Glasgow Haskell Compiler / GHC

Commits:

9 changed files:

Changes:

  • compiler/GHC/Hs/Expr.hs
    ... ... @@ -361,8 +361,9 @@ type instance XProc (GhcPass _) = (EpToken "proc", TokRarrow)
    361 361
     
    
    362 362
     type instance XStatic        GhcPs = EpToken "static"
    
    363 363
     type instance XStatic        GhcRn = NoExtField
    
    364
    -type instance XStatic        GhcTc = Type
    
    365
    -  -- Type of expression, this is stored for convenience as wiring in
    
    364
    +type instance XStatic        GhcTc = (Type, HsExpr GhcTc)
    
    365
    +  -- Type of expression, and the (fromStaticPtr function)
    
    366
    +  -- These are stored for convenience as the wiring in
    
    366 367
       -- StaticPtr is a bit tricky (see #20150)
    
    367 368
     
    
    368 369
     type instance XEmbTy         GhcPs = EpToken "type"
    

  • compiler/GHC/Hs/Syn/Type.hs
    ... ... @@ -142,7 +142,7 @@ hsExprType e@(HsTypedSplice{}) = pprPanic "hsExprType: Unexpected HsTypedSplice"
    142 142
                                           -- than in the typechecked AST.
    
    143 143
     hsExprType (HsUntypedSplice ext _) = dataConCantHappen ext
    
    144 144
     hsExprType (HsProc _ _ lcmd_top) = lhsCmdTopType lcmd_top
    
    145
    -hsExprType (HsStatic ty _s) = ty
    
    145
    +hsExprType (HsStatic (ty,_) _s) = ty
    
    146 146
     hsExprType (HsPragE _ _ e) = lhsExprType e
    
    147 147
     hsExprType (HsEmbTy x _) = dataConCantHappen x
    
    148 148
     hsExprType (HsHole (_, (HER _ ty _))) = ty
    

  • compiler/GHC/HsToCore/Expr.hs
    ... ... @@ -465,22 +465,26 @@ dsExpr (ArithSeq expr witness seq)
    465 465
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    466 466
     See Note [Grand plan for static forms] in GHC.Iface.Tidy.StaticPtrTable
    
    467 467
     for an overview.
    
    468
    -    g = ... static e ...
    
    469
    -==>
    
    470
    -    s = /\abc. e
    
    471
    -    g = ... (s @a @b @c) ...
    
    468
    +        ... static{from_static_ptr} e ...
    
    469
    +    ==>
    
    470
    +        s = /\abc. makeStatic e
    
    471
    +        ... (from_static_ptr (s @a @b @c)) ...
    
    472
    +
    
    473
    +Here `from_static_ptr` is a suitably-instantiated instantiated version of
    
    474
    +the overloaded function `fromStaticPtr`.
    
    472 475
     -}
    
    473 476
     
    
    474
    -dsExpr (HsStatic whole_ty expr@(L loc _))
    
    477
    +dsExpr (HsStatic (static_ptr_ty, from_static_fun) expr@(L loc _))
    
    475 478
       = do { dflags <- getDynFlags
    
    476 479
     
    
    477 480
            ; make_static_id <- dsLookupGlobalId makeStaticName
    
    478 481
            ; expr_ds        <- dsLExpr expr
    
    482
    +       ; from_static_ds <- dsExpr from_static_fun
    
    479 483
     
    
    480 484
            -- The static expression can have free type variables,
    
    481 485
            -- which we should quantify.  We can also have free Ids,
    
    482 486
            -- but they will be bound at top level
    
    483
    -       ; let (_, [ty]) = splitTyConApp whole_ty
    
    487
    +       ; let (_, [ty]) = splitTyConApp static_ptr_ty
    
    484 488
     
    
    485 489
                  static_fvs :: [Var]
    
    486 490
                  static_fvs = scopedSort $
    
    ... ... @@ -498,11 +502,11 @@ dsExpr (HsStatic whole_ty expr@(L loc _))
    498 502
                  static_rhs = mkLams static_fvs $
    
    499 503
                               mkCoreApps (Var make_static_id) [ Type ty, srcLoc, expr_ds ]
    
    500 504
     
    
    501
    -       ; static_id <- newStaticId (mkSpecForAllTys static_fvs whole_ty)
    
    505
    +       ; static_id <- newStaticId (mkSpecForAllTys static_fvs static_ptr_ty)
    
    502 506
     
    
    503 507
            ; emitStaticBinds [(static_id, static_rhs)]
    
    504 508
     
    
    505
    -       ; return (mkVarApps (Var static_id) static_fvs) }
    
    509
    +       ; return (App from_static_ds (mkVarApps (Var static_id) static_fvs)) }
    
    506 510
     
    
    507 511
     {- Note [Desugaring record construction]
    
    508 512
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    

  • compiler/GHC/Rename/Expr.hs
    ... ... @@ -44,7 +44,7 @@ import GHC.Rename.Pat
    44 44
     import GHC.Driver.DynFlags
    
    45 45
     import GHC.Builtin.Names
    
    46 46
     import GHC.Builtin.Types ( nilDataConName )
    
    47
    -import GHC.Unit.Module ( getModule, isInteractiveModule )
    
    47
    +import GHC.Unit.Module ( isInteractiveModule )
    
    48 48
     
    
    49 49
     import GHC.Types.Basic (TypeOrKind (TypeLevel))
    
    50 50
     import GHC.Types.FieldLabel
    

  • compiler/GHC/Tc/Gen/Expr.hs
    ... ... @@ -589,16 +589,16 @@ tcExpr (HsStatic _ expr) res_ty
    589 589
                                         [liftedTypeKind, expr_ty]
    
    590 590
     
    
    591 591
             -- Wrap the static form with the 'fromStaticPtr' call.
    
    592
    -        ; fromStaticPtr <- newMethodFromName StaticOrigin fromStaticPtrName
    
    593
    -                                             [p_ty]
    
    592
    +        --   fromStaticPtr :: forall p. (IsStatic p) =>
    
    593
    +        --                    forall a. (Typeable a) =>
    
    594
    +        --                    StaticPtr a -> p a
    
    595
    +        ; fromStaticPtr <- newMethodFromName StaticOrigin fromStaticPtrName [p_ty]
    
    594 596
             ; static_ptr_ty_con <- tcLookupTyCon staticPtrTyConName
    
    595
    -        ; loc <- getSrcSpanM
    
    596 597
             ; let wrap = mkWpEvVarApps [typeable_ev] <.> mkWpTyApps [expr_ty]
    
    597
    -              expr_ty = mkTyConApp static_ptr_ty_con [expr_ty]
    
    598
    +              static_expr_ty = mkTyConApp static_ptr_ty_con [expr_ty]
    
    598 599
             ; return $ mkHsWrapCo co $
    
    599
    -          HsApp noExtField
    
    600
    -              (L (noAnnSrcSpan loc) $ mkHsWrap wrap fromStaticPtr)
    
    601
    -              (L (noAnnSrcSpan loc) (HsStatic expr_ty expr''))
    
    600
    +          HsStatic (static_expr_ty, mkHsWrap wrap fromStaticPtr)
    
    601
    +                   expr''
    
    602 602
             }
    
    603 603
     
    
    604 604
     tcExpr (HsEmbTy _ _)      _ = failWith (TcRnIllegalTypeExpr TypeKeywordSyntax)
    

  • compiler/GHC/Tc/Module.hs
    ... ... @@ -561,10 +561,11 @@ tcRnSrcDecls explicit_mod_hdr export_ies decls
    561 561
           --    and affects how names are rendered in error messages
    
    562 562
           --  * the local env exposes the local Ids to simplifyTop,
    
    563 563
           --    so that we get better error messages (monomorphism restriction)
    
    564
    -      ; new_ev_binds <- {-# SCC "simplifyTop" #-}
    
    565
    -                        restoreEnvs (tcg_env, tcl_env) $
    
    566
    -                        do { lie_main <- checkMainType tcg_env
    
    567
    -                           ; simplifyTop (lie `andWC` lie_main) }
    
    564
    +      ; tcg_env <- {-# SCC "simplifyTop" #-}
    
    565
    +                   restoreEnvs (tcg_env, tcl_env) $
    
    566
    +                   do { lie_main <- checkMainType tcg_env
    
    567
    +                      ; ev_binds <- simplifyTop (lie `andWC` lie_main)
    
    568
    +                      ; return (tcg_env `addEvBinds` ev_binds) }
    
    568 569
     
    
    569 570
             -- Emit Typeable bindings
    
    570 571
           ; tcg_env <- setGblEnv tcg_env $
    
    ... ... @@ -579,7 +580,8 @@ tcRnSrcDecls explicit_mod_hdr export_ies decls
    579 580
             -- Even simplifyTop may do some unification.
    
    580 581
             -- This pass also warns about missing type signatures
    
    581 582
           ; (id_env, ev_binds', binds', fords', imp_specs', rules', pat_syns')
    
    582
    -            <- zonkTcGblEnv new_ev_binds tcg_env
    
    583
    +            <- zonkTcGblEnv tcg_env
    
    584
    +      ; traceTc "Tc10" empty
    
    583 585
     
    
    584 586
           --------- Run finalizers --------------
    
    585 587
           -- Finalizers must run after constraints are simplified, lest types
    
    ... ... @@ -613,12 +615,12 @@ tcRnSrcDecls explicit_mod_hdr export_ies decls
    613 615
           --------- Emit the ':Main.main = runMainIO main' declaration ----------
    
    614 616
           -- Do this /after/ rnExports, so that it can consult
    
    615 617
           -- the tcg_exports created by rnExports
    
    616
    -      ; (tcg_env, main_ev_binds)
    
    618
    +      ; tcg_env
    
    617 619
                <- restoreEnvs (tcg_env, tcl_env) $
    
    618 620
                   do { (tcg_env, lie) <- captureTopConstraints $
    
    619 621
                                          checkMain explicit_mod_hdr export_ies
    
    620 622
                      ; ev_binds <- simplifyTop lie
    
    621
    -                 ; return (tcg_env, ev_binds) }
    
    623
    +                 ; return (tcg_env `addEvBinds` ev_binds) }
    
    622 624
     
    
    623 625
           ; failIfErrsM    -- Stop now if if there have been errors
    
    624 626
                            -- Continuing is a waste of time; and we may get debug
    
    ... ... @@ -629,7 +631,7 @@ tcRnSrcDecls explicit_mod_hdr export_ies decls
    629 631
           -- and main. This won't give rise to any more finalisers as you
    
    630 632
           -- can't nest finalisers inside finalisers.
    
    631 633
           ; (id_env_mf, ev_binds_mf, binds_mf, fords_mf, imp_specs_mf, rules_mf, patsyns_mf)
    
    632
    -            <- zonkTcGblEnv main_ev_binds tcg_env
    
    634
    +            <- zonkTcGblEnv tcg_env
    
    633 635
     
    
    634 636
           ; let { !final_type_env = tcg_type_env tcg_env
    
    635 637
                                     `plusTypeEnv` id_env_mf
    
    ... ... @@ -648,20 +650,19 @@ tcRnSrcDecls explicit_mod_hdr export_ies decls
    648 650
           ; setGlobalTypeEnv tcg_env' final_type_env
    
    649 651
        }
    
    650 652
     
    
    651
    -zonkTcGblEnv :: Bag EvBind -> TcGblEnv
    
    653
    +zonkTcGblEnv :: TcGblEnv
    
    652 654
                  -> TcM (TypeEnv, Bag EvBind, LHsBinds GhcTc,
    
    653 655
                            [LForeignDecl GhcTc], [LTcSpecPrag], [LRuleDecl GhcTc], [PatSyn])
    
    654
    -zonkTcGblEnv ev_binds tcg_env@(TcGblEnv { tcg_binds     = binds
    
    655
    -                                        , tcg_ev_binds  = cur_ev_binds
    
    656
    -                                        , tcg_imp_specs = imp_specs
    
    657
    -                                        , tcg_rules     = rules
    
    658
    -                                        , tcg_fords     = fords
    
    659
    -                                        , tcg_patsyns   = pat_syns })
    
    656
    +zonkTcGblEnv tcg_env@(TcGblEnv { tcg_binds     = binds
    
    657
    +                               , tcg_ev_binds  = ev_binds
    
    658
    +                               , tcg_imp_specs = imp_specs
    
    659
    +                               , tcg_rules     = rules
    
    660
    +                               , tcg_fords     = fords
    
    661
    +                               , tcg_patsyns   = pat_syns })
    
    660 662
       = {-# SCC "zonkTopDecls" #-}
    
    661 663
         setGblEnv tcg_env $ -- This sets the GlobalRdrEnv which is used when rendering
    
    662 664
                             --   error messages during zonking (notably levity errors)
    
    663
    -    do { let all_ev_binds = cur_ev_binds `unionBags` ev_binds
    
    664
    -       ; zonkTopDecls all_ev_binds binds rules imp_specs fords pat_syns }
    
    665
    +    zonkTopDecls ev_binds binds rules imp_specs fords pat_syns
    
    665 666
     
    
    666 667
     -- | Runs TH finalizers and renames and typechecks the top-level declarations
    
    667 668
     -- that they could introduce.
    

  • compiler/GHC/Tc/Utils/Env.hs
    ... ... @@ -25,7 +25,7 @@ module GHC.Tc.Utils.Env(
    25 25
             tcLookupLocatedGlobalId, tcLookupLocatedTyCon,
    
    26 26
             tcLookupLocatedClass, tcLookupAxiom,
    
    27 27
             lookupGlobal, lookupGlobal_maybe,
    
    28
    -        addTypecheckedBinds,
    
    28
    +        addTypecheckedBinds, addEvBinds, addTopEvBinds,
    
    29 29
             failIllegalTyCon, failIllegalTyVar,
    
    30 30
     
    
    31 31
             -- Local environment
    
    ... ... @@ -90,6 +90,7 @@ import GHC.Tc.Utils.Monad
    90 90
     import GHC.Tc.Utils.TcType
    
    91 91
     import GHC.Tc.Utils.TcMType ( tcCheckUsage )
    
    92 92
     import GHC.Tc.Types.LclEnv
    
    93
    +import GHC.Tc.Types.Evidence
    
    93 94
     
    
    94 95
     import GHC.Core.InstEnv
    
    95 96
     import GHC.Core.DataCon ( DataCon, dataConTyCon, flSelector )
    
    ... ... @@ -139,6 +140,8 @@ import GHC.Rename.Unbound ( unknownNameSuggestions )
    139 140
     import GHC.Tc.Errors.Types.PromotionErr
    
    140 141
     import {-# SOURCE #-} GHC.Tc.Errors.Hole (getHoleFitDispConfig)
    
    141 142
     
    
    143
    +import GHC.Data.Bag
    
    144
    +
    
    142 145
     import Control.Monad
    
    143 146
     import Data.IORef
    
    144 147
     import Data.List          ( intercalate )
    
    ... ... @@ -212,6 +215,17 @@ addTypecheckedBinds tcg_env binds
    212 215
       | otherwise = tcg_env { tcg_binds = foldr (++)
    
    213 216
                                                 (tcg_binds tcg_env)
    
    214 217
                                                 binds }
    
    218
    +
    
    219
    +addEvBinds :: TcGblEnv -> Bag EvBind -> TcGblEnv
    
    220
    +addEvBinds tcg_env ev_binds
    
    221
    +  = tcg_env { tcg_ev_binds = tcg_ev_binds tcg_env `unionBags` ev_binds }
    
    222
    +
    
    223
    +addTopEvBinds :: Bag EvBind -> TcM a -> TcM a
    
    224
    +-- Defined here (rather than in GHC.Tc.Utils.Monad)
    
    225
    +-- because it depends on addEvBinds
    
    226
    +addTopEvBinds new_ev_binds thing_inside
    
    227
    +  = updGblEnv (\env -> env `addEvBinds` new_ev_binds) thing_inside
    
    228
    +
    
    215 229
     {-
    
    216 230
     ************************************************************************
    
    217 231
     *                                                                      *
    

  • compiler/GHC/Tc/Utils/Monad.hs
    ... ... @@ -104,7 +104,7 @@ module GHC.Tc.Utils.Monad(
    104 104
     
    
    105 105
       -- * Type constraints
    
    106 106
       newTcEvBinds, newNoTcEvBinds, cloneEvBindsVar,
    
    107
    -  addTcEvBind, addTcEvBinds, addTopEvBinds,
    
    107
    +  addTcEvBind, addTcEvBinds,
    
    108 108
       getTcEvBindsMap, setTcEvBindsMap, updTcEvBinds,
    
    109 109
       getTcEvTyCoVars, chooseUniqueOccTc,
    
    110 110
       getConstraintVar, setConstraintVar,
    
    ... ... @@ -1852,13 +1852,6 @@ debugTc thing
    1852 1852
     ************************************************************************
    
    1853 1853
     -}
    
    1854 1854
     
    
    1855
    -addTopEvBinds :: Bag EvBind -> TcM a -> TcM a
    
    1856
    -addTopEvBinds new_ev_binds thing_inside
    
    1857
    -  =updGblEnv upd_env thing_inside
    
    1858
    -  where
    
    1859
    -    upd_env tcg_env = tcg_env { tcg_ev_binds = tcg_ev_binds tcg_env
    
    1860
    -                                               `unionBags` new_ev_binds }
    
    1861
    -
    
    1862 1855
     newTcEvBinds :: TcM EvBindsVar
    
    1863 1856
     newTcEvBinds = do { binds_ref <- newTcRef emptyEvBindMap
    
    1864 1857
                       ; tcvs_ref  <- newTcRef []
    

  • compiler/GHC/Tc/Zonk/Type.hs
    ... ... @@ -1078,9 +1078,10 @@ zonkExpr (HsProc x pat body)
    1078 1078
             ; return (HsProc x new_pat new_body) }
    
    1079 1079
     
    
    1080 1080
     -- StaticPointers extension
    
    1081
    -zonkExpr (HsStatic ty expr)
    
    1081
    +zonkExpr (HsStatic (ty, fs) expr)
    
    1082 1082
       = do new_ty <- zonkTcTypeToTypeX ty
    
    1083
    -       HsStatic new_ty <$> zonkLExpr expr
    
    1083
    +       new_fs <- zonkExpr fs
    
    1084
    +       HsStatic (new_ty, new_fs) <$> zonkLExpr expr
    
    1084 1085
     
    
    1085 1086
     zonkExpr (HsEmbTy x _) = dataConCantHappen x
    
    1086 1087
     zonkExpr (HsQual x _ _) = dataConCantHappen x