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

Invariant opaque type refinement loss across external dependency #12945

Closed
soronpo opened this issue Jun 26, 2021 · 6 comments · Fixed by #13206
Closed

Invariant opaque type refinement loss across external dependency #12945

soronpo opened this issue Jun 26, 2021 · 6 comments · Fixed by #13206

Comments

@soronpo
Copy link
Contributor

soronpo commented Jun 26, 2021

Edit:

I managed to further minimize the example.


It seems that type inference behaves differently across test dependency and fails to reduce (possibly any kind of different module dependency is affected).

Compiler version

v3.0.1-RC2 (also checked on nightly 3.0.2-RC1-bin-20210624-ecbe3d2-NIGHTLY)

Minimized code

Minimized project at: https://github.com/soronpo/dottybug/tree/invariance_widen_err

Main.scala

opaque type Lie[W <: Int] = Int
object Lie:
  trait TC[-T]:
    type Out
  object TC:
    given [W <: Int]: TC[Lie[W]] with
      type Out = W

val x  = summon[Lie.TC[Lie[7]]]
val works = summon[x.Out =:= 7]

Test.scala //must be placed at scala/test

object Test:
  val x  = summon[Lie.TC[Lie[7]]]
  val fails = summon[x.Out =:= 7]

Output

[error] -- Error: C:\IdeaProjects\dottybug\src\test\scala\Test.scala:3:33
[error] 3 |  val fails = summon[x.Out =:= 7]
[error]   |                                 ^
[error]   |                 Cannot prove that Test.x.Out =:= (7 : Int).
[error] one error found

Expectation

No error. Notice the same expression works within the main module and fails within the test module.

Note

Opaque types seem to be part of the problem, since if we change Lie to a trait the error goes away.
The contravariance in Lie.TC is also crucial for the error, but it should not cause widening because Lie is invariant.

@SethTisue SethTisue changed the title Loosing invariant type refinement across test dependency Losing invariant type refinement across test dependency Jun 30, 2021
@SethTisue SethTisue changed the title Losing invariant type refinement across test dependency Losing invariant type refinement across external dependency Jun 30, 2021
@SethTisue
Copy link
Member

SethTisue commented Jun 30, 2021

(Dale and I briefly wondered if works works because it can see inside the opaque type (being in the same scope), but moving the opaque type to an inner scope doesn't break works, so apparently separate compilation is a key ingredient here.)

@soronpo soronpo changed the title Losing invariant type refinement across external dependency Invariant opaque type refinement loss across external dependency Jul 18, 2021
@soronpo
Copy link
Contributor Author

soronpo commented Jul 18, 2021

I updated the title to reflect this is an opaque type issue

@joroKr21
Copy link
Member

Another issue with separate compilation of opaque types: #13001

@anatoliykmetyuk anatoliykmetyuk added the stat:needs minimization Needs a self contained minimization label Jul 20, 2021
@anatoliykmetyuk
Copy link
Contributor

It would be good to narrow the original example down a bit. Also, it would be good to see if it can be expressed in terms of separate compilation – using scalac only, factoring out SBT.

@anatoliykmetyuk anatoliykmetyuk assigned soronpo and unassigned sjrd Jul 20, 2021
@soronpo
Copy link
Contributor Author

soronpo commented Jul 20, 2021

@anatoliykmetyuk I further minimized the example. See updated OP.
I also verified that when running the compilation manually via scala3-compiler, I still get the error.

# scala3-compiler src\main\scala\Main.scala                                                                             
# scala3-compiler src\test\scala\Test.scala                                                                             
-- Error: src\test\scala\Test.scala:3:33 -------------------------------------------------------------------------------
3 |  val fails = summon[x.Out =:= 7]                                                                                    
  |                                 ^                                                                                   
  |                                 Cannot prove that Test.x.Out =:= (7 : Int).                                         
1 error found                                                                                                           

@soronpo soronpo assigned anatoliykmetyuk and unassigned soronpo Jul 20, 2021
@soronpo soronpo removed the stat:needs minimization Needs a self contained minimization label Jul 20, 2021
@anatoliykmetyuk anatoliykmetyuk removed their assignment Jul 20, 2021
@anatoliykmetyuk
Copy link
Contributor

Looks good!

odersky added a commit to dotty-staging/dotty that referenced this issue Jul 29, 2021
odersky added a commit to dotty-staging/dotty that referenced this issue Jul 30, 2021
odersky added a commit to dotty-staging/dotty that referenced this issue Jul 30, 2021
tanishiking pushed a commit to tanishiking/scala3 that referenced this issue Aug 10, 2021
smarter pushed a commit to dotty-staging/dotty that referenced this issue Aug 11, 2021
@Kordyjan Kordyjan added this to the 3.1.0 milestone Aug 2, 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.

7 participants