Skip to content

Circular object dependency results in null field #9261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
scabug opened this issue Apr 4, 2015 · 1 comment
Open

Circular object dependency results in null field #9261

scabug opened this issue Apr 4, 2015 · 1 comment
Labels

Comments

@scabug
Copy link

scabug commented Apr 4, 2015

The following code compiles without errors but results in a field unexpectedly being null:

$ scala
Welcome to Scala version 2.11.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_40).
scala> :paste
// Entering paste mode (ctrl-D to finish)

sealed abstract class OrderType(val reverse: OrderType)
case object Buy extends OrderType(Sell)
case object Sell extends OrderType(Buy)

// Exiting paste mode, now interpreting.

defined class OrderType
defined object Buy
defined object Sell

scala> Buy.reverse
res0: OrderType = Sell

scala> Sell.reverse
res1: OrderType = null

(May or may not be related to #9115 and/or #5366.)

@scabug
Copy link
Author

scabug commented Apr 4, 2015

Imported From: https://issues.scala-lang.org/browse/SI-9261?orig=1
Reporter: Mirko Nasato (mnasato)
Affected Versions: 2.11.6
See #5366, #9115

@scabug scabug added the backend label Apr 7, 2017
liufengyun added a commit to scala/scala3 that referenced this issue Jun 16, 2023
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
#11262
scala/bug#9312
scala/bug#9115
scala/bug#9261
scala/bug#5366
scala/bug#9360
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant