Matthew Pickering pushed to branch wip/splice-imports-2025 at Glasgow Haskell Compiler / GHC

Commits:

23 changed files:

Changes:

  • compiler/GHC/Rename/Env.hs
    ... ... @@ -238,8 +238,8 @@ newTopSrcBinder (L loc rdr_name)
    238 238
                     -- Binders should not be qualified; if they are, and with a different
    
    239 239
                     -- module name, we get a confusing "M.T is not in scope" error later
    
    240 240
     
    
    241
    -        ; stage <- getStage
    
    242
    -        ; if isBrackStage stage then
    
    241
    +        ; level <- getThLevel
    
    242
    +        ; if isBrackLevel level then
    
    243 243
                     -- We are inside a TH bracket, so make an *Internal* name
    
    244 244
                     -- See Note [Top-level Names in Template Haskell decl quotes] in GHC.Rename.Names
    
    245 245
                  do { uniq <- newUnique
    
    ... ... @@ -1015,7 +1015,7 @@ lookupLocalOccRn_maybe rdr_name
    1015 1015
       = do { local_env <- getLocalRdrEnv
    
    1016 1016
            ; return (lookupLocalRdrEnv local_env rdr_name) }
    
    1017 1017
     
    
    1018
    -lookupLocalOccThLvl_maybe :: Name -> RnM (Maybe (TopLevelFlag, ThLevel))
    
    1018
    +lookupLocalOccThLvl_maybe :: Name -> RnM (Maybe (TopLevelFlag, ThLevelIndex))
    
    1019 1019
     -- Just look in the local environment
    
    1020 1020
     lookupLocalOccThLvl_maybe name
    
    1021 1021
       = do { lcl_env <- getLclEnv
    

  • compiler/GHC/Rename/Expr.hs
    ... ... @@ -33,7 +33,7 @@ import GHC.Prelude hiding (head, init, last, scanl, tail)
    33 33
     import GHC.Hs
    
    34 34
     
    
    35 35
     import GHC.Tc.Errors.Types
    
    36
    -import GHC.Tc.Utils.Env ( isBrackStage )
    
    36
    +import GHC.Tc.Utils.Env ( isBrackLevel )
    
    37 37
     import GHC.Tc.Utils.Monad
    
    38 38
     
    
    39 39
     import GHC.Rename.Bind ( rnLocalBindsAndThen, rnLocalValBindsLHS, rnLocalValBindsRHS
    
    ... ... @@ -656,8 +656,8 @@ rnExpr e@(HsStatic _ expr) = do
    656 656
         unlessXOptM LangExt.StaticPointers $
    
    657 657
           addErr $ TcRnIllegalStaticExpression e
    
    658 658
         (expr',fvExpr) <- rnLExpr expr
    
    659
    -    stage <- getStage
    
    660
    -    case stage of
    
    659
    +    level <- getThLevel
    
    660
    +    case level of
    
    661 661
           Splice _ _ -> addErr $ TcRnTHError $ IllegalStaticFormInSplice e
    
    662 662
           _        -> return ()
    
    663 663
         mod <- getModule
    
    ... ... @@ -1152,7 +1152,7 @@ postProcessStmtsForApplicativeDo ctxt stmts
    1152 1152
                             | otherwise = False
    
    1153 1153
            -- don't apply the transformation inside TH brackets, because
    
    1154 1154
            -- GHC.HsToCore.Quote does not handle ApplicativeDo.
    
    1155
    -       ; in_th_bracket <- isBrackStage <$> getStage
    
    1155
    +       ; in_th_bracket <- isBrackLevel <$> getThLevel
    
    1156 1156
            ; if ado_is_on && is_do_expr && not in_th_bracket
    
    1157 1157
                 then do { traceRn "ppsfa" (ppr stmts)
    
    1158 1158
                         ; rearrangeForApplicativeDo ctxt stmts }
    

  • compiler/GHC/Rename/Module.hs
    ... ... @@ -348,8 +348,8 @@ rnAnnDecl :: AnnDecl GhcPs -> RnM (AnnDecl GhcRn, FreeVars)
    348 348
     rnAnnDecl ann@(HsAnnotation (_, s) provenance expr)
    
    349 349
       = addErrCtxt (AnnCtxt ann) $
    
    350 350
         do { (provenance', provenance_fvs) <- rnAnnProvenance provenance
    
    351
    -       ; cur_stage <- getStage
    
    352
    -       ; (expr', expr_fvs) <- setStage (Splice Untyped cur_stage) $
    
    351
    +       ; cur_level <- getThLevel
    
    352
    +       ; (expr', expr_fvs) <- setThLevel (Splice Untyped cur_level) $
    
    353 353
                                   rnLExpr expr
    
    354 354
            ; return (HsAnnotation (noAnn, s) provenance' expr',
    
    355 355
                      provenance_fvs `plusFV` expr_fvs) }
    

  • compiler/GHC/Rename/Names.hs
    ... ... @@ -635,7 +635,7 @@ top level binders specially in two ways
    635 635
         See Note [GlobalRdrEnv shadowing]
    
    636 636
     
    
    637 637
     3. We find out whether we are inside a [d| ... |] by testing the TH
    
    638
    -   stage. This is a slight hack, because the stage field was really
    
    638
    +   level. This is a slight hack, because the level field was really
    
    639 639
        meant for the type checker, and here we are not interested in the
    
    640 640
        fields of Brack, hence the error thunks in thRnBrack.
    
    641 641
     -}
    
    ... ... @@ -651,18 +651,18 @@ extendGlobalRdrEnvRn :: [GlobalRdrElt]
    651 651
     extendGlobalRdrEnvRn new_gres new_fixities
    
    652 652
       = checkNoErrs $  -- See Note [Fail fast on duplicate definitions]
    
    653 653
         do  { (gbl_env, lcl_env) <- getEnvs
    
    654
    -        ; stage <- getStage
    
    654
    +        ; level <- getThLevel
    
    655 655
             ; isGHCi <- getIsGHCi
    
    656 656
             ; let rdr_env  = tcg_rdr_env gbl_env
    
    657 657
                   fix_env  = tcg_fix_env gbl_env
    
    658 658
                   th_bndrs = getLclEnvThBndrs lcl_env
    
    659
    -              th_lvl   = thLevel stage
    
    659
    +              th_lvl   = thLevelIndex level
    
    660 660
     
    
    661 661
                   -- Delete new_occs from global and local envs
    
    662 662
                   -- If we are in a TemplateHaskell decl bracket,
    
    663 663
                   --    we are going to shadow them
    
    664 664
                   -- See Note [GlobalRdrEnv shadowing]
    
    665
    -              inBracket = isBrackStage stage
    
    665
    +              inBracket = isBrackLevel level
    
    666 666
     
    
    667 667
                   lcl_env_TH = modifyLclCtxt (\lcl_env -> lcl_env { tcl_rdr = minusLocalRdrEnv (tcl_rdr lcl_env) new_gres_env }) lcl_env
    
    668 668
                                -- See Note [GlobalRdrEnv shadowing]
    

  • compiler/GHC/Rename/Splice.hs
    ... ... @@ -124,8 +124,8 @@ rnTypedBracket e br_body
    124 124
         do { checkForTemplateHaskellQuotes e
    
    125 125
     
    
    126 126
              -- Check for nested brackets
    
    127
    -       ; cur_stage <- getStage
    
    128
    -       ; case cur_stage of
    
    127
    +       ; cur_level <- getThLevel
    
    128
    +       ; case cur_level of
    
    129 129
                { Splice _ _       -> return ()
    
    130 130
                    -- See Note [Untyped quotes in typed splices and vice versa]
    
    131 131
                ; RunSplice _    ->
    
    ... ... @@ -141,7 +141,7 @@ rnTypedBracket e br_body
    141 141
            ; recordThUse
    
    142 142
     
    
    143 143
            ; traceRn "Renaming typed TH bracket" empty
    
    144
    -       ; (body', fvs_e) <- setStage (Brack cur_stage RnPendingTyped) $ rnLExpr br_body
    
    144
    +       ; (body', fvs_e) <- setThLevel (Brack cur_level RnPendingTyped) $ rnLExpr br_body
    
    145 145
     
    
    146 146
            ; return (HsTypedBracket noExtField body', fvs_e)
    
    147 147
     
    
    ... ... @@ -153,8 +153,8 @@ rnUntypedBracket e br_body
    153 153
         do { checkForTemplateHaskellQuotes e
    
    154 154
     
    
    155 155
              -- Check for nested brackets
    
    156
    -       ; cur_stage <- getStage
    
    157
    -       ; case cur_stage of
    
    156
    +       ; cur_level <- getThLevel
    
    157
    +       ; case cur_level of
    
    158 158
                { Splice _ _       -> return ()
    
    159 159
                    -- See Note [Untyped quotes in typed splices and vice versa]
    
    160 160
                ; RunSplice _    ->
    
    ... ... @@ -174,7 +174,7 @@ rnUntypedBracket e br_body
    174 174
            ; (body', fvs_e) <-
    
    175 175
              -- See Note [Rebindable syntax and Template Haskell]
    
    176 176
              unsetXOptM LangExt.RebindableSyntax $
    
    177
    -         setStage (Brack cur_stage (RnPendingUntyped ps_var)) $
    
    177
    +         setThLevel (Brack cur_level (RnPendingUntyped ps_var)) $
    
    178 178
                       rn_utbracket br_body
    
    179 179
            ; pendings <- readMutVar ps_var
    
    180 180
            ; return (HsUntypedBracket pendings body', fvs_e)
    
    ... ... @@ -279,14 +279,14 @@ rnUntypedSpliceGen :: (HsUntypedSplice GhcRn -> RnM (a, FreeVars))
    279 279
                        -> RnM (a, FreeVars)
    
    280 280
     rnUntypedSpliceGen run_splice pend_splice splice
    
    281 281
       = addErrCtxt (UntypedSpliceCtxt splice) $ do
    
    282
    -    { stage <- getStage
    
    283
    -    ; case stage of
    
    282
    +    { level <- getThLevel
    
    283
    +    ; case level of
    
    284 284
             Brack _ RnPendingTyped
    
    285 285
               -> failWithTc $ thSyntaxError
    
    286 286
                             $ MismatchedSpliceType Untyped IsSplice
    
    287 287
     
    
    288
    -        Brack pop_stage (RnPendingUntyped ps_var)
    
    289
    -          -> do { (splice', fvs) <- setStage pop_stage $
    
    288
    +        Brack pop_level (RnPendingUntyped ps_var)
    
    289
    +          -> do { (splice', fvs) <- setThLevel pop_level $
    
    290 290
                                         rnUntypedSplice splice
    
    291 291
                     ; loc  <- getSrcSpanM
    
    292 292
                     ; splice_name <- newLocalBndrRn (L (noAnnSrcSpan loc) unqualSplice)
    
    ... ... @@ -296,9 +296,9 @@ rnUntypedSpliceGen run_splice pend_splice splice
    296 296
                     ; return (result, fvs) }
    
    297 297
     
    
    298 298
             _ ->  do { checkTopSpliceAllowed splice
    
    299
    -                 ; cur_stage <- getStage
    
    299
    +                 ; cur_level <- getThLevel
    
    300 300
                      ; (splice', fvs1) <- checkNoErrs $
    
    301
    -                                      setStage (Splice Untyped cur_stage) $
    
    301
    +                                      setThLevel (Splice Untyped cur_level) $
    
    302 302
                                           rnUntypedSplice splice
    
    303 303
                        -- checkNoErrs: don't attempt to run the splice if
    
    304 304
                        -- renaming it failed; otherwise we get a cascade of
    
    ... ... @@ -350,7 +350,7 @@ runRnSplice flavour run_meta ppr_res splice
    350 350
     
    
    351 351
                  -- Run the expression
    
    352 352
            ; mod_finalizers_ref <- newTcRef []
    
    353
    -       ; result <- setStage (RunSplice mod_finalizers_ref) $
    
    353
    +       ; result <- setThLevel (RunSplice mod_finalizers_ref) $
    
    354 354
                          run_meta zonked_q_expr
    
    355 355
            ; mod_finalizers <- readTcRef mod_finalizers_ref
    
    356 356
            ; traceSplice (SpliceInfo { spliceDescription = what
    
    ... ... @@ -434,10 +434,10 @@ rnTypedSplice :: LHsExpr GhcPs -- Typed splice expression
    434 434
                   -> RnM (HsExpr GhcRn, FreeVars)
    
    435 435
     rnTypedSplice expr
    
    436 436
       = addErrCtxt (TypedSpliceCtxt Nothing expr) $ do
    
    437
    -    { stage <- getStage
    
    438
    -    ; case stage of
    
    439
    -        Brack pop_stage RnPendingTyped
    
    440
    -          -> setStage pop_stage rn_splice
    
    437
    +    { level <- getThLevel
    
    438
    +    ; case level of
    
    439
    +        Brack pop_level RnPendingTyped
    
    440
    +          -> setThLevel pop_level rn_splice
    
    441 441
     
    
    442 442
             Brack _ (RnPendingUntyped _)
    
    443 443
               -> failWithTc $ thSyntaxError $ MismatchedSpliceType Typed IsSplice
    
    ... ... @@ -445,8 +445,8 @@ rnTypedSplice expr
    445 445
             _ -> do { unlessXOptM LangExt.TemplateHaskell
    
    446 446
                         (failWith $ thSyntaxError IllegalTHSplice)
    
    447 447
     
    
    448
    -                ; cur_stage <- getStage
    
    449
    -                ; (result, fvs1) <- checkNoErrs $ setStage (Splice Typed cur_stage) rn_splice
    
    448
    +                ; cur_level <- getThLevel
    
    449
    +                ; (result, fvs1) <- checkNoErrs $ setThLevel (Splice Typed cur_level) rn_splice
    
    450 450
                       -- checkNoErrs: don't attempt to run the splice if
    
    451 451
                       -- renaming it failed; otherwise we get a cascade of
    
    452 452
                       -- errors from e.g. unbound variables
    
    ... ... @@ -790,9 +790,9 @@ rnTopSpliceDecls :: HsUntypedSplice GhcPs -> RnM ([LHsDecl GhcPs], FreeVars)
    790 790
     -- Declaration splice at the very top level of the module
    
    791 791
     rnTopSpliceDecls splice
    
    792 792
        =  do { checkTopSpliceAllowed splice
    
    793
    -         ; cur_stage <- getStage
    
    793
    +         ; cur_level <- getThLevel
    
    794 794
              ; (rn_splice, fvs) <- checkNoErrs $
    
    795
    -                               setStage (Splice Untyped cur_stage) $
    
    795
    +                               setThLevel (Splice Untyped cur_level) $
    
    796 796
                                    rnUntypedSplice splice
    
    797 797
                -- As always, be sure to checkNoErrs above lest we end up with
    
    798 798
                -- holes making it to typechecking, hence #12584.
    
    ... ... @@ -909,27 +909,25 @@ checkThLocalTyName name
    909 909
     
    
    910 910
       | otherwise
    
    911 911
       = do  { traceRn "checkThLocalTyName" (ppr name)
    
    912
    -        ; mb_local_use <- getStageAndBindLevel name
    
    912
    +        ; mb_local_use <- getCurrentAndBindLevel name
    
    913 913
             ; case mb_local_use of {
    
    914 914
                  Nothing -> return () ;  -- Not a locally-bound thing
    
    915
    -             Just (top_lvl, bind_lvl, use_stage) ->
    
    916
    -    do  { let use_lvl = thLevel use_stage
    
    917
    -        -- We don't check the well stageness of name here.
    
    915
    +             Just (top_lvl, bind_lvl, use_lvl) ->
    
    916
    +    do  { let use_lvl_idx = thLevelIndex use_lvl
    
    917
    +        -- We don't check the well levelledness of name here.
    
    918 918
             -- this would break test for #20969
    
    919 919
             --
    
    920 920
             -- Consequently there is no check&restiction for top level splices.
    
    921 921
             -- But it's annoying anyway.
    
    922 922
             --
    
    923
    -        -- Therefore checkCrossStageLiftingTy shouldn't assume anything
    
    923
    +        -- Therefore checkCrossLevelLiftingTy shouldn't assume anything
    
    924 924
             -- about bind_lvl and use_lvl relation.
    
    925 925
             --
    
    926
    -        -- ; checkWellStaged (StageCheckSplice name) bind_lvl use_lvl
    
    927
    -
    
    928 926
             ; traceRn "checkThLocalTyName" (ppr name <+> ppr bind_lvl
    
    929
    -                                                 <+> ppr use_stage
    
    927
    +                                                 <+> ppr use_lvl
    
    930 928
                                                      <+> ppr use_lvl)
    
    931 929
             ; dflags <- getDynFlags
    
    932
    -        ; checkCrossStageLiftingTy dflags top_lvl bind_lvl use_stage use_lvl name } } }
    
    930
    +        ; checkCrossLevelLiftingTy dflags top_lvl bind_lvl use_lvl use_lvl_idx name } } }
    
    933 931
     
    
    934 932
     -- | Check whether we are allowed to use a Name in this context (for TH purposes)
    
    935 933
     -- In the case of a level incorrect program, attempt to fix it by using
    
    ... ... @@ -953,34 +951,34 @@ checkThLocalName allow_lifting name
    953 951
     
    
    954 952
       | otherwise
    
    955 953
       = do  {
    
    956
    -          mb_local_use <- getStageAndBindLevel name
    
    954
    +          mb_local_use <- getCurrentAndBindLevel name
    
    957 955
             ; case mb_local_use of {
    
    958 956
                  Nothing -> return () ;  -- Not a locally-bound thing
    
    959
    -             Just (top_lvl, bind_lvl, use_stage) ->
    
    960
    -    do  { let use_lvl = thLevel use_stage
    
    957
    +             Just (top_lvl, bind_lvl, use_lvl) ->
    
    958
    +    do  { let use_lvl_idx = thLevelIndex use_lvl
    
    961 959
             ; cur_mod <- extractModule <$> getGblEnv
    
    962 960
             ; let is_local
    
    963 961
                       | Just mod <- nameModule_maybe name = mod == cur_mod
    
    964 962
                       | otherwise = True
    
    965
    -        ; traceRn "checkThLocalName" (ppr name <+> ppr bind_lvl <+> ppr use_lvl <+> ppr use_stage)
    
    963
    +        ; traceRn "checkThLocalName" (ppr name <+> ppr bind_lvl <+> ppr use_lvl <+> ppr use_lvl)
    
    966 964
             ; dflags <- getDynFlags
    
    967 965
             ; env <- getGlobalRdrEnv
    
    968 966
             ; let mgre = lookupGRE_Name env name
    
    969
    -        ; checkCrossStageLifting dflags (StageCheckSplice name mgre) top_lvl is_local allow_lifting bind_lvl use_stage use_lvl name } } }
    
    967
    +        ; checkCrossLevelLifting dflags (LevelCheckSplice name mgre) top_lvl is_local allow_lifting bind_lvl use_lvl use_lvl_idx name } } }
    
    970 968
     
    
    971 969
     --------------------------------------
    
    972
    -checkCrossStageLifting :: DynFlags
    
    973
    -                       -> StageCheckReason
    
    970
    +checkCrossLevelLifting :: DynFlags
    
    971
    +                       -> LevelCheckReason
    
    974 972
                            -> TopLevelFlag
    
    975 973
                            -> Bool
    
    976 974
                            -> Bool
    
    977
    -                       -> Set.Set ThLevel
    
    978
    -                       -> ThStage
    
    975
    +                       -> Set.Set ThLevelIndex
    
    979 976
                            -> ThLevel
    
    977
    +                       -> ThLevelIndex
    
    980 978
                            -> Name -> TcM ()
    
    981
    -checkCrossStageLifting dflags reason top_lvl is_local allow_lifting bind_lvl use_stage use_lvl name
    
    979
    +checkCrossLevelLifting dflags reason top_lvl is_local allow_lifting bind_lvl use_lvl use_lvl_idx name
    
    982 980
       -- 1. If name is in-scope, at the correct level.
    
    983
    -  | use_lvl `Set.member` bind_lvl = return ()
    
    981
    +  | use_lvl_idx `Set.member` bind_lvl = return ()
    
    984 982
       -- 2. Name is imported with -XImplicitStagePersistence
    
    985 983
       | not is_local
    
    986 984
       , xopt LangExt.ImplicitStagePersistence dflags = return ()
    
    ... ... @@ -988,23 +986,23 @@ checkCrossStageLifting dflags reason top_lvl is_local allow_lifting bind_lvl use
    988 986
       -- to be persisted into the future.
    
    989 987
       | isTopLevel top_lvl
    
    990 988
       , is_local
    
    991
    -  , any (use_lvl >=) (Set.toList bind_lvl)
    
    989
    +  , any (use_lvl_idx >=) (Set.toList bind_lvl)
    
    992 990
       , xopt LangExt.ImplicitStagePersistence dflags = when (isExternalName name) (keepAlive name)
    
    993 991
       -- 4. Name is in an untyped bracket, and we are allowed to attempt to lift.
    
    994
    -  | Brack _ (RnPendingUntyped ps_var) <- use_stage   -- Only for untyped brackets
    
    992
    +  | Brack _ (RnPendingUntyped ps_var) <- use_lvl   -- Only for untyped brackets
    
    995 993
       , allow_lifting
    
    996 994
       = do
    
    997 995
           dflags <- getDynFlags
    
    998
    -      check_cross_stage_lifting dflags top_lvl name ps_var
    
    996
    +      check_cross_level_lifting dflags top_lvl name ps_var
    
    999 997
       -- 5. For an typed bracket, these checks happen again later on (checkThLocalId)
    
    1000 998
       -- In the future we should do all the level checks here.
    
    1001
    -  | Brack _ RnPendingTyped <- use_stage  -- Lift for typed brackets is inserted later.
    
    999
    +  | Brack _ RnPendingTyped <- use_lvl  -- Lift for typed brackets is inserted later.
    
    1002 1000
         = return ()
    
    1003 1001
       -- Otherwise, we have a level error, report.
    
    1004
    -  | otherwise = addErrTc (TcRnBadlyLevelled reason bind_lvl use_lvl)
    
    1002
    +  | otherwise = addErrTc (TcRnBadlyLevelled reason bind_lvl use_lvl_idx)
    
    1005 1003
     
    
    1006
    -check_cross_stage_lifting :: DynFlags -> TopLevelFlag -> Name -> TcRef [PendingRnSplice] -> TcM ()
    
    1007
    -check_cross_stage_lifting dflags top_lvl name ps_var
    
    1004
    +check_cross_level_lifting :: DynFlags -> TopLevelFlag -> Name -> TcRef [PendingRnSplice] -> TcM ()
    
    1005
    +check_cross_level_lifting dflags top_lvl name ps_var
    
    1008 1006
       | isTopLevel top_lvl
    
    1009 1007
       , xopt LangExt.ImplicitStagePersistence dflags
    
    1010 1008
             -- Top-level identifiers in this module,
    
    ... ... @@ -1027,7 +1025,7 @@ check_cross_stage_lifting dflags top_lvl name ps_var
    1027 1025
             -- If 'x' occurs many times we may get many identical
    
    1028 1026
             -- bindings of the same SplicePointName, but that doesn't
    
    1029 1027
             -- matter, although it's a mite untidy.
    
    1030
    -    do  { traceRn "checkCrossStageLifting" (ppr name)
    
    1028
    +    do  { traceRn "checkCrossLevelLifting" (ppr name)
    
    1031 1029
     
    
    1032 1030
               -- Construct the (lift x) expression
    
    1033 1031
             ; let lift_expr   = nlHsApp (nlHsVar liftName) (nlHsVar name)
    
    ... ... @@ -1040,8 +1038,8 @@ check_cross_stage_lifting dflags top_lvl name ps_var
    1040 1038
             ; ps <- readMutVar ps_var
    
    1041 1039
             ; writeMutVar ps_var (pend_splice : ps) }
    
    1042 1040
     
    
    1043
    -checkCrossStageLiftingTy :: DynFlags -> TopLevelFlag -> Set.Set ThLevel -> ThStage -> ThLevel -> Name -> TcM ()
    
    1044
    -checkCrossStageLiftingTy dflags top_lvl bind_lvl _use_stage use_lvl name
    
    1041
    +checkCrossLevelLiftingTy :: DynFlags -> TopLevelFlag -> Set.Set ThLevelIndex -> ThLevel -> ThLevelIndex -> Name -> TcM ()
    
    1042
    +checkCrossLevelLiftingTy dflags top_lvl bind_lvl _use_lvl use_lvl_idx name
    
    1045 1043
       | isTopLevel top_lvl
    
    1046 1044
       , xopt LangExt.ImplicitStagePersistence dflags
    
    1047 1045
       = return ()
    
    ... ... @@ -1052,8 +1050,8 @@ checkCrossStageLiftingTy dflags top_lvl bind_lvl _use_stage use_lvl name
    1052 1050
     
    
    1053 1051
       -- Can also happen for negative cases
    
    1054 1052
       -- See comment in checkThLocalTyName:
    
    1055
    -  | use_lvl `notElem` bind_lvl
    
    1056
    -  = addDiagnostic $ TcRnBadlyLevelledType name bind_lvl use_lvl
    
    1053
    +  | use_lvl_idx `notElem` bind_lvl
    
    1054
    +  = addDiagnostic $ TcRnBadlyLevelledType name bind_lvl use_lvl_idx
    
    1057 1055
     
    
    1058 1056
       | otherwise
    
    1059 1057
       = return ()
    
    ... ... @@ -1094,7 +1092,7 @@ them in the keep-alive set.
    1094 1092
     Note [Quoting names]
    
    1095 1093
     ~~~~~~~~~~~~~~~~~~~~
    
    1096 1094
     A quoted name 'n is a bit like a quoted expression [| n |], except that we
    
    1097
    -have no cross-stage lifting (c.f. GHC.Tc.Gen.Expr.thBrackId).  So, after incrementing
    
    1095
    +have no cross-level lifting (c.f. GHC.Tc.Gen.Expr.thBrackId).  So, after incrementing
    
    1098 1096
     the use-level to account for the brackets, the cases are:
    
    1099 1097
     
    
    1100 1098
             bind > use                      Error
    
    ... ... @@ -1113,7 +1111,7 @@ Examples:
    1113 1111
     
    
    1114 1112
       \x. f 'x      -- Not ok (bind = 1, use = 1)
    
    1115 1113
                     -- (whereas \x. f [| x |] might have been ok, by
    
    1116
    -                --                               cross-stage lifting
    
    1114
    +                --                               cross-level lifting
    
    1117 1115
     
    
    1118 1116
       \y. [| \x. $(f 'y) |] -- Not ok (bind =1, use = 1)
    
    1119 1117
     
    

  • compiler/GHC/Rename/Utils.hs
    ... ... @@ -106,7 +106,7 @@ newLocalBndrsRn = mapM newLocalBndrRn
    106 106
     bindLocalNames :: [Name] -> RnM a -> RnM a
    
    107 107
     bindLocalNames names
    
    108 108
       = updLclCtxt $ \ lcl_env ->
    
    109
    -    let th_level  = thLevel (tcl_th_ctxt lcl_env)
    
    109
    +    let th_level  = thLevelIndex (tcl_th_ctxt lcl_env)
    
    110 110
             th_bndrs' = extendNameEnvList (tcl_th_bndrs lcl_env)
    
    111 111
                         [ (n, (NotTopLevel, th_level)) | n <- names ]
    
    112 112
             rdr_env'  = extendLocalRdrEnvList (tcl_rdr lcl_env) names
    

  • compiler/GHC/Tc/Errors/Ppr.hs
    ... ... @@ -1510,17 +1510,17 @@ instance Diagnostic TcRnMessage where
    1510 1510
         TcRnBadlyLevelled reason bind_lvl use_lvl
    
    1511 1511
           -> mkSimpleDecorated $
    
    1512 1512
              vcat $
    
    1513
    -         [ text "Level error:" <+> pprStageCheckReason reason <+>
    
    1513
    +         [ text "Level error:" <+> pprLevelCheckReason reason <+>
    
    1514 1514
                hsep [text "is bound at level" <+> ppr bind_lvl,
    
    1515 1515
                      text "but used at level" <+> ppr use_lvl]
    
    1516 1516
              ] ++
    
    1517 1517
              [ hsep [ text "Hint: quoting" <+> thBrackets (ppUnless (isValName n) "t") (ppr n)
    
    1518 1518
                     , text "or an enclosing expression would allow the quotation to be used at an earlier level"
    
    1519 1519
                     ]
    
    1520
    -         | StageCheckSplice n _ <- [reason]
    
    1520
    +         | LevelCheckSplice n _ <- [reason]
    
    1521 1521
              ] ++
    
    1522 1522
              [ "From imports" <+> (ppr (gre_imp gre))
    
    1523
    -         | StageCheckSplice _ (Just gre) <- [reason]
    
    1523
    +         | LevelCheckSplice _ (Just gre) <- [reason]
    
    1524 1524
              , not (isEmptyBag (gre_imp gre)) ]
    
    1525 1525
         TcRnBadlyLevelledType name bind_lvl use_lvl
    
    1526 1526
           -> mkSimpleDecorated $
    
    ... ... @@ -5770,11 +5770,11 @@ pprWrongThingSort =
    5770 5770
         WrongThingTyCon -> "type constructor"
    
    5771 5771
         WrongThingAxiom -> "axiom"
    
    5772 5772
     
    
    5773
    -pprStageCheckReason :: StageCheckReason -> SDoc
    
    5774
    -pprStageCheckReason = \case
    
    5775
    -  StageCheckInstance _ t ->
    
    5773
    +pprLevelCheckReason :: LevelCheckReason -> SDoc
    
    5774
    +pprLevelCheckReason = \case
    
    5775
    +  LevelCheckInstance _ t ->
    
    5776 5776
         text "instance for" <+> quotes (ppr t)
    
    5777
    -  StageCheckSplice t _ ->
    
    5777
    +  LevelCheckSplice t _ ->
    
    5778 5778
         quotes (ppr t)
    
    5779 5779
     
    
    5780 5780
     pprUninferrableTyVarCtx :: UninferrableTyVarCtx -> SDoc
    
    ... ... @@ -6877,10 +6877,6 @@ pprTHNameError = \case
    6877 6877
         mkSimpleDecorated $
    
    6878 6878
           hang (text "The binder" <+> quotes (ppr name) <+> text "is not a NameU.")
    
    6879 6879
              2 (text "Probable cause: you used mkName instead of newName to generate a binding.")
    
    6880
    -  QuotedNameWrongStage quote ->
    
    6881
    -    mkSimpleDecorated $
    
    6882
    -      sep [ text "Stage error: the non-top-level quoted name" <+> ppr quote
    
    6883
    -          , text "must be used at the same stage at which it is bound." ]
    
    6884 6880
     
    
    6885 6881
     pprTHReifyError :: THReifyError -> DecoratedSDoc
    
    6886 6882
     pprTHReifyError = \case
    
    ... ... @@ -7002,7 +6998,6 @@ thSyntaxErrorReason = \case
    7002 6998
     thNameErrorReason :: THNameError -> DiagnosticReason
    
    7003 6999
     thNameErrorReason = \case
    
    7004 7000
       NonExactName {}         -> ErrorWithoutFlag
    
    7005
    -  QuotedNameWrongStage {} -> ErrorWithoutFlag
    
    7006 7001
     
    
    7007 7002
     thReifyErrorReason :: THReifyError -> DiagnosticReason
    
    7008 7003
     thReifyErrorReason = \case
    
    ... ... @@ -7063,7 +7058,6 @@ thSyntaxErrorHints = \case
    7063 7058
     thNameErrorHints :: THNameError -> [GhcHint]
    
    7064 7059
     thNameErrorHints = \case
    
    7065 7060
       NonExactName {}         -> noHints
    
    7066
    -  QuotedNameWrongStage {} -> noHints
    
    7067 7061
     
    
    7068 7062
     thReifyErrorHints :: THReifyError -> [GhcHint]
    
    7069 7063
     thReifyErrorHints = \case
    

  • compiler/GHC/Tc/Errors/Types.hs
    ... ... @@ -99,7 +99,7 @@ module GHC.Tc.Errors.Types (
    99 99
       , RuleLhsErrReason(..)
    
    100 100
       , HsigShapeMismatchReason(..)
    
    101 101
       , WrongThingSort(..)
    
    102
    -  , StageCheckReason(..)
    
    102
    +  , LevelCheckReason(..)
    
    103 103
       , UninferrableTyVarCtx(..)
    
    104 104
       , PatSynInvalidRhsReason(..)
    
    105 105
       , BadFieldAnnotationReason(..)
    
    ... ... @@ -3487,19 +3487,19 @@ data TcRnMessage where
    3487 3487
         -> !LookupInstanceErrReason
    
    3488 3488
         -> TcRnMessage
    
    3489 3489
     
    
    3490
    -  {-| TcRnBadlyStaged is an error that occurs when a TH binding is used at an
    
    3490
    +  {-| TcRnBadlyLevelled is an error that occurs when a TH binding is used at an
    
    3491 3491
           invalid level.
    
    3492 3492
     
    
    3493 3493
         Test cases:
    
    3494 3494
           T17820d, T17820, T21547, T5795, qq00[1-4], annfail0{3,4,6,9}
    
    3495 3495
       -}
    
    3496 3496
       TcRnBadlyLevelled
    
    3497
    -    :: !StageCheckReason -- ^ The binding
    
    3498
    -    -> !(Set.Set Int) -- ^ The binding levels
    
    3499
    -    -> !Int -- ^ The level at which the binding is used.
    
    3497
    +    :: !LevelCheckReason -- ^ The binding
    
    3498
    +    -> !(Set.Set ThLevelIndex) -- ^ The binding levels
    
    3499
    +    -> !ThLevelIndex -- ^ The level at which the binding is used.
    
    3500 3500
         -> TcRnMessage
    
    3501 3501
     
    
    3502
    -  {-| TcRnBadlyStagedWarn is a warning that occurs when a TH type binding is
    
    3502
    +  {-| TcRnBadlyLevelledWarn is a warning that occurs when a TH type binding is
    
    3503 3503
         used in an invalid stage.
    
    3504 3504
     
    
    3505 3505
         Controlled by flags:
    
    ... ... @@ -3510,8 +3510,8 @@ data TcRnMessage where
    3510 3510
       -}
    
    3511 3511
       TcRnBadlyLevelledType
    
    3512 3512
         :: !Name  -- ^ The type binding being spliced.
    
    3513
    -    -> !(Set.Set Int) -- ^ The binding stage.
    
    3514
    -    -> !Int -- ^ The stage at which the binding is used.
    
    3513
    +    -> !(Set.Set ThLevelIndex) -- ^ The binding stage.
    
    3514
    +    -> !ThLevelIndex -- ^ The stage at which the binding is used.
    
    3515 3515
         -> TcRnMessage
    
    3516 3516
     
    
    3517 3517
       {-| TcRnTyThingUsedWrong is an error that occurs when a thing is used where another
    
    ... ... @@ -6245,9 +6245,9 @@ data WrongThingSort
    6245 6245
       | WrongThingTyCon
    
    6246 6246
       | WrongThingAxiom
    
    6247 6247
     
    
    6248
    -data StageCheckReason
    
    6249
    -  = StageCheckInstance !InstanceWhat !PredType
    
    6250
    -  | StageCheckSplice !Name !(Maybe GlobalRdrElt)
    
    6248
    +data LevelCheckReason
    
    6249
    +  = LevelCheckInstance !InstanceWhat !PredType
    
    6250
    +  | LevelCheckSplice !Name !(Maybe GlobalRdrElt)
    
    6251 6251
     
    
    6252 6252
     data UninferrableTyVarCtx
    
    6253 6253
       = UninfTyCtx_ClassContext [TcType]
    
    ... ... @@ -6745,13 +6745,6 @@ data THNameError
    6745 6745
       -}
    
    6746 6746
       = NonExactName !RdrName
    
    6747 6747
     
    
    6748
    -  {-| QuotedNameWrongStage is an error that can happen when a
    
    6749
    -      (non-top-level) Name is used at a different Template Haskell stage
    
    6750
    -      than the stage at which it is bound.
    
    6751
    -
    
    6752
    -     Test cases: T16976z
    
    6753
    -  -}
    
    6754
    -  | QuotedNameWrongStage !(HsQuote GhcPs)
    
    6755 6748
       deriving Generic
    
    6756 6749
     
    
    6757 6750
     data THReifyError
    

  • compiler/GHC/Tc/Gen/App.hs
    ... ... @@ -1217,13 +1217,13 @@ tc_inst_forall_arg conc_tvs (tvb, inner_ty) hs_ty
    1217 1217
            --
    
    1218 1218
            -- See [Wrinkle: VTA] in Note [Representation-polymorphism checking built-ins]
    
    1219 1219
            -- in GHC.Tc.Utils.Concrete.
    
    1220
    -       ; th_stage <- getStage
    
    1220
    +       ; th_lvl <- getThLevel
    
    1221 1221
            ; ty_arg <- case mb_conc of
    
    1222 1222
                Nothing   -> return ty_arg0
    
    1223 1223
                Just conc
    
    1224 1224
                  -- See [Wrinkle: Typed Template Haskell]
    
    1225 1225
                  -- in Note [hasFixedRuntimeRep] in GHC.Tc.Utils.Concrete.
    
    1226
    -             | Brack _ (TcPending {}) <- th_stage
    
    1226
    +             | Brack _ (TcPending {}) <- th_lvl
    
    1227 1227
                  -> return ty_arg0
    
    1228 1228
                  | otherwise
    
    1229 1229
                  ->
    

  • compiler/GHC/Tc/Gen/Head.hs
    ... ... @@ -1075,28 +1075,28 @@ Wrinkles
    1075 1075
     -- checkThLocalName.
    
    1076 1076
     checkThLocalId :: Id -> TcM ()
    
    1077 1077
     checkThLocalId id
    
    1078
    -  = do  { mb_local_use <- getStageAndBindLevel (idName id)
    
    1078
    +  = do  { mb_local_use <- getCurrentAndBindLevel (idName id)
    
    1079 1079
             ; case mb_local_use of
    
    1080
    -             Just (top_lvl, bind_lvl, use_stage)
    
    1081
    -                | thLevel use_stage `Set.notMember` bind_lvl
    
    1080
    +             Just (top_lvl, bind_lvl, use_lvl)
    
    1081
    +                | thLevelIndex use_lvl `Set.notMember` bind_lvl
    
    1082 1082
                     -> do
    
    1083 1083
                         dflags <- getDynFlags
    
    1084
    -                    checkCrossStageLifting dflags top_lvl id use_stage
    
    1084
    +                    checkCrossLevelLifting dflags top_lvl id use_lvl
    
    1085 1085
                  _  -> return ()   -- Not a locally-bound thing, or
    
    1086 1086
                                    -- no cross-stage link
    
    1087 1087
         }
    
    1088 1088
     
    
    1089 1089
     --------------------------------------
    
    1090
    -checkCrossStageLifting :: DynFlags -> TopLevelFlag -> Id -> ThStage -> TcM ()
    
    1090
    +checkCrossLevelLifting :: DynFlags -> TopLevelFlag -> Id -> ThLevel -> TcM ()
    
    1091 1091
     -- If we are inside typed brackets, and (use_lvl > bind_lvl)
    
    1092 1092
     -- we must check whether there's a cross-stage lift to do
    
    1093 1093
     -- Examples   \x -> [|| x ||]
    
    1094 1094
     --            [|| map ||]
    
    1095 1095
     --
    
    1096
    --- This is similar to checkCrossStageLifting in GHC.Rename.Splice, but
    
    1096
    +-- This is similar to checkCrossLevelLifting in GHC.Rename.Splice, but
    
    1097 1097
     -- this code is applied to *typed* brackets.
    
    1098 1098
     
    
    1099
    -checkCrossStageLifting dflags top_lvl id (Brack _ (TcPending ps_var lie_var q))
    
    1099
    +checkCrossLevelLifting dflags top_lvl id (Brack _ (TcPending ps_var lie_var q))
    
    1100 1100
       | isTopLevel top_lvl
    
    1101 1101
       , xopt LangExt.ImplicitStagePersistence dflags
    
    1102 1102
       = when (isExternalName id_name) (keepAlive id_name)
    
    ... ... @@ -1146,7 +1146,7 @@ checkCrossStageLifting dflags top_lvl id (Brack _ (TcPending ps_var lie_var q))
    1146 1146
       where
    
    1147 1147
         id_name = idName id
    
    1148 1148
     
    
    1149
    -checkCrossStageLifting _ _ _ _ = return ()
    
    1149
    +checkCrossLevelLifting _ _ _ _ = return ()
    
    1150 1150
     
    
    1151 1151
     {-
    
    1152 1152
     Note [Lifting strings]
    
    ... ... @@ -1181,7 +1181,7 @@ them at level 2 or 0.
    1181 1181
     The level which a name is availble at is stored in the 'GRE', in the normal
    
    1182 1182
     GlobalRdrEnv. The function `greLevels` returns the levels which a specific GRE
    
    1183 1183
     is imported at. The level information for a 'Name' is computed by `getStageAndBindLevel`.
    
    1184
    -The level validity is checked by `checkCrossStageLifting`.
    
    1184
    +The level validity is checked by `checkCrossLevelLifting`.
    
    1185 1185
     
    
    1186 1186
     Instances are checked by `checkWellStagedDFun`, which computes the level an
    
    1187 1187
     instance by calling `checkWellStagedInstanceWhat`, which sees what is available at by looking at the module graph.
    
    ... ... @@ -1189,7 +1189,7 @@ instance by calling `checkWellStagedInstanceWhat`, which sees what is available
    1189 1189
     That's it for the main implementation of the feature, and the rest is modifications
    
    1190 1190
     to the driver parts of the code to use this information. For example, in downsweep,
    
    1191 1191
     we only enable code generation for modules needed at the runtime stage.
    
    1192
    -See Note [ExplicitLevelImports and -fno-code].
    
    1192
    +See Note [-fno-code mode].
    
    1193 1193
     
    
    1194 1194
     -}
    
    1195 1195
     
    

  • compiler/GHC/Tc/Gen/Splice.hs
    ... ... @@ -173,8 +173,8 @@ import GHC.Rename.Doc (rnHsDoc)
    173 173
     {-
    
    174 174
     Note [Template Haskell state diagram]
    
    175 175
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    176
    -Here are the ThStages, s, their corresponding level numbers
    
    177
    -(the result of (thLevel s)), and their state transitions.
    
    176
    +Here are the ThLevels, their corresponding level numbers
    
    177
    +(the result of (thLevelIndex s)), and their state transitions.
    
    178 178
     The top level of the program is stage Comp:
    
    179 179
     
    
    180 180
          Start here
    
    ... ... @@ -447,7 +447,7 @@ without having to walk over the untyped bracket code. Our example
    447 447
     
    
    448 448
     RENAMER (rnUntypedBracket):
    
    449 449
     
    
    450
    -* Set the ThStage to (Brack s (RnPendingUntyped ps_var))
    
    450
    +* Set the ThLevel to (Brack s (RnPendingUntyped ps_var))
    
    451 451
     
    
    452 452
     * Rename the body
    
    453 453
     
    
    ... ... @@ -557,7 +557,7 @@ RENAMER (rnTypedSplice): the renamer adds a SplicePointName, spn:
    557 557
     
    
    558 558
     TYPECHECKER (tcTypedBracket):
    
    559 559
     
    
    560
    -* Set the ThStage to (Brack s (TcPending ps_var lie_var))
    
    560
    +* Set the ThLevel to (Brack s (TcPending ps_var lie_var))
    
    561 561
     
    
    562 562
     * Typecheck the body, and keep the elaborated result (despite never using it!)
    
    563 563
     
    
    ... ... @@ -669,7 +669,7 @@ runAnnotation :: CoreAnnTarget -> LHsExpr GhcRn -> TcM Annotation
    669 669
     -- See Note [How brackets and nested splices are handled]
    
    670 670
     tcTypedBracket rn_expr expr res_ty
    
    671 671
       = addErrCtxt (TypedTHBracketCtxt expr) $
    
    672
    -    do { cur_stage <- getStage
    
    672
    +    do { cur_lvl <- getThLevel
    
    673 673
            ; ps_ref <- newMutVar []
    
    674 674
            ; lie_var <- getConstraintVar   -- Any constraints arising from nested splices
    
    675 675
                                            -- should get thrown into the constraint set
    
    ... ... @@ -686,7 +686,7 @@ tcTypedBracket rn_expr expr res_ty
    686 686
            -- The typechecked expression won't be used, so we just discard it
    
    687 687
            --   (See Note [The life cycle of a TH quotation] in GHC.Hs.Expr)
    
    688 688
            -- We'll typecheck it again when we splice it in somewhere
    
    689
    -       ; (tc_expr, expr_ty) <- setStage (Brack cur_stage (TcPending ps_ref lie_var wrapper)) $
    
    689
    +       ; (tc_expr, expr_ty) <- setThLevel (Brack cur_lvl (TcPending ps_ref lie_var wrapper)) $
    
    690 690
                                     tcScalingUsage ManyTy $
    
    691 691
                                     -- Scale by Many, TH lifting is currently nonlinear (#18465)
    
    692 692
                                     tcInferRhoNC expr
    
    ... ... @@ -835,8 +835,8 @@ getUntypedSpliceBody (HsUntypedSpliceNested {})
    835 835
     tcTypedSplice splice_name expr res_ty
    
    836 836
       = addErrCtxt (TypedSpliceCtxt (Just splice_name) expr) $
    
    837 837
         setSrcSpan (getLocA expr)    $ do
    
    838
    -    { stage <- getStage
    
    839
    -    ; case stage of
    
    838
    +    { lvl <- getThLevel
    
    839
    +    ; case lvl of
    
    840 840
               Splice {}            -> tcTopSplice expr res_ty
    
    841 841
               Brack pop_stage pend -> tcNestedSplice pop_stage pend splice_name expr res_ty
    
    842 842
               RunSplice _          ->
    
    ... ... @@ -889,7 +889,7 @@ tcTopSpliceExpr isTypedSplice tc_action
    889 889
       = checkNoErrs $  -- checkNoErrs: must not try to run the thing
    
    890 890
                        -- if the type checker fails!
    
    891 891
     
    
    892
    -    setStage (Splice isTypedSplice Comp) $
    
    892
    +    setThLevel (Splice isTypedSplice Comp) $
    
    893 893
         do {    -- Typecheck the expression
    
    894 894
              (mb_expr', wanted) <- tryCaptureConstraints tc_action
    
    895 895
                  -- If tc_action fails (perhaps because of insoluble constraints)
    
    ... ... @@ -904,7 +904,7 @@ tcTopSpliceExpr isTypedSplice tc_action
    904 904
                 Just expr' -> return $ mkHsDictLet (EvBinds const_binds) expr' }
    
    905 905
     
    
    906 906
     ------------------
    
    907
    -tcNestedSplice :: ThStage -> PendingStuff -> Name
    
    907
    +tcNestedSplice :: ThLevel -> PendingStuff -> Name
    
    908 908
                     -> LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
    
    909 909
         -- See Note [How brackets and nested splices are handled]
    
    910 910
         -- A splice inside brackets
    
    ... ... @@ -912,7 +912,7 @@ tcNestedSplice pop_stage (TcPending ps_var lie_var q@(QuoteWrapper _ m_var)) spl
    912 912
       = do { res_ty <- expTypeToType res_ty
    
    913 913
            ; let rep = getRuntimeRep res_ty
    
    914 914
            ; meta_exp_ty <- tcTExpTy m_var res_ty
    
    915
    -       ; expr' <- setStage pop_stage $
    
    915
    +       ; expr' <- setThLevel pop_stage $
    
    916 916
                       setConstraintVar lie_var $
    
    917 917
                       tcCheckMonoExpr expr meta_exp_ty
    
    918 918
            ; untype_code <- tcLookupId unTypeCodeName
    
    ... ... @@ -940,7 +940,7 @@ runTopSplice (DelayedSplice lcl_env orig_expr res_ty q_expr)
    940 940
             -- See Note [Collecting modFinalizers in typed splices].
    
    941 941
            ; modfinalizers_ref <- newTcRef []
    
    942 942
              -- Run the expression
    
    943
    -       ; expr2 <- setStage (RunSplice modfinalizers_ref) $
    
    943
    +       ; expr2 <- setThLevel (RunSplice modfinalizers_ref) $
    
    944 944
                         runMetaE zonked_q_expr
    
    945 945
            ; mod_finalizers <- readTcRef modfinalizers_ref
    
    946 946
            ; addModFinalizersWithLclEnv $ ThModFinalizers mod_finalizers
    
    ... ... @@ -1651,15 +1651,15 @@ lookupThInstName th_type = do
    1651 1651
     -- | Adds a mod finalizer reference to the local environment.
    
    1652 1652
     addModFinalizerRef :: ForeignRef (TH.Q ()) -> TcM ()
    
    1653 1653
     addModFinalizerRef finRef = do
    
    1654
    -    th_stage <- getStage
    
    1655
    -    case th_stage of
    
    1654
    +    th_lvl <- getThLevel
    
    1655
    +    case th_lvl of
    
    1656 1656
           RunSplice th_modfinalizers_var -> updTcRef th_modfinalizers_var (finRef :)
    
    1657 1657
           -- This case happens only if a splice is executed and the caller does
    
    1658
    -      -- not set the 'ThStage' to 'RunSplice' to collect finalizers.
    
    1658
    +      -- not set the 'ThLevel' to 'RunSplice' to collect finalizers.
    
    1659 1659
           -- See Note [Delaying modFinalizers in untyped splices] in GHC.Rename.Splice.
    
    1660 1660
           _ ->
    
    1661 1661
             pprPanic "addModFinalizer was called when no finalizers were collected"
    
    1662
    -                 (ppr th_stage)
    
    1662
    +                 (ppr th_lvl)
    
    1663 1663
     
    
    1664 1664
     -- | Releases the external interpreter state.
    
    1665 1665
     finishTH :: TcM ()
    

  • compiler/GHC/Tc/Solver/Dict.hs
    ... ... @@ -904,7 +904,7 @@ checkInstanceOK :: CtLoc -> InstanceWhat -> TcPredType -> TcS CtLoc
    904 904
     -- Returns the CtLoc to used for sub-goals
    
    905 905
     -- Probably also want to call checkReductionDepth
    
    906 906
     checkInstanceOK loc what pred
    
    907
    -  = do { checkWellStagedDFun loc what pred
    
    907
    +  = do { checkWellLevelledDFun loc what pred
    
    908 908
            ; return deeper_loc }
    
    909 909
       where
    
    910 910
          deeper_loc = zap_origin (bumpCtLocDepth loc)
    

  • compiler/GHC/Tc/Solver/Monad.hs
    ... ... @@ -125,7 +125,7 @@ module GHC.Tc.Solver.Monad (
    125 125
         -- Misc
    
    126 126
         getDefaultInfo, getDynFlags, getGlobalRdrEnvTcS,
    
    127 127
         matchFam, matchFamTcM,
    
    128
    -    checkWellStagedDFun,
    
    128
    +    checkWellLevelledDFun,
    
    129 129
         pprEq,
    
    130 130
     
    
    131 131
         -- Enforcing invariants for type equalities
    
    ... ... @@ -1598,48 +1598,46 @@ recordUsedGREs gres
    1598 1598
     -- Various smaller utilities [TODO, maybe will be absorbed in the instance matcher]
    
    1599 1599
     -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1600 1600
     
    
    1601
    -checkWellStagedDFun :: CtLoc -> InstanceWhat -> PredType -> TcS ()
    
    1601
    +checkWellLevelledDFun :: CtLoc -> InstanceWhat -> PredType -> TcS ()
    
    1602 1602
     -- Check that we do not try to use an instance before it is available.  E.g.
    
    1603 1603
     --    instance Eq T where ...
    
    1604 1604
     --    f x = $( ... (\(p::T) -> p == p)... )
    
    1605 1605
     -- Here we can't use the equality function from the instance in the splice
    
    1606 1606
     
    
    1607
    -checkWellStagedDFun loc what pred
    
    1607
    +checkWellLevelledDFun loc what pred
    
    1608 1608
       = do
    
    1609
    -      mbind_lvl <- checkWellStagedInstanceWhat what
    
    1610
    -      --env <- getLclEnv
    
    1611
    -      --use_lvl <- thLevel <$> (wrapTcS $ TcM.getStage)
    
    1609
    +      mbind_lvl <- checkWellLevelledInstanceWhat what
    
    1612 1610
           case mbind_lvl of
    
    1613 1611
             Just (bind_lvl, is_local) ->
    
    1614 1612
               wrapTcS $ TcM.setCtLocM loc $ do
    
    1615
    -              { use_stage <- TcM.getStage
    
    1613
    +              { use_lvl <- thLevelIndex <$> TcM.getThLevel
    
    1616 1614
                   ; dflags <- getDynFlags
    
    1617
    -              ; checkCrossStageClass dflags (StageCheckInstance what pred) bind_lvl (thLevel use_stage) is_local  }
    
    1615
    +              ; checkCrossLevelClass dflags (LevelCheckInstance what pred) bind_lvl use_lvl is_local  }
    
    1618 1616
             -- If no level information is returned for an InstanceWhat, then it's safe to use
    
    1619 1617
             -- at any level.
    
    1620 1618
             Nothing -> return ()
    
    1621 1619
     
    
    1622 1620
     
    
    1623
    --- TODO: Unify this with checkCrossStageLifting function
    
    1624
    -checkCrossStageClass :: DynFlags -> StageCheckReason -> Set.Set ThLevel -> ThLevel
    
    1621
    +-- TODO: Unify this with checkCrossLevelLifting function
    
    1622
    +checkCrossLevelClass :: DynFlags -> LevelCheckReason -> Set.Set ThLevelIndex -> ThLevelIndex
    
    1625 1623
                                 -> Bool -> TcM ()
    
    1626
    -checkCrossStageClass dflags reason bind_lvl use_lvl is_local
    
    1627
    -  -- If the Id is imported, ie global, then allow with PathCrossStagedPersist
    
    1624
    +checkCrossLevelClass dflags reason bind_lvl use_lvl_idx is_local
    
    1625
    +  -- If the Id is imported, ie global, then allow with ImplicitStagePersistence
    
    1628 1626
       | not is_local
    
    1629 1627
       , xopt LangExt.ImplicitStagePersistence dflags
    
    1630 1628
       = return ()
    
    1631
    -  | use_lvl `Set.member` bind_lvl = return ()
    
    1629
    +  | use_lvl_idx `Set.member` bind_lvl = return ()
    
    1632 1630
       -- With path CSP, using later than bound is fine
    
    1633 1631
       | xopt LangExt.ImplicitStagePersistence dflags
    
    1634
    -  , any (use_lvl >=) bind_lvl  = return ()
    
    1635
    -  | otherwise = TcM.failWithTc (TcRnBadlyLevelled reason bind_lvl use_lvl)
    
    1632
    +  , any (use_lvl_idx >=) bind_lvl  = return ()
    
    1633
    +  | otherwise = TcM.failWithTc (TcRnBadlyLevelled reason bind_lvl use_lvl_idx)
    
    1636 1634
     
    
    1637 1635
     
    
    1638 1636
     
    
    1639 1637
     -- | Returns the ThLevel of evidence for the solved constraint (if it has evidence)
    
    1640
    --- See Note [Well-staged instance evidence]
    
    1641
    -checkWellStagedInstanceWhat :: InstanceWhat -> TcS (Maybe (Set.Set ThLevel, Bool))
    
    1642
    -checkWellStagedInstanceWhat what
    
    1638
    +-- See Note [Well-levelled instance evidence]
    
    1639
    +checkWellLevelledInstanceWhat :: InstanceWhat -> TcS (Maybe (Set.Set ThLevelIndex, Bool))
    
    1640
    +checkWellLevelledInstanceWhat what
    
    1643 1641
       | TopLevInstance { iw_dfun_id = dfun_id } <- what
    
    1644 1642
         = do
    
    1645 1643
             -- MP: I am not sure if we have to only do this check for orphan instances.
    
    ... ... @@ -1658,25 +1656,25 @@ checkWellStagedInstanceWhat what
    1658 1656
                 instance_key = if moduleUnitId name_module `Set.member` hsc_all_home_unit_ids hsc_env
    
    1659 1657
                                  then (mkKey NormalLevel name_module)
    
    1660 1658
                                  else Right (moduleUnitId name_module)
    
    1661
    -        let lvls = [ -1 | splice_lvl instance_key]
    
    1662
    -                 ++ [ 0 | normal_lvl instance_key]
    
    1663
    -                 ++ [ 1 | quote_lvl instance_key]
    
    1659
    +        let lvls = [ spliceLevelIndex | splice_lvl instance_key]
    
    1660
    +                 ++ [ topLevelIndex | normal_lvl instance_key]
    
    1661
    +                 ++ [ quoteLevelIndex | quote_lvl instance_key]
    
    1664 1662
             if isLocalId dfun_id
    
    1665
    -          then return $ Just ( (Set.singleton topLevel, True) )
    
    1663
    +          then return $ Just ( (Set.singleton topLevelIndex, True) )
    
    1666 1664
               else return $ Just ( Set.fromList lvls, False )
    
    1667 1665
     
    
    1668 1666
       | BuiltinTypeableInstance tc <- what
    
    1669 1667
         = do
    
    1670 1668
             cur_mod <- extractModule <$> getGblEnv
    
    1671 1669
             return $ Just (if nameIsLocalOrFrom cur_mod (tyConName tc)
    
    1672
    -                        then (Set.singleton topLevel, True)
    
    1670
    +                        then (Set.singleton topLevelIndex, True)
    
    1673 1671
                             -- TODO, not correct, needs similar checks to normal instances
    
    1674
    -                        else (Set.fromList [(-1), topLevel], False))
    
    1672
    +                        else (Set.fromList [spliceLevelIndex, topLevelIndex], False))
    
    1675 1673
       | otherwise = return Nothing
    
    1676 1674
     
    
    1677 1675
     {-
    
    1678
    -Note [Well-staged instance evidence]
    
    1679
    -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1676
    +Note [Well-levelled instance evidence]
    
    1677
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1680 1678
     
    
    1681 1679
     Evidence for instances must obey the same level restrictions as normal bindings.
    
    1682 1680
     In particular, it is forbidden to use an instance in a top-level splice in the
    
    ... ... @@ -1716,12 +1714,12 @@ Main.hs:12:14: error:
    1716 1714
     
    
    1717 1715
     Solving a `Typeable (T t1 ...tn)` constraint generates code that relies on
    
    1718 1716
     `$tcT`, the `TypeRep` for `T`; and we must check that this reference to `$tcT`
    
    1719
    -is well levelled.  It's easy to know the stage of `$tcT`: for imported TyCons it
    
    1717
    +is well levelled.  It's easy to know the level of `$tcT`: for imported TyCons it
    
    1720 1718
     will be the level of the imported TyCon Name, and for local TyCons it will be `toplevel`.
    
    1721 1719
     
    
    1722 1720
     Therefore the `InstanceWhat` type had to be extended with
    
    1723 1721
     a special case for `Typeable`, which recorded the TyCon the evidence was for and
    
    1724
    -could them be used to check that we were not attempting to evidence in a stage incorrect
    
    1722
    +could them be used to check that we were not attempting to evidence in a level incorrect
    
    1725 1723
     manner.
    
    1726 1724
     
    
    1727 1725
     -}
    

  • compiler/GHC/Tc/TyCl/Instance.hs
    ... ... @@ -418,8 +418,8 @@ tcInstDeclsDeriv
    418 418
       -> [LDerivDecl GhcRn]
    
    419 419
       -> TcM (TcGblEnv, [InstInfo GhcRn], HsValBinds GhcRn)
    
    420 420
     tcInstDeclsDeriv deriv_infos derivds
    
    421
    -  = do th_stage <- getStage -- See Note [Deriving inside TH brackets]
    
    422
    -       if isBrackStage th_stage
    
    421
    +  = do th_lvl <- getThLevel -- See Note [Deriving inside TH brackets]
    
    422
    +       if isBrackLevel th_lvl
    
    423 423
            then do { gbl_env <- getGblEnv
    
    424 424
                    ; return (gbl_env, bagToList emptyBag, emptyValBindsOut) }
    
    425 425
            else do { (tcg_env, info_bag, valbinds) <- tcDeriving deriv_infos derivds
    

  • compiler/GHC/Tc/Types.hs
    ... ... @@ -59,9 +59,15 @@ module GHC.Tc.Types(
    59 59
             CompleteMatch, CompleteMatches,
    
    60 60
     
    
    61 61
             -- Template Haskell
    
    62
    -        ThStage(..), SpliceType(..), SpliceOrBracket(..), PendingStuff(..),
    
    63
    -        topStage, topAnnStage, topSpliceStage,
    
    64
    -        ThLevel, topLevel,thLevel,
    
    62
    +        ThLevel(..), SpliceType(..), SpliceOrBracket(..), PendingStuff(..),
    
    63
    +        topLevel, topAnnLevel, topSpliceLevel,
    
    64
    +        ThLevelIndex,
    
    65
    +        topLevelIndex,
    
    66
    +        spliceLevelIndex,
    
    67
    +        quoteLevelIndex,
    
    68
    +
    
    69
    +        thLevelIndex,
    
    70
    +
    
    65 71
             ForeignSrcLang(..), THDocs, DocLoc(..),
    
    66 72
             ThBindEnv,
    
    67 73
     
    

  • compiler/GHC/Tc/Types/LclEnv.hs
    ... ... @@ -11,13 +11,13 @@ module GHC.Tc.Types.LclEnv (
    11 11
       , getLclEnvLoc
    
    12 12
       , getLclEnvRdrEnv
    
    13 13
       , getLclEnvTcLevel
    
    14
    -  , getLclEnvThStage
    
    14
    +  , getLclEnvThLevel
    
    15 15
       , setLclEnvTcLevel
    
    16 16
       , setLclEnvLoc
    
    17 17
       , setLclEnvRdrEnv
    
    18 18
       , setLclEnvBinderStack
    
    19 19
       , setLclEnvErrCtxt
    
    20
    -  , setLclEnvThStage
    
    20
    +  , setLclEnvThLevel
    
    21 21
       , setLclEnvTypeEnv
    
    22 22
       , modifyLclEnvTcLevel
    
    23 23
     
    
    ... ... @@ -108,7 +108,7 @@ data TcLclCtxt
    108 108
                     --   we can look up record field names
    
    109 109
     
    
    110 110
     
    
    111
    -        tcl_th_ctxt    :: ThStage,         -- Template Haskell context
    
    111
    +        tcl_th_ctxt    :: ThLevel,         -- Template Haskell context
    
    112 112
             tcl_th_bndrs   :: ThBindEnv,       -- and binder info
    
    113 113
                 -- The ThBindEnv records the TH binding level of in-scope Names
    
    114 114
                 -- defined in this module (not imported)
    
    ... ... @@ -121,11 +121,11 @@ data TcLclCtxt
    121 121
                                      -- Ids and TyVars defined in this module
    
    122 122
         }
    
    123 123
     
    
    124
    -getLclEnvThStage :: TcLclEnv -> ThStage
    
    125
    -getLclEnvThStage = tcl_th_ctxt . tcl_lcl_ctxt
    
    124
    +getLclEnvThLevel :: TcLclEnv -> ThLevel
    
    125
    +getLclEnvThLevel = tcl_th_ctxt . tcl_lcl_ctxt
    
    126 126
     
    
    127
    -setLclEnvThStage :: ThStage -> TcLclEnv -> TcLclEnv
    
    128
    -setLclEnvThStage s = modifyLclCtxt (\env -> env { tcl_th_ctxt = s })
    
    127
    +setLclEnvThLevel :: ThLevel -> TcLclEnv -> TcLclEnv
    
    128
    +setLclEnvThLevel l = modifyLclCtxt (\env -> env { tcl_th_ctxt = l })
    
    129 129
     
    
    130 130
     getLclEnvThBndrs :: TcLclEnv -> ThBindEnv
    
    131 131
     getLclEnvThBndrs = tcl_th_bndrs . tcl_lcl_ctxt
    
    ... ... @@ -187,7 +187,7 @@ modifyLclCtxt upd env =
    187 187
     
    
    188 188
     type TcTypeEnv = NameEnv TcTyThing
    
    189 189
     
    
    190
    -type ThBindEnv = NameEnv (TopLevelFlag, ThLevel)
    
    190
    +type ThBindEnv = NameEnv (TopLevelFlag, ThLevelIndex)
    
    191 191
        -- Domain = all Ids bound in this module (ie not imported)
    
    192 192
        -- The TopLevelFlag tells if the binding is syntactically top level.
    
    193 193
        -- We need to know this, because the cross-stage persistence story allows
    

  • compiler/GHC/Tc/Types/Origin.hs
    ... ... @@ -1533,7 +1533,7 @@ data InstanceWhat -- How did we solve this constraint?
    1533 1533
                              -- See GHC.Tc.Solver.InertSet Note [Solved dictionaries]
    
    1534 1534
     
    
    1535 1535
       | BuiltinTypeableInstance TyCon   -- Built-in solver for Typeable (T t1 .. tn)
    
    1536
    -                         -- See Note [Well-staged instance evidence]
    
    1536
    +                         -- See Note [Well-levelled instance evidence]
    
    1537 1537
     
    
    1538 1538
       | BuiltinInstance      -- Built-in solver for (C t1 .. tn) where C is
    
    1539 1539
                              --   KnownNat, .. etc (classes with no top-level evidence)
    

  • compiler/GHC/Tc/Types/TH.hs
    1 1
     module GHC.Tc.Types.TH (
    
    2 2
         SpliceType(..)
    
    3 3
       , SpliceOrBracket(..)
    
    4
    -  , ThStage(..)
    
    4
    +  , ThLevel(..)
    
    5 5
       , PendingStuff(..)
    
    6
    -  , ThLevel
    
    6
    +  , ThLevelIndex
    
    7 7
       , topLevel
    
    8
    -  , topStage
    
    9
    -  , topAnnStage
    
    10
    -  , topSpliceStage
    
    11
    -  , thLevel
    
    8
    +  , topAnnLevel
    
    9
    +  , topSpliceLevel
    
    10
    +  , thLevelIndex
    
    11
    +  , topLevelIndex
    
    12
    +  , spliceLevelIndex
    
    13
    +  , quoteLevelIndex
    
    12 14
       ) where
    
    13 15
     
    
    14 16
     import GHCi.RemoteTypes
    
    ... ... @@ -27,12 +29,12 @@ import GHC.Hs.Expr ( PendingTcSplice, PendingRnSplice )
    27 29
     data SpliceType = Typed | Untyped
    
    28 30
     data SpliceOrBracket = IsSplice | IsBracket
    
    29 31
     
    
    30
    -data ThStage    -- See Note [Template Haskell state diagram]
    
    32
    +data ThLevel    -- See Note [Template Haskell state diagram]
    
    31 33
                     -- and Note [Template Haskell levels] in GHC.Tc.Gen.Splice
    
    32 34
         -- Start at:   Comp
    
    33 35
         -- At bracket: wrap current stage in Brack
    
    34 36
         -- At splice:  wrap current stage in Splice
    
    35
    -  = Splice SpliceType ThStage -- Inside a splice
    
    37
    +  = Splice SpliceType ThLevel -- Inside a splice
    
    36 38
     
    
    37 39
       | RunSplice (TcRef [ForeignRef (TH.Q ())])
    
    38 40
           -- Set when running a splice, i.e. NOT when renaming or typechecking the
    
    ... ... @@ -53,7 +55,7 @@ data ThStage -- See Note [Template Haskell state diagram]
    53 55
                     -- Binding level = 0
    
    54 56
     
    
    55 57
       | Brack                       -- Inside brackets
    
    56
    -      ThStage                   --   Enclosing stage
    
    58
    +      ThLevel                   --   Enclosing level
    
    57 59
           PendingStuff
    
    58 60
     
    
    59 61
     data PendingStuff
    
    ... ... @@ -73,35 +75,51 @@ data PendingStuff
    73 75
                                       -- `lift`.
    
    74 76
     
    
    75 77
     
    
    76
    -topStage, topAnnStage, topSpliceStage :: ThStage
    
    77
    -topStage       = Comp
    
    78
    -topAnnStage    = Splice Untyped Comp
    
    79
    -topSpliceStage = Splice Untyped Comp
    
    78
    +topLevel, topAnnLevel, topSpliceLevel :: ThLevel
    
    79
    +topLevel       = Comp
    
    80
    +topAnnLevel    = Splice Untyped Comp
    
    81
    +topSpliceLevel = Splice Untyped Comp
    
    80 82
     
    
    81
    -instance Outputable ThStage where
    
    83
    +instance Outputable ThLevel where
    
    82 84
        ppr (Splice _ s)  = text "Splice" <> parens (ppr s)
    
    83 85
        ppr (RunSplice _) = text "RunSplice"
    
    84 86
        ppr Comp          = text "Comp"
    
    85 87
        ppr (Brack s _)   = text "Brack" <> parens (ppr s)
    
    86 88
     
    
    87
    -type ThLevel = Int
    
    89
    +-- | The integer which represents the level
    
    90
    +newtype ThLevelIndex = ThLevelIndex Int deriving (Eq, Ord)
    
    88 91
         -- NB: see Note [Template Haskell levels] in GHC.Tc.Gen.Splice
    
    89 92
         -- Incremented when going inside a bracket,
    
    90 93
         -- decremented when going inside a splice
    
    91 94
     
    
    92
    -topLevel :: ThLevel
    
    93
    -topLevel = thLevel Comp
    
    95
    +instance Outputable ThLevelIndex where
    
    96
    +    ppr (ThLevelIndex i) = int i
    
    94 97
     
    
    95
    -thLevel :: ThStage -> ThLevel
    
    96
    -thLevel (Splice _ s)  = thLevel s - 1
    
    97
    -thLevel Comp          = 0
    
    98
    -thLevel (Brack s _)   = thLevel s + 1
    
    99
    -thLevel (RunSplice _) = thLevel (Splice Untyped Comp) -- previously: panic "thLevel: called when running a splice"
    
    98
    +incThLevelIndex :: ThLevelIndex -> ThLevelIndex
    
    99
    +incThLevelIndex (ThLevelIndex i) = ThLevelIndex (i + 1)
    
    100
    +
    
    101
    +decThLevelIndex :: ThLevelIndex -> ThLevelIndex
    
    102
    +decThLevelIndex (ThLevelIndex i) = ThLevelIndex (i - 1)
    
    103
    +
    
    104
    +topLevelIndex :: ThLevelIndex
    
    105
    +topLevelIndex = thLevelIndex Comp
    
    106
    +
    
    107
    +spliceLevelIndex :: ThLevelIndex
    
    108
    +spliceLevelIndex = thLevelIndex (Splice Untyped Comp)
    
    109
    +
    
    110
    +quoteLevelIndex :: ThLevelIndex
    
    111
    +quoteLevelIndex = thLevelIndex (Brack Comp RnPendingTyped)
    
    112
    +
    
    113
    +thLevelIndex :: ThLevel -> ThLevelIndex
    
    114
    +thLevelIndex (Splice _ s)  = decThLevelIndex (thLevelIndex s)
    
    115
    +thLevelIndex Comp          = ThLevelIndex 0
    
    116
    +thLevelIndex (Brack s _)   = incThLevelIndex (thLevelIndex s)
    
    117
    +thLevelIndex (RunSplice _) = thLevelIndex (Splice Untyped Comp) -- previously: panic "thLevel: called when running a splice"
    
    100 118
                             -- See Note [RunSplice ThLevel].
    
    101 119
     
    
    102 120
     {- Note [RunSplice ThLevel]
    
    103 121
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    104
    -The 'RunSplice' stage is set when executing a splice, and only when running a
    
    122
    +The 'RunSplice' level is set when executing a splice, and only when running a
    
    105 123
     splice. In particular it is not set when the splice is renamed or typechecked.
    
    106 124
     
    
    107 125
     However, this is not true. `reifyInstances` for example does rename the given type,
    

  • compiler/GHC/Tc/Utils/Concrete.hs
    ... ... @@ -670,7 +670,7 @@ checkFRR_with :: HasDebugCallStack
    670 670
                       -- ^ Returns @(co, frr_ty)@ with @co :: ty ~# frr_ty@
    
    671 671
                       -- and @frr_@ty has a fixed 'RuntimeRep'.
    
    672 672
     checkFRR_with check_kind frr_ctxt ty
    
    673
    -  = do { th_stage <- getStage
    
    673
    +  = do { th_lvl <- getThLevel
    
    674 674
            ; if
    
    675 675
               -- Shortcut: check for 'Type' and 'UnliftedType' type synonyms.
    
    676 676
               | TyConApp tc [] <- ki
    
    ... ... @@ -678,7 +678,7 @@ checkFRR_with check_kind frr_ctxt ty
    678 678
               -> return refl
    
    679 679
     
    
    680 680
               -- See [Wrinkle: Typed Template Haskell] in Note [hasFixedRuntimeRep].
    
    681
    -          | Brack _ (TcPending {}) <- th_stage
    
    681
    +          | Brack _ (TcPending {}) <- th_lvl
    
    682 682
               -> return refl
    
    683 683
     
    
    684 684
               -- Otherwise: ensure that the kind 'ki' of 'ty' is concrete.
    

  • compiler/GHC/Tc/Utils/Env.hs
    ... ... @@ -60,9 +60,9 @@ module GHC.Tc.Utils.Env(
    60 60
             tcGetDefaultTys,
    
    61 61
     
    
    62 62
             -- Template Haskell stuff
    
    63
    -        StageCheckReason(..),
    
    64
    -        tcMetaTy, thLevel,
    
    65
    -        isBrackStage,
    
    63
    +        LevelCheckReason(..),
    
    64
    +        tcMetaTy, thLevelIndex,
    
    65
    +        isBrackLevel,
    
    66 66
     
    
    67 67
             -- New Ids
    
    68 68
             newDFunName,
    
    ... ... @@ -521,9 +521,8 @@ tcExtendTyConEnv tycons thing_inside
    521 521
     -- See GHC ticket #17820 .
    
    522 522
     tcTyThBinders :: [TyThing] -> TcM ThBindEnv
    
    523 523
     tcTyThBinders implicit_things = do
    
    524
    -  stage <- getStage
    
    525
    -  let th_lvl  = thLevel stage
    
    526
    -      th_bndrs = mkNameEnv
    
    524
    +  th_lvl <- thLevelIndex <$> getThLevel
    
    525
    +  let th_bndrs = mkNameEnv
    
    527 526
                       [ ( n , (TopLevel, th_lvl) ) | n <- names ]
    
    528 527
       return th_bndrs
    
    529 528
       where
    
    ... ... @@ -759,7 +758,7 @@ tc_extend_local_env top_lvl extra_env thing_inside
    759 758
       = do  { traceTc "tc_extend_local_env" (ppr extra_env)
    
    760 759
             ; updLclCtxt upd_lcl_env thing_inside }
    
    761 760
       where
    
    762
    -    upd_lcl_env env0@(TcLclCtxt { tcl_th_ctxt  = stage
    
    761
    +    upd_lcl_env env0@(TcLclCtxt { tcl_th_ctxt  = th_lvl
    
    763 762
                                    , tcl_rdr      = rdr_env
    
    764 763
                                    , tcl_th_bndrs = th_bndrs
    
    765 764
                                    , tcl_env      = lcl_type_env })
    
    ... ... @@ -776,7 +775,7 @@ tc_extend_local_env top_lvl extra_env thing_inside
    776 775
                   -- Template Haskell staging env simultaneously. Reason for extending
    
    777 776
                   -- LocalRdrEnv: after running a TH splice we need to do renaming.
    
    778 777
           where
    
    779
    -        thlvl = (top_lvl, thLevel stage)
    
    778
    +        thlvl = (top_lvl, thLevelIndex th_lvl)
    
    780 779
     
    
    781 780
     
    
    782 781
     tcExtendLocalTypeEnv :: [(Name, TcTyThing)] -> TcLclCtxt -> TcLclCtxt
    
    ... ... @@ -931,9 +930,9 @@ tcMetaTy tc_name = do
    931 930
         t <- tcLookupTyCon tc_name
    
    932 931
         return (mkTyConTy t)
    
    933 932
     
    
    934
    -isBrackStage :: ThStage -> Bool
    
    935
    -isBrackStage (Brack {}) = True
    
    936
    -isBrackStage _other     = False
    
    933
    +isBrackLevel :: ThLevel -> Bool
    
    934
    +isBrackLevel (Brack {}) = True
    
    935
    +isBrackLevel _other     = False
    
    937 936
     
    
    938 937
     {-
    
    939 938
     ************************************************************************
    

  • compiler/GHC/Tc/Utils/Monad.hs
    ... ... @@ -117,7 +117,7 @@ module GHC.Tc.Utils.Monad(
    117 117
     
    
    118 118
       -- * Template Haskell context
    
    119 119
       recordThUse, recordThNeededRuntimeDeps,
    
    120
    -  keepAlive, getStage, getStageAndBindLevel, setStage,
    
    120
    +  keepAlive, getThLevel, getCurrentAndBindLevel, setThLevel,
    
    121 121
       addModFinalizersWithLclEnv,
    
    122 122
     
    
    123 123
       -- * Safe Haskell context
    
    ... ... @@ -399,7 +399,7 @@ initTcWithGbl hsc_env gbl_env loc do_this
    399 399
                     tcl_in_gen_code = False,
    
    400 400
                     tcl_ctxt       = [],
    
    401 401
                     tcl_rdr        = emptyLocalRdrEnv,
    
    402
    -                tcl_th_ctxt    = topStage,
    
    402
    +                tcl_th_ctxt    = topLevel,
    
    403 403
                     tcl_th_bndrs   = emptyNameEnv,
    
    404 404
                     tcl_arrow_ctxt = NoArrowCtxt,
    
    405 405
                     tcl_env        = emptyNameEnv,
    
    ... ... @@ -2110,11 +2110,11 @@ keepAlive name
    2110 2110
            ; traceRn "keep alive" (ppr name)
    
    2111 2111
            ; updTcRef (tcg_keep env) (`extendNameSet` name) }
    
    2112 2112
     
    
    2113
    -getStage :: TcM ThStage
    
    2114
    -getStage = do { env <- getLclEnv; return (getLclEnvThStage env) }
    
    2113
    +getThLevel :: TcM ThLevel
    
    2114
    +getThLevel = do { env <- getLclEnv; return (getLclEnvThLevel env) }
    
    2115 2115
     
    
    2116
    -getStageAndBindLevel :: Name -> TcRn (Maybe (TopLevelFlag, Set.Set ThLevel, ThStage))
    
    2117
    -getStageAndBindLevel name
    
    2116
    +getCurrentAndBindLevel :: Name -> TcRn (Maybe (TopLevelFlag, Set.Set ThLevelIndex, ThLevel))
    
    2117
    +getCurrentAndBindLevel name
    
    2118 2118
       = do { env <- getLclEnv;
    
    2119 2119
            ; case lookupNameEnv (getLclEnvThBndrs env) name of
    
    2120 2120
                Nothing                  -> do
    
    ... ... @@ -2128,10 +2128,10 @@ getStageAndBindLevel name
    2128 2128
                       --env <- getGlobalRdrEnv
    
    2129 2129
                       --pprTrace "NO_LVLS" (ppr name) (return Nothing)
    
    2130 2130
                       return Nothing
    
    2131
    -                else return (Just (TopLevel, lvls, getLclEnvThStage env))
    
    2132
    -           Just (top_lvl, bind_lvl) -> return (Just (top_lvl, Set.singleton bind_lvl, getLclEnvThStage env)) }
    
    2131
    +                else return (Just (TopLevel, lvls, getLclEnvThLevel env))
    
    2132
    +           Just (top_lvl, bind_lvl) -> return (Just (top_lvl, Set.singleton bind_lvl, getLclEnvThLevel env)) }
    
    2133 2133
     
    
    2134
    -getExternalBindLvl :: Name -> TcRn (Set.Set ThLevel)
    
    2134
    +getExternalBindLvl :: Name -> TcRn (Set.Set ThLevelIndex)
    
    2135 2135
     getExternalBindLvl name = do
    
    2136 2136
       env <- getGlobalRdrEnv
    
    2137 2137
       mod <- getModule
    
    ... ... @@ -2139,16 +2139,16 @@ getExternalBindLvl name = do
    2139 2139
         Just gre -> return $ (Set.map convert_lvl (greLevels gre))
    
    2140 2140
         Nothing ->
    
    2141 2141
           if nameIsLocalOrFrom mod name
    
    2142
    -        then return $ Set.singleton topLevel
    
    2142
    +        then return $ Set.singleton topLevelIndex
    
    2143 2143
     --        else pprTrace "NO LVLS" (ppr name) (return Set.empty) -- pprPanic "getExternalBindLvl" (ppr env $$ ppr name $$ ppr (nameSrcSpan name))
    
    2144 2144
             else return Set.empty
    
    2145 2145
       where
    
    2146
    -    convert_lvl NormalLevel = thLevel topStage
    
    2147
    -    convert_lvl SpliceLevel = thLevel topSpliceStage
    
    2148
    -    convert_lvl QuoteLevel  = thLevel (Brack topStage undefined)
    
    2146
    +    convert_lvl NormalLevel = topLevelIndex
    
    2147
    +    convert_lvl SpliceLevel = spliceLevelIndex
    
    2148
    +    convert_lvl QuoteLevel  = quoteLevelIndex
    
    2149 2149
     
    
    2150
    -setStage :: ThStage -> TcM a -> TcRn a
    
    2151
    -setStage s = updLclEnv (setLclEnvThStage s)
    
    2150
    +setThLevel :: ThLevel -> TcM a -> TcRn a
    
    2151
    +setThLevel l = updLclEnv (setLclEnvThLevel l)
    
    2152 2152
     
    
    2153 2153
     -- | Adds the given modFinalizers to the global environment and set them to use
    
    2154 2154
     -- the current local environment.
    

  • compiler/GHC/Tc/Utils/TcMType.hs
    ... ... @@ -458,11 +458,11 @@ newInferExpType = new_inferExpType Nothing
    458 458
     
    
    459 459
     newInferExpTypeFRR :: FixedRuntimeRepContext -> TcM ExpTypeFRR
    
    460 460
     newInferExpTypeFRR frr_orig
    
    461
    -  = do { th_stage <- getStage
    
    461
    +  = do { th_lvl <- getThLevel
    
    462 462
            ; if
    
    463 463
               -- See [Wrinkle: Typed Template Haskell]
    
    464 464
               -- in Note [hasFixedRuntimeRep] in GHC.Tc.Utils.Concrete.
    
    465
    -          | Brack _ (TcPending {}) <- th_stage
    
    465
    +          | Brack _ (TcPending {}) <- th_lvl
    
    466 466
               -> new_inferExpType Nothing
    
    467 467
     
    
    468 468
               | otherwise
    
    ... ... @@ -800,11 +800,11 @@ newConcreteTyVar :: HasDebugCallStack => ConcreteTvOrigin
    800 800
                      -> FastString -> TcKind -> TcM TcTyVar
    
    801 801
     newConcreteTyVar reason fs kind
    
    802 802
       = assertPpr (isConcreteType kind) assert_msg $
    
    803
    -  do { th_stage <- getStage
    
    803
    +  do { th_lvl <- getThLevel
    
    804 804
          ; if
    
    805 805
             -- See [Wrinkle: Typed Template Haskell]
    
    806 806
             -- in Note [hasFixedRuntimeRep] in GHC.Tc.Utils.Concrete.
    
    807
    -        | Brack _ (TcPending {}) <- th_stage
    
    807
    +        | Brack _ (TcPending {}) <- th_lvl
    
    808 808
             -> newNamedAnonMetaTyVar fs TauTv kind
    
    809 809
     
    
    810 810
             | otherwise
    
    ... ... @@ -986,8 +986,8 @@ newOpenFlexiTyVar
    986 986
     -- in GHC.Tc.Utils.Concrete.
    
    987 987
     newOpenFlexiFRRTyVar :: FixedRuntimeRepContext -> TcM TcTyVar
    
    988 988
     newOpenFlexiFRRTyVar frr_ctxt
    
    989
    -  = do { th_stage <- getStage
    
    990
    -       ; case th_stage of
    
    989
    +  = do { th_lvl <- getThLevel
    
    990
    +       ; case th_lvl of
    
    991 991
               { Brack _ (TcPending {}) -- See [Wrinkle: Typed Template Haskell]
    
    992 992
                   -> newOpenFlexiTyVar -- in Note [hasFixedRuntimeRep] in GHC.Tc.Utils.Concrete.
    
    993 993
               ; _ ->
    
    ... ... @@ -1040,11 +1040,11 @@ newMetaTyVarX = new_meta_tv_x TauTv
    1040 1040
     -- | Like 'newMetaTyVarX', but for concrete type variables.
    
    1041 1041
     newConcreteTyVarX :: ConcreteTvOrigin -> Subst -> TyVar -> TcM (Subst, TcTyVar)
    
    1042 1042
     newConcreteTyVarX conc subst tv
    
    1043
    -  = do { th_stage <- getStage
    
    1043
    +  = do { th_lvl <- getThLevel
    
    1044 1044
            ; if
    
    1045 1045
               -- See [Wrinkle: Typed Template Haskell]
    
    1046 1046
               -- in Note [hasFixedRuntimeRep] in GHC.Tc.Utils.Concrete.
    
    1047
    -          | Brack _ (TcPending {}) <- th_stage
    
    1047
    +          | Brack _ (TcPending {}) <- th_lvl
    
    1048 1048
               -> new_meta_tv_x TauTv subst tv
    
    1049 1049
               | otherwise
    
    1050 1050
               -> new_meta_tv_x (ConcreteTv conc) subst tv }
    

  • compiler/GHC/Types/Error/Codes.hs
    ... ... @@ -967,7 +967,7 @@ type family GhcDiagnosticCode c = n | n -> c where
    967 967
       GhcDiagnosticCode "NestedTHBrackets"                              = 59185
    
    968 968
       GhcDiagnosticCode "AddTopDeclsUnexpectedDeclarationSplice"        = 17599
    
    969 969
       GhcDiagnosticCode "BadImplicitSplice"                             = 25277
    
    970
    -  GhcDiagnosticCode "QuotedNameWrongStage"                          = 57695
    
    970
    +  GhcDiagnosticCode "QuotedNameWrongStage"                          = Outdated 57695
    
    971 971
       GhcDiagnosticCode "IllegalStaticFormInSplice"                     = 12219
    
    972 972
     
    
    973 973
       -- Zonker messages