Skip to content

Commit f75f6dc

Browse files
committed
Add test
1 parent 8ab198d commit f75f6dc

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

tests/init/warn/type-filter.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class A(o: O):
2+
var a = 20
3+
4+
class B(o: O):
5+
var b = 20
6+
7+
class O:
8+
val o: A | B = new A(this)
9+
if o.isInstanceOf[A] then
10+
o.asInstanceOf[A].a += 1
11+
else
12+
o.asInstanceOf[B].b += 1 // o.asInstanceOf[B] is treated as bottom
13+
14+
// prevent early promotion
15+
val x = 10

tests/init/warn/type-filter2.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class A(c: C):
2+
val f: Int = 10
3+
def m() = f
4+
5+
class B(c: C):
6+
val f: Int = g() // warn
7+
def g(): Int = f
8+
9+
class C(x: Int):
10+
val a: A | B = if x > 0 then new A(this) else new B(this)
11+
12+
def cast[T](a: Any): T = a.asInstanceOf[T]
13+
14+
val c: A = a.asInstanceOf[A] // abstraction for c is {A, B}
15+
val d = c.f // treat as c.asInstanceOf[owner of f].f
16+
val e = c.m() // treat as c.asInstanceOf[owner of f].m()
17+
val c2: B = a.asInstanceOf[B]
18+
val g = c2.f // no error here
19+

0 commit comments

Comments
 (0)