Skip to content

Commit f6c6ec6

Browse files
authored
Add a test for global init checker (scala#21694)
Add a test for global init checker
2 parents ebbd685 + ca7553e commit f6c6ec6

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

tests/init-global/warn/Color.scala

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
enum Color:
2+
case None, White, Black
3+
4+
enum Player:
5+
case Black, White
6+
7+
// Explanation: See the desugaring below
8+
val color: Color =
9+
if this == Player.Black // warn
10+
then Color.Black
11+
else Color.White
12+
13+
// From the desugaring of Player, we can see the field `Player.Black` is not yet
14+
// initialized during evaluation of the first `new Player`:
15+
//
16+
// class Player:
17+
// val color: Color =
18+
// if this == Player.Black ...
19+
//
20+
// object Player:
21+
// val Black: Player = new Player // <--- problem
22+
// val White: Player = new Player
23+
//
24+
//
25+
// The complex desugaring makes it difficult to see the initialization
26+
// semantics and it is prone to make such hard-to-spot mistakes.
27+
//
28+
// Note: The desugaring above is simplified for presentation.

0 commit comments

Comments
 (0)