Skip to content

Commit 1082eb5

Browse files
committed
Disallow extending a trait twice in the same class
1 parent f89d207 commit 1082eb5

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,9 +1460,13 @@ class Typer extends Namer
14601460
ref
14611461
}
14621462

1463+
val seenParents = mutable.Set[Symbol]()
1464+
14631465
def typedParent(tree: untpd.Tree): Tree = {
14641466
var result = if (tree.isType) typedType(tree)(superCtx) else typedExpr(tree)(superCtx)
14651467
val psym = result.tpe.typeSymbol
1468+
if (seenParents.contains(psym)) ctx.error(i"$psym is extended twice", tree.pos)
1469+
seenParents += psym
14661470
if (tree.isType) {
14671471
if (psym.is(Trait) && !cls.is(Trait) && !cls.superClass.isSubClass(psym))
14681472
result = maybeCall(result, psym, psym.primaryConstructor.info)

tests/neg/i3989e.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
object Test extends App {
2+
trait A[+X](val x: X)
3+
class B extends A(5) with A("hello") // error: A is extended twice
4+
5+
def f(a: A[Int]): Int = a match {
6+
case b: B => b.x
7+
case _ => 0
8+
}
9+
10+
f(new B)
11+
}

0 commit comments

Comments
 (0)