Skip to content

Commit 72fa043

Browse files
committed
Make apply proxies work with overloaded ctors
1 parent b2ba6dc commit 72fa043

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4298,7 +4298,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
42984298
}
42994299

43004300
/** Convert constructor proxy reference to a new expression */
4301-
def newExpr =
4301+
def newExpr(ctorResultType: Type) =
43024302
val qual = qualifier(tree)
43034303
val tpt = qual match
43044304
case Ident(name) =>
@@ -4309,17 +4309,22 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
43094309
cpy.Ident(qual)(qual.symbol.name.sourceModuleName.toTypeName)
43104310
case _ =>
43114311
errorTree(tree, em"cannot convert from $tree to an instance creation expression")
4312-
val tycon = tree.tpe.widen.finalResultType.underlyingClassRef(refinementOK = false)
4312+
val tycon = ctorResultType.underlyingClassRef(refinementOK = false)
43134313
typed(
43144314
untpd.Select(
43154315
untpd.New(untpd.TypedSplice(tpt.withType(tycon))),
43164316
nme.CONSTRUCTOR),
43174317
pt)
43184318
.showing(i"convert creator $tree -> $result", typr)
43194319

4320-
def isApplyProxy(tree: Tree) = tree match
4321-
case Select(_, nme.apply) => tree.symbol.isAllOf(ApplyProxyFlags)
4322-
case _ => false
4320+
def applyProxy(tree: Tree) = tree match
4321+
case Select(_, nme.apply) =>
4322+
tree.denot.altsWith(_.isAllOf(ApplyProxyFlags)) match
4323+
case denot :: _ =>
4324+
// any of the constructors will do, in order to get the result type, so using the first one
4325+
denot.info.widen.finalResultType
4326+
case _ => NoType
4327+
case _ => NoType
43234328

43244329
tree match {
43254330
case _: MemberDef | _: PackageDef | _: Import | _: WithoutTypeOrPos[?] | _: Closure => tree
@@ -4333,7 +4338,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
43334338
if needsTupledDual(ref, pt) && Feature.autoTuplingEnabled =>
43344339
adapt(tree, pt.tupledDual, locked)
43354340
case _ =>
4336-
adaptOverloaded(ref)
4341+
val ctorResultType = applyProxy(tree)
4342+
if ctorResultType.exists then newExpr(ctorResultType)
4343+
else adaptOverloaded(ref)
43374344
}
43384345
case poly: PolyType
43394346
if !(ctx.mode is Mode.Type) && dummyTreeOfType.unapply(tree).isEmpty =>
@@ -4342,7 +4349,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
43424349
// Test case was but i18695.scala, but it got fixed by a different tweak in #18719.
43434350
// We leave test for this condition in as a defensive measure in case
43444351
// it arises somewhere else.
4345-
if isApplyProxy(tree) then newExpr
4352+
val ctorResultType = applyProxy(tree)
4353+
if ctorResultType.exists then newExpr(ctorResultType)
43464354
else if pt.isInstanceOf[PolyProto] then tree
43474355
else
43484356
var typeArgs = tree match
@@ -4356,7 +4364,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
43564364
readaptSimplified(handleStructural(tree))
43574365
else pt match {
43584366
case pt: FunProto =>
4359-
if isApplyProxy(tree) then newExpr
4367+
val ctorResultType = applyProxy(tree)
4368+
if ctorResultType.exists then newExpr(ctorResultType)
43604369
else adaptToArgs(wtp, pt)
43614370
case pt: PolyProto if !wtp.isImplicitMethod =>
43624371
tryInsertApplyOrImplicit(tree, pt, locked)(tree) // error will be reported in typedTypeApply

tests/pos/i19201.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Person(
2+
val firstName: String,
3+
val lastName: String,
4+
val birthYear: Int = -1,
5+
val address: String = ""
6+
):
7+
// Works if remove this constructor
8+
def this() = this("John", "Doe")
9+
10+
class Test:
11+
def p1 = Person("First", "Last") // was: Type Error: none of the overloads.. match arguments
12+
def p2 = Person("Josh", "Joe", 1912, "Main Street")

0 commit comments

Comments
 (0)