Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
liufengyun committed Jun 10, 2024
1 parent 8ab198d commit f75f6dc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tests/init/warn/type-filter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class A(o: O):
var a = 20

class B(o: O):
var b = 20

class O:
val o: A | B = new A(this)
if o.isInstanceOf[A] then
o.asInstanceOf[A].a += 1
else
o.asInstanceOf[B].b += 1 // o.asInstanceOf[B] is treated as bottom

// prevent early promotion
val x = 10
19 changes: 19 additions & 0 deletions tests/init/warn/type-filter2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class A(c: C):
val f: Int = 10
def m() = f

class B(c: C):
val f: Int = g() // warn
def g(): Int = f

class C(x: Int):
val a: A | B = if x > 0 then new A(this) else new B(this)

def cast[T](a: Any): T = a.asInstanceOf[T]

val c: A = a.asInstanceOf[A] // abstraction for c is {A, B}
val d = c.f // treat as c.asInstanceOf[owner of f].f
val e = c.m() // treat as c.asInstanceOf[owner of f].m()
val c2: B = a.asInstanceOf[B]
val g = c2.f // no error here

0 comments on commit f75f6dc

Please sign in to comment.