You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Compiled with both -Yexplicit-nulls and -Ysafe-init, both fields are null:
A.x ==nullB.y ==null
Expectation
I expected this not to compile. I'm not sure if detecting such cycles is in scope for -Ycheck-init, but the documentation for explicit nulls suggests that -Ycheck-init catches the unsoundness caused by null in initialization.
The text was updated successfully, but these errors were encountered:
The problem is illustrated by the example below:
``` Scala
class Foo(val opposite: Foo)
case object A extends Foo(B) // A -> B
case object B extends Foo(A) // B -> A
```
The check aims to be simple for programmers to understand, expressive,
fast, and sound.
The check is centered around two design ideas: (1) initialization-time
irrelevance; (2) partial ordering.
The check enforces the principle of _initialization-time irrelevance_,
which means that the time when a static object is initialized should not
change program semantics. For that purpose, it enforces the following
rule:
> **The initialization of a static object should not directly or
indirectly read or write mutable state owned by another static object**.
This principle not only puts the initialization of static objects on a
solid foundation but also avoids whole-program analysis.
Partial ordering means that the initialization dependencies of static
objects form a directed-acyclic graph (DAG). No cycles with length
bigger than 1 allowed --- which might lead to deadlocks in the presence
of concurrency and strong coupling & subtle contracts between objects.
Related Issues:
#16152#9176#11262scala/bug#9312scala/bug#9115scala/bug#9261scala/bug#5366scala/bug#9360
This can now be checked with the experimental option: -Ysafe-init-global.
liufengyun
changed the title
Cyclic initialization compiles with -Yexplicit-nulls and -Ycheck-init
Cyclic initialization compiles with -Yexplicit-nulls and -Ysafe-init
Jul 15, 2023
Compiler version
3.0.0-M3-bin-20201216-c8fb1fa-NIGHTLY-git-c8fb1fa
Minimized code
Output
Compiled with both
-Yexplicit-nulls
and-Ysafe-init
, both fields arenull
:Expectation
I expected this not to compile. I'm not sure if detecting such cycles is in scope for
-Ycheck-init
, but the documentation for explicit nulls suggests that-Ycheck-init
catches the unsoundness caused bynull
in initialization.The text was updated successfully, but these errors were encountered: