Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add test
  • Loading branch information
liufengyun committed Dec 8, 2025
commit 792eb7f3724434a4fd1ef25f480b672152b81ccc
16 changes: 15 additions & 1 deletion compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ class CompilationTests {
compileFilesInDir("tests/explicit-nulls/run", explicitNullsOptions)
}.checkRuns()

// initialization tests
// initialization tests for global objects
@Test def checkInitGlobal: Unit = {
implicit val testGroup: TestGroup = TestGroup("checkInitGlobal")
compileFilesInDir("tests/init-global/warn", defaultOptions.and("-Ysafe-init-global"), FileFilter.exclude(TestSources.negInitGlobalScala2LibraryTastyExcludelisted)).checkWarnings()
Expand All @@ -266,6 +266,20 @@ class CompilationTests {
compileFilesInDir("tests/init-global/warn-tasty", defaultOptions.and("-Ysafe-init-global"), FileFilter.exclude(TestSources.negInitGlobalScala2LibraryTastyExcludelisted)).checkWarnings()
compileFilesInDir("tests/init-global/pos-tasty", defaultOptions.and("-Ysafe-init-global", "-Werror"), FileFilter.exclude(TestSources.posInitGlobalScala2LibraryTastyExcludelisted)).checkCompile()
end if

locally {
val group = TestGroup("checkInitGlobal/tastySource")
val tastSourceOptions = defaultOptions.and("-Ysafe-init-global")
val outDirLib = defaultOutputDir + group + "/A/tastySource/A"

// Set -sourceroot such that the source code cannot be found by the compiler
val libOptions = tastSourceOptions.and("-sourceroot", "tests/init-global/special/tastySource")
val lib = compileFile("tests/init-global/special/tastySource/A.scala", libOptions)(group).keepOutput.checkCompile()

compileFile("tests/init-global/special/tastySource/B.scala", tastSourceOptions.withClasspath(outDirLib))(group).checkWarnings()

lib.delete()
}
}

// initialization tests
Expand Down
4 changes: 4 additions & 0 deletions tests/init-global/special/tastySource/A.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
object A:
def foo(fn: => Int) = bar(fn)

def bar(fn: => Int) = fn
13 changes: 13 additions & 0 deletions tests/init-global/special/tastySource/B.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- Warning: tests/init-global/special/tastySource/B.scala:4:14 ---------------------------------------------------------
4 | def bar = C.n * 3 // warn
| ^^^
|Reading mutable state of object C during initialization of object B.
|Reading mutable state of other static objects is forbidden as it breaks initialization-time irrelevance. Calling trace:
|├── object B: [ B.scala:1 ]
|│ ^
|├── A.scala:2
|├── A.scala:4
|├── var y = A.foo(bar) * 2 [ B.scala:2 ]
|│ ^^^
|└── def bar = C.n * 3 // warn [ B.scala:4 ]
| ^^^
7 changes: 7 additions & 0 deletions tests/init-global/special/tastySource/B.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
object B:
var y = A.foo(bar) * 2

def bar = C.n * 3 // warn

object C:
var n = 10