Skip to content

Commit

Permalink
Break looping in checkable/hasPoison
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed May 10, 2022
1 parent e159fa1 commit 8e1e2fc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/TypeTestsCasts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,13 @@ object TypeTestsCasts {
val methodSymbol = sym.owner
val tpSyms = typer.ErrorReporting.substitutableTypeSymbolsInScope(sym).toSet
def isAccessible(sym: Symbol): Boolean = sym == methodSymbol || sym.isType && isAccessible(sym.owner)
val seen = scala.collection.mutable.Set.empty[Type]
def hasPoison(tp: Type): Boolean =
seen += tp
tp.baseClasses.filter(isAccessible).exists { sym =>
sym.info.decls.exists { sym =>
sym.info.existsPart(tp => tpSyms.contains(tp.typeSymbol))
|| isAccessible(sym.info.typeSymbol.maybeOwner) && hasPoison(sym.info)
|| !seen.contains(sym.info) && isAccessible(sym.info.typeSymbol.maybeOwner) && hasPoison(sym.info)
}
}
!hasPoison(tp2)
Expand Down
14 changes: 14 additions & 0 deletions tests/neg/i4812.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ object Test:
prev = new A(x)
x

def test6[T](x: T): T =
class Foo { var bar: Bar = null }
class Bar { var foo: Foo = null; var elem: T = _ }
prev match
case prev: Foo => // error: the type test for A cannot be checked at runtime
prev.bar.elem
case _ =>
val foo = new Foo
val bar = new Bar
bar.elem = x
foo.bar = bar
prev = foo
x

def main(args: Array[String]): Unit =
test(1)
val x: String = test("") // was: ClassCastException: java.lang.Integer cannot be cast to java.lang.String

0 comments on commit 8e1e2fc

Please sign in to comment.