Skip to content

Commit 2ae831f

Browse files
committed
Fix #11178: remove unsound tweak for F-bounds
This reverts #9789. We have made several improvements to F-bounds, the unsound tweak is no longer needed.
1 parent e64d3d1 commit 2ae831f

File tree

2 files changed

+49
-19
lines changed

2 files changed

+49
-19
lines changed

compiler/src/dotty/tools/dotc/transform/TypeTestsCasts.scala

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,6 @@ object TypeTestsCasts {
7777
}
7878
}.apply(tp)
7979

80-
/** Approximate type parameters depending on variance */
81-
def stripTypeParam(tp: Type)(using Context) = new ApproximatingTypeMap {
82-
val boundTypeParams = util.HashMap[TypeRef, TypeVar]()
83-
def apply(tp: Type): Type = tp match {
84-
case _: MatchType =>
85-
tp // break cycles
86-
case tp: TypeRef if !tp.symbol.isClass =>
87-
boundTypeParams.getOrElseUpdate(tp, newTypeVar(tp.underlying.toBounds))
88-
case _ =>
89-
mapOver(tp)
90-
}
91-
}.apply(tp)
92-
9380
/** Returns true if the type arguments of `P` can be determined from `X` */
9481
def typeArgsTrivial(X: Type, P: AppliedType)(using Context) = inContext(ctx.fresh.setExploreTyperState().setFreshGADTBounds) {
9582
val AppliedType(tycon, _) = P
@@ -102,6 +89,7 @@ object TypeTestsCasts {
10289
val tvars = constrained(typeLambda, untpd.EmptyTree, alwaysAddTypeVars = true)._2.map(_.tpe)
10390
val P1 = tycon.appliedTo(tvars)
10491

92+
debug.println("before " + ctx.typerState.constraint.show)
10593
debug.println("P : " + P.show)
10694
debug.println("P1 : " + P1.show)
10795
debug.println("X : " + X.show)
@@ -111,18 +99,23 @@ object TypeTestsCasts {
11199
// conforms to the type skeleton pre.F[_]. Then it goes on to check
112100
// if P1 <: P, which means the type arguments in P are trivial,
113101
// thus no runtime checks are needed for them.
114-
P1 <:< X
102+
P1 <:< X || withMode(Mode.GadtConstraintInference) {
103+
TypeComparer.constrainPatternType(P1, X)
104+
debug.println(TypeComparer.explained(_.constrainPatternType(P1, X)))
105+
true
106+
}
115107

116108
// Maximization of the type means we try to cover all possible values
117109
// which conform to the skeleton pre.F[_] and X. Then we have to make
118110
// sure all of them are actually of the type P, which implies that the
119111
// type arguments in P are trivial (no runtime check needed).
120112
maximizeType(P1, span, fromScala2x = false)
121113

114+
debug.println("after " + ctx.typerState.constraint.show)
115+
122116
val res = P1 <:< P
123117

124118
debug.println(TypeComparer.explained(_.isSubType(P1, P)))
125-
126119
debug.println("P1 : " + P1.show)
127120
debug.println("P1 <:< P = " + res)
128121

@@ -151,10 +144,8 @@ object TypeTestsCasts {
151144
case _ =>
152145
// always false test warnings are emitted elsewhere
153146
X.classSymbol.exists && P.classSymbol.exists &&
154-
!X.classSymbol.asClass.mayHaveCommonChild(P.classSymbol.asClass) ||
155-
// first try without striping type parameters for performance
156-
typeArgsTrivial(X, tpe) ||
157-
typeArgsTrivial(stripTypeParam(X), tpe)
147+
!X.classSymbol.asClass.mayHaveCommonChild(P.classSymbol.asClass)
148+
|| typeArgsTrivial(X, tpe)
158149
}
159150
case AndType(tp1, tp2) => recur(X, tp1) && recur(X, tp2)
160151
case OrType(tp1, tp2) => recur(X, tp1) && recur(X, tp2)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
trait Box[+T]
2+
case class Foo[+S](s: S) extends Box[S]
3+
4+
def unwrap2[A](b: Box[A]): A =
5+
b match
6+
case _: Foo[Int] => 0 // error
7+
8+
object Test1 {
9+
// Invariant case, OK
10+
sealed trait Bar[A]
11+
12+
def test[A](bar: Bar[A]) =
13+
bar match {
14+
case _: Bar[Boolean] => ??? // error
15+
case _ => ???
16+
}
17+
}
18+
19+
object Test2 {
20+
// Covariant case
21+
sealed trait Bar[+A]
22+
23+
def test[A](bar: Bar[A]) =
24+
bar match {
25+
case _: Bar[Boolean] => ??? // error
26+
case _ => ???
27+
}
28+
}
29+
30+
object Test3 {
31+
// Contravariant case
32+
sealed trait Bar[-A]
33+
34+
def test[A](bar: Bar[A]) =
35+
bar match {
36+
case _: Bar[Boolean] => ??? // error
37+
case _ => ???
38+
}
39+
}

0 commit comments

Comments
 (0)