scala3 icon indicating copy to clipboard operation
scala3 copied to clipboard

Compiletime ops bounds fail inside of inlined def

Open soronpo opened this issue 3 years ago • 1 comments

If we remove inline from def check then the code compiles.

Compiler version

v3.2.0-RC2

Minimized code

https://scastie.scala-lang.org/vb2kekoaSO6lBZzwIQl8fw

import compiletime.ops.*

class Inlined[T](val value : T)
object Inlined:
  trait TC[UB, R]:
    type Out <: UB
    def apply(arg: R): Inlined[Out]
  object TC:
    transparent inline given fromVal[UB, R <: UB]: TC[UB, R] = new TC[UB, R]:
      type Out = R
      def apply(arg: R): Inlined[R] = forced[R](arg)
    transparent inline given fromInline[UB, R <: UB, I <: Inlined[R]]: TC[UB, I] =
      new TC[UB, I]:
        type Out = R
        def apply(arg: I): Inlined[R] = arg

  protected inline def forced[T](_value: Any): Inlined[T] = Inlined[T](_value.asInstanceOf[T])

  def add[T <: Int, R](lhs: Inlined[T], rhs: R)(using tc: TC[Int, R]) =
      forced[int.+[T, tc.Out]](lhs.value + tc(rhs).value)
  def get[T <: Int](inlined: Inlined[T]) : T = inlined.value

object Test:
  inline def check[UB <: Int](ub: Inlined[UB]): Unit =
    val y = Inlined.get(Inlined.add(ub, 1)) //error

Output

Found:    Playground.Inlined[UB + ?2.Out]
Required: Playground.Inlined[T]

where:    ?2 is an unknown value of type Playground.Inlined.TC[Int, Int]
          T  is a type variable which is an alias of UB + (param)1
          UB is a type in method check with bounds <: Int

Expectation

No error

soronpo avatar Aug 01 '22 19:08 soronpo

@smarter, since you're experimenting with Named Tuples, this issue may affect you because named tuples are implemented with the same compiletime.ops mechanism.

soronpo avatar May 23 '24 00:05 soronpo