From f75f6dc211081715cc6abb54e83c602b5c6f1cbb Mon Sep 17 00:00:00 2001 From: Fengyun Liu Date: Mon, 10 Jun 2024 22:27:12 +0200 Subject: [PATCH] Add test --- tests/init/warn/type-filter.scala | 15 +++++++++++++++ tests/init/warn/type-filter2.scala | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 tests/init/warn/type-filter.scala create mode 100644 tests/init/warn/type-filter2.scala diff --git a/tests/init/warn/type-filter.scala b/tests/init/warn/type-filter.scala new file mode 100644 index 000000000000..1d25454992fe --- /dev/null +++ b/tests/init/warn/type-filter.scala @@ -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 diff --git a/tests/init/warn/type-filter2.scala b/tests/init/warn/type-filter2.scala new file mode 100644 index 000000000000..65f5be8f4b53 --- /dev/null +++ b/tests/init/warn/type-filter2.scala @@ -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 +