From 1256b1f5acc12c13e80e8408c66894d4348c625b Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Thu, 23 Jul 2020 18:38:40 +0100 Subject: [PATCH] Avoid exhaustive warnings in try/catch --- .../nsc/transform/patmat/MatchTranslation.scala | 5 ++++- test/files/neg/t5365c.scala | 14 ++++++++++++++ test/files/run/sd187.check | 2 +- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/compiler/scala/tools/nsc/transform/patmat/MatchTranslation.scala b/src/compiler/scala/tools/nsc/transform/patmat/MatchTranslation.scala index b31c76136768..36756299f7c1 100644 --- a/src/compiler/scala/tools/nsc/transform/patmat/MatchTranslation.scala +++ b/src/compiler/scala/tools/nsc/transform/patmat/MatchTranslation.scala @@ -262,12 +262,15 @@ trait MatchTranslation { val exSym = freshSym(pos, pureType(ThrowableTpe), "ex") + val unchecked = AnnotationInfo(UncheckedClass.tpe, Nil, Nil) + val scrut = atPos(pos)(Typed(REF(exSym), TypeTree(pureType(ThrowableTpe).withAnnotation(unchecked)))) + List( atPos(pos) { CaseDef( Bind(exSym, Ident(nme.WILDCARD)), // TODO: does this need fixing upping? EmptyTree, - combineCasesNoSubstOnly(REF(exSym), scrutSym, casesNoSubstOnly, pt, selectorPos, matchOwner, Some(scrut => Throw(REF(exSym)))) + combineCasesNoSubstOnly(scrut, scrutSym, casesNoSubstOnly, pt, selectorPos, matchOwner, Some(scrut => Throw(REF(exSym)))) ) }) } diff --git a/test/files/neg/t5365c.scala b/test/files/neg/t5365c.scala index 7db29f004848..c09dd40cfcb3 100644 --- a/test/files/neg/t5365c.scala +++ b/test/files/neg/t5365c.scala @@ -14,4 +14,18 @@ object C { def uncheckedUnsealedTrait(z: Z) = (z: @unchecked) match { case Q(_) => } + + def catchBlock() = { + try { 42 } catch { case MyException(_) => 43 } // Throwable isn't sealed, but don't warn here + } + + def catchBlockWithTypePattern() = { + try { 42 } catch { case _: MyException => 43 } // See? Just behave like Java. + } + + def partialFunction(): PartialFunction[Throwable, Int] = { // Or like PartialFunction behaves. + case MyException(_) => 67 + } } + +case class MyException(x: String) extends Exception diff --git a/test/files/run/sd187.check b/test/files/run/sd187.check index 2257aa0f0ccd..1b0643b0f1ad 100644 --- a/test/files/run/sd187.check +++ b/test/files/run/sd187.check @@ -74,7 +74,7 @@ [1607][1607][1607]C.this.toString() } catch { [1505]case [1505](ex6 @ [1505]_) => [1505]{ - [1812] val x4: [1812]Throwable = [1812]ex6; + [1812] val x4: [1812]Throwable = [1505]([1505]ex6: [1505]Throwable @unchecked); [1505]case9(){ [1812]if ([1812][1812]x4.ne([1812]null)) [1812]{