Skip to content

Commit a142ceb

Browse files
Use default values' symbols instead of trees
- This avoids having to somehow retype the value
1 parent c79729e commit a142ceb

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,31 +46,30 @@ import Annotations.Annotation
4646
* }
4747
*/
4848
object MainProxies {
49-
private type DefaultValues = Map[Int, Tree[_]]
49+
private type DefaultValueSymbols = Map[Int, Symbol]
5050
private type ParameterAnnotations = Vector[Option[Annotation]]
5151

5252
def mainProxies(stats: List[tpd.Tree])(using Context): List[untpd.Tree] = {
5353
import tpd._
5454

5555
/**
56-
* Compute the default values of the function. Since they cannot be infered anymore at this point
57-
* of the compilation, they must be explicitely passed by [[mainProxy]].
56+
* Computes the symbols of the default values of the function. Since they cannot be infered anymore at this
57+
* point of the compilation, they must be explicitely passed by [[mainProxy]].
5858
*/
59-
def defaultValues(scope: Tree, funSymbol: Symbol): DefaultValues =
59+
def defaultValueSymbols(scope: Tree, funSymbol: Symbol): DefaultValueSymbols =
6060
scope match {
6161
case TypeDef(_, template: Template) =>
6262
template.body.flatMap((_: Tree) match {
6363
case dd @ DefDef(name, _, _, _) if name.is(DefaultGetterName) && name.firstPart == funSymbol.name =>
6464
val index: Int = name.toString.split("\\$").last.toInt - 1 // FIXME please!!
65-
val valueTree = dd.rhs
66-
List(index -> valueTree)
65+
List(index -> dd.symbol)
6766
case _ => List()
6867
}).toMap
69-
case _ => Map[Int, Tree]()
68+
case _ => Map.empty
7069
}
7170

7271
/** Computes the list of main methods present in the code. */
73-
def mainMethods(scope: Tree, stats: List[Tree]): List[(Symbol, ParameterAnnotations, DefaultValues, Option[Comment])] = stats.flatMap {
72+
def mainMethods(scope: Tree, stats: List[Tree]): List[(Symbol, ParameterAnnotations, DefaultValueSymbols, Option[Comment])] = stats.flatMap {
7473
case stat: DefDef =>
7574
val sym = stat.symbol
7675
sym.annotations.filter(_.matches(defn.MainAnnot)) match {
@@ -84,7 +83,7 @@ object MainProxies {
8483
case paramAnnot :: others => report.error(s"parameters cannot have multiple annotations", paramAnnot.tree); None
8584
}
8685
))
87-
(sym, paramAnnotations.toVector, defaultValues(scope, sym), stat.rawComment) :: Nil
86+
(sym, paramAnnotations.toVector, defaultValueSymbols(scope, sym), stat.rawComment) :: Nil
8887
case mainAnnot :: others =>
8988
report.error(s"method cannot have multiple main annotations", mainAnnot.tree)
9089
Nil
@@ -100,7 +99,7 @@ object MainProxies {
10099
}
101100

102101
import untpd._
103-
def mainProxy(mainFun: Symbol, paramAnnotations: ParameterAnnotations, defaultValues: DefaultValues, docComment: Option[Comment])(using Context): List[TypeDef] = {
102+
def mainProxy(mainFun: Symbol, paramAnnotations: ParameterAnnotations, defaultValueSymbols: DefaultValueSymbols, docComment: Option[Comment])(using Context): List[TypeDef] = {
104103
val mainAnnot = mainFun.getAnnotation(defn.MainAnnot).get
105104
def pos = mainFun.sourcePos
106105
val mainArgsName: TermName = nme.args
@@ -124,7 +123,6 @@ object MainProxies {
124123
val argName = mainArgsName ++ n.toString
125124

126125
val isRepeated = formal.isRepeatedParam
127-
val defaultValue = defaultValues.get(n)
128126

129127
var argRef: Tree = Apply(Ident(argName), Nil)
130128
var formalType = formal
@@ -157,8 +155,8 @@ object MainProxies {
157155
* ("documentation", some(lit("my param x")))
158156
*/
159157
var assignations: List[(String, Tree)] = Nil
160-
for (dv <- defaultValue)
161-
assignations = ("defaultValue" -> some(dv)) :: assignations
158+
for (dvSym <- defaultValueSymbols.get(n))
159+
assignations = ("defaultValue" -> some(ref(dvSym.termRef))) :: assignations
162160
for (annot <- paramAnnotations(n))
163161
assignations = ("annotation" -> some(instanciateAnnotation(annot))) :: assignations
164162
for (doc <- documentation.argDocs.get(param))

tests/run/main-annotation-homemade-parser-4.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ given [T : FromString]: FromString[Either[T, String]] with
2222

2323
object myProgram:
2424

25-
@main def getOption(o: Option[Int] = Some[Int](42)) = println(o)
25+
@main def getOption(o: Option[Int] = Some(42)) = println(o)
2626

27-
@main def getEither(e: Either[Int, String] = Right[Int, String]("No argument given")) = println(e)
27+
@main def getEither(e: Either[Int, String] = Right("No argument given")) = println(e)
2828

2929
end myProgram
3030

0 commit comments

Comments
 (0)