diff --git a/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala b/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala index 273879f3c3cb..7f9e0cdca0bf 100644 --- a/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala +++ b/compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala @@ -457,17 +457,18 @@ object PatternMatcher { // begin patternPlan swapBind(tree) match { case Typed(pat, tpt) => - val isTrusted = pat match { + def isTrusted(pattern: Tree): Boolean = pattern match { case UnApply(extractor, _, _) => extractor.symbol.is(Synthetic) && extractor.symbol.owner.linkedClass.is(Case) && !hasExplicitTypeArgs(extractor) + case Bind(_, body) => isTrusted(body) case _ => false } val castTp = if Feature.ccEnabled then CapturingType(tpt.tpe, scrutinee.termRef.singletonCaptureSet) else tpt.tpe - TestPlan(TypeTest(tpt, isTrusted), scrutinee, tree.span, + TestPlan(TypeTest(tpt, isTrusted(pat)), scrutinee, tree.span, letAbstract(ref(scrutinee).cast(castTp)) { casted => nonNull += casted patternPlan(casted, pat, onSuccess) diff --git a/tests/pos/i24557.scala b/tests/pos/i24557.scala new file mode 100644 index 000000000000..9ed743dee806 --- /dev/null +++ b/tests/pos/i24557.scala @@ -0,0 +1,9 @@ +object test { + sealed trait X[T] + trait Iterable[T] + case class I[T, C <: Iterable[T]]() extends X[C] + + def t[T](i: X[T]): Unit = + i match + case i @ I() => () +}