
Matthew Pickering pushed to branch wip/splice-imports-2025 at Glasgow Haskell Compiler / GHC Commits: 5e145778 by Matthew Pickering at 2025-04-17T15:37:32+01:00 More stage -> level - - - - - be437442 by Matthew Pickering at 2025-04-17T15:42:05+01:00 Remove quoted name wrong stage error - - - - - 88820305 by Matthew Pickering at 2025-04-17T15:44:55+01:00 fix note references - - - - - 23 changed files: - compiler/GHC/Rename/Env.hs - compiler/GHC/Rename/Expr.hs - compiler/GHC/Rename/Module.hs - compiler/GHC/Rename/Names.hs - compiler/GHC/Rename/Splice.hs - compiler/GHC/Rename/Utils.hs - compiler/GHC/Tc/Errors/Ppr.hs - compiler/GHC/Tc/Errors/Types.hs - compiler/GHC/Tc/Gen/App.hs - compiler/GHC/Tc/Gen/Head.hs - compiler/GHC/Tc/Gen/Splice.hs - compiler/GHC/Tc/Solver/Dict.hs - compiler/GHC/Tc/Solver/Monad.hs - compiler/GHC/Tc/TyCl/Instance.hs - compiler/GHC/Tc/Types.hs - compiler/GHC/Tc/Types/LclEnv.hs - compiler/GHC/Tc/Types/Origin.hs - compiler/GHC/Tc/Types/TH.hs - compiler/GHC/Tc/Utils/Concrete.hs - compiler/GHC/Tc/Utils/Env.hs - compiler/GHC/Tc/Utils/Monad.hs - compiler/GHC/Tc/Utils/TcMType.hs - compiler/GHC/Types/Error/Codes.hs Changes: ===================================== compiler/GHC/Rename/Env.hs ===================================== @@ -238,8 +238,8 @@ newTopSrcBinder (L loc rdr_name) -- Binders should not be qualified; if they are, and with a different -- module name, we get a confusing "M.T is not in scope" error later - ; stage <- getStage - ; if isBrackStage stage then + ; level <- getThLevel + ; if isBrackLevel level then -- We are inside a TH bracket, so make an *Internal* name -- See Note [Top-level Names in Template Haskell decl quotes] in GHC.Rename.Names do { uniq <- newUnique @@ -1015,7 +1015,7 @@ lookupLocalOccRn_maybe rdr_name = do { local_env <- getLocalRdrEnv ; return (lookupLocalRdrEnv local_env rdr_name) } -lookupLocalOccThLvl_maybe :: Name -> RnM (Maybe (TopLevelFlag, ThLevel)) +lookupLocalOccThLvl_maybe :: Name -> RnM (Maybe (TopLevelFlag, ThLevelIndex)) -- Just look in the local environment lookupLocalOccThLvl_maybe name = do { lcl_env <- getLclEnv ===================================== compiler/GHC/Rename/Expr.hs ===================================== @@ -33,7 +33,7 @@ import GHC.Prelude hiding (head, init, last, scanl, tail) import GHC.Hs import GHC.Tc.Errors.Types -import GHC.Tc.Utils.Env ( isBrackStage ) +import GHC.Tc.Utils.Env ( isBrackLevel ) import GHC.Tc.Utils.Monad import GHC.Rename.Bind ( rnLocalBindsAndThen, rnLocalValBindsLHS, rnLocalValBindsRHS @@ -656,8 +656,8 @@ rnExpr e@(HsStatic _ expr) = do unlessXOptM LangExt.StaticPointers $ addErr $ TcRnIllegalStaticExpression e (expr',fvExpr) <- rnLExpr expr - stage <- getStage - case stage of + level <- getThLevel + case level of Splice _ _ -> addErr $ TcRnTHError $ IllegalStaticFormInSplice e _ -> return () mod <- getModule @@ -1152,7 +1152,7 @@ postProcessStmtsForApplicativeDo ctxt stmts | otherwise = False -- don't apply the transformation inside TH brackets, because -- GHC.HsToCore.Quote does not handle ApplicativeDo. - ; in_th_bracket <- isBrackStage <$> getStage + ; in_th_bracket <- isBrackLevel <$> getThLevel ; if ado_is_on && is_do_expr && not in_th_bracket then do { traceRn "ppsfa" (ppr stmts) ; rearrangeForApplicativeDo ctxt stmts } ===================================== compiler/GHC/Rename/Module.hs ===================================== @@ -348,8 +348,8 @@ rnAnnDecl :: AnnDecl GhcPs -> RnM (AnnDecl GhcRn, FreeVars) rnAnnDecl ann@(HsAnnotation (_, s) provenance expr) = addErrCtxt (AnnCtxt ann) $ do { (provenance', provenance_fvs) <- rnAnnProvenance provenance - ; cur_stage <- getStage - ; (expr', expr_fvs) <- setStage (Splice Untyped cur_stage) $ + ; cur_level <- getThLevel + ; (expr', expr_fvs) <- setThLevel (Splice Untyped cur_level) $ rnLExpr expr ; return (HsAnnotation (noAnn, s) provenance' expr', provenance_fvs `plusFV` expr_fvs) } ===================================== compiler/GHC/Rename/Names.hs ===================================== @@ -635,7 +635,7 @@ top level binders specially in two ways See Note [GlobalRdrEnv shadowing] 3. We find out whether we are inside a [d| ... |] by testing the TH - stage. This is a slight hack, because the stage field was really + level. This is a slight hack, because the level field was really meant for the type checker, and here we are not interested in the fields of Brack, hence the error thunks in thRnBrack. -} @@ -651,18 +651,18 @@ extendGlobalRdrEnvRn :: [GlobalRdrElt] extendGlobalRdrEnvRn new_gres new_fixities = checkNoErrs $ -- See Note [Fail fast on duplicate definitions] do { (gbl_env, lcl_env) <- getEnvs - ; stage <- getStage + ; level <- getThLevel ; isGHCi <- getIsGHCi ; let rdr_env = tcg_rdr_env gbl_env fix_env = tcg_fix_env gbl_env th_bndrs = getLclEnvThBndrs lcl_env - th_lvl = thLevel stage + th_lvl = thLevelIndex level -- Delete new_occs from global and local envs -- If we are in a TemplateHaskell decl bracket, -- we are going to shadow them -- See Note [GlobalRdrEnv shadowing] - inBracket = isBrackStage stage + inBracket = isBrackLevel level lcl_env_TH = modifyLclCtxt (\lcl_env -> lcl_env { tcl_rdr = minusLocalRdrEnv (tcl_rdr lcl_env) new_gres_env }) lcl_env -- See Note [GlobalRdrEnv shadowing] ===================================== compiler/GHC/Rename/Splice.hs ===================================== @@ -124,8 +124,8 @@ rnTypedBracket e br_body do { checkForTemplateHaskellQuotes e -- Check for nested brackets - ; cur_stage <- getStage - ; case cur_stage of + ; cur_level <- getThLevel + ; case cur_level of { Splice _ _ -> return () -- See Note [Untyped quotes in typed splices and vice versa] ; RunSplice _ -> @@ -141,7 +141,7 @@ rnTypedBracket e br_body ; recordThUse ; traceRn "Renaming typed TH bracket" empty - ; (body', fvs_e) <- setStage (Brack cur_stage RnPendingTyped) $ rnLExpr br_body + ; (body', fvs_e) <- setThLevel (Brack cur_level RnPendingTyped) $ rnLExpr br_body ; return (HsTypedBracket noExtField body', fvs_e) @@ -153,8 +153,8 @@ rnUntypedBracket e br_body do { checkForTemplateHaskellQuotes e -- Check for nested brackets - ; cur_stage <- getStage - ; case cur_stage of + ; cur_level <- getThLevel + ; case cur_level of { Splice _ _ -> return () -- See Note [Untyped quotes in typed splices and vice versa] ; RunSplice _ -> @@ -174,7 +174,7 @@ rnUntypedBracket e br_body ; (body', fvs_e) <- -- See Note [Rebindable syntax and Template Haskell] unsetXOptM LangExt.RebindableSyntax $ - setStage (Brack cur_stage (RnPendingUntyped ps_var)) $ + setThLevel (Brack cur_level (RnPendingUntyped ps_var)) $ rn_utbracket br_body ; pendings <- readMutVar ps_var ; return (HsUntypedBracket pendings body', fvs_e) @@ -279,14 +279,14 @@ rnUntypedSpliceGen :: (HsUntypedSplice GhcRn -> RnM (a, FreeVars)) -> RnM (a, FreeVars) rnUntypedSpliceGen run_splice pend_splice splice = addErrCtxt (UntypedSpliceCtxt splice) $ do - { stage <- getStage - ; case stage of + { level <- getThLevel + ; case level of Brack _ RnPendingTyped -> failWithTc $ thSyntaxError $ MismatchedSpliceType Untyped IsSplice - Brack pop_stage (RnPendingUntyped ps_var) - -> do { (splice', fvs) <- setStage pop_stage $ + Brack pop_level (RnPendingUntyped ps_var) + -> do { (splice', fvs) <- setThLevel pop_level $ rnUntypedSplice splice ; loc <- getSrcSpanM ; splice_name <- newLocalBndrRn (L (noAnnSrcSpan loc) unqualSplice) @@ -296,9 +296,9 @@ rnUntypedSpliceGen run_splice pend_splice splice ; return (result, fvs) } _ -> do { checkTopSpliceAllowed splice - ; cur_stage <- getStage + ; cur_level <- getThLevel ; (splice', fvs1) <- checkNoErrs $ - setStage (Splice Untyped cur_stage) $ + setThLevel (Splice Untyped cur_level) $ rnUntypedSplice splice -- checkNoErrs: don't attempt to run the splice if -- renaming it failed; otherwise we get a cascade of @@ -350,7 +350,7 @@ runRnSplice flavour run_meta ppr_res splice -- Run the expression ; mod_finalizers_ref <- newTcRef [] - ; result <- setStage (RunSplice mod_finalizers_ref) $ + ; result <- setThLevel (RunSplice mod_finalizers_ref) $ run_meta zonked_q_expr ; mod_finalizers <- readTcRef mod_finalizers_ref ; traceSplice (SpliceInfo { spliceDescription = what @@ -434,10 +434,10 @@ rnTypedSplice :: LHsExpr GhcPs -- Typed splice expression -> RnM (HsExpr GhcRn, FreeVars) rnTypedSplice expr = addErrCtxt (TypedSpliceCtxt Nothing expr) $ do - { stage <- getStage - ; case stage of - Brack pop_stage RnPendingTyped - -> setStage pop_stage rn_splice + { level <- getThLevel + ; case level of + Brack pop_level RnPendingTyped + -> setThLevel pop_level rn_splice Brack _ (RnPendingUntyped _) -> failWithTc $ thSyntaxError $ MismatchedSpliceType Typed IsSplice @@ -445,8 +445,8 @@ rnTypedSplice expr _ -> do { unlessXOptM LangExt.TemplateHaskell (failWith $ thSyntaxError IllegalTHSplice) - ; cur_stage <- getStage - ; (result, fvs1) <- checkNoErrs $ setStage (Splice Typed cur_stage) rn_splice + ; cur_level <- getThLevel + ; (result, fvs1) <- checkNoErrs $ setThLevel (Splice Typed cur_level) rn_splice -- checkNoErrs: don't attempt to run the splice if -- renaming it failed; otherwise we get a cascade of -- errors from e.g. unbound variables @@ -790,9 +790,9 @@ rnTopSpliceDecls :: HsUntypedSplice GhcPs -> RnM ([LHsDecl GhcPs], FreeVars) -- Declaration splice at the very top level of the module rnTopSpliceDecls splice = do { checkTopSpliceAllowed splice - ; cur_stage <- getStage + ; cur_level <- getThLevel ; (rn_splice, fvs) <- checkNoErrs $ - setStage (Splice Untyped cur_stage) $ + setThLevel (Splice Untyped cur_level) $ rnUntypedSplice splice -- As always, be sure to checkNoErrs above lest we end up with -- holes making it to typechecking, hence #12584. @@ -909,27 +909,25 @@ checkThLocalTyName name | otherwise = do { traceRn "checkThLocalTyName" (ppr name) - ; mb_local_use <- getStageAndBindLevel name + ; mb_local_use <- getCurrentAndBindLevel name ; case mb_local_use of { Nothing -> return () ; -- Not a locally-bound thing - Just (top_lvl, bind_lvl, use_stage) -> - do { let use_lvl = thLevel use_stage - -- We don't check the well stageness of name here. + Just (top_lvl, bind_lvl, use_lvl) -> + do { let use_lvl_idx = thLevelIndex use_lvl + -- We don't check the well levelledness of name here. -- this would break test for #20969 -- -- Consequently there is no check&restiction for top level splices. -- But it's annoying anyway. -- - -- Therefore checkCrossStageLiftingTy shouldn't assume anything + -- Therefore checkCrossLevelLiftingTy shouldn't assume anything -- about bind_lvl and use_lvl relation. -- - -- ; checkWellStaged (StageCheckSplice name) bind_lvl use_lvl - ; traceRn "checkThLocalTyName" (ppr name <+> ppr bind_lvl - <+> ppr use_stage + <+> ppr use_lvl <+> ppr use_lvl) ; dflags <- getDynFlags - ; checkCrossStageLiftingTy dflags top_lvl bind_lvl use_stage use_lvl name } } } + ; checkCrossLevelLiftingTy dflags top_lvl bind_lvl use_lvl use_lvl_idx name } } } -- | Check whether we are allowed to use a Name in this context (for TH purposes) -- In the case of a level incorrect program, attempt to fix it by using @@ -953,34 +951,34 @@ checkThLocalName allow_lifting name | otherwise = do { - mb_local_use <- getStageAndBindLevel name + mb_local_use <- getCurrentAndBindLevel name ; case mb_local_use of { Nothing -> return () ; -- Not a locally-bound thing - Just (top_lvl, bind_lvl, use_stage) -> - do { let use_lvl = thLevel use_stage + Just (top_lvl, bind_lvl, use_lvl) -> + do { let use_lvl_idx = thLevelIndex use_lvl ; cur_mod <- extractModule <$> getGblEnv ; let is_local | Just mod <- nameModule_maybe name = mod == cur_mod | otherwise = True - ; traceRn "checkThLocalName" (ppr name <+> ppr bind_lvl <+> ppr use_lvl <+> ppr use_stage) + ; traceRn "checkThLocalName" (ppr name <+> ppr bind_lvl <+> ppr use_lvl <+> ppr use_lvl) ; dflags <- getDynFlags ; env <- getGlobalRdrEnv ; let mgre = lookupGRE_Name env name - ; checkCrossStageLifting dflags (StageCheckSplice name mgre) top_lvl is_local allow_lifting bind_lvl use_stage use_lvl name } } } + ; checkCrossLevelLifting dflags (LevelCheckSplice name mgre) top_lvl is_local allow_lifting bind_lvl use_lvl use_lvl_idx name } } } -------------------------------------- -checkCrossStageLifting :: DynFlags - -> StageCheckReason +checkCrossLevelLifting :: DynFlags + -> LevelCheckReason -> TopLevelFlag -> Bool -> Bool - -> Set.Set ThLevel - -> ThStage + -> Set.Set ThLevelIndex -> ThLevel + -> ThLevelIndex -> Name -> TcM () -checkCrossStageLifting dflags reason top_lvl is_local allow_lifting bind_lvl use_stage use_lvl name +checkCrossLevelLifting dflags reason top_lvl is_local allow_lifting bind_lvl use_lvl use_lvl_idx name -- 1. If name is in-scope, at the correct level. - | use_lvl `Set.member` bind_lvl = return () + | use_lvl_idx `Set.member` bind_lvl = return () -- 2. Name is imported with -XImplicitStagePersistence | not is_local , xopt LangExt.ImplicitStagePersistence dflags = return () @@ -988,23 +986,23 @@ checkCrossStageLifting dflags reason top_lvl is_local allow_lifting bind_lvl use -- to be persisted into the future. | isTopLevel top_lvl , is_local - , any (use_lvl >=) (Set.toList bind_lvl) + , any (use_lvl_idx >=) (Set.toList bind_lvl) , xopt LangExt.ImplicitStagePersistence dflags = when (isExternalName name) (keepAlive name) -- 4. Name is in an untyped bracket, and we are allowed to attempt to lift. - | Brack _ (RnPendingUntyped ps_var) <- use_stage -- Only for untyped brackets + | Brack _ (RnPendingUntyped ps_var) <- use_lvl -- Only for untyped brackets , allow_lifting = do dflags <- getDynFlags - check_cross_stage_lifting dflags top_lvl name ps_var + check_cross_level_lifting dflags top_lvl name ps_var -- 5. For an typed bracket, these checks happen again later on (checkThLocalId) -- In the future we should do all the level checks here. - | Brack _ RnPendingTyped <- use_stage -- Lift for typed brackets is inserted later. + | Brack _ RnPendingTyped <- use_lvl -- Lift for typed brackets is inserted later. = return () -- Otherwise, we have a level error, report. - | otherwise = addErrTc (TcRnBadlyLevelled reason bind_lvl use_lvl) + | otherwise = addErrTc (TcRnBadlyLevelled reason bind_lvl use_lvl_idx) -check_cross_stage_lifting :: DynFlags -> TopLevelFlag -> Name -> TcRef [PendingRnSplice] -> TcM () -check_cross_stage_lifting dflags top_lvl name ps_var +check_cross_level_lifting :: DynFlags -> TopLevelFlag -> Name -> TcRef [PendingRnSplice] -> TcM () +check_cross_level_lifting dflags top_lvl name ps_var | isTopLevel top_lvl , xopt LangExt.ImplicitStagePersistence dflags -- Top-level identifiers in this module, @@ -1027,7 +1025,7 @@ check_cross_stage_lifting dflags top_lvl name ps_var -- If 'x' occurs many times we may get many identical -- bindings of the same SplicePointName, but that doesn't -- matter, although it's a mite untidy. - do { traceRn "checkCrossStageLifting" (ppr name) + do { traceRn "checkCrossLevelLifting" (ppr name) -- Construct the (lift x) expression ; let lift_expr = nlHsApp (nlHsVar liftName) (nlHsVar name) @@ -1040,8 +1038,8 @@ check_cross_stage_lifting dflags top_lvl name ps_var ; ps <- readMutVar ps_var ; writeMutVar ps_var (pend_splice : ps) } -checkCrossStageLiftingTy :: DynFlags -> TopLevelFlag -> Set.Set ThLevel -> ThStage -> ThLevel -> Name -> TcM () -checkCrossStageLiftingTy dflags top_lvl bind_lvl _use_stage use_lvl name +checkCrossLevelLiftingTy :: DynFlags -> TopLevelFlag -> Set.Set ThLevelIndex -> ThLevel -> ThLevelIndex -> Name -> TcM () +checkCrossLevelLiftingTy dflags top_lvl bind_lvl _use_lvl use_lvl_idx name | isTopLevel top_lvl , xopt LangExt.ImplicitStagePersistence dflags = return () @@ -1052,8 +1050,8 @@ checkCrossStageLiftingTy dflags top_lvl bind_lvl _use_stage use_lvl name -- Can also happen for negative cases -- See comment in checkThLocalTyName: - | use_lvl `notElem` bind_lvl - = addDiagnostic $ TcRnBadlyLevelledType name bind_lvl use_lvl + | use_lvl_idx `notElem` bind_lvl + = addDiagnostic $ TcRnBadlyLevelledType name bind_lvl use_lvl_idx | otherwise = return () @@ -1094,7 +1092,7 @@ them in the keep-alive set. Note [Quoting names] ~~~~~~~~~~~~~~~~~~~~ A quoted name 'n is a bit like a quoted expression [| n |], except that we -have no cross-stage lifting (c.f. GHC.Tc.Gen.Expr.thBrackId). So, after incrementing +have no cross-level lifting (c.f. GHC.Tc.Gen.Expr.thBrackId). So, after incrementing the use-level to account for the brackets, the cases are: bind > use Error @@ -1113,7 +1111,7 @@ Examples: \x. f 'x -- Not ok (bind = 1, use = 1) -- (whereas \x. f [| x |] might have been ok, by - -- cross-stage lifting + -- cross-level lifting \y. [| \x. $(f 'y) |] -- Not ok (bind =1, use = 1) ===================================== compiler/GHC/Rename/Utils.hs ===================================== @@ -106,7 +106,7 @@ newLocalBndrsRn = mapM newLocalBndrRn bindLocalNames :: [Name] -> RnM a -> RnM a bindLocalNames names = updLclCtxt $ \ lcl_env -> - let th_level = thLevel (tcl_th_ctxt lcl_env) + let th_level = thLevelIndex (tcl_th_ctxt lcl_env) th_bndrs' = extendNameEnvList (tcl_th_bndrs lcl_env) [ (n, (NotTopLevel, th_level)) | n <- names ] rdr_env' = extendLocalRdrEnvList (tcl_rdr lcl_env) names ===================================== compiler/GHC/Tc/Errors/Ppr.hs ===================================== @@ -1510,17 +1510,17 @@ instance Diagnostic TcRnMessage where TcRnBadlyLevelled reason bind_lvl use_lvl -> mkSimpleDecorated $ vcat $ - [ text "Level error:" <+> pprStageCheckReason reason <+> + [ text "Level error:" <+> pprLevelCheckReason reason <+> hsep [text "is bound at level" <+> ppr bind_lvl, text "but used at level" <+> ppr use_lvl] ] ++ [ hsep [ text "Hint: quoting" <+> thBrackets (ppUnless (isValName n) "t") (ppr n) , text "or an enclosing expression would allow the quotation to be used at an earlier level" ] - | StageCheckSplice n _ <- [reason] + | LevelCheckSplice n _ <- [reason] ] ++ [ "From imports" <+> (ppr (gre_imp gre)) - | StageCheckSplice _ (Just gre) <- [reason] + | LevelCheckSplice _ (Just gre) <- [reason] , not (isEmptyBag (gre_imp gre)) ] TcRnBadlyLevelledType name bind_lvl use_lvl -> mkSimpleDecorated $ @@ -5770,11 +5770,11 @@ pprWrongThingSort = WrongThingTyCon -> "type constructor" WrongThingAxiom -> "axiom" -pprStageCheckReason :: StageCheckReason -> SDoc -pprStageCheckReason = \case - StageCheckInstance _ t -> +pprLevelCheckReason :: LevelCheckReason -> SDoc +pprLevelCheckReason = \case + LevelCheckInstance _ t -> text "instance for" <+> quotes (ppr t) - StageCheckSplice t _ -> + LevelCheckSplice t _ -> quotes (ppr t) pprUninferrableTyVarCtx :: UninferrableTyVarCtx -> SDoc @@ -6877,10 +6877,6 @@ pprTHNameError = \case mkSimpleDecorated $ hang (text "The binder" <+> quotes (ppr name) <+> text "is not a NameU.") 2 (text "Probable cause: you used mkName instead of newName to generate a binding.") - QuotedNameWrongStage quote -> - mkSimpleDecorated $ - sep [ text "Stage error: the non-top-level quoted name" <+> ppr quote - , text "must be used at the same stage at which it is bound." ] pprTHReifyError :: THReifyError -> DecoratedSDoc pprTHReifyError = \case @@ -7002,7 +6998,6 @@ thSyntaxErrorReason = \case thNameErrorReason :: THNameError -> DiagnosticReason thNameErrorReason = \case NonExactName {} -> ErrorWithoutFlag - QuotedNameWrongStage {} -> ErrorWithoutFlag thReifyErrorReason :: THReifyError -> DiagnosticReason thReifyErrorReason = \case @@ -7063,7 +7058,6 @@ thSyntaxErrorHints = \case thNameErrorHints :: THNameError -> [GhcHint] thNameErrorHints = \case NonExactName {} -> noHints - QuotedNameWrongStage {} -> noHints thReifyErrorHints :: THReifyError -> [GhcHint] thReifyErrorHints = \case ===================================== compiler/GHC/Tc/Errors/Types.hs ===================================== @@ -99,7 +99,7 @@ module GHC.Tc.Errors.Types ( , RuleLhsErrReason(..) , HsigShapeMismatchReason(..) , WrongThingSort(..) - , StageCheckReason(..) + , LevelCheckReason(..) , UninferrableTyVarCtx(..) , PatSynInvalidRhsReason(..) , BadFieldAnnotationReason(..) @@ -3487,19 +3487,19 @@ data TcRnMessage where -> !LookupInstanceErrReason -> TcRnMessage - {-| TcRnBadlyStaged is an error that occurs when a TH binding is used at an + {-| TcRnBadlyLevelled is an error that occurs when a TH binding is used at an invalid level. Test cases: T17820d, T17820, T21547, T5795, qq00[1-4], annfail0{3,4,6,9} -} TcRnBadlyLevelled - :: !StageCheckReason -- ^ The binding - -> !(Set.Set Int) -- ^ The binding levels - -> !Int -- ^ The level at which the binding is used. + :: !LevelCheckReason -- ^ The binding + -> !(Set.Set ThLevelIndex) -- ^ The binding levels + -> !ThLevelIndex -- ^ The level at which the binding is used. -> TcRnMessage - {-| TcRnBadlyStagedWarn is a warning that occurs when a TH type binding is + {-| TcRnBadlyLevelledWarn is a warning that occurs when a TH type binding is used in an invalid stage. Controlled by flags: @@ -3510,8 +3510,8 @@ data TcRnMessage where -} TcRnBadlyLevelledType :: !Name -- ^ The type binding being spliced. - -> !(Set.Set Int) -- ^ The binding stage. - -> !Int -- ^ The stage at which the binding is used. + -> !(Set.Set ThLevelIndex) -- ^ The binding stage. + -> !ThLevelIndex -- ^ The stage at which the binding is used. -> TcRnMessage {-| TcRnTyThingUsedWrong is an error that occurs when a thing is used where another @@ -6245,9 +6245,9 @@ data WrongThingSort | WrongThingTyCon | WrongThingAxiom -data StageCheckReason - = StageCheckInstance !InstanceWhat !PredType - | StageCheckSplice !Name !(Maybe GlobalRdrElt) +data LevelCheckReason + = LevelCheckInstance !InstanceWhat !PredType + | LevelCheckSplice !Name !(Maybe GlobalRdrElt) data UninferrableTyVarCtx = UninfTyCtx_ClassContext [TcType] @@ -6745,13 +6745,6 @@ data THNameError -} = NonExactName !RdrName - {-| QuotedNameWrongStage is an error that can happen when a - (non-top-level) Name is used at a different Template Haskell stage - than the stage at which it is bound. - - Test cases: T16976z - -} - | QuotedNameWrongStage !(HsQuote GhcPs) deriving Generic data THReifyError ===================================== compiler/GHC/Tc/Gen/App.hs ===================================== @@ -1217,13 +1217,13 @@ tc_inst_forall_arg conc_tvs (tvb, inner_ty) hs_ty -- -- See [Wrinkle: VTA] in Note [Representation-polymorphism checking built-ins] -- in GHC.Tc.Utils.Concrete. - ; th_stage <- getStage + ; th_lvl <- getThLevel ; ty_arg <- case mb_conc of Nothing -> return ty_arg0 Just conc -- See [Wrinkle: Typed Template Haskell] -- in Note [hasFixedRuntimeRep] in GHC.Tc.Utils.Concrete. - | Brack _ (TcPending {}) <- th_stage + | Brack _ (TcPending {}) <- th_lvl -> return ty_arg0 | otherwise -> ===================================== compiler/GHC/Tc/Gen/Head.hs ===================================== @@ -1075,28 +1075,28 @@ Wrinkles -- checkThLocalName. checkThLocalId :: Id -> TcM () checkThLocalId id - = do { mb_local_use <- getStageAndBindLevel (idName id) + = do { mb_local_use <- getCurrentAndBindLevel (idName id) ; case mb_local_use of - Just (top_lvl, bind_lvl, use_stage) - | thLevel use_stage `Set.notMember` bind_lvl + Just (top_lvl, bind_lvl, use_lvl) + | thLevelIndex use_lvl `Set.notMember` bind_lvl -> do dflags <- getDynFlags - checkCrossStageLifting dflags top_lvl id use_stage + checkCrossLevelLifting dflags top_lvl id use_lvl _ -> return () -- Not a locally-bound thing, or -- no cross-stage link } -------------------------------------- -checkCrossStageLifting :: DynFlags -> TopLevelFlag -> Id -> ThStage -> TcM () +checkCrossLevelLifting :: DynFlags -> TopLevelFlag -> Id -> ThLevel -> TcM () -- If we are inside typed brackets, and (use_lvl > bind_lvl) -- we must check whether there's a cross-stage lift to do -- Examples \x -> [|| x ||] -- [|| map ||] -- --- This is similar to checkCrossStageLifting in GHC.Rename.Splice, but +-- This is similar to checkCrossLevelLifting in GHC.Rename.Splice, but -- this code is applied to *typed* brackets. -checkCrossStageLifting dflags top_lvl id (Brack _ (TcPending ps_var lie_var q)) +checkCrossLevelLifting dflags top_lvl id (Brack _ (TcPending ps_var lie_var q)) | isTopLevel top_lvl , xopt LangExt.ImplicitStagePersistence dflags = when (isExternalName id_name) (keepAlive id_name) @@ -1146,7 +1146,7 @@ checkCrossStageLifting dflags top_lvl id (Brack _ (TcPending ps_var lie_var q)) where id_name = idName id -checkCrossStageLifting _ _ _ _ = return () +checkCrossLevelLifting _ _ _ _ = return () {- Note [Lifting strings] @@ -1181,7 +1181,7 @@ them at level 2 or 0. The level which a name is availble at is stored in the 'GRE', in the normal GlobalRdrEnv. The function `greLevels` returns the levels which a specific GRE is imported at. The level information for a 'Name' is computed by `getStageAndBindLevel`. -The level validity is checked by `checkCrossStageLifting`. +The level validity is checked by `checkCrossLevelLifting`. Instances are checked by `checkWellStagedDFun`, which computes the level an 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 That's it for the main implementation of the feature, and the rest is modifications to the driver parts of the code to use this information. For example, in downsweep, we only enable code generation for modules needed at the runtime stage. -See Note [ExplicitLevelImports and -fno-code]. +See Note [-fno-code mode]. -} ===================================== compiler/GHC/Tc/Gen/Splice.hs ===================================== @@ -173,8 +173,8 @@ import GHC.Rename.Doc (rnHsDoc) {- Note [Template Haskell state diagram] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Here are the ThStages, s, their corresponding level numbers -(the result of (thLevel s)), and their state transitions. +Here are the ThLevels, their corresponding level numbers +(the result of (thLevelIndex s)), and their state transitions. The top level of the program is stage Comp: Start here @@ -447,7 +447,7 @@ without having to walk over the untyped bracket code. Our example RENAMER (rnUntypedBracket): -* Set the ThStage to (Brack s (RnPendingUntyped ps_var)) +* Set the ThLevel to (Brack s (RnPendingUntyped ps_var)) * Rename the body @@ -557,7 +557,7 @@ RENAMER (rnTypedSplice): the renamer adds a SplicePointName, spn: TYPECHECKER (tcTypedBracket): -* Set the ThStage to (Brack s (TcPending ps_var lie_var)) +* Set the ThLevel to (Brack s (TcPending ps_var lie_var)) * Typecheck the body, and keep the elaborated result (despite never using it!) @@ -669,7 +669,7 @@ runAnnotation :: CoreAnnTarget -> LHsExpr GhcRn -> TcM Annotation -- See Note [How brackets and nested splices are handled] tcTypedBracket rn_expr expr res_ty = addErrCtxt (TypedTHBracketCtxt expr) $ - do { cur_stage <- getStage + do { cur_lvl <- getThLevel ; ps_ref <- newMutVar [] ; lie_var <- getConstraintVar -- Any constraints arising from nested splices -- should get thrown into the constraint set @@ -686,7 +686,7 @@ tcTypedBracket rn_expr expr res_ty -- The typechecked expression won't be used, so we just discard it -- (See Note [The life cycle of a TH quotation] in GHC.Hs.Expr) -- We'll typecheck it again when we splice it in somewhere - ; (tc_expr, expr_ty) <- setStage (Brack cur_stage (TcPending ps_ref lie_var wrapper)) $ + ; (tc_expr, expr_ty) <- setThLevel (Brack cur_lvl (TcPending ps_ref lie_var wrapper)) $ tcScalingUsage ManyTy $ -- Scale by Many, TH lifting is currently nonlinear (#18465) tcInferRhoNC expr @@ -835,8 +835,8 @@ getUntypedSpliceBody (HsUntypedSpliceNested {}) tcTypedSplice splice_name expr res_ty = addErrCtxt (TypedSpliceCtxt (Just splice_name) expr) $ setSrcSpan (getLocA expr) $ do - { stage <- getStage - ; case stage of + { lvl <- getThLevel + ; case lvl of Splice {} -> tcTopSplice expr res_ty Brack pop_stage pend -> tcNestedSplice pop_stage pend splice_name expr res_ty RunSplice _ -> @@ -889,7 +889,7 @@ tcTopSpliceExpr isTypedSplice tc_action = checkNoErrs $ -- checkNoErrs: must not try to run the thing -- if the type checker fails! - setStage (Splice isTypedSplice Comp) $ + setThLevel (Splice isTypedSplice Comp) $ do { -- Typecheck the expression (mb_expr', wanted) <- tryCaptureConstraints tc_action -- If tc_action fails (perhaps because of insoluble constraints) @@ -904,7 +904,7 @@ tcTopSpliceExpr isTypedSplice tc_action Just expr' -> return $ mkHsDictLet (EvBinds const_binds) expr' } ------------------ -tcNestedSplice :: ThStage -> PendingStuff -> Name +tcNestedSplice :: ThLevel -> PendingStuff -> Name -> LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc) -- See Note [How brackets and nested splices are handled] -- A splice inside brackets @@ -912,7 +912,7 @@ tcNestedSplice pop_stage (TcPending ps_var lie_var q@(QuoteWrapper _ m_var)) spl = do { res_ty <- expTypeToType res_ty ; let rep = getRuntimeRep res_ty ; meta_exp_ty <- tcTExpTy m_var res_ty - ; expr' <- setStage pop_stage $ + ; expr' <- setThLevel pop_stage $ setConstraintVar lie_var $ tcCheckMonoExpr expr meta_exp_ty ; untype_code <- tcLookupId unTypeCodeName @@ -940,7 +940,7 @@ runTopSplice (DelayedSplice lcl_env orig_expr res_ty q_expr) -- See Note [Collecting modFinalizers in typed splices]. ; modfinalizers_ref <- newTcRef [] -- Run the expression - ; expr2 <- setStage (RunSplice modfinalizers_ref) $ + ; expr2 <- setThLevel (RunSplice modfinalizers_ref) $ runMetaE zonked_q_expr ; mod_finalizers <- readTcRef modfinalizers_ref ; addModFinalizersWithLclEnv $ ThModFinalizers mod_finalizers @@ -1651,15 +1651,15 @@ lookupThInstName th_type = do -- | Adds a mod finalizer reference to the local environment. addModFinalizerRef :: ForeignRef (TH.Q ()) -> TcM () addModFinalizerRef finRef = do - th_stage <- getStage - case th_stage of + th_lvl <- getThLevel + case th_lvl of RunSplice th_modfinalizers_var -> updTcRef th_modfinalizers_var (finRef :) -- This case happens only if a splice is executed and the caller does - -- not set the 'ThStage' to 'RunSplice' to collect finalizers. + -- not set the 'ThLevel' to 'RunSplice' to collect finalizers. -- See Note [Delaying modFinalizers in untyped splices] in GHC.Rename.Splice. _ -> pprPanic "addModFinalizer was called when no finalizers were collected" - (ppr th_stage) + (ppr th_lvl) -- | Releases the external interpreter state. finishTH :: TcM () ===================================== compiler/GHC/Tc/Solver/Dict.hs ===================================== @@ -904,7 +904,7 @@ checkInstanceOK :: CtLoc -> InstanceWhat -> TcPredType -> TcS CtLoc -- Returns the CtLoc to used for sub-goals -- Probably also want to call checkReductionDepth checkInstanceOK loc what pred - = do { checkWellStagedDFun loc what pred + = do { checkWellLevelledDFun loc what pred ; return deeper_loc } where deeper_loc = zap_origin (bumpCtLocDepth loc) ===================================== compiler/GHC/Tc/Solver/Monad.hs ===================================== @@ -125,7 +125,7 @@ module GHC.Tc.Solver.Monad ( -- Misc getDefaultInfo, getDynFlags, getGlobalRdrEnvTcS, matchFam, matchFamTcM, - checkWellStagedDFun, + checkWellLevelledDFun, pprEq, -- Enforcing invariants for type equalities @@ -1598,48 +1598,46 @@ recordUsedGREs gres -- Various smaller utilities [TODO, maybe will be absorbed in the instance matcher] -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -checkWellStagedDFun :: CtLoc -> InstanceWhat -> PredType -> TcS () +checkWellLevelledDFun :: CtLoc -> InstanceWhat -> PredType -> TcS () -- Check that we do not try to use an instance before it is available. E.g. -- instance Eq T where ... -- f x = $( ... (\(p::T) -> p == p)... ) -- Here we can't use the equality function from the instance in the splice -checkWellStagedDFun loc what pred +checkWellLevelledDFun loc what pred = do - mbind_lvl <- checkWellStagedInstanceWhat what - --env <- getLclEnv - --use_lvl <- thLevel <$> (wrapTcS $ TcM.getStage) + mbind_lvl <- checkWellLevelledInstanceWhat what case mbind_lvl of Just (bind_lvl, is_local) -> wrapTcS $ TcM.setCtLocM loc $ do - { use_stage <- TcM.getStage + { use_lvl <- thLevelIndex <$> TcM.getThLevel ; dflags <- getDynFlags - ; checkCrossStageClass dflags (StageCheckInstance what pred) bind_lvl (thLevel use_stage) is_local } + ; checkCrossLevelClass dflags (LevelCheckInstance what pred) bind_lvl use_lvl is_local } -- If no level information is returned for an InstanceWhat, then it's safe to use -- at any level. Nothing -> return () --- TODO: Unify this with checkCrossStageLifting function -checkCrossStageClass :: DynFlags -> StageCheckReason -> Set.Set ThLevel -> ThLevel +-- TODO: Unify this with checkCrossLevelLifting function +checkCrossLevelClass :: DynFlags -> LevelCheckReason -> Set.Set ThLevelIndex -> ThLevelIndex -> Bool -> TcM () -checkCrossStageClass dflags reason bind_lvl use_lvl is_local - -- If the Id is imported, ie global, then allow with PathCrossStagedPersist +checkCrossLevelClass dflags reason bind_lvl use_lvl_idx is_local + -- If the Id is imported, ie global, then allow with ImplicitStagePersistence | not is_local , xopt LangExt.ImplicitStagePersistence dflags = return () - | use_lvl `Set.member` bind_lvl = return () + | use_lvl_idx `Set.member` bind_lvl = return () -- With path CSP, using later than bound is fine | xopt LangExt.ImplicitStagePersistence dflags - , any (use_lvl >=) bind_lvl = return () - | otherwise = TcM.failWithTc (TcRnBadlyLevelled reason bind_lvl use_lvl) + , any (use_lvl_idx >=) bind_lvl = return () + | otherwise = TcM.failWithTc (TcRnBadlyLevelled reason bind_lvl use_lvl_idx) -- | Returns the ThLevel of evidence for the solved constraint (if it has evidence) --- See Note [Well-staged instance evidence] -checkWellStagedInstanceWhat :: InstanceWhat -> TcS (Maybe (Set.Set ThLevel, Bool)) -checkWellStagedInstanceWhat what +-- See Note [Well-levelled instance evidence] +checkWellLevelledInstanceWhat :: InstanceWhat -> TcS (Maybe (Set.Set ThLevelIndex, Bool)) +checkWellLevelledInstanceWhat what | TopLevInstance { iw_dfun_id = dfun_id } <- what = do -- MP: I am not sure if we have to only do this check for orphan instances. @@ -1658,25 +1656,25 @@ checkWellStagedInstanceWhat what instance_key = if moduleUnitId name_module `Set.member` hsc_all_home_unit_ids hsc_env then (mkKey NormalLevel name_module) else Right (moduleUnitId name_module) - let lvls = [ -1 | splice_lvl instance_key] - ++ [ 0 | normal_lvl instance_key] - ++ [ 1 | quote_lvl instance_key] + let lvls = [ spliceLevelIndex | splice_lvl instance_key] + ++ [ topLevelIndex | normal_lvl instance_key] + ++ [ quoteLevelIndex | quote_lvl instance_key] if isLocalId dfun_id - then return $ Just ( (Set.singleton topLevel, True) ) + then return $ Just ( (Set.singleton topLevelIndex, True) ) else return $ Just ( Set.fromList lvls, False ) | BuiltinTypeableInstance tc <- what = do cur_mod <- extractModule <$> getGblEnv return $ Just (if nameIsLocalOrFrom cur_mod (tyConName tc) - then (Set.singleton topLevel, True) + then (Set.singleton topLevelIndex, True) -- TODO, not correct, needs similar checks to normal instances - else (Set.fromList [(-1), topLevel], False)) + else (Set.fromList [spliceLevelIndex, topLevelIndex], False)) | otherwise = return Nothing {- -Note [Well-staged instance evidence] -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Note [Well-levelled instance evidence] +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Evidence for instances must obey the same level restrictions as normal bindings. 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: Solving a `Typeable (T t1 ...tn)` constraint generates code that relies on `$tcT`, the `TypeRep` for `T`; and we must check that this reference to `$tcT` -is well levelled. It's easy to know the stage of `$tcT`: for imported TyCons it +is well levelled. It's easy to know the level of `$tcT`: for imported TyCons it will be the level of the imported TyCon Name, and for local TyCons it will be `toplevel`. Therefore the `InstanceWhat` type had to be extended with a special case for `Typeable`, which recorded the TyCon the evidence was for and -could them be used to check that we were not attempting to evidence in a stage incorrect +could them be used to check that we were not attempting to evidence in a level incorrect manner. -} ===================================== compiler/GHC/Tc/TyCl/Instance.hs ===================================== @@ -418,8 +418,8 @@ tcInstDeclsDeriv -> [LDerivDecl GhcRn] -> TcM (TcGblEnv, [InstInfo GhcRn], HsValBinds GhcRn) tcInstDeclsDeriv deriv_infos derivds - = do th_stage <- getStage -- See Note [Deriving inside TH brackets] - if isBrackStage th_stage + = do th_lvl <- getThLevel -- See Note [Deriving inside TH brackets] + if isBrackLevel th_lvl then do { gbl_env <- getGblEnv ; return (gbl_env, bagToList emptyBag, emptyValBindsOut) } else do { (tcg_env, info_bag, valbinds) <- tcDeriving deriv_infos derivds ===================================== compiler/GHC/Tc/Types.hs ===================================== @@ -59,9 +59,15 @@ module GHC.Tc.Types( CompleteMatch, CompleteMatches, -- Template Haskell - ThStage(..), SpliceType(..), SpliceOrBracket(..), PendingStuff(..), - topStage, topAnnStage, topSpliceStage, - ThLevel, topLevel,thLevel, + ThLevel(..), SpliceType(..), SpliceOrBracket(..), PendingStuff(..), + topLevel, topAnnLevel, topSpliceLevel, + ThLevelIndex, + topLevelIndex, + spliceLevelIndex, + quoteLevelIndex, + + thLevelIndex, + ForeignSrcLang(..), THDocs, DocLoc(..), ThBindEnv, ===================================== compiler/GHC/Tc/Types/LclEnv.hs ===================================== @@ -11,13 +11,13 @@ module GHC.Tc.Types.LclEnv ( , getLclEnvLoc , getLclEnvRdrEnv , getLclEnvTcLevel - , getLclEnvThStage + , getLclEnvThLevel , setLclEnvTcLevel , setLclEnvLoc , setLclEnvRdrEnv , setLclEnvBinderStack , setLclEnvErrCtxt - , setLclEnvThStage + , setLclEnvThLevel , setLclEnvTypeEnv , modifyLclEnvTcLevel @@ -108,7 +108,7 @@ data TcLclCtxt -- we can look up record field names - tcl_th_ctxt :: ThStage, -- Template Haskell context + tcl_th_ctxt :: ThLevel, -- Template Haskell context tcl_th_bndrs :: ThBindEnv, -- and binder info -- The ThBindEnv records the TH binding level of in-scope Names -- defined in this module (not imported) @@ -121,11 +121,11 @@ data TcLclCtxt -- Ids and TyVars defined in this module } -getLclEnvThStage :: TcLclEnv -> ThStage -getLclEnvThStage = tcl_th_ctxt . tcl_lcl_ctxt +getLclEnvThLevel :: TcLclEnv -> ThLevel +getLclEnvThLevel = tcl_th_ctxt . tcl_lcl_ctxt -setLclEnvThStage :: ThStage -> TcLclEnv -> TcLclEnv -setLclEnvThStage s = modifyLclCtxt (\env -> env { tcl_th_ctxt = s }) +setLclEnvThLevel :: ThLevel -> TcLclEnv -> TcLclEnv +setLclEnvThLevel l = modifyLclCtxt (\env -> env { tcl_th_ctxt = l }) getLclEnvThBndrs :: TcLclEnv -> ThBindEnv getLclEnvThBndrs = tcl_th_bndrs . tcl_lcl_ctxt @@ -187,7 +187,7 @@ modifyLclCtxt upd env = type TcTypeEnv = NameEnv TcTyThing -type ThBindEnv = NameEnv (TopLevelFlag, ThLevel) +type ThBindEnv = NameEnv (TopLevelFlag, ThLevelIndex) -- Domain = all Ids bound in this module (ie not imported) -- The TopLevelFlag tells if the binding is syntactically top level. -- 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? -- See GHC.Tc.Solver.InertSet Note [Solved dictionaries] | BuiltinTypeableInstance TyCon -- Built-in solver for Typeable (T t1 .. tn) - -- See Note [Well-staged instance evidence] + -- See Note [Well-levelled instance evidence] | BuiltinInstance -- Built-in solver for (C t1 .. tn) where C is -- KnownNat, .. etc (classes with no top-level evidence) ===================================== compiler/GHC/Tc/Types/TH.hs ===================================== @@ -1,14 +1,16 @@ module GHC.Tc.Types.TH ( SpliceType(..) , SpliceOrBracket(..) - , ThStage(..) + , ThLevel(..) , PendingStuff(..) - , ThLevel + , ThLevelIndex , topLevel - , topStage - , topAnnStage - , topSpliceStage - , thLevel + , topAnnLevel + , topSpliceLevel + , thLevelIndex + , topLevelIndex + , spliceLevelIndex + , quoteLevelIndex ) where import GHCi.RemoteTypes @@ -27,12 +29,12 @@ import GHC.Hs.Expr ( PendingTcSplice, PendingRnSplice ) data SpliceType = Typed | Untyped data SpliceOrBracket = IsSplice | IsBracket -data ThStage -- See Note [Template Haskell state diagram] +data ThLevel -- See Note [Template Haskell state diagram] -- and Note [Template Haskell levels] in GHC.Tc.Gen.Splice -- Start at: Comp -- At bracket: wrap current stage in Brack -- At splice: wrap current stage in Splice - = Splice SpliceType ThStage -- Inside a splice + = Splice SpliceType ThLevel -- Inside a splice | RunSplice (TcRef [ForeignRef (TH.Q ())]) -- 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] -- Binding level = 0 | Brack -- Inside brackets - ThStage -- Enclosing stage + ThLevel -- Enclosing level PendingStuff data PendingStuff @@ -73,35 +75,51 @@ data PendingStuff -- `lift`. -topStage, topAnnStage, topSpliceStage :: ThStage -topStage = Comp -topAnnStage = Splice Untyped Comp -topSpliceStage = Splice Untyped Comp +topLevel, topAnnLevel, topSpliceLevel :: ThLevel +topLevel = Comp +topAnnLevel = Splice Untyped Comp +topSpliceLevel = Splice Untyped Comp -instance Outputable ThStage where +instance Outputable ThLevel where ppr (Splice _ s) = text "Splice" <> parens (ppr s) ppr (RunSplice _) = text "RunSplice" ppr Comp = text "Comp" ppr (Brack s _) = text "Brack" <> parens (ppr s) -type ThLevel = Int +-- | The integer which represents the level +newtype ThLevelIndex = ThLevelIndex Int deriving (Eq, Ord) -- NB: see Note [Template Haskell levels] in GHC.Tc.Gen.Splice -- Incremented when going inside a bracket, -- decremented when going inside a splice -topLevel :: ThLevel -topLevel = thLevel Comp +instance Outputable ThLevelIndex where + ppr (ThLevelIndex i) = int i -thLevel :: ThStage -> ThLevel -thLevel (Splice _ s) = thLevel s - 1 -thLevel Comp = 0 -thLevel (Brack s _) = thLevel s + 1 -thLevel (RunSplice _) = thLevel (Splice Untyped Comp) -- previously: panic "thLevel: called when running a splice" +incThLevelIndex :: ThLevelIndex -> ThLevelIndex +incThLevelIndex (ThLevelIndex i) = ThLevelIndex (i + 1) + +decThLevelIndex :: ThLevelIndex -> ThLevelIndex +decThLevelIndex (ThLevelIndex i) = ThLevelIndex (i - 1) + +topLevelIndex :: ThLevelIndex +topLevelIndex = thLevelIndex Comp + +spliceLevelIndex :: ThLevelIndex +spliceLevelIndex = thLevelIndex (Splice Untyped Comp) + +quoteLevelIndex :: ThLevelIndex +quoteLevelIndex = thLevelIndex (Brack Comp RnPendingTyped) + +thLevelIndex :: ThLevel -> ThLevelIndex +thLevelIndex (Splice _ s) = decThLevelIndex (thLevelIndex s) +thLevelIndex Comp = ThLevelIndex 0 +thLevelIndex (Brack s _) = incThLevelIndex (thLevelIndex s) +thLevelIndex (RunSplice _) = thLevelIndex (Splice Untyped Comp) -- previously: panic "thLevel: called when running a splice" -- See Note [RunSplice ThLevel]. {- Note [RunSplice ThLevel] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -The 'RunSplice' stage is set when executing a splice, and only when running a +The 'RunSplice' level is set when executing a splice, and only when running a splice. In particular it is not set when the splice is renamed or typechecked. 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 -- ^ Returns @(co, frr_ty)@ with @co :: ty ~# frr_ty@ -- and @frr_@ty has a fixed 'RuntimeRep'. checkFRR_with check_kind frr_ctxt ty - = do { th_stage <- getStage + = do { th_lvl <- getThLevel ; if -- Shortcut: check for 'Type' and 'UnliftedType' type synonyms. | TyConApp tc [] <- ki @@ -678,7 +678,7 @@ checkFRR_with check_kind frr_ctxt ty -> return refl -- See [Wrinkle: Typed Template Haskell] in Note [hasFixedRuntimeRep]. - | Brack _ (TcPending {}) <- th_stage + | Brack _ (TcPending {}) <- th_lvl -> return refl -- 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( tcGetDefaultTys, -- Template Haskell stuff - StageCheckReason(..), - tcMetaTy, thLevel, - isBrackStage, + LevelCheckReason(..), + tcMetaTy, thLevelIndex, + isBrackLevel, -- New Ids newDFunName, @@ -521,9 +521,8 @@ tcExtendTyConEnv tycons thing_inside -- See GHC ticket #17820 . tcTyThBinders :: [TyThing] -> TcM ThBindEnv tcTyThBinders implicit_things = do - stage <- getStage - let th_lvl = thLevel stage - th_bndrs = mkNameEnv + th_lvl <- thLevelIndex <$> getThLevel + let th_bndrs = mkNameEnv [ ( n , (TopLevel, th_lvl) ) | n <- names ] return th_bndrs where @@ -759,7 +758,7 @@ tc_extend_local_env top_lvl extra_env thing_inside = do { traceTc "tc_extend_local_env" (ppr extra_env) ; updLclCtxt upd_lcl_env thing_inside } where - upd_lcl_env env0@(TcLclCtxt { tcl_th_ctxt = stage + upd_lcl_env env0@(TcLclCtxt { tcl_th_ctxt = th_lvl , tcl_rdr = rdr_env , tcl_th_bndrs = th_bndrs , tcl_env = lcl_type_env }) @@ -776,7 +775,7 @@ tc_extend_local_env top_lvl extra_env thing_inside -- Template Haskell staging env simultaneously. Reason for extending -- LocalRdrEnv: after running a TH splice we need to do renaming. where - thlvl = (top_lvl, thLevel stage) + thlvl = (top_lvl, thLevelIndex th_lvl) tcExtendLocalTypeEnv :: [(Name, TcTyThing)] -> TcLclCtxt -> TcLclCtxt @@ -931,9 +930,9 @@ tcMetaTy tc_name = do t <- tcLookupTyCon tc_name return (mkTyConTy t) -isBrackStage :: ThStage -> Bool -isBrackStage (Brack {}) = True -isBrackStage _other = False +isBrackLevel :: ThLevel -> Bool +isBrackLevel (Brack {}) = True +isBrackLevel _other = False {- ************************************************************************ ===================================== compiler/GHC/Tc/Utils/Monad.hs ===================================== @@ -117,7 +117,7 @@ module GHC.Tc.Utils.Monad( -- * Template Haskell context recordThUse, recordThNeededRuntimeDeps, - keepAlive, getStage, getStageAndBindLevel, setStage, + keepAlive, getThLevel, getCurrentAndBindLevel, setThLevel, addModFinalizersWithLclEnv, -- * Safe Haskell context @@ -399,7 +399,7 @@ initTcWithGbl hsc_env gbl_env loc do_this tcl_in_gen_code = False, tcl_ctxt = [], tcl_rdr = emptyLocalRdrEnv, - tcl_th_ctxt = topStage, + tcl_th_ctxt = topLevel, tcl_th_bndrs = emptyNameEnv, tcl_arrow_ctxt = NoArrowCtxt, tcl_env = emptyNameEnv, @@ -2110,11 +2110,11 @@ keepAlive name ; traceRn "keep alive" (ppr name) ; updTcRef (tcg_keep env) (`extendNameSet` name) } -getStage :: TcM ThStage -getStage = do { env <- getLclEnv; return (getLclEnvThStage env) } +getThLevel :: TcM ThLevel +getThLevel = do { env <- getLclEnv; return (getLclEnvThLevel env) } -getStageAndBindLevel :: Name -> TcRn (Maybe (TopLevelFlag, Set.Set ThLevel, ThStage)) -getStageAndBindLevel name +getCurrentAndBindLevel :: Name -> TcRn (Maybe (TopLevelFlag, Set.Set ThLevelIndex, ThLevel)) +getCurrentAndBindLevel name = do { env <- getLclEnv; ; case lookupNameEnv (getLclEnvThBndrs env) name of Nothing -> do @@ -2128,10 +2128,10 @@ getStageAndBindLevel name --env <- getGlobalRdrEnv --pprTrace "NO_LVLS" (ppr name) (return Nothing) return Nothing - else return (Just (TopLevel, lvls, getLclEnvThStage env)) - Just (top_lvl, bind_lvl) -> return (Just (top_lvl, Set.singleton bind_lvl, getLclEnvThStage env)) } + else return (Just (TopLevel, lvls, getLclEnvThLevel env)) + Just (top_lvl, bind_lvl) -> return (Just (top_lvl, Set.singleton bind_lvl, getLclEnvThLevel env)) } -getExternalBindLvl :: Name -> TcRn (Set.Set ThLevel) +getExternalBindLvl :: Name -> TcRn (Set.Set ThLevelIndex) getExternalBindLvl name = do env <- getGlobalRdrEnv mod <- getModule @@ -2139,16 +2139,16 @@ getExternalBindLvl name = do Just gre -> return $ (Set.map convert_lvl (greLevels gre)) Nothing -> if nameIsLocalOrFrom mod name - then return $ Set.singleton topLevel + then return $ Set.singleton topLevelIndex -- else pprTrace "NO LVLS" (ppr name) (return Set.empty) -- pprPanic "getExternalBindLvl" (ppr env $$ ppr name $$ ppr (nameSrcSpan name)) else return Set.empty where - convert_lvl NormalLevel = thLevel topStage - convert_lvl SpliceLevel = thLevel topSpliceStage - convert_lvl QuoteLevel = thLevel (Brack topStage undefined) + convert_lvl NormalLevel = topLevelIndex + convert_lvl SpliceLevel = spliceLevelIndex + convert_lvl QuoteLevel = quoteLevelIndex -setStage :: ThStage -> TcM a -> TcRn a -setStage s = updLclEnv (setLclEnvThStage s) +setThLevel :: ThLevel -> TcM a -> TcRn a +setThLevel l = updLclEnv (setLclEnvThLevel l) -- | Adds the given modFinalizers to the global environment and set them to use -- the current local environment. ===================================== compiler/GHC/Tc/Utils/TcMType.hs ===================================== @@ -458,11 +458,11 @@ newInferExpType = new_inferExpType Nothing newInferExpTypeFRR :: FixedRuntimeRepContext -> TcM ExpTypeFRR newInferExpTypeFRR frr_orig - = do { th_stage <- getStage + = do { th_lvl <- getThLevel ; if -- See [Wrinkle: Typed Template Haskell] -- in Note [hasFixedRuntimeRep] in GHC.Tc.Utils.Concrete. - | Brack _ (TcPending {}) <- th_stage + | Brack _ (TcPending {}) <- th_lvl -> new_inferExpType Nothing | otherwise @@ -800,11 +800,11 @@ newConcreteTyVar :: HasDebugCallStack => ConcreteTvOrigin -> FastString -> TcKind -> TcM TcTyVar newConcreteTyVar reason fs kind = assertPpr (isConcreteType kind) assert_msg $ - do { th_stage <- getStage + do { th_lvl <- getThLevel ; if -- See [Wrinkle: Typed Template Haskell] -- in Note [hasFixedRuntimeRep] in GHC.Tc.Utils.Concrete. - | Brack _ (TcPending {}) <- th_stage + | Brack _ (TcPending {}) <- th_lvl -> newNamedAnonMetaTyVar fs TauTv kind | otherwise @@ -986,8 +986,8 @@ newOpenFlexiTyVar -- in GHC.Tc.Utils.Concrete. newOpenFlexiFRRTyVar :: FixedRuntimeRepContext -> TcM TcTyVar newOpenFlexiFRRTyVar frr_ctxt - = do { th_stage <- getStage - ; case th_stage of + = do { th_lvl <- getThLevel + ; case th_lvl of { Brack _ (TcPending {}) -- See [Wrinkle: Typed Template Haskell] -> newOpenFlexiTyVar -- in Note [hasFixedRuntimeRep] in GHC.Tc.Utils.Concrete. ; _ -> @@ -1040,11 +1040,11 @@ newMetaTyVarX = new_meta_tv_x TauTv -- | Like 'newMetaTyVarX', but for concrete type variables. newConcreteTyVarX :: ConcreteTvOrigin -> Subst -> TyVar -> TcM (Subst, TcTyVar) newConcreteTyVarX conc subst tv - = do { th_stage <- getStage + = do { th_lvl <- getThLevel ; if -- See [Wrinkle: Typed Template Haskell] -- in Note [hasFixedRuntimeRep] in GHC.Tc.Utils.Concrete. - | Brack _ (TcPending {}) <- th_stage + | Brack _ (TcPending {}) <- th_lvl -> new_meta_tv_x TauTv subst tv | otherwise -> 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 GhcDiagnosticCode "NestedTHBrackets" = 59185 GhcDiagnosticCode "AddTopDeclsUnexpectedDeclarationSplice" = 17599 GhcDiagnosticCode "BadImplicitSplice" = 25277 - GhcDiagnosticCode "QuotedNameWrongStage" = 57695 + GhcDiagnosticCode "QuotedNameWrongStage" = Outdated 57695 GhcDiagnosticCode "IllegalStaticFormInSplice" = 12219 -- Zonker messages View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/49dd72380ae886224fb16671699a584... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/49dd72380ae886224fb16671699a584... You're receiving this email because of your account on gitlab.haskell.org.
participants (1)
-
Matthew Pickering (@mpickering)