Skip to content

Commit 9a602e1

Browse files
authored
Merge pull request scala#10808 from som-snytt/issue/13018-boolean-extraction
NonFatal opt needs type test
2 parents 3d54267 + 1925611 commit 9a602e1

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ trait PatternMatching extends Transform
8080
if guard.isEmpty && qual.symbol == definitions.NonFatalModule =>
8181
transform(treeCopy.CaseDef(
8282
tree,
83-
bind,
83+
treeCopy.Bind(bind, bind.name, Typed(Ident(nme.WILDCARD), TypeTree(definitions.ThrowableTpe)).setType(definitions.ThrowableTpe)),
8484
localTyper.typed(atPos(tree.pos)(Apply(gen.mkAttributedRef(definitions.NonFatal_apply), List(Ident(bind.symbol))))),
8585
body))
8686

test/files/run/nonfatal.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,22 @@ object Test extends BytecodeTest {
1212
val g = getMethod(classNode, "g")
1313
assertEquals(instructionsFromMethod(f), instructionsFromMethod(g))
1414
//sameBytecode(f, g) // prints
15+
new C().run() // should not crash
1516
}
1617
}
1718

1819
class C {
1920
import scala.util.control.NonFatal
2021
def x = 42
2122
def f = try x catch { case NonFatal(e) => e.printStackTrace(); -1 }
22-
def g = try x catch { case e if NonFatal(e) => e.printStackTrace(); -1 }
23+
def g = try x catch { case e: Throwable if NonFatal(e) => e.printStackTrace(); -1 }
24+
25+
def run(): Unit = {
26+
val any: Any = 42
27+
28+
any match {
29+
case NonFatal(e) => ???
30+
case _ =>
31+
}
32+
}
2333
}

0 commit comments

Comments
 (0)