Skip to content

Inline methods of opaque types leave redundant proxies in byte code #21334

Open
@soronpo

Description

@soronpo

Compiler version

v3.4.2
v3.5.0-RC6

Minimized code

//> using scala 3.5.0-RC6
//> using option -Xprint:genBCode

opaque type MyOpaque <: Int = Int
object MyOpaque:
  inline def apply(inline value: Int): MyOpaque = value

class Foo:
  val x = MyOpaque(8)

Output

genBCode phase printing

package <empty> {
  final module class ID$package$MyOpaque extends Object {
    def <init>(): Unit =
      {
        super()
        ()
      }
    private def writeReplace(): Object =
      new scala.runtime.ModuleSerializationProxy(classOf[ID$package$MyOpaque])
  }
  @SourceFile("ID.scala") class Foo extends Object {
    def <init>(): Unit =
      {
        super()
        this.x =
          {
            val $proxy1: ID$package = ID$package:ID$package
            val ID$package$_this: ID$package = $proxy1
            8:Int
          }:Int
        ()
      }
    private val x: Int
    def x(): Int = this.x
  }
  @SourceFile("ID.scala") final module class ID$package extends Object {
    def <init>(): Unit =
      {
        super()
        ()
      }
    private def writeReplace(): Object =
      new scala.runtime.ModuleSerializationProxy(classOf[ID$package])
    final lazy module <static> val MyOpaque: ID$package$MyOpaque =
      new ID$package$MyOpaque()
  }
  final lazy module val ID$package: ID$package = new ID$package()
}

Foo.class byte code

public class Foo {
  private final int x;

  public Foo();
    Code:
       0: aload_0
       1: invokespecial #11                 // Method java/lang/Object."<init>":()V
       4: aload_0
       5: getstatic     #17                 // Field ID$package$.MODULE$:LID$package$;
       8: astore_1
       9: aload_1
      10: astore_2
      11: bipush        10
      13: putfield      #19                 // Field x:I
      16: return

  public int x();
    Code:
       0: aload_0
       1: getfield      #19                 // Field x:I
       4: ireturn
}

Expectation

The proxies should be removed, as they are completely redundant in this case. x should just get the value 8.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions