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

Type inference failure for type variable in intersection type #12077

Closed
heaven-born opened this issue Apr 13, 2021 · 4 comments · Fixed by #18235
Closed

Type inference failure for type variable in intersection type #12077

heaven-born opened this issue Apr 13, 2021 · 4 comments · Fixed by #18235
Labels
backlog No work planned on this by the core team for the time being. compat:scala2 itype:bug
Milestone

Comments

@heaven-born
Copy link

Compiler version

2.13.5 and 3.0.0-RC2

Minimized code

This code compiles in 2.13.5 but not in 3.0.0-RC2.

    trait Wrapper[K]
    trait Has0[T]
    
    def test[R](v: Wrapper[Has0[String] with R]):R = ???

    val zz:Wrapper[Has0[String] with Has0[Int]] = ???
    test(zz)

Output

Error in 3.0.0-RC2

Found:    (zz : Wrapper[Has0[String] & Has0[Int]])
Required: Wrapper[Has0[String] & R]

where:    R is a type variable

      test(zz)

Expectation

@heaven-born
Copy link
Author

this issue and #12051 may have some connections

@smarter
Copy link
Member

smarter commented Apr 13, 2021

This is kind of a weird encoding (Has0[String] with Has0[Int] is an empty type like Nothing: because its type parameter is invariant and String is not the same type as Int, there's no value of this type), so I'm not sure how much we can or should do here. Alternatively, you could use unions:

trait Wrapper[K]
trait Has0[T]

object Test {
  def test[R](v: Wrapper[Has0[String] | R]): R = ???

  val zz: Wrapper[Has0[String] | Has0[Int]] = ???

  test(zz) // Infers R=Has0[Int]
}

@smarter smarter removed their assignment Apr 13, 2021
@heaven-born
Copy link
Author

heaven-born commented Apr 13, 2021

@smarter not sure if this is an issue or not, but it seems that ZIO uses this kind of encoding now. Except that K is contravariant. Like: ZIO[Has[Database] & Has[Logging],..,..]

@smarter smarter changed the title Intersection types are not compatible with some of existing code from 2.13.5 Type inference failure for type variable in intersection type Apr 13, 2021
@heaven-born
Copy link
Author

btw, the inference works for zero-kinded types val zz: Wrapper[Has0[String] & Int] = ???
And this is example of the library that use this type trick, so it will not be able to survive the migration.

@odersky odersky added the backlog No work planned on this by the core team for the time being. label Apr 8, 2022
odersky added a commit to dotty-staging/dotty that referenced this issue Jul 18, 2023
odersky added a commit that referenced this issue Jul 19, 2023
Fixes #18226
Fixes #12077 

Might fix some other reported issues with AndTypes as well.
@Kordyjan Kordyjan added this to the 3.4.0 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backlog No work planned on this by the core team for the time being. compat:scala2 itype:bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants