Skip to content

Commit 4ab86db

Browse files
committed
Simplify ClassfileParser#addAnnotationConstructor
It turns out that Java annotations cannot have type parameters, so part of this method was dead code.
1 parent 929d4be commit 4ab86db

File tree

1 file changed

+42
-46
lines changed

1 file changed

+42
-46
lines changed

compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@ class ClassfileParser(
166166
for (i <- 0 until in.nextChar) parseMember(method = false)
167167
for (i <- 0 until in.nextChar) parseMember(method = true)
168168
classInfo = parseAttributes(classRoot.symbol, classInfo)
169-
if (isAnnotation) addAnnotationConstructor(classInfo)
169+
if (isAnnotation)
170+
// classInfo must be a TempClassInfoType and not a TempPolyType,
171+
// because Java annotations cannot have type parameters.
172+
addAnnotationConstructor(classInfo.asInstanceOf[TempClassInfoType])
170173

171174
classRoot.registerCompanion(moduleRoot.symbol)
172175
moduleRoot.registerCompanion(classRoot.symbol)
@@ -639,61 +642,54 @@ class ClassfileParser(
639642
* Note that default getters have type Nothing. That's OK because we need
640643
* them only to signal that the corresponding parameter is optional.
641644
*/
642-
def addAnnotationConstructor(classInfo: Type, tparams: List[TypeSymbol] = Nil)(implicit ctx: Context): Unit = {
645+
def addAnnotationConstructor(classInfo: TempClassInfoType)(implicit ctx: Context): Unit = {
643646
def addDefaultGetter(attr: Symbol, n: Int) =
644647
ctx.newSymbol(
645648
owner = moduleRoot.symbol,
646649
name = DefaultGetterName(nme.CONSTRUCTOR, n),
647650
flags = attr.flags & Flags.AccessFlags,
648651
info = defn.NothingType).entered
649652

650-
classInfo match {
651-
case classInfo @ TempPolyType(tparams, restpe) if tparams.isEmpty =>
652-
addAnnotationConstructor(restpe, tparams)
653-
case classInfo: TempClassInfoType =>
654-
val attrs = classInfo.decls.toList.filter(_.isTerm)
655-
val targs = tparams.map(_.typeRef)
656-
val paramNames = attrs.map(_.name.asTermName)
657-
val paramTypes = attrs.map(_.info.resultType)
658-
659-
def addConstr(ptypes: List[Type]) = {
660-
val mtype = MethodType(paramNames, ptypes, classRoot.typeRef.appliedTo(targs))
661-
val constrType = if (tparams.isEmpty) mtype else TempPolyType(tparams, mtype)
662-
val constr = ctx.newSymbol(
663-
owner = classRoot.symbol,
664-
name = nme.CONSTRUCTOR,
665-
flags = Flags.Synthetic | Flags.JavaDefined | Flags.Method,
666-
info = constrType
667-
).entered
668-
for ((attr, i) <- attrs.zipWithIndex)
669-
if (attr.hasAnnotation(defn.AnnotationDefaultAnnot)) {
670-
constr.setFlag(Flags.DefaultParameterized)
671-
addDefaultGetter(attr, i)
672-
}
653+
val attrs = classInfo.decls.toList.filter(_.isTerm)
654+
val paramNames = attrs.map(_.name.asTermName)
655+
val paramTypes = attrs.map(_.info.resultType)
656+
657+
def addConstr(ptypes: List[Type]) = {
658+
val mtype = MethodType(paramNames, ptypes, classRoot.typeRef)
659+
val constr = ctx.newSymbol(
660+
owner = classRoot.symbol,
661+
name = nme.CONSTRUCTOR,
662+
flags = Flags.Synthetic | Flags.JavaDefined | Flags.Method,
663+
info = mtype
664+
).entered
665+
for ((attr, i) <- attrs.zipWithIndex)
666+
if (attr.hasAnnotation(defn.AnnotationDefaultAnnot)) {
667+
constr.setFlag(Flags.DefaultParameterized)
668+
addDefaultGetter(attr, i)
673669
}
670+
}
674671

675-
addConstr(paramTypes)
676-
677-
// The code below added an extra constructor to annotations where the
678-
// last parameter of the constructor is an Array[X] for some X, the
679-
// array was replaced by a vararg argument. Unfortunately this breaks
680-
// inference when doing:
681-
// @Annot(Array())
682-
// The constructor is overloaded so the expected type of `Array()` is
683-
// WildcardType, and the type parameter of the Array apply method gets
684-
// instantiated to `Nothing` instead of `X`.
685-
// I'm leaving this commented out in case we improve inference to make this work.
686-
// Note that if this is reenabled then JavaParser will also need to be modified
687-
// to add the extra constructor (this was not implemented before).
688-
/*
689-
if (paramTypes.nonEmpty)
690-
paramTypes.last match {
691-
case defn.ArrayOf(elemtp) =>
692-
addConstr(paramTypes.init :+ defn.RepeatedParamType.appliedTo(elemtp))
693-
case _ =>
694-
}
695-
*/
672+
addConstr(paramTypes)
673+
674+
// The code below added an extra constructor to annotations where the
675+
// last parameter of the constructor is an Array[X] for some X, the
676+
// array was replaced by a vararg argument. Unfortunately this breaks
677+
// inference when doing:
678+
// @Annot(Array())
679+
// The constructor is overloaded so the expected type of `Array()` is
680+
// WildcardType, and the type parameter of the Array apply method gets
681+
// instantiated to `Nothing` instead of `X`.
682+
// I'm leaving this commented out in case we improve inference to make this work.
683+
// Note that if this is reenabled then JavaParser will also need to be modified
684+
// to add the extra constructor (this was not implemented before).
685+
/*
686+
if (paramTypes.nonEmpty)
687+
paramTypes.last match {
688+
case defn.ArrayOf(elemtp) =>
689+
addConstr(paramTypes.init :+ defn.RepeatedParamType.appliedTo(elemtp))
690+
case _ =>
696691
}
692+
*/
697693
}
698694

699695
/** Enter own inner classes in the right scope. It needs the scopes to be set up,

0 commit comments

Comments
 (0)