Skip to content

Commit 5cf22ca

Browse files
authored
Merge pull request #6469 from dotty-staging/fix-#6368
Fix #6368: Check that secondary constructors call a preceding constru…
2 parents 5c2f658 + 16752df commit 5cf22ca

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1557,10 +1557,20 @@ class Typer extends Namer
15571557
PrepareInlineable.registerInlineInfo(sym, _ => rhs1)
15581558
}
15591559

1560-
if (sym.isConstructor && !sym.isPrimaryConstructor)
1560+
if (sym.isConstructor && !sym.isPrimaryConstructor) {
15611561
for (param <- tparams1 ::: vparamss1.flatten)
15621562
checkRefsLegal(param, sym.owner, (name, sym) => sym.is(TypeParam), "secondary constructor")
15631563

1564+
def checkThisConstrCall(tree: Tree): Unit = tree match {
1565+
case app: Apply if untpd.isSelfConstrCall(app) =>
1566+
if (sym.span.exists && app.symbol.span.exists && sym.span.start <= app.symbol.span.start)
1567+
ctx.error("secondary constructor must call a preceding constructor", app.sourcePos)
1568+
case Block(call :: _, _) => checkThisConstrCall(call)
1569+
case _ =>
1570+
}
1571+
checkThisConstrCall(rhs1)
1572+
}
1573+
15641574
assignType(cpy.DefDef(ddef)(name, tparams1, vparamss1, tpt1, rhs1), sym).setDefTree
15651575
//todo: make sure dependent method types do not depend on implicits or by-name params
15661576
}

tests/neg/i6368.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class A(x: Int, y: Int) {
2+
def this() = {
3+
this() // error
4+
}
5+
6+
def this(x: Long) = {
7+
this(x.toInt) // error
8+
}
9+
10+
def this(a: Int) = {
11+
this(a, a) // OK
12+
val b: Int = c
13+
val c = 2
14+
}
15+
}

0 commit comments

Comments
 (0)