Skip to content

Commit 72656e7

Browse files
committed
Fix regression in switch pattern translation
The performance optimization in scala#6607 assumed that a translated match would always be a `Match` node itself, but it can also be a `{ synthetic val x1 = ...; x1 match { .. } }` block.
1 parent 1b1aefe commit 72656e7

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
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

test/files/run/patmat-origtp-switch.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ package <empty>{<empty>.type} {
1111
case 0{Int(0)} => a{A}
1212
case 1{Int(1)} => b{A with C}
1313
case _{Int} => throw new MatchError{MatchError}{(obj: Any)MatchError}(x1{Int}){MatchError}{Nothing}
14-
}{Any}
14+
}{A}
1515
}{A}
1616
}
1717
}

0 commit comments

Comments
 (0)