Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,17 @@ trait PatternTypeConstrainer { self: TypeComparer =>
val result =
tyconS.typeParams.lazyZip(argsS).lazyZip(argsP).forall { (param, argS, argP) =>
val variance = param.paramVarianceSign
if variance != 0 && !assumeInvariantRefinement then true
else {
if variance == 0 || assumeInvariantRefinement ||
// As a special case, when pattern and scrutinee types have the same type constructor,
// we infer better bounds for pattern-bound abstract types.
argP.typeSymbol.isPatternBound && patternTp.classSymbol == scrutineeTp.classSymbol
then
val TypeBounds(loS, hiS) = argS.bounds
var res = true
if variance < 1 then res &&= isSubType(loS, argP)
if variance > -1 then res &&= isSubType(argP, hiS)
res
}
else true
}
if !result then
constraint = saved
Expand Down
4 changes: 4 additions & 0 deletions tests/pos/i14739.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
abstract class Foo[+A]:
def head: A
def foo[T](xs: Foo[T]): T = xs match
case xs: Foo[u] => xs.head: u
4 changes: 4 additions & 0 deletions tests/pos/i14739.works.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
abstract class Foo[T]:
def head: T
def foo(xs: Foo[T]): T = xs match
case xs: Foo[u] => xs.head: u