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

Fix VarianceChecker's relativeVariance #21627

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/src/dotty/tools/dotc/printing/Formatting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ object Formatting {
def show(x: H *: T) =
CtxShow(toStr(x.head) *: toShown(x.tail).asInstanceOf[Tuple])

given Show[typer.VarianceChecker.VarianceError] with
def show(x: typer.VarianceChecker.VarianceError) =
CtxShow("VarianceError(" + toStr(x.tvar) + ", " + toStr(x.required) + ")")

given Show[FlagSet] with
def show(x: FlagSet) = x.flagsString

Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/VarianceChecker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ class VarianceChecker(using Context) {

/** The variance of a symbol occurrence of `tvar` seen at the level of the definition of `base`.
* The search proceeds from `base` to the owner of `tvar`.
* Initially the state is covariant, but it might change along the search.
* Initially the state is the accumulator's variance, but it might change along the search.
*/
def relativeVariance(tvar: Symbol, base: Symbol, v: Variance = Covariant): Variance = /*trace(i"relative variance of $tvar wrt $base, so far: $v")*/
def relativeVariance(tvar: Symbol, base: Symbol, v: Variance = varianceFromInt(variance)): Variance = /*trace(i"relative variance of $tvar wrt $base, so far: $v")*/
if base == tvar.owner then
v
else if base.is(Param) && base.owner.isTerm && !base.owner.isAllOf(PrivateLocal) then
Expand Down
4 changes: 4 additions & 0 deletions tests/neg/i21625.alt4.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Error: tests/neg/i21625.alt4.scala:2:11 -----------------------------------------------------------------------------
2 | def test[F[_ >: L] >: Unit]: Unit // error
| ^^^^^^^^^^^^^^^^^
| covariant type L occurs in contravariant position in type [_$1 >: L] >: Unit of type F
3 changes: 3 additions & 0 deletions tests/neg/i21625.alt4.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
trait Foo[+L] {
def test[F[_ >: L] >: Unit]: Unit // error
}
3 changes: 3 additions & 0 deletions tests/pos/i21625.alt1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
trait Foo[-L] {
def test[F[_ >: L]]: Unit
}
3 changes: 3 additions & 0 deletions tests/pos/i21625.alt2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
trait Foo[-L] {
def test[F[_ >: L] >: Unit]: Unit
}
3 changes: 3 additions & 0 deletions tests/pos/i21625.alt3.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
trait Foo[-L] {
def test[F[_ >: L] >: Nothing]: Unit
}
3 changes: 3 additions & 0 deletions tests/pos/i21625.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
trait Foo[-L] {
def test[LF, F[_ >: L] >: LF]: Unit
}
Loading