Skip to content

Inferred type of transparent def is RHS of opaque type #20302

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
tschuchortdev opened this issue Apr 30, 2024 · 3 comments
Closed

Inferred type of transparent def is RHS of opaque type #20302

tschuchortdev opened this issue Apr 30, 2024 · 3 comments

Comments

@tschuchortdev
Copy link

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 tschuchortdev added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Apr 30, 2024
@Gedochao Gedochao added area:inline area:opaque-types and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels May 9, 2024
@erikerlandson
Copy link

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

@tschuchortdev
Copy link
Author

tschuchortdev commented May 21, 2024

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.

@som-snytt
Copy link
Contributor

There is discussion of the intersection and links at the other ticket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants