@@ -315,24 +315,41 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
315
315
def TypeDef (sym : TypeSymbol )(using Context ): TypeDef =
316
316
ta.assignType(untpd.TypeDef (sym.name, TypeTree (sym.info)), sym)
317
317
318
- def ClassDef (cls : ClassSymbol , constr : DefDef , body : List [Tree ], superArgs : List [Tree ] = Nil )(using Context ): TypeDef = {
318
+ /** Create a class definition
319
+ * @param cls the class symbol of the created class
320
+ * @param constr its primary constructor
321
+ * @param body the statements in its template
322
+ * @param superArgs the arguments to pass to the superclass constructor
323
+ * @param adaptVarargs if true, allow matching a vararg superclass constructor
324
+ * with a missing argument in superArgs, and synthesize an
325
+ * empty repeated parameter in the supercall in this case
326
+ */
327
+ def ClassDef (cls : ClassSymbol , constr : DefDef , body : List [Tree ],
328
+ superArgs : List [Tree ] = Nil , adaptVarargs : Boolean = false )(using Context ): TypeDef =
319
329
val firstParent :: otherParents = cls.info.parents: @ unchecked
330
+
331
+ def adaptedSuperArgs (ctpe : Type ): List [Tree ] = ctpe match
332
+ case ctpe : PolyType =>
333
+ adaptedSuperArgs(ctpe.instantiate(firstParent.argTypes))
334
+ case ctpe : MethodType
335
+ if ctpe.paramInfos.length == superArgs.length + 1 =>
336
+ // last argument must be a vararg, otherwise isApplicable would have failed
337
+ superArgs :+
338
+ repeated(Nil , TypeTree (ctpe.paramInfos.last.argInfos.head, inferred = true ))
339
+ case _ =>
340
+ superArgs
341
+
320
342
val superRef =
321
- if (cls.is(Trait )) TypeTree (firstParent)
322
- else {
323
- def isApplicable (ctpe : Type ): Boolean = ctpe match {
324
- case ctpe : PolyType =>
325
- isApplicable(ctpe.instantiate(firstParent.argTypes))
326
- case ctpe : MethodType =>
327
- (superArgs corresponds ctpe.paramInfos)(_.tpe <:< _)
328
- case _ =>
329
- false
330
- }
331
- val constr = firstParent.decl(nme.CONSTRUCTOR ).suchThat(constr => isApplicable(constr.info))
332
- New (firstParent, constr.symbol.asTerm, superArgs)
333
- }
343
+ if cls.is(Trait ) then TypeTree (firstParent)
344
+ else
345
+ val parentConstr = firstParent.applicableConstructors(superArgs.tpes, adaptVarargs) match
346
+ case Nil => assert(false , i " no applicable parent constructor of $firstParent for supercall arguments $superArgs" )
347
+ case constr :: Nil => constr
348
+ case _ => assert(false , i " multiple applicable parent constructors of $firstParent for supercall arguments $superArgs" )
349
+ New (firstParent, parentConstr.asTerm, adaptedSuperArgs(parentConstr.info))
350
+
334
351
ClassDefWithParents (cls, constr, superRef :: otherParents.map(TypeTree (_)), body)
335
- }
352
+ end ClassDef
336
353
337
354
def ClassDefWithParents (cls : ClassSymbol , constr : DefDef , parents : List [Tree ], body : List [Tree ])(using Context ): TypeDef = {
338
355
val selfType =
@@ -359,13 +376,18 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
359
376
* @param parents a non-empty list of class types
360
377
* @param termForwarders a non-empty list of forwarding definitions specified by their name and the definition they forward to.
361
378
* @param typeMembers a possibly-empty list of type members specified by their name and their right hand side.
379
+ * @param adaptVarargs if true, allow matching a vararg superclass constructor
380
+ * with a missing argument in superArgs, and synthesize an
381
+ * empty repeated parameter in the supercall in this case
362
382
*
363
383
* The class has the same owner as the first function in `termForwarders`.
364
384
* Its position is the union of all symbols in `termForwarders`.
365
385
*/
366
- def AnonClass (parents : List [Type ], termForwarders : List [(TermName , TermSymbol )],
367
- typeMembers : List [(TypeName , TypeBounds )] = Nil )(using Context ): Block = {
368
- AnonClass (termForwarders.head._2.owner, parents, termForwarders.map(_._2.span).reduceLeft(_ union _)) { cls =>
386
+ def AnonClass (parents : List [Type ],
387
+ termForwarders : List [(TermName , TermSymbol )],
388
+ typeMembers : List [(TypeName , TypeBounds )],
389
+ adaptVarargs : Boolean )(using Context ): Block = {
390
+ AnonClass (termForwarders.head._2.owner, parents, termForwarders.map(_._2.span).reduceLeft(_ union _), adaptVarargs) { cls =>
369
391
def forwarder (name : TermName , fn : TermSymbol ) = {
370
392
val fwdMeth = fn.copy(cls, name, Synthetic | Method | Final ).entered.asTerm
371
393
for overridden <- fwdMeth.allOverriddenSymbols do
@@ -385,6 +407,9 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
385
407
* with the specified owner and position.
386
408
*/
387
409
def AnonClass (owner : Symbol , parents : List [Type ], coord : Coord )(body : ClassSymbol => List [Tree ])(using Context ): Block =
410
+ AnonClass (owner, parents, coord, adaptVarargs = false )(body)
411
+
412
+ private def AnonClass (owner : Symbol , parents : List [Type ], coord : Coord , adaptVarargs : Boolean )(body : ClassSymbol => List [Tree ])(using Context ): Block =
388
413
val parents1 =
389
414
if (parents.head.classSymbol.is(Trait )) {
390
415
val head = parents.head.parents.head
@@ -393,7 +418,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
393
418
else parents
394
419
val cls = newNormalizedClassSymbol(owner, tpnme.ANON_CLASS , Synthetic | Final , parents1, coord = coord)
395
420
val constr = newConstructor(cls, Synthetic , Nil , Nil ).entered
396
- val cdef = ClassDef (cls, DefDef (constr), body(cls))
421
+ val cdef = ClassDef (cls, DefDef (constr), body(cls), Nil , adaptVarargs )
397
422
Block (cdef :: Nil , New (cls.typeRef, Nil ))
398
423
399
424
def Import (expr : Tree , selectors : List [untpd.ImportSelector ])(using Context ): Import =
0 commit comments