scala3 icon indicating copy to clipboard operation
scala3 copied to clipboard

Inferred type of transparent def is RHS of opaque type

Open tschuchortdev opened this issue 1 year ago • 2 comments

Compiler version

3.3.3

Minimized code

opaque type Opaque = Int
transparent inline def op: Opaque = 123

object Main {
   def main(args: Array[String]): Unit = {
      val o: Opaque = 123 // OK: does not compile
      val o2: Opaque = op //  BUG: Does not compile because inferred type is int, but should compile
   }
}

Expectation

In my opinion, the inferred type of o2 should be Opaque since Opaque is not transparent within Main. If the RHS of the opaque type is a private class, it manages to escape its scope and is inferred here even if it is not visible.

tschuchortdev avatar Apr 30 '24 17:04 tschuchortdev

I think the actual inferred type is Opaque & Int, see #20434

erikerlandson avatar May 21 '24 15:05 erikerlandson

It depends. If the type were Opaque & Int, then val o2: Opaque = op should compile, right? I encountered this problem mostly when writing transparent def macros and found that when the written type of the transparent method is fully qualified, then an intersection is inferred; when it is not fully qualified (and the transparent method is defined in the opaque type's transparent scope), then only the RHS is inferred. So it definitely also depends on the scope of the transparent method and possibly other factors.

package com.example
opaque type Opaque = Int
transparent inline def op: com.example.Opaque = 123

would probably infer val o2: Opaque & Int.

tschuchortdev avatar May 21 '24 17:05 tschuchortdev