Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7f51a86

Browse files
committedJul 6, 2022
Handle OrType in local class checking
1 parent 62fdf0e commit 7f51a86

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed
 

‎compiler/src/dotty/tools/dotc/transform/TypeTestsCasts.scala

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ object TypeTestsCasts {
154154
case tp2: RefinedType => recur(X, tp2.parent) && TypeComparer.hasMatchingMember(tp2.refinedName, X, tp2)
155155
case tp2: RecType => recur(X, tp2.parent)
156156
case _
157-
if P.classSymbol.isLocal && P.classSymbol.isInaccessibleChildOf(X.classSymbol) => // 8
157+
if P.classSymbol.isLocal && foundClasses(X, Nil).exists(P.classSymbol.isInaccessibleChildOf) => // 8
158158
false
159159
case _ => true
160160
})
@@ -178,15 +178,6 @@ object TypeTestsCasts {
178178
def derivedTree(expr1: Tree, sym: Symbol, tp: Type) =
179179
cpy.TypeApply(tree)(expr1.select(sym).withSpan(expr.span), List(TypeTree(tp)))
180180

181-
def effectiveClass(tp: Type): Symbol =
182-
if tp.isRef(defn.PairClass) then effectiveClass(erasure(tp))
183-
else if tp.isRef(defn.AnyValClass) then defn.AnyClass
184-
else tp.classSymbol
185-
186-
def foundClasses(tp: Type, acc: List[Symbol]): List[Symbol] = tp.dealias match
187-
case OrType(tp1, tp2) => foundClasses(tp2, foundClasses(tp1, acc))
188-
case _ => effectiveClass(tp) :: acc
189-
190181
def inMatch =
191182
tree.fun.symbol == defn.Any_typeTest || // new scheme
192183
expr.symbol.is(Case) // old scheme
@@ -376,4 +367,13 @@ object TypeTestsCasts {
376367
}
377368
interceptWith(expr)
378369
}
370+
371+
private def effectiveClass(tp: Type)(using Context): Symbol =
372+
if tp.isRef(defn.PairClass) then effectiveClass(erasure(tp))
373+
else if tp.isRef(defn.AnyValClass) then defn.AnyClass
374+
else tp.classSymbol
375+
376+
private def foundClasses(tp: Type, acc: List[Symbol])(using Context): List[Symbol] = tp.dealias match
377+
case OrType(tp1, tp2) => foundClasses(tp2, foundClasses(tp1, acc))
378+
case _ => effectiveClass(tp) :: acc
379379
}

‎tests/neg/i4812.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ object Test:
108108
x match
109109
case x: B => x
110110

111+
def test12 =
112+
class Foo
113+
class Bar
114+
val x: Foo | Bar = new Foo
115+
x.isInstanceOf[Foo]
116+
111117
def main(args: Array[String]): Unit =
112118
test(1)
113119
val x: String = test("") // was: ClassCastException: java.lang.Integer cannot be cast to java.lang.String

0 commit comments

Comments
 (0)
Please sign in to comment.