Skip to content

Commit c0864da

Browse files
dwijnandWojciechMazur
authored andcommitted
Make apply proxies work with overloaded ctors
[Cherry-picked 72fa043]
1 parent 0122dc7 commit c0864da

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
@@ -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

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)