-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 spurious subtype check pruning when both sides have unions #18213
Changes from 1 commit
455aa50
380bd1b
33c8b60
8275cef
13c87ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1479,9 +1479,39 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling | |
|
||
/** Like tp1 <:< tp2, but returns false immediately if we know that | ||
* the case was covered previously during subtyping. | ||
* | ||
* A type has been covered previously in subtype checking if it | ||
* is some combination of TypeRefs that point to classes, where the | ||
* combiners are AppliedTypes, RefinedTypes, RecTypes, And/Or-Types or AnnotatedTypes. | ||
* | ||
* The exception is that if both sides contain OrTypes, the check hasn't been covered. | ||
* See #17465. | ||
*/ | ||
def isNewSubType(tp1: Type): Boolean = | ||
if (isCovered(tp1) && isCovered(tp2)) | ||
|
||
def isCovered(tp: Type): (Boolean, Boolean) = | ||
var containsOr: Boolean = false | ||
@annotation.tailrec def recur(todos: List[Type]): Boolean = todos match | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why recur over a list if types? |
||
case tp :: todos => | ||
tp.dealiasKeepRefiningAnnots.stripTypeVar match | ||
case tp: TypeRef => | ||
if tp.symbol.isClass && tp.symbol != NothingClass && tp.symbol != NullClass then recur(todos) | ||
else false | ||
case tp: AppliedType => recur(tp.tycon :: todos) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These nested recurrences might turn out to be really expensive. Why do we need them? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The recursion is equivalent to the one before. I refactored it to take a list of types, which is essentially a working list of recursive invocations, to make the function tail-recursive. (e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously we did not recurse in arguments of Generally, I think using stack is cheaper than allocating. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we are not recursing on the arguments of the Thanks for pointing this out! I will revert this rewriting, and in the future I'll prefer reducing heap allocations over achieving tail recursion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, right, I had misread that! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In fact, maybe it's simplest to just use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I just pushed the commit that reverts the tailrec rewriting and uses an integer-based representation for the result of |
||
case tp: RefinedOrRecType => recur(tp.parent :: todos) | ||
case tp: AndType => recur(tp.tp1 :: tp.tp2 :: todos) | ||
case tp: OrType => | ||
containsOr = true | ||
recur(tp.tp1 :: tp.tp2 :: todos) | ||
case _ => false | ||
case Nil => true | ||
val result = recur(tp :: Nil) | ||
(result, containsOr) | ||
|
||
val (covered1, hasOr1) = isCovered(tp1) | ||
val (covered2, hasOr2) = isCovered(tp2) | ||
|
||
if covered1 && covered2 && !(hasOr1 && hasOr2) then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So total, 4 allocations just here, not counting all the lists built in the recursive calls. We should avoid them in subtype checks, where possible. Techniques do so:
|
||
//println(s"useless subtype: $tp1 <:< $tp2") | ||
false | ||
else isSubType(tp1, tp2, approx.addLow) | ||
|
@@ -2091,19 +2121,6 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling | |
tp1.parent.asInstanceOf[RefinedType], | ||
tp2.parent.asInstanceOf[RefinedType], limit)) | ||
|
||
/** A type has been covered previously in subtype checking if it | ||
* is some combination of TypeRefs that point to classes, where the | ||
* combiners are AppliedTypes, RefinedTypes, RecTypes, And/Or-Types or AnnotatedTypes. | ||
*/ | ||
private def isCovered(tp: Type): Boolean = tp.dealiasKeepRefiningAnnots.stripTypeVar match { | ||
case tp: TypeRef => tp.symbol.isClass && tp.symbol != NothingClass && tp.symbol != NullClass | ||
case tp: AppliedType => isCovered(tp.tycon) | ||
case tp: RefinedOrRecType => isCovered(tp.parent) | ||
case tp: AndType => isCovered(tp.tp1) && isCovered(tp.tp2) | ||
case tp: OrType => isCovered(tp.tp1) && isCovered(tp.tp2) | ||
case _ => false | ||
} | ||
|
||
/** Defer constraining type variables when compared against prototypes */ | ||
def isMatchedByProto(proto: ProtoType, tp: Type): Boolean = tp.stripTypeVar match { | ||
case tp: TypeParamRef if constraint contains tp => true | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
def test1[A, B]: Unit = { | ||
def f[T](x: T{ def *(y: Int): T }): T = ??? | ||
def test = f[scala.collection.StringOps | String]("Hello") | ||
locally: | ||
val test1 : (scala.collection.StringOps | String) { def *(y: Int): (scala.collection.StringOps | String) } = ??? | ||
val test2 : (scala.collection.StringOps | String) { def *(y: Int): (scala.collection.StringOps | String) } = test1 | ||
|
||
locally: | ||
val test1 : (Int | String) { def foo(x: Int): Int } = ??? | ||
val test2 : (Int | String) { def foo(x: Int): Int } = test1 | ||
|
||
locally: | ||
val test1 : ((Int | String) & Any) { def foo(): Int } = ??? | ||
val test2 : ((Int | String) & Any) { def foo(): Int } = test1 | ||
|
||
locally: | ||
val test1 : Int { def foo(): Int } = ??? | ||
val test2 : Int { def foo(): Int } = test1 | ||
|
||
locally: | ||
val test1 : (Int | String) { def foo(): Int } = ??? | ||
val test2 : (Int | String) & Any = test1 | ||
|
||
locally: | ||
val test1 : (Int | B) { def *(y: Int): Int } = ??? | ||
val test2 : (Int | B) { def *(y: Int): Int } = test1 | ||
|
||
locally: | ||
val test1 : (Int | String) = ??? | ||
val test2 : (Int | String) = test1 | ||
|
||
type Foo = Int | String | ||
locally: | ||
val test1 : Foo { type T = Int } = ??? | ||
val test2 : (Int | String) = test1 | ||
} | ||
|
||
def test2: Unit = { | ||
import reflect.Selectable.reflectiveSelectable | ||
|
||
trait A[T](x: T{ def *(y: Int): T }): | ||
def f: T = x * 2 | ||
|
||
class B extends A("Hello") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This entails an allocation since variables accessed from local functions are heap allocated