Skip to content

Commit 9bb028d

Browse files
Disable distribution of intersection types over applied types
Fixes #23435
1 parent fb66af2 commit 9bb028d

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

compiler/src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2771,7 +2771,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
27712771
* @pre !(tp1 <: tp2) && !(tp2 <:< tp1) -- these cases were handled before
27722772
*/
27732773
private def distributeAnd(tp1: Type, tp2: Type): Type = tp1 match {
2774-
case tp1 @ AppliedType(tycon1, args1) =>
2774+
case tp1 @ AppliedType(tycon1, args1) if false =>
27752775
tp2 match {
27762776
case AppliedType(tycon2, args2)
27772777
if tycon1.typeSymbol == tycon2.typeSymbol && tycon1 =:= tycon2 =>

tests/neg/i23435-min.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
type Or[+A, +B] = A | B
3+
4+
object Test1:
5+
val x: Or[Int, String] & Or[String, Int] = 3
6+
val y: Or[Int & String, String & Int] = x // error
7+
val z: String = y
8+
9+
// shows the distributeAnd logic should not be applied even when
10+
// the targs are pairwise TypeComparer#singletonInterval
11+
object Test2:
12+
val x: Or["3", Singleton] & Or[Singleton, "3"] = 3
13+
val y: Or["3", "3"] = x // error
14+
val z: String = y

tests/neg/i23435.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
trait L[+A]{val a:A}
3+
trait R[+B]{val b: B}
4+
5+
class LR(val a: Int, val b: String) extends L[Int] with R[String]
6+
7+
type E[+A] = L[A] | R[A]
8+
9+
val x: E[Int] & E[String] = LR(4, "hi")
10+
val y: E[Int&String] = x // error
11+
12+
val z = y match
13+
case l : L[Int&String] => l.a
14+
case r : R[Int&String] => r.b
15+
16+
val _ = z:String // was: java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String

0 commit comments

Comments
 (0)