Skip to content

Commit c7251ab

Browse files
authored
Merge pull request scala#7016 from retronym/topic/swatch-precision
Fix regression in switch pattern translation
2 parents 7cecbdb + 72656e7 commit c7251ab

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

src/compiler/scala/tools/nsc/transform/patmat/PatternMatching.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ trait PatternMatching extends Transform
6565
// Keep 2.12 behaviour of using wildcard expected type, recomputing the LUB, then throwing it away for the continuations plugins
6666
// but for the rest of us pass in top as the expected type to avoid waste.
6767
val pt = if (origTp <:< definitions.AnyTpe) definitions.AnyTpe else WildcardType
68-
localTyper.typed(translated, pt) setType origTp
68+
localTyper.typed(translated, pt) match {
69+
case b @ Block(stats, m: Match) =>
70+
b.setType(origTp)
71+
m.setType(origTp)
72+
b
73+
case tree => tree setType origTp
74+
}
6975
} catch {
7076
case x: (Types#TypeError) =>
7177
// TODO: this should never happen; error should've been reported during type checking
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[[syntax trees at end of patmat]] // newSource1.scala
2+
package <empty>{<empty>.type} {
3+
class C extends scala.AnyRef {
4+
def <init>(): C = {
5+
C.super{C.super.type}.<init>{()Object}(){Object};
6+
(){Unit}
7+
}{Unit};
8+
def foo[A](a: A, b: A with C, i: Int): A = {
9+
case <synthetic> val x1: Int = i{Int};
10+
x1{Int} match {
11+
case 0{Int(0)} => a{A}
12+
case 1{Int(1)} => b{A with C}
13+
case _{Int} => throw new MatchError{MatchError}{(obj: Any)MatchError}(x1{Int}){MatchError}{Nothing}
14+
}{A}
15+
}{A}
16+
}
17+
}
18+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import scala.tools.partest._
2+
import java.io.{Console => _, _}
3+
4+
object Test extends DirectTest {
5+
6+
override def extraSettings: String = "-usejavacp -Xprint:patmat -Xprint-types -d " + testOutput.path
7+
8+
override def code = """class C {
9+
def foo[A](a: A, b: A with C, i: Int) = i match {
10+
case 0 => a
11+
case 1 => b
12+
}
13+
}
14+
"""
15+
16+
override def show(): Unit = {
17+
Console.withErr(System.out) {
18+
compile()
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)