Skip to content

Commit 2f045b1

Browse files
mboveltgodzik
authored andcommitted
Fix copy of annotation on @main methods
[Cherry-picked 15c4e51]
1 parent 2333135 commit 2f045b1

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

compiler/src/dotty/tools/dotc/ast/MainProxies.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

tests/pos/annot-main-22364.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def id[T](x: T): T = x
2+
3+
class ann(x: Int) extends annotation.Annotation
4+
5+
@ann(id(22)) @main def blop = ()

tests/pos/annot-main-22364b.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import util.chaining.*
2+
3+
class ann(x: Int = 1, y: Int) extends annotation.Annotation
4+
5+
@ann(y = 22.tap(println)) @main def blop = ()
6+

tests/pos/annot-main-22364c.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package p
2+
3+
object P1:
4+
class ann(x: Int) extends annotation.Annotation
5+
6+
object P2:
7+
def id[T](x: T): T = x
8+
9+
object P3:
10+
@P1.ann(P2.id(22)) @main def blop = ()

0 commit comments

Comments
 (0)