Skip to content

Commit 7393a9b

Browse files
committed
Add test
1 parent 2cb6965 commit 7393a9b

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ class CompilationTests {
243243
compileFilesInDir("tests/explicit-nulls/run", explicitNullsOptions)
244244
}.checkRuns()
245245

246-
// initialization tests
246+
// initialization tests for global objects
247247
@Test def checkInitGlobal: Unit = {
248248
implicit val testGroup: TestGroup = TestGroup("checkInitGlobal")
249249
compileFilesInDir("tests/init-global/warn", defaultOptions.and("-Ysafe-init-global"), FileFilter.exclude(TestSources.negInitGlobalScala2LibraryTastyExcludelisted)).checkWarnings()
@@ -252,6 +252,20 @@ class CompilationTests {
252252
compileFilesInDir("tests/init-global/warn-tasty", defaultOptions.and("-Ysafe-init-global"), FileFilter.exclude(TestSources.negInitGlobalScala2LibraryTastyExcludelisted)).checkWarnings()
253253
compileFilesInDir("tests/init-global/pos-tasty", defaultOptions.and("-Ysafe-init-global", "-Werror"), FileFilter.exclude(TestSources.posInitGlobalScala2LibraryTastyExcludelisted)).checkCompile()
254254
end if
255+
256+
locally {
257+
val group = TestGroup("checkInitGlobal/tastySource")
258+
val tastSourceOptions = defaultOptions.and("-Ysafe-init-global")
259+
val outDirLib = defaultOutputDir + group + "/A/tastySource/A"
260+
261+
// Set -sourceroot such that the source code cannot be found by the compiler
262+
val libOptions = tastSourceOptions.and("-sourceroot", "tests/init-global/special/tastySource")
263+
val lib = compileFile("tests/init-global/special/tastySource/A.scala", libOptions)(group).keepOutput.checkCompile()
264+
265+
compileFile("tests/init-global/special/tastySource/B.scala", tastSourceOptions.withClasspath(outDirLib))(group).checkWarnings()
266+
267+
lib.delete()
268+
}
255269
}
256270

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

0 commit comments

Comments
 (0)