Skip to content

Commit 8c28293

Browse files
committed
Implement checking for illegal parent trait constructor calls.
A parent trait may not be parameterized (as in T()) if the calling class does not directly implement that trait.
1 parent 065a0b4 commit 8c28293

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

src/dotty/tools/dotc/typer/Checking.scala

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import annotation.unchecked
2020
import util.Positions._
2121
import util.{Stats, SimpleMap}
2222
import util.common._
23+
import transform.SymUtils._
2324
import Decorators._
2425
import Uniques._
2526
import ErrorReporting.{err, errorType, DiagnosticString}
@@ -328,9 +329,15 @@ trait Checking {
328329
}
329330
}
330331

331-
def checkInstantiatable(cls: ClassSymbol, pos: Position): Unit = {
332-
??? // to be done in later phase: check that class `cls` is legal in a new.
333-
}
332+
def checkParentCall(call: Tree, caller: ClassSymbol)(implicit ctx: Context) =
333+
if (!ctx.isAfterTyper) {
334+
val called = call.tpe.classSymbol
335+
if (caller is Trait)
336+
ctx.error(i"$caller may not call constructor of $called", call.pos)
337+
else if (called.is(Trait) && !caller.mixins.contains(called))
338+
ctx.error(i"""$called is already implemented by super${caller.superClass},
339+
|its constructor cannot be called again""".stripMargin, call.pos)
340+
}
334341
}
335342

336343
trait NoChecking extends Checking {
@@ -343,4 +350,5 @@ trait NoChecking extends Checking {
343350
override def checkImplicitParamsNotSingletons(vparamss: List[List[ValDef]])(implicit ctx: Context): Unit = ()
344351
override def checkFeasible(tp: Type, pos: Position, where: => String = "")(implicit ctx: Context): Type = tp
345352
override def checkNoDoubleDefs(cls: Symbol)(implicit ctx: Context): Unit = ()
353+
override def checkParentCall(call: Tree, caller: ClassSymbol)(implicit ctx: Context) = ()
346354
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -911,8 +911,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
911911
if (tree.isType) typedType(tree)(superCtx)
912912
else {
913913
val result = typedExpr(tree)(superCtx)
914-
if ((cls is Trait) && result.tpe.classSymbol.isRealClass && !ctx.isAfterTyper)
915-
ctx.error(s"trait may not call constructor of ${result.tpe.classSymbol}", tree.pos)
914+
checkParentCall(result, cls)
916915
result
917916
}
918917

test/dotc/tests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ class tests extends CompilerTest {
138138
@Test def neg_instantiateAbstract = compileFile(negDir, "instantiateAbstract", xerrors = 8)
139139
@Test def neg_selfInheritance = compileFile(negDir, "selfInheritance", xerrors = 5)
140140
@Test def neg_shadowedImplicits = compileFile(negDir, "arrayclone-new", xerrors = 2)
141+
@Test def neg_traitParamsTyper = compileFile(negDir, "traitParamsTyper", xerrors = 5)
141142

142143
@Test def run_all = runFiles(runDir)
143144

tests/neg/traitParamsTyper.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
trait T(x: Int) {
2+
def f = x
3+
}
4+
5+
class C(x: Int) extends T() // error
6+
7+
trait U extends C with T
8+
9+
trait V extends C(1) with T(2) // two errors
10+
11+
trait W extends T(3) // error
12+
13+
14+
class E extends T(0)
15+
class F extends E with T(1) // error
16+

0 commit comments

Comments
 (0)