Skip to content

Commit

Permalink
Fix VarianceChecker's relativeVariance
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Sep 23, 2024
1 parent 4293a45 commit 865f15d
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 2 deletions.
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
}

0 comments on commit 865f15d

Please sign in to comment.