Skip to content

Value classes' extractable methods don't follow the pattern in the SIP #22495

Closed as not planned
@hamzaremmal

Description

@hamzaremmal

Compiler version

590691b

Minimized code

class Bar(val self: Short) extends AnyVal {
    private def foo = ???
}

Output

Scala 3

[[syntax trees at end of                  genBCode]] // playground.scala
package <empty> {
  @SourceFile("playground.scala") final class Bar extends Object {
    def <init>(self: Short): Unit =
      {
        this.self = self
        super()
        ()
      }
    override def hashCode(): Int = Bar.hashCode$extension(this.self())
    override def equals(x$0: Object): Boolean =
      Bar.equals$extension(this.self(), x$0)
    private val self: Short
    def self(): Short = this.self
    private def foo(): Nothing = Bar.Bar$$$foo$extension(this.self())
  }
  @SourceFile("playground.scala") final module class Bar extends Object {
    def <init>(): Unit =
      {
        super()
        ()
      }
    private def writeReplace(): Object =
      new scala.runtime.ModuleSerializationProxy(classOf[Bar])
    final def hashCode$extension($this: Short): Int =
      Short.box($this).hashCode()
    final def equals$extension($this: Short, x$0: Object): Boolean =
      matchResult1[Boolean]: 
        {
          case val x1: Object = x$0
          if x1.isInstanceOf[Bar] then
            {
              case val x$0: Short =
                if x1.eq(null) then Short.unbox(null) else 
                  x1.asInstanceOf[Bar].self()
              return[matchResult1] $this.==(x$0)
            }
           else ()
          return[matchResult1] false
        }
    final def Bar$$$foo$extension($this: Short): Nothing = ???()
  }
  final lazy module val Bar: Bar = new Bar()
}

Scala 2

[[syntax trees at end of                       jvm]] // playground.scala
package <empty> {
  final class Bar extends Object {
    <paramaccessor> private[this] val self: Short = _;
    <stable> <accessor> <paramaccessor> def self(): Short = Bar.this.self;
    private def foo(): Nothing = Bar.foo$extension(Bar.this.self());
    override <synthetic> def hashCode(): Int = Bar.hashCode$extension(Bar.this.self());
    override <synthetic> def equals(x$1: Object): Boolean = Bar.equals$extension(Bar.this.self(), x$1);
    def <init>(self: Short): Bar = {
      Bar.this.self = self;
      Bar.super.<init>();
      ()
    }
  };
  <synthetic> object Bar extends Object {
    final def foo$extension($this: Short): Nothing = scala.Predef.???();
    final <synthetic> def hashCode$extension($this: Short): Int = java.lang.Short.hashCode($this);
    final <synthetic> def equals$extension($this: Short, x$1: Object): Boolean = {
  case <synthetic> val x1: Object = x$1;
  case5(){
    if (x1.$isInstanceOf[Bar]())
      matchEnd4(true)
    else
      case6()
  };
  case6(){
    matchEnd4(false)
  };
  matchEnd4(x: Boolean){
    x
  }
}.&&({
      <synthetic> val Bar$1: Short = x$1.$asInstanceOf[Bar]().self();
      $this.==(Bar$1)
    });
    def <init>(): Bar.type = {
      Bar.super.<init>();
      ()
    }
  }
}

Expectation

Dotty should follow SIP-15 and Scala 2 and generate a method called foo$extension not Bar$$$foo$extension

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions