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 spurious subtype check pruning when both sides have unions #18213

Merged
merged 5 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
45 changes: 31 additions & 14 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

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

@annotation.tailrec def recur(todos: List[Type]): Boolean = todos match
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

@odersky odersky Jul 17, 2023

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

@Linyxus Linyxus Jul 17, 2023

Choose a reason for hiding this comment

The 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. recur(tp.tp1) && recur(tp.tp2) becomes recur(tp.tp1 :: tp.tp2 :: todos)) I thought that this saves the stack and could lead to better performance, but I could revert this refactorization to avoid passing around a list during recursion and creating a new list object at each new invocation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously we did not recurse in arguments of AppliedTypes, just in the type constructor. That's the one I would expect to matter most.

Generally, I think using stack is cheaper than allocating.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we are not recursing on the arguments of the AppliedType: note that the todos is the working list, not the arguments of the AppliedType.

Thanks for pointing this out! I will revert this rewriting, and in the future I'll prefer reducing heap allocations over achieving tail recursion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right, I had misread that!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, maybe it's simplest to just use Ints as the result of recur. Then && could be replaced by min.

Copy link
Contributor Author

@Linyxus Linyxus Jul 17, 2023

Choose a reason for hiding this comment

The 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 isCovered, interpreted bit-wisely. min is used to aggregate results as you suggested. (I was at first using a bitwise & for aggregation but later found that min is more logical). We use bitwise operations to analyze the results as well.

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
Copy link
Contributor

@odersky odersky Jul 17, 2023

Choose a reason for hiding this comment

The 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:

  • Instead of a variable, pass a second parameter to recur.
  • Use an enum instead of a pair of booleans. We need three states: Uncovered, CoveredWithOr, Covered

//println(s"useless subtype: $tp1 <:< $tp2")
false
else isSubType(tp1, tp2, approx.addLow)
Expand Down Expand Up @@ -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
Expand Down
45 changes: 45 additions & 0 deletions tests/pos/i17465.scala
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")
}