Skip to content

Commit 2e7d6af

Browse files
committed
Keep annotation of @main methods in def main
Fixes #13848
1 parent 70d3c56 commit 2e7d6af

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

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

+12
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,20 @@ object MainProxies {
9494
val body = Try(call, handler :: Nil, EmptyTree)
9595
val mainArg = ValDef(nme.args, TypeTree(defn.ArrayType.appliedTo(defn.StringType)), EmptyTree)
9696
.withFlags(Param)
97+
/** Replace typed `Ident`s that have been typed with a TypeSplice with the reference to the symbol.
98+
* The annotations will be retype-checked in another scope that may not have the same imports.
99+
*/
100+
def insertTypeSplices = new TreeMap {
101+
override def transform(tree: Tree)(using Context): Tree = tree match
102+
case tree: Ident if tree.symbol.exists => ref(tree.symbol)
103+
case tree => super.transform(tree)
104+
}
105+
val annots = mainFun.annotations
106+
.filterNot(_.matches(defn.MainAnnot))
107+
.map(annot => insertTypeSplices.transform(annot.tree))
97108
val mainMeth = DefDef(nme.main, (mainArg :: Nil) :: Nil, TypeTree(defn.UnitType), body)
98109
.withFlags(JavaStatic)
110+
.withAnnotations(annots)
99111
val mainTempl = Template(emptyConstructor, Nil, Nil, EmptyValDef, mainMeth :: Nil)
100112
val mainCls = TypeDef(mainFun.name.toTypeName, mainTempl)
101113
.withFlags(Final | Invisible)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import annotation.experimental
2+
3+
@main
4+
def run(): Unit = f // error
5+
6+
@experimental
7+
def f = 2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import annotation.experimental
2+
3+
@main
4+
@experimental
5+
def run(): Unit = f
6+
7+
@experimental
8+
def f = 2

0 commit comments

Comments
 (0)