Skip to content

Commit

Permalink
Avoid exhaustive warnings in try/catch
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Jul 28, 2020
1 parent 251bd1c commit 1256b1f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
Expand Up @@ -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))))
)
})
}
Expand Down
14 changes: 14 additions & 0 deletions test/files/neg/t5365c.scala
Expand Up @@ -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
2 changes: 1 addition & 1 deletion test/files/run/sd187.check
Expand Up @@ -74,7 +74,7 @@
[1607][1607][1607]C.this.toString()
} catch {
[1505]case [1505](ex6 @ [1505]_) => [1505]{
[1812]<synthetic> val x4: [1812]Throwable = [1812]ex6;
[1812]<synthetic> val x4: [1812]Throwable = [1505]([1505]ex6: [1505]Throwable @unchecked);
[1505]case9(){
[1812]if ([1812][1812]x4.ne([1812]null))
[1812]{
Expand Down

0 comments on commit 1256b1f

Please sign in to comment.