Skip to content

Commit 7d58e8c

Browse files
committed
Simplify ParamInfo.documentation
1 parent eeb3aa5 commit 7d58e8c

File tree

4 files changed

+14
-27
lines changed

4 files changed

+14
-27
lines changed

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -243,23 +243,14 @@ object MainProxies {
243243
val paramTypeStr = formal.dealias.typeSymbol.owner.showFullName + "." + paramType.show
244244
val hasDefault = defaultValueSymbols.contains(idx)
245245
val isRepeated = formal.isRepeatedParam
246+
val paramDoc = documentation.argDocs.getOrElse(param, "")
246247

247-
val constructorArgs = List(paramName.toString, paramTypeStr, hasDefault, isRepeated)
248+
val constructorArgs = List(param, paramTypeStr, hasDefault, isRepeated, paramDoc)
248249
.map(value => Literal(Constant(value)))
249250

250251
var paramInfos =
251252
New(TypeTree(defn.MainAnnotationParameterInfo.typeRef), List(constructorArgs))
252253

253-
/*
254-
* Assignations to be made after the creation of the ParameterInfo.
255-
* For example:
256-
* args0paramInfos.withDocumentation("my param x")
257-
* is represented by the pair
258-
* defn.MainAnnotationParameterInfo_withDocumentation -> List(Literal("my param x")))
259-
*/
260-
for doc <- documentation.argDocs.get(param) do
261-
paramInfos = paramInfos.withProperty(defn.MainAnnotationParameterInfo_withDocumentation, List(Literal(Constant(doc))))
262-
263254
val paramAnnots = paramAnnotations(idx)
264255
if paramAnnots.nonEmpty then
265256
paramInfos = paramInfos.withProperty(defn.MainAnnotationParameterInfo_withAnnotations, paramAnnots.map(instantiateAnnotation).toList)

library/src/scala/annotation/MainAnnotation.scala

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ object MainAnnotation:
3232
final class ParameterInfo private (
3333
paramName: String,
3434
paramTypeName: String,
35-
paramDocumentation: Option[String],
36-
paramAnnotations: Seq[ParameterAnnotation],
3735
paramHasDefault: Boolean,
3836
paramIsVarargs: Boolean,
37+
paramDocumentation: String,
38+
paramAnnotations: Seq[ParameterAnnotation],
3939
) {
4040
/** ParameterInfo with a name, the type of the parameter and if it has a default */
41-
def this(name: String, typeName: String, hasDefault: Boolean, isVarargs: Boolean) =
42-
this(name, typeName, None, Seq.empty, hasDefault, isVarargs)
41+
def this(name: String, typeName: String, hasDefault: Boolean, isVarargs: Boolean, documentation: String) =
42+
this(name, typeName, hasDefault, isVarargs, documentation, Seq.empty)
4343

4444
/** The name of the parameter */
4545
def name: String = paramName
@@ -53,19 +53,15 @@ object MainAnnotation:
5353
/** If this is a varargs parameter. Can only be true if it is the last parameter. */
5454
def isVarargs: Boolean = paramIsVarargs
5555

56-
/** The docstring of the parameter. Defaults to None. */
57-
def documentation: Option[String] = paramDocumentation
56+
/** The docstring of the parameter. */
57+
def documentation: String = paramDocumentation
5858

5959
/** The ParameterAnnotations associated with the parameter. Defaults to Seq.empty. */
6060
def annotations: Seq[ParameterAnnotation] = paramAnnotations
6161

62-
/** Copy this ParameterInfo and sets the documentation */
63-
def withDocumentation(doc: String): ParameterInfo =
64-
new ParameterInfo(paramName, typeName, Some(doc), annotations, paramHasDefault, paramIsVarargs)
65-
6662
/** Copy this ParameterInfo and sets the annotations */
6763
def withAnnotations(annotations: ParameterAnnotation*): ParameterInfo =
68-
new ParameterInfo(paramName, typeName, documentation, annotations, paramHasDefault, paramIsVarargs)
64+
new ParameterInfo(paramName, typeName, paramHasDefault, paramIsVarargs, documentation, annotations)
6965

7066
override def toString: String = s"$name: $typeName"
7167
}

tests/run/main-annotation-homemade-annot-6.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ command(
33
foo,
44
"Foo docs",
55
Seq(
6-
ParameterInfo(name="i", typeName="scala.Int", hasDefault=false, isVarargs=false, documentation=None, annotations=List()),
7-
ParameterInfo(name="j", typeName="java.lang.String", hasDefault=true, isVarargs=false, documentation=None, annotations=List())
6+
ParameterInfo(name="i", typeName="scala.Int", hasDefault=false, isVarargs=false, documentation="", annotations=List()),
7+
ParameterInfo(name="j", typeName="java.lang.String", hasDefault=true, isVarargs=false, documentation="", annotations=List())
88
)*
99
)
1010
argGetter(0, None)
@@ -17,8 +17,8 @@ command(
1717
bar,
1818
"Bar docs",
1919
Seq(
20-
ParameterInfo(name="i", typeName="scala.collection.immutable.List[Int]", hasDefault=false, isVarargs=false, documentation=Some("the first parameter"), annotations=ArraySeq(MyParamAnnot(3))),
21-
ParameterInfo(name="rest", typeName="scala.Int", hasDefault=false, isVarargs=true, documentation=None, annotations=List())
20+
ParameterInfo(name="i", typeName="scala.collection.immutable.List[Int]", hasDefault=false, isVarargs=false, documentation="the first parameter", annotations=ArraySeq(MyParamAnnot(3))),
21+
ParameterInfo(name="rest", typeName="scala.Int", hasDefault=false, isVarargs=true, documentation="", annotations=List())
2222
)*
2323
)
2424
argGetter(0, None)

tests/run/main-annotation-homemade-annot-6.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class myMain extends MainAnnotation:
2626
override def command(args: Array[String], commandName: String, docs: String, parameterInfos: ParameterInfo*) =
2727
def paramInfoString(paramInfo: ParameterInfo) =
2828
import paramInfo.*
29-
s" ParameterInfo(name=\"$name\", typeName=\"$typeName\", hasDefault=$hasDefault, isVarargs=$isVarargs, documentation=${documentation.map("\"" + _ + "\"")}, annotations=$annotations)"
29+
s" ParameterInfo(name=\"$name\", typeName=\"$typeName\", hasDefault=$hasDefault, isVarargs=$isVarargs, documentation=\"$documentation\", annotations=$annotations)"
3030
println(
3131
s"""command(
3232
| ${args.mkString("Array(", ", ", ")")},

0 commit comments

Comments
 (0)