Skip to content

Backport "Fix copy of annotation on @main methods" to 3.3 LTS #244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -103,17 +103,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 = ()