Apoorv Ingle pushed to branch wip/ani/T27156 at Glasgow Haskell Compiler / GHC Commits: b9199939 by Apoorv Ingle at 2026-10-24T15:07:01-05:00 make a new ReSynBinds to store the module name - - - - - 8 changed files: - compiler/GHC/Hs/Expr.hs - compiler/GHC/Hs/Instances.hs - compiler/GHC/Rename/Expr.hs - compiler/GHC/Tc/Gen/Expand.hs - compiler/GHC/Tc/Gen/Expr.hs - compiler/GHC/ThToHs.hs - libraries/ghc-boot-th/GHC/Boot/TH/Ppr.hs - libraries/ghc-internal/src/GHC/Internal/TH/Syntax.hs Changes: ===================================== compiler/GHC/Hs/Expr.hs ===================================== @@ -652,7 +652,7 @@ tupArgsPresent_maybe = traverse tupArgPresent_maybe * * ********************************************************************* -} -type instance XXExpr GhcPs = DataConCantHappen +type instance XXExpr GhcPs = XXExprGhcPs type instance XXExpr GhcRn = XXExprGhcRn type instance XXExpr GhcTc = XXExprGhcTc -- XXExprGhcRn: see Note [Rebindable syntax and XXExprGhcRn] below @@ -665,6 +665,10 @@ type instance XXExpr GhcTc = XXExprGhcTc * * ********************************************************************* -} +data XXExprGhcPs + = ReSynBinds HsModuleName (HsExpr GhcPs) + + -- See Note [Rebindable syntax and XXExprGhcRn] -- See Note [Expanding HsDo with XXExprGhcRn] in `GHC.Tc.Gen.Do` -- See Note [Typechecking by expansion: overview] @@ -681,6 +685,7 @@ data XXExprGhcRn -- See Note [Non-overloaded record field selectors] and -- Note [Record selectors in the AST] + data XXExprGhcTc = WrapExpr -- Type and evidence application and abstractions HsWrapper (HsExpr GhcTc) @@ -1029,9 +1034,12 @@ ppr_expr (HsFunArr _ arr arg res) = sep [ppr_lexpr arg, pprHsModifiedFunArr arr <+> ppr_lexpr res] ppr_expr (XExpr x) = case ghcPass @p of + GhcPs -> ppr x GhcRn -> ppr x GhcTc -> ppr x +instance Outputable XXExprGhcPs where + ppr (ReSynBinds m e) = text "{{" <+> ppr m <+> text "}}" <+> ppr e ppr_hse :: forall p. (IsPass p) => HsExpansion (GhcPass p) -> SDoc ppr_hse hse @@ -1088,10 +1096,14 @@ ppr_infix_expr (HsHole x) = Just $ pprInfixOcc $ case (ghcPass @p, x) of (GhcTc, (HoleVar (L _ v), _)) -> v _ -> unnamedHoleRdrName -- TODO: this is the HoleError case; this should print the source text instead of "_". ppr_infix_expr (XExpr x) = case ghcPass @p of + GhcPs -> ppr_infix_expr_ps x GhcRn -> ppr_infix_expr_rn x GhcTc -> ppr_infix_expr_tc x ppr_infix_expr _ = Nothing +ppr_infix_expr_ps :: XXExprGhcPs -> Maybe SDoc +ppr_infix_expr_ps (ReSynBinds{}) = Nothing + ppr_infix_expr_rn :: XXExprGhcRn -> Maybe SDoc ppr_infix_expr_rn (ExpandedThingRn (HSE thing _)) = ppr_infix_hs_expansion thing ppr_infix_expr_rn (HsRecSelRn f) = Just (pprInfixOcc f) @@ -1182,9 +1194,13 @@ hsExprNeedsParens prec = go go (HsQual{}) = prec >= funPrec go (HsFunArr{}) = prec >= funPrec go (XExpr x) = case ghcPass @p of + GhcPs -> go_x_ps x GhcTc -> go_x_tc x GhcRn -> go_x_rn x + go_x_ps :: XXExprGhcPs -> Bool + go_x_ps (ReSynBinds _ e) = hsExprNeedsParens prec e + go_x_tc :: XXExprGhcTc -> Bool go_x_tc (WrapExpr _ e) = hsExprNeedsParens prec e go_x_tc (ExpandedThingTc (HSE thing _)) = hsExpandedNeedsParens thing ===================================== compiler/GHC/Hs/Instances.hs ===================================== @@ -735,6 +735,7 @@ deriving instance Data (HsExpansion GhcTc) deriving instance Data a => Data (WithUserRdr a) -- ------------------------------- +deriving instance Data XXExprGhcPs deriving instance Data XXExprGhcTc deriving instance Data XXPatGhcTc ===================================== compiler/GHC/Rename/Expr.hs ===================================== @@ -46,7 +46,6 @@ import GHC.Driver.DynFlags import GHC.Builtin.KnownKeys import GHC.Builtin.KnownOccs import GHC.Builtin.WiredIn.Types ( nilDataConName, oneDataConName ) -import GHC.Builtin.WiredIn.Ids( rightSectionName, leftSectionName ) import GHC.Unit.Module ( isInteractiveModule ) @@ -327,8 +326,8 @@ rnExpr (HsHole h) -- HsOverLabel: see Note [Handling overloaded and rebindable constructs] rnExpr (HsOverLabel src v) - = do { (from_label, fvs) <- lookupSyntaxName fromLabelClassOpName - ; let rs_table = Rebindable [(nameOccName fromLabelClassOpName, from_label)] + = do { (from_label, fvs) <- lookupSyntaxName fromLabelClassOpOcc + ; let rs_table = Rebindable [(fromLabelClassOpOcc, from_label)] ; return (HsOverLabel (src, rs_table) v, fvs) } @@ -353,7 +352,7 @@ rnExpr (HsOverLit x lit) , fvs ) } rnExpr (HsQualLit x QualLit{..}) - = do { (funName, fvs) <- lookupNameWithQualifier fromStringName ql_mod + = do { (funName, fvs) <- lookupNameWithQualifier ql_mod fromStringClassOpOcc ; let ql_val' = case ql_val of HsQualString st s -> HsQualString st s ; return (HsQualLit x (QualLit (L noAnn funName) ql_mod ql_val'), fvs) @@ -401,11 +400,11 @@ rnExpr (HsGetField _ e f) = do { (getField, fv_getField) <- lookupSyntaxName getFieldClassOpOcc ; (e, fv_e) <- rnLExpr e ; let f' = rnDotFieldOcc <$> f - rs_table = Rebindable [(nameOccName getFieldName, getField)] + rs_table = Rebindable [(getFieldClassOpOcc, getField)] ; return (HsGetField rs_table e f', fv_e `plusFN` fv_getField) } rnExpr (HsProjection _ fs) - = do { (getFieldName, fv_getField) <- lookupSyntaxName getFieldName + = do { (getFieldName, fv_getField) <- lookupSyntaxName getFieldClassOpOcc ; circName <- rnLookupKnownOccName composeIdOcc ; let fs' = NE.map rnDotFieldOcc fs rs_table = Rebindable [(nameOccName getFieldName , getFieldName), (rdrNameOcc compose_RDR, circName)] @@ -476,7 +475,7 @@ rnExpr (ExplicitList _ exps) then return (ExplicitList NoRebindable exps', fvs) else do { (from_list_n_name, fvs') <- lookupSyntaxName fromListNClassOpOcc - ; let rs_table = Rebindable [(nameOccName fromListNClassOpOcc, from_list_n_name)] + ; let rs_table = Rebindable [(fromListNClassOpOcc, from_list_n_name)] rn_list = ExplicitList rs_table exps' ; return ( rn_list , fvs `plusFN` fvs') } } @@ -608,6 +607,12 @@ rnExpr (HsFunArr _ mult arg res) _ -> False oneType = noLocA $ HsVar noExtField $ noLocA $ noUserRdr oneDataConName +rnExpr (XExpr (ReSynBinds m e')) + = do { traceRn "XExpr ReSynBinds" (text "module:" <+> ppr m) + ; rnExpr e' -- for now just return the renamed expression + -- TODO: Bring into scope top level environment of the module m + } + {- ************************************************************************ * * ===================================== compiler/GHC/Tc/Gen/Expand.hs ===================================== @@ -19,12 +19,11 @@ import {-# SOURCE #-} GHC.Tc.Gen.Splice( getUntypedSpliceBody ) import GHC.Types.Name import GHC.Types.Name.Reader -import GHC.Types.Id.Make -import GHC.Types.SrcLoc +import GHC.Types.SrcLoc ( GenLocated(L), unLoc ) import GHC.Types.SourceText ( SourceText(..) ) -import GHC.Builtin.Names - +import GHC.Builtin.KnownOccs +import GHC.Builtin.WiredIn.Ids ( leftSectionName, rightSectionName ) import GHC.Rename.Utils import GHC.Utils.Panic @@ -34,6 +33,7 @@ import qualified Data.List.NonEmpty as NE ( map, head, (<|) ) import Data.List.NonEmpty ( NonEmpty(..), init, last, tail ) import qualified GHC.LanguageExtensions as LangExt +import GHC.Builtin.KnownKeys (getFieldClassOpOcc) {- Note [Typechecking by expansion: overview] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -123,7 +123,7 @@ tcExpand :: HsExpr GhcRn -> TcM (Maybe (HsExpansion GhcRn)) ------------------------------------------ -- Overloaded labels tcExpand e@(HsOverLabel (_, Rebindable rs_table) v) - | Just fromLabelName <- lookup (nameOccName fromLabelClassOpName) rs_table + | Just fromLabelName <- lookup fromLabelClassOpOcc rs_table , let hs_ty_arg = mkEmptyWildCardBndrs $ wrapGenSpan $ HsTyLit noExtField (HsString NoSourceText v) = return $ Just $ @@ -178,7 +178,7 @@ tcExpand e@(HsIf (Rebindable rs_table) p b1 b2) -- Record dot syntax tcExpand e@(HsGetField (Rebindable rs_table) expr f) - | Just getField <- lookup (nameOccName getFieldName) rs_table + | Just getField <- lookup getFieldClassOpOcc rs_table = return $ Just $ HSE { hse_ctxt = ExprCtxt e , hse_exp = wrapGenSpan $ (mkGetField getField expr (fmap (unLoc . dfoLabel) f)) } @@ -187,7 +187,7 @@ tcExpand e@(HsGetField (Rebindable rs_table) expr f) , ppr e ]) tcExpand e@(HsProjection (Rebindable rs_table) fs) - | Just getField <- lookup (nameOccName getFieldName) rs_table + | Just getField <- lookup getFieldClassOpOcc rs_table , Just circ <- lookup (rdrNameOcc compose_RDR) rs_table = return $ Just $ HSE { hse_ctxt = ExprCtxt e @@ -200,8 +200,8 @@ tcExpand e@(HsProjection (Rebindable rs_table) fs) tcExpand (RecordUpd NoRebindable _ _ ) = return Nothing -- until #27160 is fixed tcExpand e@(RecordUpd (Rebindable rs_table) (L l expr) (OverloadedRecUpdFields { olRecUpdFields = us})) - | Just getField <- lookup (nameOccName getFieldName) rs_table - , Just setField <- lookup (nameOccName setFieldName) rs_table + | Just getField <- lookup getFieldClassOpOcc rs_table + , Just setField <- lookup setFieldClassOpOcc rs_table = return $ Just $ HSE { hse_ctxt = ExprCtxt e , hse_exp = wrapGenSpan $ mkRecordDotUpd getField setField (L l expr) us } @@ -235,7 +235,7 @@ tcExpand e@(SectionL _ expr op) tcExpand (ExplicitList NoRebindable _ ) = return Nothing -- rebindable syntax is off tcExpand e@(ExplicitList (Rebindable rs_table) exps) - | Just from_list_n_name <- lookup (nameOccName fromListNName) rs_table + | Just from_list_n_name <- lookup fromListNClassOpOcc rs_table = do { loc <- getSrcSpanM -- See Note [Source locations for implicit function calls] ; let lit_n = mkIntegralLit (length exps) hs_lit = genHsIntegralLit lit_n ===================================== compiler/GHC/Tc/Gen/Expr.hs ===================================== @@ -595,7 +595,7 @@ tcExprNoExpand (HsProc x pat cmd) res_ty -- and wrap (static e) in a call to -- fromStaticPtr :: IsStatic p => StaticPtr a -> p a -tcExpr (HsStatic free_names expr) res_ty +tcExprNoExpand (HsStatic free_names expr) res_ty = do { res_ty <- expTypeToType res_ty ; (co, (p_ty, expr_ty)) <- matchExpectedAppTy res_ty ; (expr', lie) <- captureConstraints $ ===================================== compiler/GHC/ThToHs.hs ===================================== @@ -1228,6 +1228,8 @@ cvtl e = wrapLA (cvt e) ; let tele = setTelescopeBndrsNameSpace varName $ mkHsForAllVisTele noAnn tvs' ; return $ HsForAll noExtField tele body' } + cvt (ReSynBindsE m e) = do { e' <- cvt e + ; return $ XExpr (ReSynBinds (mk_modtext m) e') } {- | #16895 Ensure an infix expression's operator is a variable/constructor. Consider this example: ===================================== libraries/ghc-boot-th/GHC/Boot/TH/Ppr.hs ===================================== @@ -56,6 +56,9 @@ instance Ppr a => Ppr [a] where instance Ppr Name where ppr v = pprName v +instance Ppr ModName where + ppr (ModName s) = text s + ------------------------------ instance Ppr Info where ppr (TyConI d) = ppr d @@ -250,6 +253,7 @@ pprExp i (ForallE tvars body) = parensIf (i >= funPrec) $ sep [pprForall tvars [], pprExp qualPrec body] pprExp i (ConstrainedE ctx body) = parensIf (i >= funPrec) $ sep [pprCtxWith pprExp ctx, pprExp qualPrec body] +pprExp i (ReSynBindsE m e) = text "{{" <+> ppr m <+> text "}}" <+> pprExp i e pprFields :: [(Name,Exp)] -> Doc pprFields = sep . punctuate comma . map (\(s,e) -> pprName' Applied s <+> equals <+> ppr e) ===================================== libraries/ghc-internal/src/GHC/Internal/TH/Syntax.hs ===================================== @@ -904,6 +904,8 @@ data Exp | ForallE [TyVarBndr Specificity] Exp -- ^ @forall \<vars\>. \<expr\>@ | ForallVisE [TyVarBndr ()] Exp -- ^ @forall \<vars\> -> \<expr\>@ | ConstrainedE [Exp] Exp -- ^ @\<ctxt\> => \<expr\>@ + | ReSynBindsE ModName Exp -- Remember to use bindings from the specified modname + -- while typechecking a typed splice for rebindable syntax deriving( Show, Eq, Ord, Generic ) -- | A (field name, expression) pair. See 'RecConE' and 'RecUpdE'. View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/b9199939a5ad5ff4f907ae0844f82c29... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/b9199939a5ad5ff4f907ae0844f82c29... You're receiving this email because of your account on gitlab.haskell.org. Manage all notifications: https://gitlab.haskell.org/-/profile/notifications | Help: https://gitlab.haskell.org/help