Open
Description
In some situations the compiler delays module loads, which is not according to the spec.
Examples:
- for a case class
A
,A.apply
is replaced bynew A
SI-5304 - accessing a nested module doesn't initialize the outer module:
object O { ???; object I }; O.I
- the inliner currently does not delay module initializations when inlining a method defined in a module, the module is loaded before the inlined method body
Changing this to follow the spec is not straightforward, see discussion on the above issue, and also linked issues.
The same question arises for static initializers of java classes.
For example, the inliner currently delays class initialization when inlining a static method.
$ cat Test.java
public class Test {
static { System.out.println("Test init"); }
public static int m() { return 1; }
}
$ cat Test.scala
object A extends App { println(Test.m) }
$ javac Test.java
$ qsc Test.scala
$ qs A
Test init
1
$ qsc -Yopt:l:classpath -Yopt-inline-heuristics:everything Test.scala
$ qs A
1
A related question is whether the compiler can assume that a module load is always non-null, which is not the case SI-9655.
The scala-js optimizer for example makes this assumption (scala/scala#2954 (comment)).