Skip to content

Commit 54039f7

Browse files
committed
Update newMain annotation
1 parent 366b1b3 commit 54039f7

24 files changed

+477
-421
lines changed

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import NameKinds.DefaultGetterName
1111
import Annotations.Annotation
1212

1313
object MainProxies {
14+
15+
/** Generate proxy classes for @main functions and @myMain functions where myMain <:< MainAnnotation */
16+
def proxies(stats: List[tpd.Tree])(using Context): List[untpd.Tree] = {
17+
mainAnnotationProxies(stats) ++ mainProxies(stats)
18+
}
19+
1420
/** Generate proxy classes for @main functions.
1521
* A function like
1622
*
@@ -29,7 +35,7 @@ object MainProxies {
2935
* catch case err: ParseError => showError(err)
3036
* }
3137
*/
32-
def mainProxiesOld(stats: List[tpd.Tree])(using Context): List[untpd.Tree] = {
38+
private def mainProxies(stats: List[tpd.Tree])(using Context): List[untpd.Tree] = {
3339
import tpd._
3440
def mainMethods(stats: List[Tree]): List[Symbol] = stats.flatMap {
3541
case stat: DefDef if stat.symbol.hasAnnotation(defn.MainAnnot) =>
@@ -39,11 +45,11 @@ object MainProxies {
3945
case _ =>
4046
Nil
4147
}
42-
mainMethods(stats).flatMap(mainProxyOld)
48+
mainMethods(stats).flatMap(mainProxy)
4349
}
4450

4551
import untpd._
46-
def mainProxyOld(mainFun: Symbol)(using Context): List[TypeDef] = {
52+
private def mainProxy(mainFun: Symbol)(using Context): List[TypeDef] = {
4753
val mainAnnotSpan = mainFun.getAnnotation(defn.MainAnnot).get.tree.span
4854
def pos = mainFun.sourcePos
4955
val argsRef = Ident(nme.args)
@@ -163,7 +169,7 @@ object MainProxies {
163169
* }
164170
* }
165171
*/
166-
def mainProxies(stats: List[tpd.Tree])(using Context): List[untpd.Tree] = {
172+
private def mainAnnotationProxies(stats: List[tpd.Tree])(using Context): List[untpd.Tree] = {
167173
import tpd._
168174

169175
/**
@@ -186,12 +192,12 @@ object MainProxies {
186192
def mainMethods(scope: Tree, stats: List[Tree]): List[(Symbol, ParameterAnnotationss, DefaultValueSymbols, Option[Comment])] = stats.flatMap {
187193
case stat: DefDef =>
188194
val sym = stat.symbol
189-
sym.annotations.filter(_.matches(defn.MainAnnot)) match {
195+
sym.annotations.filter(_.matches(defn.MainAnnotationClass)) match {
190196
case Nil =>
191197
Nil
192198
case _ :: Nil =>
193199
val paramAnnotations = stat.paramss.flatMap(_.map(
194-
valdef => valdef.symbol.annotations.filter(_.matches(defn.MainAnnotParameterAnnotation))
200+
valdef => valdef.symbol.annotations.filter(_.matches(defn.MainAnnotationParameterAnnotation))
195201
))
196202
(sym, paramAnnotations.toVector, defaultValueSymbols(scope, sym), stat.rawComment) :: Nil
197203
case mainAnnot :: others =>
@@ -205,7 +211,7 @@ object MainProxies {
205211
}
206212

207213
// Assuming that the top-level object was already generated, all main methods will have a scope
208-
mainMethods(EmptyTree, stats).flatMap(mainProxy)
214+
mainMethods(EmptyTree, stats).flatMap(mainAnnotationProxy)
209215
}
210216

211217
private def mainAnnotationProxy(mainFun: Symbol, paramAnnotations: ParameterAnnotationss, defaultValueSymbols: DefaultValueSymbols, docComment: Option[Comment])(using Context): Option[TypeDef] = {
@@ -357,7 +363,7 @@ object MainProxies {
357363
case tree => super.transform(tree)
358364
}
359365
val annots = mainFun.annotations
360-
.filterNot(_.matches(defn.MainAnnot))
366+
.filterNot(_.matches(defn.MainAnnotationClass))
361367
.map(annot => insertTypeSplices.transform(annot.tree))
362368
val mainMeth = DefDef(nme.main, (mainArg :: Nil) :: Nil, TypeTree(defn.UnitType), body)
363369
.withFlags(JavaStatic)

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,7 @@ class Definitions {
916916
@tu lazy val InlineParamAnnot: ClassSymbol = requiredClass("scala.annotation.internal.InlineParam")
917917
@tu lazy val ErasedParamAnnot: ClassSymbol = requiredClass("scala.annotation.internal.ErasedParam")
918918
@tu lazy val InvariantBetweenAnnot: ClassSymbol = requiredClass("scala.annotation.internal.InvariantBetween")
919+
@tu lazy val MainAnnot: ClassSymbol = requiredClass("scala.main")
919920
@tu lazy val MigrationAnnot: ClassSymbol = requiredClass("scala.annotation.migration")
920921
@tu lazy val NowarnAnnot: ClassSymbol = requiredClass("scala.annotation.nowarn")
921922
@tu lazy val TransparentTraitAnnot: ClassSymbol = requiredClass("scala.annotation.transparentTrait")

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2602,7 +2602,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
26022602
pkg.moduleClass.info.decls.lookup(topLevelClassName).ensureCompleted()
26032603
var stats1 = typedStats(tree.stats, pkg.moduleClass)._1
26042604
if (!ctx.isAfterTyper)
2605-
stats1 = stats1 ++ typedBlockStats(MainProxies.mainProxies(stats1))._1
2605+
stats1 = stats1 ++ typedBlockStats(MainProxies.proxies(stats1))._1
26062606
cpy.PackageDef(tree)(pid1, stats1).withType(pkg.termRef)
26072607
}
26082608
case _ =>

0 commit comments

Comments
 (0)