Skip to content

Unnecessary boxing when calling > on complex int type #15405

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

Closed
scf37 opened this issue Jun 8, 2022 · 2 comments · Fixed by #15416
Closed

Unnecessary boxing when calling > on complex int type #15405

scf37 opened this issue Jun 8, 2022 · 2 comments · Fixed by #15416

Comments

@scf37
Copy link

scf37 commented Jun 8, 2022

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;
            }
        };
@scf37 scf37 added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jun 8, 2022
@nicolasstucki
Copy link
Contributor

nicolasstucki commented Jun 9, 2022

Minimization

type Validated[A] = A
def foo(v: Int|Validated[Int]): Unit = v > 43

Problem

Even though the type of v is upper-bounded by Int we use the intWrapper for the > operation. Resulting with the following program after typing

type Validated[A >: Nothing <: Any] = A
def foo(v: Int | Validated[Int]): Unit = {
  intWrapper(v).>(43)
  ()
}

This is the source of the boxing as the operation will erase into RichInt.

Note that Int | Validated[Int] is correctly erased to Int.

@nicolasstucki nicolasstucki added area:typer itype:enhancement and removed stat:needs triage Every issue needs to have an "area" and "itype" label itype:bug labels Jun 9, 2022
@nicolasstucki nicolasstucki changed the title Unnecessary boxing when inlining complex types Unnecessary boxing when calling > on complex int type Jun 9, 2022
@nicolasstucki
Copy link
Contributor

Workaround

-      val v = r.read
+      val v: Int = r.read

odersky added a commit that referenced this issue Jun 15, 2022
Fix #15405: Dealias Or type constituents when finding its dominator
@Kordyjan Kordyjan added this to the 3.2.0 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants