Skip to content

Commit 34cd237

Browse files
committed
Fixed
1 parent 454596b commit 34cd237

File tree

6 files changed

+42
-36
lines changed

6 files changed

+42
-36
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ object TypeTestsCasts {
7474
}.apply(tp)
7575

7676
/** Returns true if the type arguments of `P` can be determined from `X` */
77-
def typeArgsTrivial(X: Type, P: AppliedType)(using Context) = inContext(ctx.fresh.setExploreTyperState().setFreshGADTBounds) {
77+
def typeArgsDeterminable(X: Type, P: AppliedType)(using Context) = inContext(ctx.fresh.setExploreTyperState().setFreshGADTBounds) {
7878
val AppliedType(tycon, _) = P
7979

8080
def underlyingLambda(tp: Type): TypeLambda = tp.ensureLambdaSub match {
@@ -155,7 +155,7 @@ object TypeTestsCasts {
155155
case x =>
156156
// always false test warnings are emitted elsewhere
157157
TypeComparer.provablyDisjoint(x, tpe.derivedAppliedType(tycon, targs.map(_ => WildcardType)))
158-
|| typeArgsTrivial(X, tpe)
158+
|| typeArgsDeterminable(X, tpe)
159159
||| i"its type arguments can't be determined from $X"
160160
}
161161
case AndType(tp1, tp2) => recur(X, tp1) && recur(X, tp2)

compiler/src/dotty/tools/dotc/transform/patmat/Space.scala

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ object SpaceEngine {
385385
project(pat)
386386

387387
case Typed(_, tpt) =>
388-
Typ(erase(tpt.tpe.stripAnnots, isValue = true), decomposed = false)
388+
Typ(erase(tpt.tpe.stripAnnots, isValue = true, isTyped = true), decomposed = false)
389389

390390
case This(_) =>
391391
Typ(pat.tpe.stripAnnots, decomposed = false)
@@ -453,24 +453,26 @@ object SpaceEngine {
453453
* If `isValue` is true, then pattern-bound symbols are erased to its upper bound.
454454
* This is needed to avoid spurious unreachable warnings. See tests/patmat/i6197.scala.
455455
*/
456-
private def erase(tp: Type, inArray: Boolean = false, isValue: Boolean = false)(using Context): Type =
457-
trace(i"erase($tp${if inArray then " inArray" else ""}${if isValue then " isValue" else ""})", debug)(tp match {
456+
private def erase(tp: Type, inArray: Boolean = false, isValue: Boolean = false, isTyped: Boolean = false)(using Context): Type =
457+
trace(i"erase($tp${if inArray then " inArray" else ""}${if isValue then " isValue" else ""}${if isTyped then " isTyped" else ""})", debug)(tp match {
458458
case tp @ AppliedType(tycon, args) if tycon.typeSymbol.isPatternBound =>
459459
WildcardType
460460

461461
case tp @ AppliedType(tycon, args) =>
462462
val inArray = tycon.isRef(defn.ArrayClass)
463-
val args2 = args.map(arg => erase(arg, inArray = inArray, isValue = false))
463+
val args2 =
464+
if isTyped && !inArray then args.map(_ => WildcardType)
465+
else args.map(arg => erase(arg, inArray = inArray, isValue = false))
464466
tp.derivedAppliedType(erase(tycon, inArray, isValue = false), args2)
465467

466468
case tp @ OrType(tp1, tp2) =>
467-
OrType(erase(tp1, inArray, isValue), erase(tp2, inArray, isValue), tp.isSoft)
469+
OrType(erase(tp1, inArray, isValue, isTyped), erase(tp2, inArray, isValue, isTyped), tp.isSoft)
468470

469471
case AndType(tp1, tp2) =>
470-
AndType(erase(tp1, inArray, isValue), erase(tp2, inArray, isValue))
472+
AndType(erase(tp1, inArray, isValue, isTyped), erase(tp2, inArray, isValue, isTyped))
471473

472474
case tp @ RefinedType(parent, _, _) =>
473-
erase(parent, inArray, isValue)
475+
erase(parent, inArray, isValue, isTyped)
474476

475477
case tref: TypeRef if tref.symbol.isPatternBound =>
476478
if inArray then tref.underlying

tests/warn/enum-approx2.check

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
-- [E030] Match case Unreachable Warning: tests/warn/enum-approx2.scala:7:12 -------------------------------------------
2+
7 | case Fun(x: Exp[Int => String]) => ??? // warn: unreachable // warn: unchecked
3+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
4+
| Unreachable case
5+
-- [E121] Pattern Match Warning: tests/warn/enum-approx2.scala:8:9 -----------------------------------------------------
6+
8 | case _ => // warn: unreachable-only-null
7+
| ^
8+
| Unreachable case except for null (if this is intentional, consider writing case null => instead).
19
-- [E092] Pattern Match Unchecked Warning: tests/warn/enum-approx2.scala:6:13 ------------------------------------------
210
6 | case Fun(x: Fun[Int, Double]) => ??? // warn: unchecked
311
| ^

tests/warn/i16451.check

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,31 @@
11
-- [E030] Match case Unreachable Warning: tests/warn/i16451.scala:14:9 -------------------------------------------------
2-
14 | case x: Wrapper[Color.Green.type] => None // error: unchecked
2+
14 | case x: Wrapper[Color.Green.type] => None // warn: unreachable // warn: unchecked
33
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
44
| Unreachable case
55
-- [E030] Match case Unreachable Warning: tests/warn/i16451.scala:22:9 -------------------------------------------------
6-
22 | case x: Wrapper[Color.Green.type] => None // error: unchecked
6+
22 | case x: Wrapper[Color.Green.type] => None // warn: unreachable // warn: unchecked
77
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
88
| Unreachable case
9-
-- [E121] Pattern Match Warning: tests/warn/i16451.scala:26:9 ----------------------------------------------------------
10-
26 | case _ => None
11-
| ^
12-
| Unreachable case except for null (if this is intentional, consider writing case null => instead).
13-
-- [E121] Pattern Match Warning: tests/warn/i16451.scala:30:9 ----------------------------------------------------------
14-
30 | case _ => None
15-
| ^
16-
| Unreachable case except for null (if this is intentional, consider writing case null => instead).
179
-- [E092] Pattern Match Unchecked Warning: tests/warn/i16451.scala:13:9 ------------------------------------------------
18-
13 | case x: Wrapper[Color.Red.type] => Some(x) // error: unchecked
10+
13 | case x: Wrapper[Color.Red.type] => Some(x) // warn: unchecked
1911
| ^
2012
|the type test for Wrapper[(Color.Red : Color)] cannot be checked at runtime because its type arguments can't be determined from Wrapper[Color]
2113
|
2214
| longer explanation available when compiling with `-explain`
2315
-- [E092] Pattern Match Unchecked Warning: tests/warn/i16451.scala:14:9 ------------------------------------------------
24-
14 | case x: Wrapper[Color.Green.type] => None // error: unchecked
16+
14 | case x: Wrapper[Color.Green.type] => None // warn: unreachable // warn: unchecked
2517
| ^
2618
|the type test for Wrapper[(Color.Green : Color)] cannot be checked at runtime because its type arguments can't be determined from Wrapper[Color]
2719
|
2820
| longer explanation available when compiling with `-explain`
2921
-- [E092] Pattern Match Unchecked Warning: tests/warn/i16451.scala:21:9 ------------------------------------------------
30-
21 | case x: Wrapper[Color.Red.type] => Some(x) // error: unchecked
22+
21 | case x: Wrapper[Color.Red.type] => Some(x) // warn: unchecked
3123
| ^
3224
|the type test for Wrapper[(Color.Red : Color)] cannot be checked at runtime because its type arguments can't be determined from Any
3325
|
3426
| longer explanation available when compiling with `-explain`
3527
-- [E092] Pattern Match Unchecked Warning: tests/warn/i16451.scala:22:9 ------------------------------------------------
36-
22 | case x: Wrapper[Color.Green.type] => None // error: unchecked
28+
22 | case x: Wrapper[Color.Green.type] => None // warn: unreachable // warn: unchecked
3729
| ^
3830
|the type test for Wrapper[(Color.Green : Color)] cannot be checked at runtime because its type arguments can't be determined from Any
3931
|
@@ -45,19 +37,19 @@
4537
|
4638
| longer explanation available when compiling with `-explain`
4739
-- [E092] Pattern Match Unchecked Warning: tests/warn/i16451.scala:29:9 ------------------------------------------------
48-
29 | case x: Wrapper[Color.Red.type] => Some(x) // error: unreachable // error: unchecked
40+
29 | case x: Wrapper[Color.Red.type] => Some(x)
4941
| ^
5042
|the type test for Wrapper[(Color.Red : Color)] cannot be checked at runtime because its type arguments can't be determined from A1
5143
|
5244
| longer explanation available when compiling with `-explain`
5345
-- [E092] Pattern Match Unchecked Warning: tests/warn/i16451.scala:34:11 -----------------------------------------------
54-
34 | case x: Wrapper[Color.Red.type] => x // error: unreachable // error: unchecked
46+
34 | case x: Wrapper[Color.Red.type] => x
5547
| ^
5648
|the type test for Wrapper[(Color.Red : Color)] cannot be checked at runtime because its type arguments can't be determined from Wrapper[Color]
5749
|
5850
| longer explanation available when compiling with `-explain`
5951
-- [E092] Pattern Match Unchecked Warning: tests/warn/i16451.scala:39:11 -----------------------------------------------
60-
39 | case x: Wrapper[Color.Red.type] => x // error: unreachable // error: unchecked
52+
39 | case x: Wrapper[Color.Red.type] => x
6153
| ^
6254
|the type test for Wrapper[(Color.Red : Color)] cannot be checked at runtime because its type arguments can't be determined from Wrapper[Color]
6355
|

tests/warn/i16451.scala

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,34 @@ enum Color:
99
case class Wrapper[A](value: A)
1010

1111
object Test:
12-
def test_correct(x: Wrapper[Color]): Option[Wrapper[Color.Red.type]] = x match // error: inexhaustive
13-
case x: Wrapper[Color.Red.type] => Some(x) // error: unchecked
14-
case x: Wrapper[Color.Green.type] => None // error: unchecked
12+
def test_correct(x: Wrapper[Color]): Option[Wrapper[Color.Red.type]] = x match
13+
case x: Wrapper[Color.Red.type] => Some(x) // warn: unchecked
14+
case x: Wrapper[Color.Green.type] => None // warn: unreachable // warn: unchecked
1515

1616
def test_different(x: Wrapper[Color]): Option[Wrapper[Color]] = x match
1717
case x @ Wrapper(_: Color.Red.type) => Some(x)
1818
case x @ Wrapper(_: Color.Green.type) => None
1919

2020
def test_any(x: Any): Option[Wrapper[Color.Red.type]] = x match
21-
case x: Wrapper[Color.Red.type] => Some(x) // error: unchecked
22-
case x: Wrapper[Color.Green.type] => None // error: unchecked
21+
case x: Wrapper[Color.Red.type] => Some(x) // warn: unchecked
22+
case x: Wrapper[Color.Green.type] => None // warn: unreachable // warn: unchecked
2323

2424
def test_wrong(x: Wrapper[Color]): Option[Wrapper[Color.Red.type]] = x match
2525
case x: Wrapper[Color.Red.type] => Some(x) // error: unreachable // error: unchecked
26-
case _ => None
26+
case null => None
2727

2828
def t2[A1 <: Wrapper[Color]](x: A1): Option[Wrapper[Color.Red.type]] = x match
29-
case x: Wrapper[Color.Red.type] => Some(x) // error: unreachable // error: unchecked
30-
case _ => None
29+
case x: Wrapper[Color.Red.type] => Some(x)
30+
case null => None
3131

3232
def test_wrong_seq(xs: Seq[Wrapper[Color]]): Seq[Wrapper[Color.Red.type]] =
3333
xs.collect {
34-
case x: Wrapper[Color.Red.type] => x // error: unreachable // error: unchecked
34+
case x: Wrapper[Color.Red.type] => x
3535
}
3636

3737
def test_wrong_seq2(xs: Seq[Wrapper[Color]]): Seq[Wrapper[Color.Red.type]] =
3838
xs.collect { x => x match
39-
case x: Wrapper[Color.Red.type] => x // error: unreachable // error: unchecked
39+
case x: Wrapper[Color.Red.type] => x
4040
}
4141

4242
def main(args: Array[String]): Unit =

tests/warn/i5826.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
-- [E121] Pattern Match Warning: tests/warn/i5826.scala:9:9 ------------------------------------------------------------
2+
9 | case _ => 0 // warn: unreachable-only-null
3+
| ^
4+
| Unreachable case except for null (if this is intentional, consider writing case null => instead).
15
-- [E092] Pattern Match Unchecked Warning: tests/warn/i5826.scala:3:9 --------------------------------------------------
26
3 | case ls: List[Int] => ls.head // error, A = List[String]
37
| ^

0 commit comments

Comments
 (0)