Skip to content

Heal pattern-bound type by gathering constraints #15095

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

Merged
merged 2 commits into from
May 23, 2022
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