Skip to content

Commit 8baa5de

Browse files
oderskyDarkDimius
authored andcommitted
Fixing tpd.ClassDef.
The superclass comnstructor of a ClassDef is supposed to be a constructor call. The fix ensures this is the case when creating classes with tpd.ClassDef.
1 parent 9a25199 commit 8baa5de

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/dotty/tools/dotc/ast/tpd.scala

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,24 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
210210
def TypeDef(sym: TypeSymbol)(implicit ctx: Context): TypeDef =
211211
ta.assignType(untpd.TypeDef(Modifiers(sym), sym.name, TypeTree(sym.info)), sym)
212212

213-
def ClassDef(cls: ClassSymbol, constr: DefDef, body: List[Tree])(implicit ctx: Context): TypeDef = {
214-
val parents = cls.info.parents map (TypeTree(_))
213+
def ClassDef(cls: ClassSymbol, constr: DefDef, body: List[Tree], superArgs: List[Tree] = Nil)(implicit ctx: Context): TypeDef = {
214+
val firstParent :: otherParents = cls.info.parents
215+
val superRef =
216+
if (cls is Trait) TypeTree(firstParent)
217+
else {
218+
def isApplicable(ctpe: Type): Boolean = ctpe match {
219+
case ctpe: PolyType =>
220+
isApplicable(ctpe.instantiate(firstParent.argTypes))
221+
case ctpe: MethodType =>
222+
(superArgs corresponds ctpe.paramTypes)(_.tpe <:< _)
223+
case _ =>
224+
false
225+
}
226+
val constr = firstParent.decl(nme.CONSTRUCTOR).suchThat(constr => isApplicable(constr.info))
227+
New(firstParent, constr.symbol.asTerm, superArgs)
228+
}
229+
val parents = superRef :: otherParents.map(TypeTree(_))
230+
215231
val selfType =
216232
if (cls.classInfo.selfInfo ne NoType) ValDef(ctx.newSelfSym(cls))
217233
else EmptyValDef
@@ -260,10 +276,13 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
260276

261277
// ------ Creating typed equivalents of trees that exist only in untyped form -------
262278

263-
/** new C(args) */
264-
def New(tp: Type, args: List[Tree])(implicit ctx: Context): Apply = {
279+
/** new C(args), calling the primary constructor of C */
280+
def New(tp: Type, args: List[Tree])(implicit ctx: Context): Apply =
281+
New(tp, tp.typeSymbol.primaryConstructor.asTerm, args)
282+
283+
/** new C(args), calling given constructor `constr` of C */
284+
def New(tp: Type, constr: TermSymbol, args: List[Tree])(implicit ctx: Context): Apply = {
265285
val targs = tp.argTypes
266-
val constr = tp.typeSymbol.primaryConstructor.asTerm
267286
Apply(
268287
Select(
269288
New(tp withoutArgs targs),

0 commit comments

Comments
 (0)