Skip to content

Clarify / unify delaying of module / class initializers by the compiler / optimizer #112

Open
@lrytz

Description

@lrytz

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 by new 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)).

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions