@@ -103,17 +103,17 @@ object MainProxies {
103103 val body = Try (call, handler :: Nil , EmptyTree )
104104 val mainArg = ValDef (nme.args, TypeTree (defn.ArrayType .appliedTo(defn.StringType )), EmptyTree )
105105 .withFlags(Param )
106- /** Replace typed `Ident`s that have been typed with a TypeSplice with the reference to the symbol.
107- * The annotations will be retype-checked in another scope that may not have the same imports.
106+
107+ /** This context is used to create the `TypeSplices` wrapping annotations
108+ * below. These should have `mainFun` as their owner (and not the
109+ * enclosing package class that we would get otherwise) so that
110+ * subsequent owner changes (for example in `Typer.typedTypedSplice`) are
111+ * correct. See #22364 and associated tests.
108112 */
109- def insertTypeSplices = new TreeMap {
110- override def transform (tree : Tree )(using Context ): Tree = tree match
111- case tree : tpd.Ident @ unchecked => TypedSplice (tree)
112- case tree => super .transform(tree)
113- }
113+ val annotsCtx = ctx.fresh.setOwner(mainFun)
114114 val annots = mainFun.annotations
115115 .filterNot(_.matches(defn.MainAnnot ))
116- .map(annot => insertTypeSplices.transform (annot.tree))
116+ .map(annot => TypedSplice (annot.tree)( using annotsCtx ))
117117 val mainMeth = DefDef (nme.main, (mainArg :: Nil ) :: Nil , TypeTree (defn.UnitType ), body)
118118 .withFlags(JavaStatic | Synthetic )
119119 .withAnnotations(annots)
0 commit comments