@@ -4238,7 +4238,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
42384238 }
42394239
42404240 /** Convert constructor proxy reference to a new expression */
4241- def newExpr =
4241+ def newExpr ( ctorResultType : Type ) =
42424242 val qual = qualifier(tree)
42434243 val tpt = qual match
42444244 case Ident (name) =>
@@ -4249,17 +4249,22 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
42494249 cpy.Ident (qual)(qual.symbol.name.sourceModuleName.toTypeName)
42504250 case _ =>
42514251 errorTree(tree, em " cannot convert from $tree to an instance creation expression " )
4252- val tycon = tree.tpe.widen.finalResultType .underlyingClassRef(refinementOK = false )
4252+ val tycon = ctorResultType .underlyingClassRef(refinementOK = false )
42534253 typed(
42544254 untpd.Select (
42554255 untpd.New (untpd.TypedSplice (tpt.withType(tycon))),
42564256 nme.CONSTRUCTOR ),
42574257 pt)
42584258 .showing(i " convert creator $tree -> $result" , typr)
42594259
4260- def isApplyProxy (tree : Tree ) = tree match
4261- case Select (_, nme.apply) => tree.symbol.isAllOf(ApplyProxyFlags )
4262- case _ => false
4260+ def applyProxy (tree : Tree ) = tree match
4261+ case Select (_, nme.apply) =>
4262+ tree.denot.altsWith(_.isAllOf(ApplyProxyFlags )) match
4263+ case denot :: _ =>
4264+ // any of the constructors will do, in order to get the result type, so using the first one
4265+ denot.info.widen.finalResultType
4266+ case _ => NoType
4267+ case _ => NoType
42634268
42644269 tree match {
42654270 case _ : MemberDef | _ : PackageDef | _ : Import | _ : WithoutTypeOrPos [? ] | _ : Closure => tree
@@ -4273,7 +4278,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
42734278 if needsTupledDual(ref, pt) && Feature .autoTuplingEnabled =>
42744279 adapt(tree, pt.tupledDual, locked)
42754280 case _ =>
4276- adaptOverloaded(ref)
4281+ val ctorResultType = applyProxy(tree)
4282+ if ctorResultType.exists then newExpr(ctorResultType)
4283+ else adaptOverloaded(ref)
42774284 }
42784285 case poly : PolyType
42794286 if ! (ctx.mode is Mode .Type ) && dummyTreeOfType.unapply(tree).isEmpty =>
@@ -4282,7 +4289,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
42824289 // Test case was but i18695.scala, but it got fixed by a different tweak in #18719.
42834290 // We leave test for this condition in as a defensive measure in case
42844291 // it arises somewhere else.
4285- if isApplyProxy(tree) then newExpr
4292+ val ctorResultType = applyProxy(tree)
4293+ if ctorResultType.exists then newExpr(ctorResultType)
42864294 else if pt.isInstanceOf [PolyProto ] then tree
42874295 else
42884296 var typeArgs = tree match
@@ -4296,7 +4304,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
42964304 readaptSimplified(handleStructural(tree))
42974305 else pt match {
42984306 case pt : FunProto =>
4299- if isApplyProxy(tree) then newExpr
4307+ val ctorResultType = applyProxy(tree)
4308+ if ctorResultType.exists then newExpr(ctorResultType)
43004309 else adaptToArgs(wtp, pt)
43014310 case pt : PolyProto if ! wtp.isImplicitMethod =>
43024311 tryInsertApplyOrImplicit(tree, pt, locked)(tree) // error will be reported in typedTypeApply
0 commit comments