@@ -4237,8 +4237,12 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
4237
4237
case _ =>
4238
4238
}
4239
4239
4240
- /** Convert constructor proxy reference to a new expression */
4241
- def newExpr =
4240
+ /** If `tree` is a constructor proxy reference, convert it to a `new` expression,
4241
+ * otherwise return EmptyTree.
4242
+ */
4243
+ def newExpr (tree : Tree ): Tree =
4244
+ val ctorResultType = applyProxyResultType(tree)
4245
+ if ! ctorResultType.exists then return EmptyTree
4242
4246
val qual = qualifier(tree)
4243
4247
val tpt = qual match
4244
4248
case Ident (name) =>
@@ -4249,17 +4253,27 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
4249
4253
cpy.Ident (qual)(qual.symbol.name.sourceModuleName.toTypeName)
4250
4254
case _ =>
4251
4255
errorTree(tree, em " cannot convert from $tree to an instance creation expression " )
4252
- val tycon = tree.tpe.widen.finalResultType .underlyingClassRef(refinementOK = false )
4256
+ val tycon = ctorResultType .underlyingClassRef(refinementOK = false )
4253
4257
typed(
4254
4258
untpd.Select (
4255
4259
untpd.New (untpd.TypedSplice (tpt.withType(tycon))),
4256
4260
nme.CONSTRUCTOR ),
4257
4261
pt)
4258
4262
.showing(i " convert creator $tree -> $result" , typr)
4259
4263
4260
- def isApplyProxy (tree : Tree ) = tree match
4261
- case Select (_, nme.apply) => tree.symbol.isAllOf(ApplyProxyFlags )
4262
- case _ => false
4264
+ /** If `tree` is a constructor proxy reference, return the type it constructs,
4265
+ * otherwise return NoType.
4266
+ */
4267
+ def applyProxyResultType (tree : Tree ): Type = tree match
4268
+ case Select (_, nme.apply) =>
4269
+ // can't use tree.symbol and tree.tpe.widen.finalResultType, because when overloaded
4270
+ // tree.symbol is NoSymbol (via MultiDenotation.symbol) and tree.tpe won't widen.
4271
+ tree.denot.altsWith(_.isAllOf(ApplyProxyFlags )) match
4272
+ case denot :: _ =>
4273
+ // any of the constructors will do, in order to get the result type, so using the first one
4274
+ denot.info.widen.finalResultType
4275
+ case _ => NoType
4276
+ case _ => NoType
4263
4277
4264
4278
tree match {
4265
4279
case _ : MemberDef | _ : PackageDef | _ : Import | _ : WithoutTypeOrPos [? ] | _ : Closure => tree
@@ -4273,7 +4287,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
4273
4287
if needsTupledDual(ref, pt) && Feature .autoTuplingEnabled =>
4274
4288
adapt(tree, pt.tupledDual, locked)
4275
4289
case _ =>
4276
- adaptOverloaded(ref)
4290
+ newExpr(tree).orElse( adaptOverloaded(ref) )
4277
4291
}
4278
4292
case poly : PolyType
4279
4293
if ! (ctx.mode is Mode .Type ) && dummyTreeOfType.unapply(tree).isEmpty =>
@@ -4282,22 +4296,21 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
4282
4296
// Test case was but i18695.scala, but it got fixed by a different tweak in #18719.
4283
4297
// We leave test for this condition in as a defensive measure in case
4284
4298
// it arises somewhere else.
4285
- if isApplyProxy (tree) then newExpr
4286
- else if pt.isInstanceOf [PolyProto ] then tree
4287
- else
4288
- var typeArgs = tree match
4289
- case Select (qual, nme.CONSTRUCTOR ) => qual.tpe.widenDealias.argTypesLo.map(TypeTree (_))
4290
- case _ => Nil
4291
- if typeArgs.isEmpty then typeArgs = constrained(poly, tree)._2.map(_.wrapInTypeTree(tree))
4292
- convertNewGenericArray(readapt(tree.appliedToTypeTrees(typeArgs)))
4299
+ newExpr (tree).orElse :
4300
+ if pt.isInstanceOf [PolyProto ] then tree
4301
+ else
4302
+ var typeArgs = tree match
4303
+ case Select (qual, nme.CONSTRUCTOR ) => qual.tpe.widenDealias.argTypesLo.map(TypeTree (_))
4304
+ case _ => Nil
4305
+ if typeArgs.isEmpty then typeArgs = constrained(poly, tree)._2.map(_.wrapInTypeTree(tree))
4306
+ convertNewGenericArray(readapt(tree.appliedToTypeTrees(typeArgs)))
4293
4307
case wtp =>
4294
4308
val isStructuralCall = wtp.isValueType && isStructuralTermSelectOrApply(tree)
4295
4309
if (isStructuralCall)
4296
4310
readaptSimplified(handleStructural(tree))
4297
4311
else pt match {
4298
4312
case pt : FunProto =>
4299
- if isApplyProxy(tree) then newExpr
4300
- else adaptToArgs(wtp, pt)
4313
+ newExpr(tree).orElse(adaptToArgs(wtp, pt))
4301
4314
case pt : PolyProto if ! wtp.isImplicitMethod =>
4302
4315
tryInsertApplyOrImplicit(tree, pt, locked)(tree) // error will be reported in typedTypeApply
4303
4316
case _ =>
0 commit comments