Skip to content

Commit

Permalink
Merge pull request #7555 from dotty-staging/fix-#7554
Browse files Browse the repository at this point in the history
Fix #7554: Add TypeTest for sound pattern type test
  • Loading branch information
nicolasstucki authored Nov 12, 2020
2 parents ced6639 + 919acc2 commit 0134f2c
Show file tree
Hide file tree
Showing 28 changed files with 687 additions and 18 deletions.
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,8 @@ class Definitions {
@tu lazy val ClassTagModule: Symbol = ClassTagClass.companionModule
@tu lazy val ClassTagModule_apply: Symbol = ClassTagModule.requiredMethod(nme.apply)

@tu lazy val TypeTestClass: ClassSymbol = requiredClass("scala.reflect.TypeTest")
@tu lazy val TypeTestModule_identity: Symbol = TypeTestClass.companionModule.requiredMethod(nme.identity)

@tu lazy val QuotedExprClass: ClassSymbol = requiredClass("scala.quoted.Expr")
@tu lazy val QuotedExprModule: Symbol = QuotedExprClass.companionModule
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1331,7 +1331,7 @@ trait Applications extends Compatibility {
val result = assignType(cpy.UnApply(tree)(unapplyFn, unapplyImplicits(unapplyApp), unapplyPatterns), ownType)
unapp.println(s"unapply patterns = $unapplyPatterns")
if ((ownType eq selType) || ownType.isError) result
else tryWithClassTag(Typed(result, TypeTree(ownType)), selType)
else tryWithTypeTest(Typed(result, TypeTree(ownType)), selType)
case tp =>
val unapplyErr = if (tp.isError) unapplyFn else notAnExtractor(unapplyFn)
val typedArgsErr = args mapconserve (typed(_, defn.AnyType))
Expand Down
33 changes: 33 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Synthesizer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,38 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
case _ => EmptyTree
end synthesizedClassTag

val synthesizedTypeTest: SpecialHandler =
(formal, span) => formal.argInfos match {
case arg1 :: arg2 :: Nil if !defn.isBottomClass(arg2.typeSymbol) =>
val tp1 = fullyDefinedType(arg1, "TypeTest argument", span)
val tp2 = fullyDefinedType(arg2, "TypeTest argument", span)
val sym2 = tp2.typeSymbol
if tp1 <:< tp2 then
// optimization when we know the typetest will always succeed
ref(defn.TypeTestModule_identity).appliedToType(tp2).withSpan(span)
else if sym2 == defn.AnyValClass || sym2 == defn.AnyRefAlias || sym2 == defn.ObjectClass then
EmptyTree
else
// Generate SAM: (s: <tp1>) => if s.isInstanceOf[<tp2>] then Some(s.asInstanceOf[s.type & <tp2>]) else None
def body(args: List[Tree]): Tree = {
val arg :: Nil = args
val t = arg.tpe & tp2
If(
arg.select(defn.Any_isInstanceOf).appliedToType(tp2),
ref(defn.SomeClass.companionModule.termRef).select(nme.apply)
.appliedToType(t)
.appliedTo(arg.select(nme.asInstanceOf_).appliedToType(t)),
ref(defn.NoneModule))
}
val tpe = MethodType(List(nme.s))(_ => List(tp1), mth => defn.OptionClass.typeRef.appliedTo(mth.newParamRef(0) & tp2))
val meth = newSymbol(ctx.owner, nme.ANON_FUN, Synthetic | Method, tpe, coord = span)
val typeTestType = defn.TypeTestClass.typeRef.appliedTo(List(tp1, tp2))
Closure(meth, tss => body(tss.head).changeOwner(ctx.owner, meth), targetType = typeTestType).withSpan(span)
case _ =>
EmptyTree
}
end synthesizedTypeTest

val synthesizedTupleFunction: SpecialHandler = (formal, span) =>
formal match
case AppliedType(_, funArgs @ fun :: tupled :: Nil) =>
Expand Down Expand Up @@ -374,6 +406,7 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):

val specialHandlers = List(
defn.ClassTagClass -> synthesizedClassTag,
defn.TypeTestClass -> synthesizedTypeTest,
defn.EqlClass -> synthesizedEql,
defn.TupledFunctionClass -> synthesizedTupleFunction,
defn.ValueOfClass -> synthesizedValueOf,
Expand Down
39 changes: 23 additions & 16 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ class Typer extends Namer
TypeComparer.constrainPatternType(tpt1.tpe, pt)
}
// special case for an abstract type that comes with a class tag
tryWithClassTag(ascription(tpt1, isWildcard = true), pt)
tryWithTypeTest(ascription(tpt1, isWildcard = true), pt)
}
cases(
ifPat = handlePattern,
Expand All @@ -773,21 +773,28 @@ class Typer extends Namer
}
}

