@@ -4298,8 +4298,12 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
4298
4298
case _ =>
4299
4299
}
4300
4300
4301
- /** Convert constructor proxy reference to a new expression */
4302
- def newExpr =
4301
+ /** If `tree` is a constructor proxy reference, convert it to a `new` expression,
4302
+ * otherwise return EmptyTree.
4303
+ */
4304
+ def newExpr (tree : Tree ): Tree =
4305
+ val ctorResultType = applyProxyResultType(tree)
4306
+ if ! ctorResultType.exists then return EmptyTree
4303
4307
val qual = qualifier(tree)
4304
4308
val tpt = qual match
4305
4309
case Ident (name) =>
@@ -4310,17 +4314,27 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
4310
4314
cpy.Ident (qual)(qual.symbol.name.sourceModuleName.toTypeName)
4311
4315
case _ =>
4312
4316
errorTree(tree, em " cannot convert from $tree to an instance creation expression " )
4313
- val tycon = tree.tpe.widen.finalResultType .underlyingClassRef(refinementOK = false )
4317
+ val tycon = ctorResultType .underlyingClassRef(refinementOK = false )
4314
4318
typed(
4315
4319
untpd.Select (
4316
4320
untpd.New (untpd.TypedSplice (tpt.withType(tycon))),
4317
4321
nme.CONSTRUCTOR ),
4318
4322
pt)
4319
4323
.showing(i " convert creator $tree -> $result" , typr)
4320
4324
4321
- def isApplyProxy (tree : Tree ) = tree match
4322
- case Select (_, nme.apply) => tree.symbol.isAllOf(ApplyProxyFlags )
4323
- case _ => false
4325
+ /** If `tree` is a constructor proxy reference, return the type it constructs,
4326
+ * otherwise return NoType.
4327
+ */
4328
+ def applyProxyResultType (tree : Tree ): Type = tree match
4329
+ case Select (_, nme.apply) =>
4330
+ // can't use tree.symbol and tree.tpe.widen.finalResultType, because when overloaded
4331
+ // tree.symbol is NoSymbol (via MultiDenotation.symbol) and tree.tpe won't widen.
4332
+ tree.denot.altsWith(_.isAllOf(ApplyProxyFlags )) match
4333
+ case denot :: _ =>
4334
+ // any of the constructors will do, in order to get the result type, so using the first one
4335
+ denot.info.widen.finalResultType
4336
+ case _ => NoType
4337
+ case _ => NoType
4324
4338
4325
4339
tree match {
4326
4340
case _ : MemberDef | _ : PackageDef | _ : Import | _ : WithoutTypeOrPos [? ] | _ : Closure => tree
@@ -4334,7 +4348,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
4334
4348
if needsTupledDual(ref, pt) && Feature .autoTuplingEnabled =>
4335
4349
adapt(tree, pt.tupledDual, locked)
4336
4350
case _ =>
4337
- adaptOverloaded(ref)
4351
+ newExpr(tree).orElse( adaptOverloaded(ref) )
4338
4352
}
4339
4353
case poly : PolyType
4340
4354
if ! (ctx.mode is Mode .Type ) && dummyTreeOfType.unapply(tree).isEmpty =>
@@ -4343,22 +4357,21 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
4343
4357
// Test case was but i18695.scala, but it got fixed by a different tweak in #18719.
4344
4358
// We leave test for this condition in as a defensive measure in case
4345
4359
// it arises somewhere else.
4346
- if isApplyProxy (tree) then newExpr
4347
- else if pt.isInstanceOf [PolyProto ] then tree
4348
- else
4349
- var typeArgs = tree match
4350
- case Select (qual, nme.CONSTRUCTOR ) => qual.tpe.widenDealias.argTypesLo.map(TypeTree (_))
4351
- case _ => Nil
4352
- if typeArgs.isEmpty then typeArgs = constrained(poly, tree)._2.map(_.wrapInTypeTree(tree))
4353
- convertNewGenericArray(readapt(tree.appliedToTypeTrees(typeArgs)))
4360
+ newExpr (tree).orElse :
4361
+ if pt.isInstanceOf [PolyProto ] then tree
4362
+ else
4363
+ var typeArgs = tree match
4364
+ case Select (qual, nme.CONSTRUCTOR ) => qual.tpe.widenDealias.argTypesLo.map(TypeTree (_))
4365
+ case _ => Nil
4366
+ if typeArgs.isEmpty then typeArgs = constrained(poly, tree)._2.map(_.wrapInTypeTree(tree))
4367
+ convertNewGenericArray(readapt(tree.appliedToTypeTrees(typeArgs)))
4354
4368
case wtp =>
4355
4369
val isStructuralCall = wtp.isValueType && isStructuralTermSelectOrApply(tree)
4356
4370
if (isStructuralCall)
4357
4371
readaptSimplified(handleStructural(tree))
4358
4372
else pt match {
4359
4373
case pt : FunProto =>
4360
- if isApplyProxy(tree) then newExpr
4361
- else adaptToArgs(wtp, pt)
4374
+ newExpr(tree).orElse(adaptToArgs(wtp, pt))
4362
4375
case pt : PolyProto if ! wtp.isImplicitMethod =>
4363
4376
tryInsertApplyOrImplicit(tree, pt, locked)(tree) // error will be reported in typedTypeApply
4364
4377
case _ =>
0 commit comments