Skip to content

Commit 618c631

Browse files
committed
Remove new main
1 parent f6e8146 commit 618c631

File tree

82 files changed

+62
-1937
lines changed

Some content is hidden

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

82 files changed

+62
-1937
lines changed

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

Lines changed: 21 additions & 15 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)
@@ -160,7 +166,7 @@ object MainProxies {
160166
* }
161167
* }
162168
*/
163-
def mainProxies(stats: List[tpd.Tree])(using Context): List[untpd.Tree] = {
169+
private def mainAnnotationProxies(stats: List[tpd.Tree])(using Context): List[untpd.Tree] = {
164170
import tpd._
165171

166172
/**
@@ -183,12 +189,12 @@ object MainProxies {
183189
def mainMethods(scope: Tree, stats: List[Tree]): List[(Symbol, ParameterAnnotationss, DefaultValueSymbols, Option[Comment])] = stats.flatMap {
184190
case stat: DefDef =>
185191
val sym = stat.symbol
186-
sym.annotations.filter(_.matches(defn.MainAnnot)) match {
192+
sym.annotations.filter(_.matches(defn.MainAnnotationClass)) match {
187193
case Nil =>
188194
Nil
189195
case _ :: Nil =>
190196
val paramAnnotations = stat.paramss.flatMap(_.map(
191-
valdef => valdef.symbol.annotations.filter(_.matches(defn.MainAnnotParameterAnnotation))
197+
valdef => valdef.symbol.annotations.filter(_.matches(defn.MainAnnotationParameterAnnotation))
192198
))
193199
(sym, paramAnnotations.toVector, defaultValueSymbols(scope, sym), stat.rawComment) :: Nil
194200
case mainAnnot :: others =>
@@ -202,11 +208,11 @@ object MainProxies {
202208
}
203209

204210
// Assuming that the top-level object was already generated, all main methods will have a scope
205-
mainMethods(EmptyTree, stats).flatMap(mainProxy)
211+
mainMethods(EmptyTree, stats).flatMap(mainAnnotationProxy)
206212
}
207213

208-
def mainProxy(mainFun: Symbol, paramAnnotations: ParameterAnnotationss, defaultValueSymbols: DefaultValueSymbols, docComment: Option[Comment])(using Context): List[TypeDef] = {
209-
val mainAnnot = mainFun.getAnnotation(defn.MainAnnot).get
214+
private def mainAnnotationProxy(mainFun: Symbol, paramAnnotations: ParameterAnnotationss, defaultValueSymbols: DefaultValueSymbols, docComment: Option[Comment])(using Context): List[TypeDef] = {
215+
val mainAnnot = mainFun.getAnnotation(defn.MainAnnotationClass).get
210216
def pos = mainFun.sourcePos
211217
val cmdName: TermName = Names.termName("cmd")
212218

@@ -244,15 +250,15 @@ object MainProxies {
244250
val (argRef, formalType, getterSym) = {
245251
val argRef0 = Apply(Ident(argName), Nil)
246252
if formal.isRepeatedParam then
247-
(repeated(argRef0), formal.argTypes.head, defn.MainAnnotCommand_varargGetter)
248-
else (argRef0, formal, defn.MainAnnotCommand_argGetter)
253+
(repeated(argRef0), formal.argTypes.head, defn.MainAnnotationCommand_varargGetter)
254+
else (argRef0, formal, defn.MainAnnotationCommand_argGetter)
249255
}
250256

251257
// The ParameterInfos
252258
val parameterInfos = {
253259
val param = paramName.toString
254260
val paramInfosTree = New(
255-
TypeTree(defn.MainAnnotParameterInfos.typeRef),
261+
TypeTree(defn.MainAnnotationParameterInfos.typeRef),
256262
// Arguments to be passed to ParameterInfos' constructor
257263
List(List(lit(param), lit(formalType.show)))
258264
)
@@ -352,11 +358,11 @@ object MainProxies {
352358
cmdName,
353359
TypeTree(),
354360
Apply(
355-
Select(instanciateAnnotation(mainAnnot), defn.MainAnnot_command.name),
361+
Select(instanciateAnnotation(mainAnnot), defn.MainAnnotation_command.name),
356362
Ident(nme.args) :: lit(mainFun.showName) :: lit(documentation.mainDoc) :: parameterInfoss
357363
)
358364
)
359-
val run = Apply(Select(Ident(cmdName), defn.MainAnnotCommand_run.name), mainCall)
365+
val run = Apply(Select(Ident(cmdName), defn.MainAnnotationCommand_run.name), mainCall)
360366
val body = Block(cmd :: args, run)
361367
val mainArg = ValDef(nme.args, TypeTree(defn.ArrayType.appliedTo(defn.StringType)), EmptyTree)
362368
.withFlags(Param)
@@ -369,7 +375,7 @@ object MainProxies {
369375
case tree => super.transform(tree)
370376
}
371377
val annots = mainFun.annotations
372-
.filterNot(_.matches(defn.MainAnnot))
378+
.filterNot(_.matches(defn.MainAnnotationClass))
373379
.map(annot => insertTypeSplices.transform(annot.tree))
374380
val mainMeth = DefDef(nme.main, (mainArg :: Nil) :: Nil, TypeTree(defn.UnitType), body)
375381
.withFlags(JavaStatic)

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -849,16 +849,16 @@ class Definitions {
849849

850850
@tu lazy val XMLTopScopeModule: Symbol = requiredModule("scala.xml.TopScope")
851851

852-
@tu lazy val MainAnnot: ClassSymbol = requiredClass("scala.annotation.MainAnnotation")
853-
@tu lazy val MainAnnot_command: Symbol = MainAnnot.requiredMethod("command")
854-
@tu lazy val MainAnnotParameterInfos: ClassSymbol = requiredClass("scala.annotation.MainAnnotation.ParameterInfos")
855-
@tu lazy val MainAnnotationParameterInfos_withDocumentation: Symbol = MainAnnotParameterInfos.requiredMethod("withDocumentation")
856-
@tu lazy val MainAnnotationParameterInfos_withAnnotations: Symbol = MainAnnotParameterInfos.requiredMethod("withAnnotations")
857-
@tu lazy val MainAnnotParameterAnnotation: ClassSymbol = requiredClass("scala.annotation.MainAnnotation.ParameterAnnotation")
858-
@tu lazy val MainAnnotCommand: ClassSymbol = requiredClass("scala.annotation.MainAnnotation.Command")
859-
@tu lazy val MainAnnotCommand_argGetter: Symbol = MainAnnotCommand.requiredMethod("argGetter")
860-
@tu lazy val MainAnnotCommand_varargGetter: Symbol = MainAnnotCommand.requiredMethod("varargGetter")
861-
@tu lazy val MainAnnotCommand_run: Symbol = MainAnnotCommand.requiredMethod("run")
852+
@tu lazy val MainAnnotationClass: ClassSymbol = requiredClass("scala.annotation.MainAnnotation")
853+
@tu lazy val MainAnnotation_command: Symbol = MainAnnotationClass.requiredMethod("command")
854+
@tu lazy val MainAnnotationParameterInfos: ClassSymbol = requiredClass("scala.annotation.MainAnnotation.ParameterInfos")
855+
@tu lazy val MainAnnotationParameterInfos_withDocumentation: Symbol = MainAnnotationParameterInfos.requiredMethod("withDocumentation")
856+
@tu lazy val MainAnnotationParameterInfos_withAnnotations: Symbol = MainAnnotationParameterInfos.requiredMethod("withAnnotations")
857+
@tu lazy val MainAnnotationParameterAnnotation: ClassSymbol = requiredClass("scala.annotation.MainAnnotation.ParameterAnnotation")
858+
@tu lazy val MainAnnotationCommand: ClassSymbol = requiredClass("scala.annotation.MainAnnotation.Command")
859+
@tu lazy val MainAnnotationCommand_argGetter: Symbol = MainAnnotationCommand.requiredMethod("argGetter")
860+
@tu lazy val MainAnnotationCommand_varargGetter: Symbol = MainAnnotationCommand.requiredMethod("varargGetter")
861+
@tu lazy val MainAnnotationCommand_run: Symbol = MainAnnotationCommand.requiredMethod("run")
862862

863863
@tu lazy val CommandLineParserModule: Symbol = requiredModule("scala.util.CommandLineParser")
864864
@tu lazy val CLP_ParseError: ClassSymbol = CommandLineParserModule.requiredClass("ParseError").typeRef.symbol.asClass
@@ -915,6 +915,7 @@ class Definitions {
915915
@tu lazy val InlineParamAnnot: ClassSymbol = requiredClass("scala.annotation.internal.InlineParam")
916916
@tu lazy val ErasedParamAnnot: ClassSymbol = requiredClass("scala.annotation.internal.ErasedParam")
917917
@tu lazy val InvariantBetweenAnnot: ClassSymbol = requiredClass("scala.annotation.internal.InvariantBetween")
918+
@tu lazy val MainAnnot: ClassSymbol = requiredClass("scala.main")
918919
@tu lazy val MigrationAnnot: ClassSymbol = requiredClass("scala.annotation.migration")
919920
@tu lazy val NowarnAnnot: ClassSymbol = requiredClass("scala.annotation.nowarn")
920921
@tu lazy val TransparentTraitAnnot: ClassSymbol = requiredClass("scala.annotation.transparentTrait")

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1348,9 +1348,10 @@ trait Checking {
13481348
/** check that annotation `annot` is applicable to symbol `sym` */
13491349
def checkAnnotApplicable(annot: Tree, sym: Symbol)(using Context): Boolean =
13501350
!ctx.reporter.reportsErrorsFor {
1351+
val annotCls = Annotations.annotClass(annot)
13511352
val concreteAnnot = Annotations.ConcreteAnnotation(annot)
13521353
val pos = annot.srcPos
1353-
if (concreteAnnot.matches(defn.MainAnnot)) {
1354+
if (annotCls == defn.MainAnnot || concreteAnnot.matches(defn.MainAnnotationClass)) {
13541355
if (!sym.isRealMethod)
13551356
report.error(em"main annotation cannot be applied to $sym", pos)
13561357
if (!sym.owner.is(Module) || !sym.owner.isStatic)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,7 +1227,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
12271227
if pos < mtpe.paramInfos.length then
12281228
mtpe.paramInfos(pos)
12291229
// This works only if vararg annotations match up.
1230-
// See neg/i14367.scala for an example where the inferred type is mispredicted.
1230+
// See neg/i14367.scala for an example where the inferred type is mispredicted.
12311231
// Nevertheless, the alternative would be to give up completely, so this is
12321232
// defensible.
12331233
else NoType
@@ -2581,7 +2581,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
25812581
pkg.moduleClass.info.decls.lookup(topLevelClassName).ensureCompleted()
25822582
var stats1 = typedStats(tree.stats, pkg.moduleClass)._1
25832583
if (!ctx.isAfterTyper)
2584-
stats1 = stats1 ++ typedBlockStats(MainProxies.mainProxies(stats1))._1
2584+
stats1 = stats1 ++ typedBlockStats(MainProxies.proxies(stats1))._1
25852585
cpy.PackageDef(tree)(pid1, stats1).withType(pkg.termRef)
25862586
}
25872587
case _ =>

library/src/scala/annotation/MainAnnotation.scala

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package scala.annotation
1010
* or `command.argsGetter` if is a final varargs parameter,
1111
* - a call to `command.run` with the closure of user-main applied to all arguments.
1212
*/
13+
@experimental
1314
trait MainAnnotation extends StaticAnnotation:
1415

1516
/** The class used for argument string parsing. E.g. `scala.util.CommandLineParser.FromString`,
@@ -24,8 +25,9 @@ trait MainAnnotation extends StaticAnnotation:
2425
def command(args: Array[String], commandName: String, documentation: String, parameterInfoss: MainAnnotation.ParameterInfos*): MainAnnotation.Command[ArgumentParser, MainResultType]
2526
end MainAnnotation
2627

28+
@experimental
2729
object MainAnnotation:
28-
// Inspired by https://github.com/scala-js/scala-js/blob/0708917912938714d52be1426364f78a3d1fd269/linker-interface/shared/src/main/scala/org/scalajs/linker/interface/StandardConfig.scala#L23-L218
30+
2931
final class ParameterInfos private (
3032
/** The name of the parameter */
3133
val name: String,
@@ -36,13 +38,15 @@ object MainAnnotation:
3638
/** The ParameterAnnotations associated with the parameter. Defaults to Seq.empty. */
3739
val annotations: Seq[ParameterAnnotation],
3840
) {
39-
// Main public constructor
41+
/** ParameterInfos with a name and the type of the parameter */
4042
def this(name: String, typeName: String) =
4143
this(name, typeName, None, Seq.empty)
4244

45+
/** Copy this ParameterInfos and sets the documentation */
4346
def withDocumentation(doc: String): ParameterInfos =
4447
new ParameterInfos(name, typeName, Some(doc), annotations)
4548

49+
/** Copy this ParameterInfos and sets the annotations */
4650
def withAnnotations(annots: ParameterAnnotation*): ParameterInfos =
4751
new ParameterInfos(name, typeName, documentation, annots)
4852

@@ -64,6 +68,6 @@ object MainAnnotation:
6468
def run(program: => MainResultType): Unit
6569
end Command
6670

67-
/** An annotation for the parameters of a MainAnnotated method. */
71+
/** Marker trait for annotations that will be included in the ParameterInfos annotations. */
6872
trait ParameterAnnotation extends StaticAnnotation
6973
end MainAnnotation

0 commit comments

Comments
 (0)