@@ -2091,6 +2091,89 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
20912091 true
20922092 }
20932093 }
2094+
2095+ def notIntersection (tp : Type , pt : Type ): Boolean =
2096+ trace(i " notIntersection( $tp, $pt) " )
2097+ {
2098+ import typer .Inferencing ._
2099+
2100+ def incompatibleClasses : Boolean = {
2101+ import Flags ._
2102+ val tpClassSym = tp.classSymbol
2103+ val ptClassSym = pt.classSymbol
2104+ println(i " tpClassSym= $tpClassSym, fin= ${tpClassSym.is(Final )}" )
2105+ println(i " ptClassSym= $ptClassSym, fin= ${ptClassSym.is(Final )}" )
2106+ tpClassSym.exists && ptClassSym.exists && {
2107+ println(" here" )
2108+ if (tpClassSym.is(Final )) ! tpClassSym.derivesFrom(ptClassSym)
2109+ else if (ptClassSym.is(Final )) ! ptClassSym.derivesFrom(tpClassSym)
2110+ else if (! tpClassSym.is(Flags .Trait ) && ! ptClassSym.is(Flags .Trait ))
2111+ ! (tpClassSym.derivesFrom(ptClassSym) || ptClassSym.derivesFrom(tpClassSym))
2112+ else false
2113+ }
2114+ }
2115+
2116+ def loop (tp : Type ): Boolean =
2117+ trace.force(i " loop( $tp) // ${tp.toString}" )
2118+ {
2119+ if (constrainPatternType(pt, tp)) true
2120+ else if (incompatibleClasses) {
2121+ // println("incompatible classes")
2122+ false
2123+ }
2124+ else tp match {
2125+ case _ : ConstantType =>
2126+ // constants cannot possibly intersect with types that aren't their supertypes
2127+ false
2128+ case tp : SingletonType => loop(tp.underlying)
2129+ case tp : TypeRef if tp.symbol.isClass => loop(tp.firstParent)
2130+ case tp @ AppliedType (tycon : TypeRef , _) if tycon.symbol.isClass =>
2131+ val ptClassSym = pt.classSymbol
2132+ def firstParentSharedWithPt (tp : Type , tpClassSym : ClassSymbol ): Symbol =
2133+ trace.force(i " f( $tp) " )
2134+ {
2135+ var parents = tpClassSym.info.parents
2136+ // println(i"parents of $tpClassSym = $parents%, %")
2137+ parents match {
2138+ case first :: rest =>
2139+ if (first.classSymbol == defn.ObjectClass ) parents = rest
2140+ case _ => ;
2141+ }
2142+ parents match {
2143+ case first :: _ =>
2144+ val firstClassSym = first.classSymbol.asClass
2145+ val res = if (ptClassSym.derivesFrom(firstClassSym)) firstClassSym
2146+ else firstParentSharedWithPt(first, firstClassSym)
2147+ res
2148+ case _ => NoSymbol
2149+ }
2150+ }
2151+ val sym = firstParentSharedWithPt(tycon, tycon.symbol.asClass)
2152+ // println(i"sym=$sym ; tyconsym=${tycon.symbol}")
2153+ if (! sym.exists) true
2154+ else ! (sym == tycon.symbol) && loop(tp.baseType(sym))
2155+ case tp : TypeProxy =>
2156+ loop(tp.superType)
2157+ case _ => false
2158+ }
2159+ }
2160+
2161+ pt match {
2162+ case AndType (pt1, pt2) =>
2163+ notIntersection(tp, pt1) && notIntersection(tp, pt2)
2164+ case OrType (pt1, pt2) =>
2165+ either(notIntersection(tp, pt1), notIntersection(tp, pt2))
2166+ case _ =>
2167+ tp match {
2168+ case OrType (tp1, tp2) =>
2169+ either(notIntersection(tp1, pt), notIntersection(tp2, pt))
2170+ case AndType (tp1, tp2) =>
2171+ notIntersection(tp1, pt) && notIntersection(tp2, pt)
2172+ case _ =>
2173+ loop(tp)
2174+ }
2175+ }
2176+ }
20942177}
20952178
20962179object TypeComparer {
0 commit comments