/** For a typed tree `e: T`, if `T` is an abstract type for which an implicit class tag `ctag`
* exists, rewrite to `ctag(e)`.
/** For a typed tree `e: T`, if `T` is an abstract type for which an implicit type test or class tag `tt`
* exists, rewrite to `tt(e)`.
* @pre We are in pattern-matching mode (Mode.Pattern)
*/
def tryWithClassTag(tree: Typed, pt: Type)(using Context): Tree = tree.tpt.tpe.dealias match {
def tryWithTypeTest(tree: Typed, pt: Type)(using Context): Tree = tree.tpt.tpe.dealias match {
case tref: TypeRef if !tref.symbol.isClass && !ctx.isAfterTyper && !(tref =:= pt) =>
require(ctx.mode.is(Mode.Pattern))
withoutMode(Mode.Pattern)(
inferImplicit(defn.ClassTagClass.typeRef.appliedTo(tref), EmptyTree, tree.tpt.span)
) match {
case SearchSuccess(clsTag, _, _) =>
typed(untpd.Apply(untpd.TypedSplice(clsTag), untpd.TypedSplice(tree.expr)), pt)
case _ =>
tree
def withTag(tpe: Type): Option[Tree] = {
require(ctx.mode.is(Mode.Pattern))
withoutMode(Mode.Pattern)(
inferImplicit(tpe, EmptyTree, tree.tpt.span)
) match
case SearchSuccess(clsTag, _, _) =>
Some(typed(untpd.Apply(untpd.TypedSplice(clsTag), untpd.TypedSplice(tree.expr)), pt))
case _ =>
None
}
val tag = withTag(defn.TypeTestClass.typeRef.appliedTo(pt, tref))
.orElse(withTag(defn.ClassTagClass.typeRef.appliedTo(tref)))
.getOrElse(tree)
if tag.symbol.owner == defn.ClassTagClass && config.Feature.sourceVersion.isAtLeast(config.SourceVersion.`3.1`) then
report.warning("Use of ClassTag for type testing may be unsound. Consider using `reflect.Typable` instead.", tree.srcPos)
tag
case _ => tree
}

Expand Down Expand Up @@ -1835,10 +1842,10 @@ class Typer extends Namer
val body1 = typed(tree.body, pt)
body1 match {
case UnApply(fn, Nil, arg :: Nil)
if fn.symbol.exists && fn.symbol.owner == defn.ClassTagClass && !body1.tpe.isError =>
// A typed pattern `x @ (e: T)` with an implicit `ctag: ClassTag[T]`
// was rewritten to `x @ ctag(e)` by `tryWithClassTag`.
// Rewrite further to `ctag(x @ e)`
if fn.symbol.exists && (fn.symbol.owner.derivesFrom(defn.TypeTestClass) || fn.symbol.owner == defn.ClassTagClass) && !body1.tpe.isError =>
// A typed pattern `x @ (e: T)` with an implicit `tt: TypeTest[T]` or `ctag: ClassTag[T]`
// was rewritten to `x @ tt(e)` `x @ ctag(e)` by `tryWithTypeTest`.
// Rewrite further to `tt(x @ e)` or `ctag(x @ e)`
tpd.cpy.UnApply(body1)(fn, Nil,
typed(untpd.Bind(tree.name, untpd.TypedSplice(arg)).withSpan(tree.span), arg.tpe) :: Nil)
case _ =>
Expand Down
11 changes: 10 additions & 1 deletion docs/docs/reference/changed-features/pattern-matching.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,13 @@ def foo(f: Foo) = f match {
```

There are plans for further simplification, in particular to factor out *product
match* and *name-based match* into a single type of extractor.
match* and *name-based match* into a single type of extractor.

## Type testing

Abstract type testing with `ClassTag` is replaced with `TypeTest` or the alias `Typeable`.

- pattern `_: X` for an abstract type requires a `TypeTest` in scope
- pattern `x @ X()` for an unapply that takes an abstract type requires a `TypeTest` in scope

[More details on TypeTest](../other-new-features/type-test.md)
146 changes: 146 additions & 0 deletions docs/docs/reference/other-new-features/type-test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
---
layout: doc-page
title: "TypeTest"
---

TypeTest
--------

When pattern matching there are two situations where were a runtime type test must be performed.
The first is kind is an explicit type test using the ascription pattern notation.
```scala
(x: X) match
case y: Y =>
```
The second is when an extractor takes an argument that is not a subtype of the scrutinee type.
```scala
(x: X) match
case y @ Y(n) =>

object Y:
def unapply(x: Y): Some[Int] = ...
```

In both cases, a class test will be performed at runtime.
But when the type test is on an abstract type (type parameter or type member), the test cannot be performed because the type is erased at runtime.

A `TypeTest` can be provided to make this test possible.

```scala
package scala.reflect

trait TypeTest[-S, T]:
def unapply(s: S): Option[s.type & T]
```

It provides an extractor that returns its argument typed as a `T` if the argument is a `T`.
It can be used to encode a type test.
```scala
def f[X, Y](x: X)(using tt: TypeTest[X, Y]): Option[Y] =
x match
case tt(x @ Y(1)) => Some(x)
case tt(x) => Some(x)
case _ => None
```

To avoid the syntactic overhead the compiler will look for a type test automatically if it detects that the type test is on abstract types.
This means that `x: Y` is transformed to `tt(x)` and `x @ Y(_)` to `tt(x @ Y(_))` if there is a contextual `TypeTest[X, Y]` in scope.
The previous code is equivalent to

```scala
def f[X, Y](x: X)(using TypeTest[X, Y]): Option[Y] =
x match
case x @ Y(1) => Some(x)
case x: Y => Some(x)
case _ => None
```

We could create a type test at call site where the type test can be performed with runtime class tests directly as follows

```scala
val tt: TypeTest[Any, String] =
new TypeTest[Any, String]
def unapply(s: Any): Option[s.type & String] =
s match
case s: String => Some(s)
case _ => None

f[AnyRef, String]("acb")(using tt)
```

The compiler will synthesize a new instance of a type test if none is found in scope as:
```scala
new TypeTest[A, B]:
def unapply(s: A): Option[s.type & B] =
s match
case s: B => Some(s)
case _ => None
```
If the type tests cannot be done there will be an unchecked warning that will be raised on the `case s: B =>` test.

The most common `TypeTest` instances are the ones that take any parameters (i.e. `TypeTest[Any, T]`).
To make it possible to use such instances directly in context bounds we provide the alias
```scala
package scala.reflect

type Typeable[T] = TypeTest[Any, T]
```

This alias can be used as

```scala
def f[T: Typeable]: Boolean =
"abc" match
case x: T => true
case _ => false

f[String] // true
f[Int] // fasle
```

### TypeTest and ClassTag
`TypeTest` is a replacement for functionality provided previously by `ClassTag.unapply`.
Using `ClassTag` instances was unsound since classtags can check only the class component of a type.
`TypeTest` fixes that unsoundness.
`ClassTag` type tests are still supported but a warning will be emitted after 3.0.


Examples
--------

Given the following abstract definition of `Peano` numbers that provides `TypeTest[Nat, Zero]` and `TypeTest[Nat, Succ]`

```scala
trait Peano:
type Nat
type Zero <: Nat
type Succ <: Nat
def safeDiv(m: Nat, n: Succ): (Nat, Nat)
val Zero: Zero
val Succ: SuccExtractor
trait SuccExtractor {
def apply(nat: Nat): Succ
def unapply(nat: Succ): Option[Nat]
}
given TypeTest[Nat, Zero] = typeTestOfZero
protected def typeTestOfZero: TypeTest[Nat, Zero]
given TypeTest[Nat, Succ] = typeTestOfSucc
protected def typeTestOfSucc: TypeTest[Nat, Succ]
```

it will be possible to write the following program

```scala
val peano: Peano = ...
import peano._
def divOpt(m: Nat, n: Nat): Option[(Nat, Nat)] =
n match
case Zero => None
case s @ Succ(_) => Some(safeDiv(m, s))

val two = Succ(Succ(Zero))
val five = Succ(Succ(Succ(two)))
println(divOpt(five, two))
```

Note that without the `TypeTest[Nat, Succ]` the pattern `Succ.unapply(nat: Succ)` would be unchecked.
28 changes: 28 additions & 0 deletions library/src/scala/reflect/TypeTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package scala.reflect

/** A `TypeTest[S, T] contains the logic needed to know at runtime if a value of
* type `S` can be downcasted to `T`.
*
* If a pattern match is performed on a term of type `s: S` that is uncheckable with `s.isInstanceOf[T]` and
* the pattern are of the form:
* - `t: T`
* - `t @ X()` where the `X.unapply` has takes an argument of type `T`
* then a given instance of `TypeTest[S, T]` is summoned and used to perform the test.
*/
@scala.annotation.implicitNotFound(msg = "No TypeTest available for [${S}, ${T}]")
trait TypeTest[-S, T] extends Serializable:

/** A TypeTest[S, T] can serve as an extractor that matches only S of type T.
*
* The compiler tries to turn unchecked type tests in pattern matches into checked ones
* by wrapping a `(_: T)` type pattern as `tt(_: T)`, where `tt` is the `TypeTest[S, T]` instance.
* Type tests necessary before calling other extractors are treated similarly.
* `SomeExtractor(...)` is turned into `tt(SomeExtractor(...))` if `T` in `SomeExtractor.unapply(x: T)`
* is uncheckable, but we have an instance of `TypeTest[S, T]`.
*/
def unapply(x: S): Option[x.type & T]

object TypeTest:

/** Trivial type test that always succeeds */
def identity[T]: TypeTest[T, T] = Some(_)
12 changes: 12 additions & 0 deletions library/src/scala/reflect/Typeable.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package scala.reflect

/** A shorhand for `TypeTest[Any, T]`. A `Typeable[T] contains the logic needed to
* know at runtime if a value can be downcasted to `T`.
*
* If a pattern match is performed on a term of type `s: Any` that is uncheckable with `s.isInstanceOf[T]` and
* the pattern are of the form:
* - `t: T`
* - `t @ X()` where the `X.unapply` has takes an argument of type `T`
* then a given instance of `Typeable[T]` (`TypeTest[Any, T]`) is summoned and used to perform the test.
*/
type Typeable[T] = TypeTest[Any, T]
24 changes: 24 additions & 0 deletions tests/neg-custom-args/fatal-warnings/IsInstanceOfClassTag.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import scala.reflect.ClassTag

object IsInstanceOfClassTag {
def safeCast[T: ClassTag](x: Any): Option[T] = {
x match {
case x: T => Some(x) // TODO error: deprecation waring
case _ => None
}
}

def main(args: Array[String]): Unit = {
safeCast[List[String]](List[Int](1)) match {
case None =>
case Some(xs) =>
xs.head.substring(0)
}

safeCast[List[_]](List[Int](1)) match {
case None =>
case Some(xs) =>
xs.head.substring(0) // error
}
}
}
22 changes: 22 additions & 0 deletions tests/neg-custom-args/fatal-warnings/IsInstanceOfClassTag2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import scala.reflect.TypeTest

object IsInstanceOfClassTag {
def safeCast[T](x: Any)(using TypeTest[Any, T]): Option[T] = {
x match {
case x: T => Some(x)
case _ => None
}
}

def main(args: Array[String]): Unit = {
safeCast[List[String]](List[Int](1)) match { // error
case None =>
case Some(xs) =>
}

safeCast[List[_]](List[Int](1)) match {
case None =>
case Some(xs) =>
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import scala.language.`3.0-migration`
import scala.reflect.ClassTag

def f3_0m[T: ClassTag](x: Any): Unit =
x match
case _: T =>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import scala.language.`3.0`
import scala.reflect.ClassTag

def f3_0[T: ClassTag](x: Any): Unit =
x match
case _: T =>
Loading

0 comments on commit 0134f2c

Please sign in to comment.