File tree 3 files changed +41
-4
lines changed
compiler/src/dotty/tools/dotc/typer 3 files changed +41
-4
lines changed Original file line number Diff line number Diff line change @@ -1718,10 +1718,16 @@ class Typer extends Namer
1718
1718
}
1719
1719
if (name == nme.WILDCARD ) body1
1720
1720
else {
1721
- // for a singleton pattern like `x @ Nil`, `x` should get the type from the scrutinee
1722
- // see tests/neg/i3200b.scala and SI-1503
1721
+ // In `x @ Nil`, `Nil` is a _stable identifier pattern_ and will be compiled
1722
+ // to an `==` test, so the type of `x` is unrelated to the type of `Nil`.
1723
+ // Similarly, in `x @ 1`, `1` is a _literal pattern_ and will also be compiled
1724
+ // to an `==` test.
1725
+ // See i3200*.scala and https://github.com/scala/bug/issues/1503.
1726
+ val isStableIdentifierOrLiteral =
1727
+ body1.isInstanceOf [RefTree ] && ! isWildcardArg(body1)
1728
+ || body1.isInstanceOf [Literal ]
1723
1729
val symTp =
1724
- if body1.tpe. isInstanceOf [ TermRef ] then pt
1730
+ if isStableIdentifierOrLiteral then pt
1725
1731
else if isWildcardStarArg(body1)
1726
1732
|| pt == defn.ImplicitScrutineeTypeRef
1727
1733
|| body1.tpe <:< pt // There is some strange interaction with gadt matching.
Original file line number Diff line number Diff line change 1
1
object Test {
2
2
def main (args : Array [String ]): Unit = {
3
- val a : Nil .type = (Vector (): Any ) match { case n @ Nil => n } // error
3
+ val a : Nil .type = (Vector (): Any ) match { case n @ Nil => n } // error
4
+ val b : Nil .type = (Vector (): Any ) match { case n @ (m @ Nil ) => n } // error
5
+ val c : Int = (1.0 : Any ) match { case n @ 1 => n } // error
6
+ val d : Int = (1.0 : Any ) match { case n @ (m @ 1 ) => n } // error
4
7
}
5
8
}
Original file line number Diff line number Diff line change
1
+ sealed trait X
2
+ case object Y extends X
3
+
4
+ object Test {
5
+ def yIs1 (proof : Y .type ): Int = 1
6
+
7
+ def test (x : X ) = x match {
8
+ case y : Y .type => yIs1(y)
9
+ }
10
+
11
+ def test2 (x : X ) = x match {
12
+ case y @ (yy : Y .type ) =>
13
+ yIs1(y)
14
+ yIs1(yy)
15
+ }
16
+
17
+ def main (args : Array [String ]): Unit = {
18
+ test(Y )
19
+
20
+ val a : Nil .type = (Vector (): Any ) match {
21
+ case n : Nil .type =>
22
+ assert(false , " should not be reached" )
23
+ n
24
+ case _ =>
25
+ Nil
26
+ }
27
+ }
28
+ }
You can’t perform that action at this time.
0 commit comments