Skip to content

Modified abstract domain in global initialization checker #23138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
910 changes: 431 additions & 479 deletions compiler/src/dotty/tools/dotc/transform/init/Objects.scala

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ global-list.scala
t5366.scala
mutable-read7.scala
t9115.scala
Color.scala
Color.scala
unapplySeq-implicit-arg2.scala
unapplySeq-implicit-arg3.scala
7 changes: 7 additions & 0 deletions tests/init-global/pos/cyclic-param.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class B {}

class C(c: B) extends B

object O:
def f(param: B): Int = f(new C(param))
val a = f(new B)
9 changes: 9 additions & 0 deletions tests/init-global/pos/get-local-val-from-other-owner.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
object O:
val a = m(2)
def m(f: Int) =
val a = f
() => a

object O2:
val func = O.a
val a = func()
4 changes: 4 additions & 0 deletions tests/init-global/pos/global-recursion3.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class A(val a: A)
object B:
val a: A = loop().a
def loop(): A = new A(loop())
14 changes: 14 additions & 0 deletions tests/init-global/pos/inner-extends-outer.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Outer {
val f = 5
class Inner extends Outer {
val g = Outer.this.f
}
}

object O {
def foo(i: Outer): Unit =
val i2 = new i.Inner // i2.outer should always be OfClass(Outer)
foo(i2)

foo(new Outer)
}
22 changes: 22 additions & 0 deletions tests/init-global/pos/local-class.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Outer {
def foo = {
val y = 5
class C {
val x = y
}
class D {
new C
}

new D
}

foo

val n = 10 // warn
}

