Skip to content

Commit f4223d8

Browse files
authored
Merge pull request #244 from scala/backport-lts-3.3-22582
Backport "Fix copy of annotation on @main methods" to 3.3 LTS
2 parents 4019c30 + 2f045b1 commit f4223d8

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

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

+8-8
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

+5
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

+6
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

+10
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)