Skip to content

Commit b916f9c

Browse files
Add newMain prototype
Based on prototype in #13727 Co-authored-by: Timothée Loyck Andres <[email protected]>
1 parent 9d68778 commit b916f9c

File tree

77 files changed

+1900
-23
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+1900
-23
lines changed

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

+8-14
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ 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-
2014
/** Generate proxy classes for @main functions.
2115
* A function like
2216
*
@@ -35,7 +29,7 @@ object MainProxies {
3529
* catch case err: ParseError => showError(err)
3630
* }
3731
*/
38-
private def mainProxies(stats: List[tpd.Tree])(using Context): List[untpd.Tree] = {
32+
def mainProxiesOld(stats: List[tpd.Tree])(using Context): List[untpd.Tree] = {
3933
import tpd._
4034
def mainMethods(stats: List[Tree]): List[Symbol] = stats.flatMap {
4135
case stat: DefDef if stat.symbol.hasAnnotation(defn.MainAnnot) =>
@@ -45,11 +39,11 @@ object MainProxies {
4539
case _ =>
4640
Nil
4741
}
48-
mainMethods(stats).flatMap(mainProxy)
42+
mainMethods(stats).flatMap(mainProxyOld)
4943
}
5044

5145
import untpd._
52-
private def mainProxy(mainFun: Symbol)(using Context): List[TypeDef] = {
46+
def mainProxyOld(mainFun: Symbol)(using Context): List[TypeDef] = {
5347
val mainAnnotSpan = mainFun.getAnnotation(defn.MainAnnot).get.tree.span
5448
def pos = mainFun.sourcePos
5549
val argsRef = Ident(nme.args)
@@ -171,7 +165,7 @@ object MainProxies {
171165
* }
172166
* }
173167
*/
174-
private def mainAnnotationProxies(stats: List[tpd.Tree])(using Context): List[untpd.Tree] = {
168+
def mainProxies(stats: List[tpd.Tree])(using Context): List[untpd.Tree] = {
175169
import tpd._
176170

177171
/**
@@ -194,12 +188,12 @@ object MainProxies {
194188
def mainMethods(scope: Tree, stats: List[Tree]): List[(Symbol, ParameterAnnotationss, DefaultValueSymbols, Option[Comment])] = stats.flatMap {
195189
case stat: DefDef =>
196190
val sym = stat.symbol
197-
sym.annotations.filter(_.matches(defn.MainAnnotationClass)) match {
191+
sym.annotations.filter(_.matches(defn.MainAnnot)) match {
198192
case Nil =>
199193
Nil
200194
case _ :: Nil =>
201195
val paramAnnotations = stat.paramss.flatMap(_.map(
202-
valdef => valdef.symbol.annotations.filter(_.matches(defn.MainAnnotationParameterAnnotation))
196+
valdef => valdef.symbol.annotations.filter(_.matches(defn.MainAnnotParameterAnnotation))
203197
))
204198
(sym, paramAnnotations.toVector, defaultValueSymbols(scope, sym), stat.rawComment) :: Nil
205199
case mainAnnot :: others =>
@@ -213,7 +207,7 @@ object MainProxies {
213207
}
214208

215209
// Assuming that the top-level object was already generated, all main methods will have a scope
216-
mainMethods(EmptyTree, stats).flatMap(mainAnnotationProxy)
210+
mainMethods(EmptyTree, stats).flatMap(mainProxy)
217211
}
218212

219213
private def mainAnnotationProxy(mainFun: Symbol, paramAnnotations: ParameterAnnotationss, defaultValueSymbols: DefaultValueSymbols, docComment: Option[Comment])(using Context): Option[TypeDef] = {
@@ -367,7 +361,7 @@ object MainProxies {
367361
case tree => super.transform(tree)
368362
}
369363
val annots = mainFun.annotations
370-
.filterNot(_.matches(defn.MainAnnotationClass))
364+
.filterNot(_.matches(defn.MainAnnot))
371365
.map(annot => insertTypeSplices.transform(annot.tree))
372366
val mainMeth = DefDef(nme.main, (mainArg :: Nil) :: Nil, TypeTree(defn.UnitType), body)
373367
.withFlags(JavaStatic)

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

-1
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,6 @@ class Definitions {
954954
@tu lazy val InlineParamAnnot: ClassSymbol = requiredClass("scala.annotation.internal.InlineParam")
955955
@tu lazy val ErasedParamAnnot: ClassSymbol = requiredClass("scala.annotation.internal.ErasedParam")
956956
@tu lazy val InvariantBetweenAnnot: ClassSymbol = requiredClass("scala.annotation.internal.InvariantBetween")
957-
@tu lazy val MainAnnot: ClassSymbol = requiredClass("scala.main")
958957
@tu lazy val MigrationAnnot: ClassSymbol = requiredClass("scala.annotation.migration")
959958
@tu lazy val NowarnAnnot: ClassSymbol = requiredClass("scala.annotation.nowarn")
960959
@tu lazy val TransparentTraitAnnot: ClassSymbol = requiredClass("scala.annotation.transparentTrait")

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -1382,10 +1382,9 @@ trait Checking {
13821382
/** check that annotation `annot` is applicable to symbol `sym` */
13831383
def checkAnnotApplicable(annot: Tree, sym: Symbol)(using Context): Boolean =
13841384
!ctx.reporter.reportsErrorsFor {
1385-
val annotCls = Annotations.annotClass(annot)
13861385
val concreteAnnot = Annotations.ConcreteAnnotation(annot)
13871386
val pos = annot.srcPos
1388-
if (annotCls == defn.MainAnnot || concreteAnnot.matches(defn.MainAnnotationClass)) {
1387+
if (concreteAnnot.matches(defn.MainAnnot)) {
13891388
if (!sym.isRealMethod)
13901389
report.error(em"main annotation cannot be applied to $sym", pos)
13911390
if (!sym.owner.is(Module) || !sym.owner.isStatic)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2626,7 +2626,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
26262626
pkg.moduleClass.info.decls.lookup(topLevelClassName).ensureCompleted()
26272627
var stats1 = typedStats(tree.stats, pkg.moduleClass)._1
26282628
if (!ctx.isAfterTyper)
2629-
stats1 = stats1 ++ typedBlockStats(MainProxies.proxies(stats1))._1
2629+
stats1 = stats1 ++ typedBlockStats(MainProxies.mainProxies(stats1))._1
26302630
cpy.PackageDef(tree)(pid1, stats1).withType(pkg.termRef)
26312631
}
26322632
case _ =>

0 commit comments

Comments
 (0)