-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix spurious subtype check pruning when both sides have unions #18213
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
Changes from 2 commits
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,27 @@ 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): CoveredStatus = | ||||||
tp.dealiasKeepRefiningAnnots.stripTypeVar match | ||||||
case tp: TypeRef if tp.symbol.isClass && tp.symbol != NothingClass && tp.symbol != NullClass => CoveredStatus.Covered | ||||||
case tp: AppliedType => isCovered(tp.tycon) | ||||||
case tp: RefinedOrRecType => isCovered(tp.parent) | ||||||
case tp: AndType => isCovered(tp.tp1) combinedWith isCovered(tp.tp2) | ||||||
case tp: OrType => isCovered(tp.tp1) combinedWith isCovered(tp.tp2) | ||||||
case _ => CoveredStatus.Uncovered | ||||||
|
||||||
val covered1 = isCovered(tp1) | ||||||
val covered2 = isCovered(tp2) | ||||||
if CoveredStatus.bothCovered(covered1, covered2) && !CoveredStatus.bothHaveOr(covered1, covered2) 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.
Suggested change
Then no helper functions are needed, and it's still quite clear, I think. 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, that's much simpler and clearer. Thanks! |
||||||
//println(s"useless subtype: $tp1 <:< $tp2") | ||||||
false | ||||||
else isSubType(tp1, tp2, approx.addLow) | ||||||
|
@@ -2091,19 +2109,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 | ||||||
|
@@ -2991,6 +2996,31 @@ object TypeComparer { | |||||
end ApproxState | ||||||
type ApproxState = ApproxState.Repr | ||||||
|
||||||
/** Result of `isCovered` check. */ | ||||||
object CoveredStatus: | ||||||
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. Can be private |
||||||
opaque type Repr = Int | ||||||
|
||||||
private inline val IsCovered = 2 | ||||||
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 two are no longer needed. |
||||||
private inline val NotHasOr = 1 | ||||||
|
||||||
/** The type is not covered. */ | ||||||
val Uncovered: Repr = 1 | ||||||
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. Style: In this case I'd write the three vals on subsequent lines with // comments to the right. It's more compact taht way and just as legible. |
||||||
|
||||||
/** The type is covered and contains OrTypes. */ | ||||||
val CoveredWithOr: Repr = 2 | ||||||
|
||||||
/** The type is covered and free from OrTypes. */ | ||||||
val Covered: Repr = 3 | ||||||
|
||||||
object Repr: | ||||||
extension (s: Repr) | ||||||
inline def combinedWith(that: Repr): Repr = s min that | ||||||
|
||||||
inline def bothHaveOr(s1: Repr, s2: Repr): Boolean = ~(s1 | s2 & NotHasOr) != 0 | ||||||
Linyxus marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
inline def bothCovered(s1: Repr, s2: Repr): Boolean = (s1 & s2 & IsCovered) != 0 | ||||||
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.
Suggested change
Or just inline rhs at the point of 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. Then you don't need the isCovered and NotHasOr fields. Better not to be too clever with bitsets unless it really gains performance - it's easy to get these wrong, and is generally harder to read than straight comparisons. |
||||||
end CoveredStatus | ||||||
type CoveredStatus = CoveredStatus.Repr | ||||||
|
||||||
def topLevelSubType(tp1: Type, tp2: Type)(using Context): Boolean = | ||||||
comparing(_.topLevelSubType(tp1, tp2)) | ||||||
|
||||||
|
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.
It's a bit faster since it does not try the other patterns in case of a non-class TypeRef.