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

Invalid variance inferred / variance check failed #21625

Open
hobwekiva opened this issue Sep 22, 2024 · 2 comments · May be fixed by #21627
Open

Invalid variance inferred / variance check failed #21625

hobwekiva opened this issue Sep 22, 2024 · 2 comments · May be fixed by #21627
Assignees
Labels
area:variance Issues related to covariance & contravariance. itype:bug

Comments

@hobwekiva
Copy link

hobwekiva commented Sep 22, 2024

Compiler version

3.5.0

Minimized code

trait Foo[-L] {
  def test[LF, F[_ >: L] >: LF]: Unit
}

Output

contravariant type L occurs in covariant position in type [_$1 >: L] >: LF of type F

Expectation

Should compile. Does compile in Scala 2.

This works for example and I am not sure that the outer lower bound should change anything.

trait Foo[-L] {
  def test[F[_ >: L]]: Unit
}

This also doesn't work

trait Foo[-L] {
  def test[F[_ >: L] >: Unit]: Unit
}

But this does:

trait Foo[-L] {
  def test[F[_ >: L] >: Nothing]: Unit
}
@hobwekiva hobwekiva added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Sep 22, 2024
@hobwekiva
Copy link
Author

Even funnier:

trait Foo[+L] {
  def test[F[_ >: L] >: Unit]: Unit
}

this complains that a covariant type occurs in ... contravariant position.

@dwijnand dwijnand self-assigned this Sep 23, 2024
@dwijnand dwijnand removed the stat:needs triage Every issue needs to have an "area" and "itype" label label Sep 23, 2024
@dwijnand dwijnand linked a pull request Sep 23, 2024 that will close this issue
@dwijnand
Copy link
Member

Firstly F[_ >: L] >: LF is our syntax for F >: [_ >: L] =>> LF. When we check the lower bound we flip to contravariance. But when we try to figure out what the variance of that L is, we accidentally bias to being in a covariant position, when we're actually in contravariant position. So seems like it should be an easy fix (waiting for CI).

@Gedochao Gedochao added the area:variance Issues related to covariance & contravariance. label Sep 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:variance Issues related to covariance & contravariance. itype:bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants