Skip to content
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

Opaque type parameter partially loses opacity when compiled separately #21946

Open
gaelrenoux opened this issue Nov 13, 2024 · 2 comments
Open

Comments

@gaelrenoux
Copy link

gaelrenoux commented Nov 13, 2024

Compiler version

3.5.2

Minimized code

src/main/scala/definition.scala

opaque type SlotId = Int
opaque type TopicId = Int

opaque type SmallIdSet[I] = Long
extension [I](s: SmallIdSet[I]) {
  inline def has(id: I): Boolean = ???
}

src/test/scala/usage.scala

val id: TopicId = ???

val smallIdSet: SmallIdSet[SlotId] = ???
smallIdSet.has(id)

Output

The code compiles.

Expectation

The code shouldn't compile, as the expected argument for has is of type SlotId.

Further investigations

If both files are compiled together (both in src/main/scala or both in src/test/scala), the code doesn't compile, as expected.

The following line doesn't compile in usage.scala, as expected:

val otherSmallIdSet: SmallIdSet[TopicId] = smallIdSet

If defining the opaque type using a parameterized type in definition.scala, for example:

opaque type SmallIdSet[I] = Set[I]

the final line of usage.scala doesn't compile, as expected.

@gaelrenoux gaelrenoux added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Nov 13, 2024
@som-snytt
Copy link
Contributor

som-snytt commented Nov 13, 2024

The symptom requires "separate compilation", not just "different compilation units" (meaning source files).

➜  snips scalac -d ~/sandbox i21946a.scala
➜  snips scalac -d ~/sandbox -cp ~/sandbox i21946b.scala
➜  snips scalac -d ~/sandbox i21946*.scala
-- [E007] Type Mismatch Error: i21946b.scala:5:23 ------------------------------
5 |def f = smallIdSet.has(id)
  |                       ^^
  |                       Found:    (id : TopicId)
  |                       Required: SlotId

@gaelrenoux gaelrenoux changed the title Opaque type parameter partially loses opacity when in different compilation unit Opaque type parameter partially loses opacity when in compiled separately Nov 13, 2024
@gaelrenoux gaelrenoux changed the title Opaque type parameter partially loses opacity when in compiled separately Opaque type parameter partially loses opacity when compiled separately Nov 13, 2024
@gaelrenoux
Copy link
Author

Fixed the title and description, sorry for the vocabulary confusion.

@Gedochao Gedochao added area:opaque-types and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Nov 14, 2024
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

3 participants