Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hamzaremmal opened this issue Feb 1, 2025 · 1 comment
Open
Assignees
Labels
area:spec area:value-classes Issues tied to value classes. itype:bug Spree Suitable for a future Spree

Comments

@hamzaremmal
Copy link
Member

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

@hamzaremmal hamzaremmal added itype:bug area:value-classes Issues tied to value classes. labels Feb 1, 2025
@hamzaremmal
Copy link
Member Author

hamzaremmal commented Feb 1, 2025

For now, I'm not considering this issue as a blocker for #22480 because value classes's extension methods are implementation details, and those methods are private so the resolution will always have the correct method to call.
Nevertheless, raising the issue to clear the specification once and for all.

@hamzaremmal hamzaremmal self-assigned this Feb 2, 2025
@hamzaremmal hamzaremmal added the Spree Suitable for a future Spree label Feb 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:spec area:value-classes Issues tied to value classes. itype:bug Spree Suitable for a future Spree
Projects
None yet
Development

No branches or pull requests

1 participant