Skip to content

Fix #6368: Check that secondary constructors call a preceding constru… #6469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1557,10 +1557,20 @@ class Typer extends Namer
PrepareInlineable.registerInlineInfo(sym, _ => rhs1)
}

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

def checkThisConstrCall(tree: Tree): Unit = tree match {
case app: Apply if untpd.isSelfConstrCall(app) =>
if (sym.span.exists && app.symbol.span.exists && sym.span.start <= app.symbol.span.start)
ctx.error("secondary constructor must call a preceding constructor", app.sourcePos)
case Block(call :: _, _) => checkThisConstrCall(call)
case _ =>
}
checkThisConstrCall(rhs1)
}

assignType(cpy.DefDef(ddef)(name, tparams1, vparamss1, tpt1, rhs1), sym).setDefTree
//todo: make sure dependent method types do not depend on implicits or by-name params
}
Expand Down
15 changes: 15 additions & 0 deletions tests/neg/i6368.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class A(x: Int, y: Int) {
def this() = {
this() // error
}

def this(x: Long) = {
this(x.toInt) // error
}

def this(a: Int) = {
this(a, a) // OK
val b: Int = c
val c = 2
}
}