-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add supports for type cast and filtering type for field and method ow…
…ner in global initialization checker (#19612) This PR adds support for more precise analysis of type casting in global initialization checker, which filters impossible classes after reference casting and preserves only possible classes. It also automatically filter the set of possible classes during field selection or method invocation and only preserves owners of the field/method
- Loading branch information
Showing
8 changed files
with
106 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
object A { | ||
val f: Int = 10 | ||
def m() = f | ||
} | ||
object B { | ||
val f: Int = g() | ||
def g(): Int = f // error | ||
} | ||
object C { | ||
val a: A.type | B.type = if ??? then A else B | ||
def cast[T](a: Any): T = a.asInstanceOf[T] | ||
val c: A.type = cast[A.type](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.type = cast[B.type](a) | ||
val g = c2.f // no error here | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class A: | ||
class B(val b: Int) | ||
|
||
object O: | ||
val o: A | Array[Int] = new Array[Int](10) | ||
o match | ||
case a: A => new a.B(10) | ||
case arr: Array[Int] => arr(5) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class A: | ||
class B(val b: Int) | ||
|
||
object O: | ||
val o: A | (Int => Int) = (x: Int) => x + 1 | ||
o match | ||
case a: A => new a.B(10) | ||
case f: (_ => _) => f.asInstanceOf[Int => Int](5) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class A: | ||
var x: Int = 10 | ||
|
||
object O: | ||
val o: A | (Int => Int) = (x: Int) => x + 1 | ||
o match | ||
case a: A => a.x = 20 | ||
case f: (_ => _) => f.asInstanceOf[Int => Int](5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class A: | ||
var x: Int = 10 | ||
|
||
object O: | ||
val o: A | Array[Int] = new Array[Int](10) | ||
o match | ||
case a: A => a.x = 20 | ||
case arr: Array[Int] => arr(5) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class A: | ||
var a = 20 | ||
|
||
class B: | ||
var b = 20 | ||
|
||
object O: | ||
val o: A | B = new A | ||
if o.isInstanceOf[A] then | ||
o.asInstanceOf[A].a += 1 | ||
else | ||
o.asInstanceOf[B].b += 1 // o.asInstanceOf[B] is treated as bottom | ||
o match | ||
case o: A => o.a += 1 | ||
case o: B => o.b += 1 |