object O {
val c = new Outer
val d: Object = c.foo
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object O:
def bar(t: T) = {
class A {
class B {
t.foo() // warn
t.foo()
}

val b = new B
Expand Down
20 changes: 14 additions & 6 deletions tests/init-global/warn/global-cycle6.check
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,8 @@
| │ ^
| ├── object B { [ global-cycle6.scala:8 ]
| │ ^
| ├── val a = new A.Inner [ global-cycle6.scala:9 ]
| │ ^^^^^^^^^^^
| ├── class Inner { [ global-cycle6.scala:3 ]
| │ ^
| └── println(n) // warn [ global-cycle6.scala:4 ]
| ^
| └── val a = new A.Inner [ global-cycle6.scala:9 ]
| ^^^^^^^^^^^
-- Warning: tests/init-global/warn/global-cycle6.scala:4:12 ------------------------------------------------------------
4 | println(n) // warn
| ^
Expand All @@ -26,3 +22,15 @@
| │ ^
| └── println(n) // warn [ global-cycle6.scala:4 ]
| ^
-- Warning: tests/init-global/warn/global-cycle6.scala:14:9 ------------------------------------------------------------
14 | object A { // warn
| ^
| Cyclic initialization: object A -> object B -> object A. Calling trace:
| ├── object A { // warn [ global-cycle6.scala:14 ]
| │ ^
| ├── val n: Int = B.m [ global-cycle6.scala:15 ]
| │ ^
| ├── object B { [ global-cycle6.scala:21 ]
| │ ^
| └── val a = new A.Inner [ global-cycle6.scala:22 ]
| ^^^^^^^^^^^
2 changes: 1 addition & 1 deletion tests/init-global/warn/global-cycle6.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object B {
}

object O {
object A {
object A { // warn
val n: Int = B.m
class Inner {
val x: Int = 4
Expand Down
5 changes: 5 additions & 0 deletions tests/init-global/warn/global-irrelevance3.check
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@
|│ ^^^^^^^
|└── (() => x) // warn [ global-irrelevance3.scala:9 ]
| ^
|The mutable state is created through:
|├── object A: [ global-irrelevance3.scala:1 ]
|│ ^
|└── val p: Pair = foo() [ global-irrelevance3.scala:3 ]
| ^^^^^
5 changes: 5 additions & 0 deletions tests/init-global/warn/global-irrelevance4.check
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@
| │ ^^^^^^^^^
| └── (y => x = y), // warn [ global-irrelevance4.scala:8 ]
| ^^^^^^^^^^
| The mutable state is created through:
| ├── object A: [ global-irrelevance4.scala:1 ]
| │ ^
| └── val p: Pair = foo() [ global-irrelevance4.scala:3 ]
| ^^^^^
6 changes: 2 additions & 4 deletions tests/init-global/warn/mutable-array.check
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@
|The mutable state is created through:
|├── object A: [ mutable-array.scala:1 ]
|│ ^
|├── val box: Box = new Box(0) [ mutable-array.scala:3 ]
|│ ^^^^^^^^^^
|└── class Box(var value: Int) [ mutable-array.scala:2 ]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|└── val box: Box = new Box(0) [ mutable-array.scala:3 ]
| ^^^^^^^^^^
6 changes: 2 additions & 4 deletions tests/init-global/warn/mutable-read1.check
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@
|The mutable state is created through:
|├── object A: [ mutable-read1.scala:3 ]
|│ ^
|├── val box: Box = new Box(4) [ mutable-read1.scala:4 ]
|│ ^^^^^^^^^^
|└── class Box(var value: Int) [ mutable-read1.scala:1 ]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|└── val box: Box = new Box(4) [ mutable-read1.scala:4 ]
| ^^^^^^^^^^
6 changes: 2 additions & 4 deletions tests/init-global/warn/mutable-read2.check
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@
|The mutable state is created through:
|├── object A: [ mutable-read2.scala:1 ]
|│ ^
|├── val box: Box = new Box(0) [ mutable-read2.scala:5 ]
|│ ^^^^^^^^^^
|└── class Box(var value: Int) { [ mutable-read2.scala:2 ]
| ^
|└── val box: Box = new Box(0) [ mutable-read2.scala:5 ]
| ^^^^^^^^^^
6 changes: 2 additions & 4 deletions tests/init-global/warn/mutable-read3.check
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@
|The mutable state is created through:
|├── object A: [ mutable-read3.scala:1 ]
|│ ^
|├── val box: Box = new Box(0) [ mutable-read3.scala:3 ]
|│ ^^^^^^^^^^
|└── class Box(var value: Int) [ mutable-read3.scala:2 ]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|└── val box: Box = new Box(0) [ mutable-read3.scala:3 ]
| ^^^^^^^^^^
6 changes: 2 additions & 4 deletions tests/init-global/warn/mutable-read4.check
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,5 @@
|The mutable state is created through:
|├── object A: [ mutable-read4.scala:3 ]
|│ ^
|├── val box: Box = new Box(4) [ mutable-read4.scala:4 ]
|│ ^^^^^^^^^^
|└── class Box(var value: Int) [ mutable-read4.scala:1 ]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|└── val box: Box = new Box(4) [ mutable-read4.scala:4 ]
| ^^^^^^^^^^
6 changes: 2 additions & 4 deletions tests/init-global/warn/mutable-read6.check
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@
|The mutable state is created through:
|├── object Contexts: [ mutable-read6.scala:3 ]
|│ ^
|├── val NoContext: Context = new Context [ mutable-read6.scala:4 ]
|│ ^^^^^^^^^^^
|└── class Context: [ mutable-read6.scala:5 ]
| ^
|└── val NoContext: Context = new Context [ mutable-read6.scala:4 ]
| ^^^^^^^^^^^
20 changes: 20 additions & 0 deletions tests/init-global/warn/unsoundness.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- Warning: tests/init-global/warn/unsoundness.scala:12:6 --------------------------------------------------------------
12 | O.x // warn
| ^^^
| Access uninitialized field value x. Calling trace:
| ├── object O: [ unsoundness.scala:15 ]
| │ ^
| ├── f(if m > 5 then Box(A(3)) else Box(B(4))) [ unsoundness.scala:17 ]
| │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ├── def f(a: Box[Base[Int]]): Unit = [ unsoundness.scala:21 ]
| │ ^
| ├── h(a.value) [ unsoundness.scala:22 ]
| │ ^^^^^^^^^^
| ├── def h(a: Base[Int]): Unit = [ unsoundness.scala:24 ]
| │ ^
| ├── a.update(10) [ unsoundness.scala:25 ]
| │ ^^^^^^^^^^^^
| ├── def update(n: T) = [ unsoundness.scala:11 ]
| │ ^
| └── O.x // warn [ unsoundness.scala:12 ]
| ^^^
25 changes: 25 additions & 0 deletions tests/init-global/warn/unsoundness.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Box[T](val value: T)

abstract class Base[T]:
def update(n: T): Unit

class A[T](var a: T) extends Base[T]:
def update(n: T) =
a = n

class B[T](var b: T) extends Base[T]:
def update(n: T) =
O.x // warn
b = n

object O:
val m: Int = 3
f(if m > 5 then Box(A(3)) else Box(B(4)))

val x: Int = 10

def f(a: Box[Base[Int]]): Unit =
h(a.value)

def h(a: Base[Int]): Unit =
a.update(10)
20 changes: 0 additions & 20 deletions tests/init-global/warn/widen.check

This file was deleted.

Loading