Skip to content

vals in companion classes are not initialized in the emitted code #477

Closed
@smarter

Description

@smarter
object OV {
  val x = "foo"
}
> run -Xprint:genBCode try/OV.scala
[info] Running dotty.tools.dotc.Main -Xprint:genBCode try/OV.scala
[info] result of try/OV.scala after genBCode:
[info] package <empty> {
[info]   final lazy module val OV: OV$ = new OV$()
[info]   final module class OV$ extends Object { this: <notype> => 
[info]     def <init>(): OV$ = {
[info]       super()
[info]       OV.x$$local = "foo"
[info]       ()
[info]     }
[info]     private val x$$local: String
[info]     <accessor> def x(): String = OV.x$$local
[info]   }
[info] }
% javap -p -c OV\$
Compiled from "OV.scala"
public final class OV$ {
  public static final OV$ MODULE$;

  private final java.lang.String x$$local;

  public static {};
    Code:
       0: new           #2                  // class OV$
       3: invokespecial #14                 // Method "<init>":()V
       6: return

  public OV$();
    Code:
       0: aload_0
       1: invokespecial #15                 // Method java/lang/Object."<init>":()V
       4: aload_0
       5: putstatic     #17                 // Field MODULE$:LOV$;
       8: ldc           #19                 // String foo
      10: astore_1
      11: return

  public java.lang.String x();
    Code:
       0: aload_0
       1: getfield      #24                 // Field x$$local:Ljava/lang/String;
       4: areturn
}

Note that in the constructor for OV$, "foo" is never actually stored in x, astore_1 is used instead of putfield.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions