Skip to content

Fix #5494: Spurious unchecked warning when type testing an alias #8808

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

Merged
merged 19 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
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
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/ast/Trees.scala
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ object Trees {
type ThisTree[-T >: Untyped] = If[T]
def isInline = false
}
class InlineIf[T >: Untyped] private[ast] (cond: Tree[T], thenp: Tree[T], elsep: Tree[T])(implicit @constructorOnly src: SourceFile)
class InlineIf[-T >: Untyped] private[ast] (cond: Tree[T], thenp: Tree[T], elsep: Tree[T])(implicit @constructorOnly src: SourceFile)
extends If(cond, thenp, elsep) {
override def isInline = true
override def toString = s"InlineIf($cond, $thenp, $elsep)"
Expand All @@ -529,7 +529,7 @@ object Trees {
type ThisTree[-T >: Untyped] = Match[T]
def isInline = false
}
class InlineMatch[T >: Untyped] private[ast] (selector: Tree[T], cases: List[CaseDef[T]])(implicit @constructorOnly src: SourceFile)
class InlineMatch[-T >: Untyped] private[ast] (selector: Tree[T], cases: List[CaseDef[T]])(implicit @constructorOnly src: SourceFile)
extends Match(selector, cases) {
override def isInline = true
override def toString = s"InlineMatch($selector, $cases)"
Expand Down Expand Up @@ -579,7 +579,7 @@ object Trees {
}

/** Array(elems) */
class JavaSeqLiteral[T >: Untyped] private[ast] (elems: List[Tree[T]], elemtpt: Tree[T])(implicit @constructorOnly src: SourceFile)
class JavaSeqLiteral[-T >: Untyped] private[ast] (elems: List[Tree[T]], elemtpt: Tree[T])(implicit @constructorOnly src: SourceFile)
Copy link
Member Author

Choose a reason for hiding this comment

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

This is good for uniformity but why would this make any difference for type test safety warnings ?

Copy link
Contributor

Choose a reason for hiding this comment

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

The following code shows why it matters:

trait Tree[-T]

class JavaSeqLiteral[T] extends Tree[T]

trait Type

class DummyTree extends JavaSeqLiteral[Any]

def foo1(tree: Tree[Type]) =
  tree.isInstanceOf[JavaSeqLiteral[Type]]   // error

foo1(new DummyTree)

extends SeqLiteral(elems, elemtpt) {
override def toString: String = s"JavaSeqLiteral($elems, $elemtpt)"
}
Expand Down
11 changes: 5 additions & 6 deletions compiler/src/dotty/tools/dotc/transform/TypeTestsCasts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,9 @@ object TypeTestsCasts {
debug.println("P1 : " + P1.show)
debug.println("X : " + X.show)

P1 <:< X // constraint P1
P1 <:< X

// use fromScala2x to avoid generating pattern bound symbols
maximizeType(P1, span, fromScala2x = true)
maximizeType(P1, span, fromScala2x = false)

val res = P1 <:< P
debug.println("P1 : " + P1.show)
Expand All @@ -117,7 +116,7 @@ object TypeTestsCasts {
res
}

def recur(X: Type, P: Type): Boolean = (X <:< P) || (P match {
def recur(X: Type, P: Type): Boolean = (X <:< P) || (P.dealias match {
case _: SingletonType => true
case _: TypeProxy
if isAbstract(P) => false
Expand All @@ -138,8 +137,8 @@ object TypeTestsCasts {
case _ =>
// first try withou striping type parameters for performance
X.classSymbol.exists && P.classSymbol.exists && !X.classSymbol.asClass.mayHaveCommonChild(P.classSymbol.asClass) ||
isClassDetermined(X, tpe)(ctx.fresh.setNewTyperState()) ||
isClassDetermined(stripTypeParam(X), tpe)(ctx.fresh.setNewTyperState())
isClassDetermined(X, tpe)(ctx.fresh.setNewTyperState().setFreshGADTBounds) ||
Copy link
Member Author

Choose a reason for hiding this comment

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

This method is called isClassDetermined but actually I don't think it's specific to classes: P could be an application of a type lambda for example.

Copy link
Contributor

Choose a reason for hiding this comment

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

The case for non-class type constructors is tested in the case TypeProxy, where we assume it cannot be checked at runtime. It seems the assumption is practical enough to cover use cases we know of. When the need emerges, we may relax the check a little bit to make it more expressive.

Copy link
Member Author

Choose a reason for hiding this comment

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

The case for non-class type constructors is tested in the case TypeProxy, where we assume it cannot be checked at runtime.

Ah I see, but this is incomplete: it doesn't handle situation where a type lambda is directly applied (or appears behind an alias):

trait A[T]
trait B[T] extends A[T]

object Test {
  def foo(x: ([X] =>> A[X])[Any]) = x match {
    case x: ([X] =>> B[X])[Any] =>
    case _ =>
  }
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, we haven't seen use cases like this (though the example above type checks). When valid use cases emerge we can easily support them using the method typeArgsTrivial.

isClassDetermined(stripTypeParam(X), tpe)(ctx.fresh.setNewTyperState().setFreshGADTBounds)
}
case AndType(tp1, tp2) => recur(X, tp1) && recur(X, tp2)
case OrType(tp1, tp2) => recur(X, tp1) && recur(X, tp2)
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-custom-args/isInstanceOf/3324g.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Test {
}

def quux[T](a: A[T]): Unit = a match {
case _: B[T] => // should be an error!!
case _: B[T] => // error!!
}

quux(new C[Int])
Expand Down
13 changes: 13 additions & 0 deletions tests/neg-custom-args/isInstanceOf/gadt.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Test {
trait A[+T]
class B[T] extends A[T]

class C
class D extends C

def quux(a: A[C]): Unit = a match {
case _: B[C] => // error!!
}

quux(new B[D])
}
12 changes: 12 additions & 0 deletions tests/neg-custom-args/isInstanceOf/i5495.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class A
class B

type AorB = A | B

def foo(any: Any) = any match {
case aorb: AorB =>
case _ =>
}

def bar[T](x: List[T]) = x.isInstanceof[List[Int]] // error

13 changes: 13 additions & 0 deletions tests/neg-custom-args/isInstanceOf/or-type-trees.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
trait Tree
trait Context

def foo(myTree: Tree | (Context => Tree)) =
println(myTree.isInstanceOf[Tree])
myTree match
case treeFn: (Context => Tree) => // error
case _ =>

def bar(myTree: Tree | (Context => Tree)) =
myTree match
case treeFn: (_ => _) => // ok
case _ =>
11 changes: 11 additions & 0 deletions tests/neg-custom-args/isInstanceOf/patmat-applied.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class A[-T]
class B[T] extends A[T]

object Test {
def foo(x: A[Null]) = x match {
case x: B[Null] =>
case _ =>
}
}

def bar[T](x: List[T]) = x.isInstanceof[List[Int]] // error