Skip to content
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

Don't re-balance AndTypes arising from supertypes #20400

Merged
merged 1 commit into from
May 26, 2024
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
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ object TypeOps:
// Mapping over a skolem creates a new skolem which by definition won't
// be =:= to the original one.
tp
case tp: SuperType =>
// Mapping a supertype might re-balance an AndType which is not permitted since
// we need the original order of parents for current super resolution.
tp
case _ =>
mapOver
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ trait TypeAssigner {
else if (ctx.erasedTypes) cls.info.firstParent.typeConstructor
else {
val ps = cls.classInfo.parents
if (ps.isEmpty) defn.AnyType else ps.reduceLeft((x: Type, y: Type) => x & y)
if ps.isEmpty then defn.AnyType else ps.reduceLeft(AndType(_, _))
}
SuperType(cls.thisType, owntype)

Expand Down
15 changes: 15 additions & 0 deletions tests/run/i20284.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Test 1
D
B
C
A
Test 2
D
B
C
A
Test 3
D
B
C
A
54 changes: 54 additions & 0 deletions tests/run/i20284.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
trait A {
def print: Unit = println("A")
}

trait B extends A {
override def print: Unit = {
println("B")
super.print
}
}

trait C extends A {
override def print: Unit = {
println("C")
super.print
}
}

trait D extends B {
override def print: Unit = {
println("D")
super.print
}
}

trait BB extends B

trait X
trait Y
trait Z

class Test1 extends C with B with BB with D with X with Y with Z:
override def print: Unit = {
println("Test 1")
super.print
}

class Test2 extends C with B with BB with D with X with Y {
override def print: Unit = {
println("Test 2")
super.print
}
}

class Test3 extends X with Y with Z with C with B with BB with D {
override def print: Unit = {
println("Test 3")
super.print
}
}
@main def Test =
new Test1().print
new Test2().print
new Test3().print
Loading