Skip to content

Commit

Permalink
Fix copy of annotation on @main methods (#22582)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbovel authored Feb 12, 2025
2 parents 37206cc + 15c4e51 commit a61c12e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 deletions.
16 changes: 8 additions & 8 deletions compiler/src/dotty/tools/dotc/ast/MainProxies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,17 @@ object MainProxies {
val body = Try(call, handler :: Nil, EmptyTree)
val mainArg = ValDef(nme.args, TypeTree(defn.ArrayType.appliedTo(defn.StringType)), EmptyTree)
.withFlags(Param)
/** Replace typed `Ident`s that have been typed with a TypeSplice with the reference to the symbol.
* The annotations will be retype-checked in another scope that may not have the same imports.

/** This context is used to create the `TypeSplices` wrapping annotations
* below. These should have `mainFun` as their owner (and not the
* enclosing package class that we would get otherwise) so that
* subsequent owner changes (for example in `Typer.typedTypedSplice`) are
* correct. See #22364 and associated tests.
*/
def insertTypeSplices = new TreeMap {
override def transform(tree: Tree)(using Context): Tree = tree match
case tree: tpd.Ident @unchecked => TypedSplice(tree)
case tree => super.transform(tree)
}
val annotsCtx = ctx.fresh.setOwner(mainFun)
val annots = mainFun.annotations
.filterNot(_.matches(defn.MainAnnot))
.map(annot => insertTypeSplices.transform(annot.tree))
.map(annot => TypedSplice(annot.tree)(using annotsCtx))
val mainMeth = DefDef(nme.main, (mainArg :: Nil) :: Nil, TypeTree(defn.UnitType), body)
.withFlags(JavaStatic | Synthetic)
.withAnnotations(annots)
Expand Down
5 changes: 5 additions & 0 deletions tests/pos/annot-main-22364.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def id[T](x: T): T = x

class ann(x: Int) extends annotation.Annotation

@ann(id(22)) @main def blop = ()
6 changes: 6 additions & 0 deletions tests/pos/annot-main-22364b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import util.chaining.*

class ann(x: Int = 1, y: Int) extends annotation.Annotation

@ann(y = 22.tap(println)) @main def blop = ()

10 changes: 10 additions & 0 deletions tests/pos/annot-main-22364c.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package p

object P1:
class ann(x: Int) extends annotation.Annotation

object P2:
def id[T](x: T): T = x

object P3:
@P1.ann(P2.id(22)) @main def blop = ()

0 comments on commit a61c12e

Please sign in to comment.