Skip to content

Unnecessary boxing when calling > on complex int type #15405

Closed
@scf37

Description

@scf37

Compiler version

3.1.2

Minimized code

import scala.compiletime.constValue

object Main {
  object types:
    type Validated[A] = A
    opaque type Max[A, max] <: A with Validated[A] = A
  import types.*

  trait Reader[A]:
    def read: A

  given Reader[Int] with
    def read: Int = 1

  inline given [max <: Int, V <: Int|Validated[Int], R <: Reader[V]](using r: R): Reader[Max[V, max]] = new Reader:
    def read: Max[V, max] =
      val v = r.read
      val limit = constValue[max]
      if (v > limit) v.asInstanceOf[Max[V, max]] else limit.asInstanceOf[Max[V, max]]

  val y = summon[Reader[Max[Int, 42]]]
}

Output

        y = new Main.Reader<Object>(){

            public int read() {
                int v = Main.given_Reader_Int$.MODULE$.read();
                int limit = 42;
                return new scala.runtime.RichInt(scala.Predef$.MODULE$.intWrapper(v)).$greater((Object)scala.runtime.BoxesRunTime.boxToInteger((int)42)) ? v : 42;
            }
        };

Expectation

        y = new Main.Reader<Object>(){

            public int read() {
                int v = Main.given_Reader_Int$.MODULE$.read();
                int limit = 42;
                return v > 42 ? v : 42;
            }
        };

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions