@@ -1895,9 +1895,9 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
1895
1895
/** Returns last check's debug mode, if explicitly enabled. */
1896
1896
def lastTrace (): String = " "
1897
1897
1898
- /** Do `tp1` and `tp2` share a non-null inhabitant ?
1898
+ /** Are `tp1` and `tp2` disjoint types ?
1899
1899
*
1900
- * `false ` implies that we found a proof; uncertainty default to `true `.
1900
+ * `true ` implies that we found a proof; uncertainty default to `false `.
1901
1901
*
1902
1902
* Proofs rely on the following properties of Scala types:
1903
1903
*
@@ -1906,8 +1906,8 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
1906
1906
* 3. ConstantTypes with distinc values are non intersecting
1907
1907
* 4. There is no value of type Nothing
1908
1908
*/
1909
- def intersecting (tp1 : Type , tp2 : Type )(implicit ctx : Context ): Boolean = {
1910
- // println(s"intersecting (${tp1.show}, ${tp2.show})")
1909
+ def disjoint (tp1 : Type , tp2 : Type )(implicit ctx : Context ): Boolean = {
1910
+ // println(s"disjoint (${tp1.show}, ${tp2.show})")
1911
1911
/** Can we enumerate all instantiations of this type? */
1912
1912
def isClosedSum (tp : Symbol ): Boolean =
1913
1913
tp.is(Sealed ) && tp.is(AbstractOrTrait ) && ! tp.hasAnonymousChild
@@ -1921,34 +1921,34 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
1921
1921
1922
1922
(tp1.dealias, tp2.dealias) match {
1923
1923
case (tp1 : TypeRef , tp2 : TypeRef ) if tp1.symbol == defn.SingletonClass || tp2.symbol == defn.SingletonClass =>
1924
- true
1924
+ false
1925
1925
case (tp1 : ConstantType , tp2 : ConstantType ) =>
1926
- tp1 = = tp2
1926
+ tp1 ! = tp2
1927
1927
case (tp1 : TypeRef , tp2 : TypeRef ) if tp1.symbol.isClass && tp2.symbol.isClass =>
1928
1928
val cls1 = tp1.classSymbol
1929
1929
val cls2 = tp2.classSymbol
1930
1930
if (cls1.derivesFrom(cls2) || cls2.derivesFrom(cls1)) {
1931
- true
1931
+ false
1932
1932
} else {
1933
1933
if (cls1.is(Final ) || cls2.is(Final ))
1934
1934
// One of these types is final and they are not mutually
1935
1935
// subtype, so they must be unrelated.
1936
- false
1936
+ true
1937
1937
else if (! cls2.is(Trait ) && ! cls1.is(Trait ))
1938
1938
// Both of these types are classes and they are not mutually
1939
1939
// subtype, so they must be unrelated by single inheritance
1940
1940
// of classes.
1941
- false
1941
+ true
1942
1942
else if (isClosedSum(cls1))
1943
- decompose(cls1, tp1).exists (x => intersecting (x, tp2))
1943
+ decompose(cls1, tp1).forall (x => disjoint (x, tp2))
1944
1944
else if (isClosedSum(cls2))
1945
- decompose(cls2, tp2).exists (x => intersecting (x, tp1))
1945
+ decompose(cls2, tp2).forall (x => disjoint (x, tp1))
1946
1946
else
1947
- true
1947
+ false
1948
1948
}
1949
1949
case (AppliedType (tycon1, args1), AppliedType (tycon2, args2)) if tycon1 == tycon2 =>
1950
- def covariantIntersecting (tp1 : Type , tp2 : Type , tparam : TypeParamInfo ): Boolean = {
1951
- intersecting (tp1, tp2) || {
1950
+ def covariantDisjoint (tp1 : Type , tp2 : Type , tparam : TypeParamInfo ): Boolean = {
1951
+ disjoint (tp1, tp2) && {
1952
1952
// We still need to proof that `Nothing` is not a valid
1953
1953
// instantiation of this type parameter. We have two ways
1954
1954
// to get to that conclusion:
@@ -1966,21 +1966,21 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
1966
1966
case _ =>
1967
1967
false
1968
1968
}
1969
- lowerBoundedByNothing && ! typeUsedAsField
1969
+ ! lowerBoundedByNothing || typeUsedAsField
1970
1970
}
1971
1971
}
1972
1972
1973
- (args1, args2, tycon1.typeParams).zipped.forall {
1973
+ (args1, args2, tycon1.typeParams).zipped.exists {
1974
1974
(arg1, arg2, tparam) =>
1975
1975
val v = tparam.paramVariance
1976
1976
if (v > 0 )
1977
- covariantIntersecting (arg1, arg2, tparam)
1977
+ covariantDisjoint (arg1, arg2, tparam)
1978
1978
else if (v < 0 )
1979
1979
// Contravariant case: a value where this type parameter is
1980
1980
// instantiated to `Any` belongs to both types.
1981
- true
1981
+ false
1982
1982
else
1983
- covariantIntersecting (arg1, arg2, tparam) && ( isSameType(arg1, arg2) || {
1983
+ covariantDisjoint (arg1, arg2, tparam) || ! isSameType(arg1, arg2) && {
1984
1984
// We can only trust a "no" from `isSameType` when both
1985
1985
// `arg1` and `arg2` are fully instantiated.
1986
1986
def fullyInstantiated (tp : Type ): Boolean = new TypeAccumulator [Boolean ] {
@@ -1993,37 +1993,35 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
1993
1993
}
1994
1994
}
1995
1995
}.apply(true , tp)
1996
- ! ( fullyInstantiated(arg1) && fullyInstantiated(arg2) )
1997
- })
1996
+ fullyInstantiated(arg1) && fullyInstantiated(arg2)
1997
+ }
1998
1998
}
1999
1999
case (tp1 : HKLambda , tp2 : HKLambda ) =>
2000
- intersecting (tp1.resType, tp2.resType)
2000
+ disjoint (tp1.resType, tp2.resType)
2001
2001
case (_ : HKLambda , _) =>
2002
- // The intersection is ill kinded and therefore empty .
2003
- false
2002
+ // The intersection of these two types would be ill kinded, they are therefore disjoint .
2003
+ true
2004
2004
case (_, _ : HKLambda ) =>
2005
- false
2005
+ true
2006
2006
case (tp1 : OrType , _) =>
2007
- intersecting (tp1.tp1, tp2) || intersecting (tp1.tp2, tp2)
2007
+ disjoint (tp1.tp1, tp2) && disjoint (tp1.tp2, tp2)
2008
2008
case (_, tp2 : OrType ) =>
2009
- intersecting (tp1, tp2.tp1) || intersecting (tp1, tp2.tp2)
2009
+ disjoint (tp1, tp2.tp1) && disjoint (tp1, tp2.tp2)
2010
2010
case (tp1 : AndType , tp2 : AndType ) =>
2011
- intersecting( tp1.tp1, tp2.tp1) && intersecting (tp1.tp2, tp2.tp2) ||
2012
- intersecting( tp1.tp1, tp2.tp2) && intersecting (tp1.tp2, tp2.tp1)
2011
+ (disjoint( tp1.tp1, tp2.tp1) || disjoint (tp1.tp2, tp2.tp2)) &&
2012
+ (disjoint( tp1.tp1, tp2.tp2) || disjoint (tp1.tp2, tp2.tp1) )
2013
2013
case (tp1 : AndType , _) =>
2014
- intersecting(tp1.tp1, tp2) && intersecting(tp1.tp2, tp2) ||
2015
- intersecting(tp1.tp2, tp2) && intersecting(tp1.tp1, tp2)
2014
+ disjoint(tp1.tp2, tp2) || disjoint(tp1.tp1, tp2)
2016
2015
case (_, tp2 : AndType ) =>
2017
- intersecting(tp1, tp2.tp1) && intersecting(tp1, tp2.tp2) ||
2018
- intersecting(tp1, tp2.tp2) && intersecting(tp1, tp2.tp1)
2016
+ disjoint(tp1, tp2.tp2) || disjoint(tp1, tp2.tp1)
2019
2017
case (tp1 : TypeProxy , tp2 : TypeProxy ) =>
2020
- intersecting (tp1.underlying, tp2) && intersecting (tp1, tp2.underlying)
2018
+ disjoint (tp1.underlying, tp2) || disjoint (tp1, tp2.underlying)
2021
2019
case (tp1 : TypeProxy , _) =>
2022
- intersecting (tp1.underlying, tp2)
2020
+ disjoint (tp1.underlying, tp2)
2023
2021
case (_, tp2 : TypeProxy ) =>
2024
- intersecting (tp1, tp2.underlying)
2022
+ disjoint (tp1, tp2.underlying)
2025
2023
case _ =>
2026
- true
2024
+ false
2027
2025
}
2028
2026
}
2029
2027
}
@@ -2175,7 +2173,7 @@ class TrackingTypeComparer(initctx: Context) extends TypeComparer(initctx) {
2175
2173
}
2176
2174
else if (isSubType(widenAbstractTypes(scrut), widenAbstractTypes(pat)))
2177
2175
Some (NoType )
2178
- else if (! intersecting (scrut, pat))
2176
+ else if (disjoint (scrut, pat))
2179
2177
// We found a proof that `scrut` and `pat` are incompatible.
2180
2178
// The search continues.
2181
2179
None
0 commit comments