Skip to content

Commit 67024a9

Browse files
committed
Only trust the type application part for case class unapplies
1 parent 8cfa37a commit 67024a9

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

compiler/src/dotty/tools/dotc/transform/TypeTestsCasts.scala

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ object TypeTestsCasts {
5656
* 9. if `X` is `T1 | T2`, checkable(T1, P) && checkable(T2, P).
5757
* 10. otherwise, ""
5858
*/
59-
def whyUncheckable(X: Type, P: Type, span: Span)(using Context): String = atPhase(Phases.refchecksPhase.next) {
59+
def whyUncheckable(X: Type, P: Type, span: Span, trustTypeApplication: Boolean)(using Context): String = atPhase(Phases.refchecksPhase.next) {
6060
extension (inline s1: String) inline def &&(inline s2: String): String = if s1 == "" then s2 else s1
6161
extension (inline b: Boolean) inline def |||(inline s: String): String = if b then "" else s
6262

@@ -143,7 +143,7 @@ object TypeTestsCasts {
143143
case defn.ArrayOf(tpE) => recur(tpE, tpT)
144144
case _ => recur(defn.AnyType, tpT)
145145
}
146-
case tpe @ AppliedType(tycon, targs) =>
146+
case tpe @ AppliedType(tycon, targs) if !trustTypeApplication =>
147147
X.widenDealias match {
148148
case OrType(tp1, tp2) =>
149149
// This case is required to retrofit type inference,
@@ -366,8 +366,7 @@ object TypeTestsCasts {
366366
if (sym.isTypeTest) {
367367
val argType = tree.args.head.tpe
368368
val isTrusted = tree.hasAttachment(PatternMatcher.TrustedTypeTestKey)
369-
if !isTrusted then
370-
checkTypePattern(expr.tpe, argType, expr.srcPos)
369+
checkTypePattern(expr.tpe, argType, expr.srcPos, isTrusted)
371370
transformTypeTest(expr, argType,
372371
flagUnrelated = enclosingInlineds.isEmpty) // if test comes from inlined code, dont't flag it even if it always false
373372
}
@@ -392,10 +391,10 @@ object TypeTestsCasts {
392391
def checkBind(tree: Bind)(using Context) =
393392
checkTypePattern(defn.ThrowableType, tree.body.tpe, tree.srcPos)
394393

395-
private def checkTypePattern(exprTpe: Type, castTpe: Type, pos: SrcPos)(using Context) =
394+
private def checkTypePattern(exprTpe: Type, castTpe: Type, pos: SrcPos, trustTypeApplication: Boolean = false)(using Context) =
396395
val isUnchecked = exprTpe.widenTermRefExpr.hasAnnotation(defn.UncheckedAnnot)
397396
if !isUnchecked then
398-
val whyNot = whyUncheckable(exprTpe, castTpe, pos.span)
397+
val whyNot = whyUncheckable(exprTpe, castTpe, pos.span, trustTypeApplication)
399398
if whyNot.nonEmpty then
400399
report.uncheckedWarning(UncheckedTypePattern(castTpe, whyNot), pos)
401400

tests/neg/i22051.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//> using options -Werror
2+
3+
def boundary[T](body: (T => RuntimeException) => T): T =
4+
case class Break(value: T) extends RuntimeException
5+
try body(Break.apply)
6+
catch case Break(t) => t // error: pattern matching on local classes is unsound currently
7+
8+
def test =
9+
boundary[Int]: EInt =>
10+
val v: String = boundary[String]: EString =>
11+
throw EInt(3)
12+
v.length // a runtime error: java.lang.ClassCastException
13+
14+
def boundaryCorrectBehaviour[T](body: (T => RuntimeException) => T): T =
15+
object local:
16+
// A correct implementation, but is still treated as a local class right now
17+
case class Break(value: T) extends RuntimeException
18+
try body(local.Break.apply)
19+
catch case local.Break(t) => t // error

0 commit comments

Comments
 (0)