Skip to content

Commit

Permalink
Make safe init checker skip global objects (#18906)
Browse files Browse the repository at this point in the history
Decouple the checking of classes and global objects. The
`Ysafe-init-global` flag can be used for checking global objects
instead.
  • Loading branch information
olhotak authored Nov 27, 2023
2 parents 5fb2299 + 3fac025 commit 41e7d95
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1140,7 +1140,7 @@ object Semantic:
*/
def checkClasses(classes: List[ClassSymbol])(using Context): Unit =
given Cache.Data()
for classSym <- classes if isConcreteClass(classSym) do
for classSym <- classes if isConcreteClass(classSym) && !classSym.isStaticObject do
checkClass(classSym)

// ----- Semantic definition --------------------------------
Expand Down
2 changes: 2 additions & 0 deletions tests/init-global/neg/i15883.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
val a = b // error
val b = 1
2 changes: 1 addition & 1 deletion tests/init/neg/apply2.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
object O:
class O:
case class A(b: B):
println(n)

Expand Down
10 changes: 5 additions & 5 deletions tests/init/neg/final-fields.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ trait U {
val f2: Int
}

object Test0 extends U {
class Test0 extends U {
final val f1 = 1
final val f2 = 2
final val f3 = f1 + f2
val f4: 3 = f3
}

object Test1 extends U {
class Test1 extends U {
final val f1 = 1
final val f3 = f1 + f2
final val f2 = 2
Expand All @@ -28,7 +28,7 @@ object Test1 extends U {

}

object Test extends T {
class Test extends T {
override final val f1 = /*super.f1*/ 1 + f2 // error
override final val f2 = 2 // error
override final val f3 = {println(3); 3} // error
Expand All @@ -37,7 +37,7 @@ object Test extends T {
def g: 3 = { println("g"); 3 }
final val x = g + 1
def main(args: Array[String]): Unit = {
Test0
Test1
new Test0
new Test1
}
}
25 changes: 15 additions & 10 deletions tests/init/neg/i12544.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@ enum Enum:
case Case
case Case2(x: Int)

def g(b: Enum.B): Int = b.foo()
class Outer:
val e = new Enum2

object Enum:
object nested:
val a: Enum = Case
class Enum2:
class nested:
val a: Enum = Enum.Case

val b: Enum = f(nested.a)
val b: Enum = f((new nested).a)

def f(e: Enum): Enum = e
def f(e: Enum): Enum = e

class B() { def foo() = n + 1 }
g(new B()) // error
val n: Int = 10
class B() { def foo() = n + 1 }
def g(b: B): Int = b.foo()
g(new B()) // error
val n: Int = 10

@main def main(): Unit = println(Enum.b)
@main def main(): Unit = {
val o = new Outer
print(o.e.b)
}
2 changes: 0 additions & 2 deletions tests/init/neg/i15883.scala

This file was deleted.

2 changes: 1 addition & 1 deletion tests/init/neg/i4031.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
object App {
class App {
trait A { type L >: Any}
def upcast(a: A, x: Any): a.L = x
val p: A { type L <: Nothing } = p // error
Expand Down
2 changes: 1 addition & 1 deletion tests/init/neg/inner30.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
object Scanners {
class Scanners {
enum IndentWidth {
case Run(ch: Char, n: Int)
case Conc(l: IndentWidth, r: Run)
Expand Down
31 changes: 17 additions & 14 deletions tests/init/neg/inner9.scala
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
object Flags {
class Inner {
println(b)
}
class Outer:
val flags = new Flags // error

new Flags.Inner
class Flags {
class Inner {
println(b)
}

val a = this.b + 3
val b = 5 // error
}
new flags.Inner

object Flags2 {
class Inner {
println(b)
val a = this.b + 3
val b = 5 // error
}

class Flags2 {
class Inner {
println(b)
}


lazy val a = 3
val b = 5
}
lazy val a = 3
val b = 5
}
2 changes: 1 addition & 1 deletion tests/init/neg/leak-warm.check
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
19 | val l2 = l.map(_.m()) // error
| ^^^^^^^^^^^^
| Call method method map on an uninitialized (Cold) object. Calling trace:
| ├── object leakWarm { [ leak-warm.scala:1 ]
| ├── class leakWarm { [ leak-warm.scala:1 ]
| │ ^
| └── val l2 = l.map(_.m()) // error [ leak-warm.scala:19 ]
| ^^^^^^^^^^^^
2 changes: 1 addition & 1 deletion tests/init/neg/leak-warm.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
object leakWarm {
class leakWarm {
abstract class A(tag: Int) {
class B(x: Int) {
val y = x
Expand Down
10 changes: 5 additions & 5 deletions tests/init/neg/t3273.check
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
-- Error: tests/init/neg/t3273.scala:4:42 ------------------------------------------------------------------------------
4 | val num1: LazyList[Int] = 1 #:: num1.map(_ + 1) // error
| ^^^^^^^^^^^^^^^
|Could not verify that the method argument is transitively initialized (Hot). It was found to be a function where "this" is (the original object of type (object Test) where initialization checking started). Only transitively initialized arguments may be passed to methods (except constructors). Calling trace:
|├── object Test { [ t3273.scala:3 ]
|Could not verify that the method argument is transitively initialized (Hot). It was found to be a function where "this" is (the original object of type (class Test) where initialization checking started). Only transitively initialized arguments may be passed to methods (except constructors). Calling trace:
|├── class Test { [ t3273.scala:3 ]
|│ ^
|└── val num1: LazyList[Int] = 1 #:: num1.map(_ + 1) // error [ t3273.scala:4 ]
| ^^^^^^^^^^^^^^^
Expand All @@ -14,13 +14,13 @@
-- Error: tests/init/neg/t3273.scala:5:61 ------------------------------------------------------------------------------
5 | val num2: LazyList[Int] = 1 #:: num2.iterator.map(_ + 1).to(LazyList) // error
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|Could not verify that the method argument is transitively initialized (Hot). It was found to be a function where "this" is (the original object of type (object Test) where initialization checking started). Only transitively initialized arguments may be passed to methods (except constructors). Calling trace:
|├── object Test { [ t3273.scala:3 ]
|Could not verify that the method argument is transitively initialized (Hot). It was found to be a function where "this" is (the original object of type (class Test) where initialization checking started). Only transitively initialized arguments may be passed to methods (except constructors). Calling trace:
|├── class Test { [ t3273.scala:3 ]
|│ ^
|└── val num2: LazyList[Int] = 1 #:: num2.iterator.map(_ + 1).to(LazyList) // error [ t3273.scala:5 ]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|Promoting the value to transitively initialized (Hot) failed due to the following problem:
|Access non-initialized value num2. Promotion trace:
|└── val num2: LazyList[Int] = 1 #:: num2.iterator.map(_ + 1).to(LazyList) // error [ t3273.scala:5 ]
| ^^^^
| ^^^^
2 changes: 1 addition & 1 deletion tests/init/neg/t3273.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import scala.language.implicitConversions

object Test {
class Test {
val num1: LazyList[Int] = 1 #:: num1.map(_ + 1) // error
val num2: LazyList[Int] = 1 #:: num2.iterator.map(_ + 1).to(LazyList) // error

Expand Down

0 comments on commit 41e7d95

Please sign in to comment.