diff options
author | Adriaan Moors <adriaan.moors@epfl.ch> | 2012-06-04 15:35:35 +0200 |
---|---|---|
committer | Eugene Burmako <xeno.by@gmail.com> | 2012-06-08 15:35:09 +0200 |
commit | 6b3ef4f1676adcbe6dbdbf59a3bd359f339b0626 (patch) | |
tree | 44de8ceb19ff698d92c17bc37fcd6bbb84731ba1 /test/files | |
parent | 10292e4acbb8eb0143a5a087c750ed9699f31807 (diff) | |
download | scala-6b3ef4f1676adcbe6dbdbf59a3bd359f339b0626.tar.gz scala-6b3ef4f1676adcbe6dbdbf59a3bd359f339b0626.tar.bz2 scala-6b3ef4f1676adcbe6dbdbf59a3bd359f339b0626.zip |
turn unchecked type patterns into checked ones
the pattern `(_: T)` is made checkable using (ct: ClassTag[T]).unapply
by rewriting it to `ct(_: T)` (if there's a ClassTag[T] available)
similarly for extractors: if the formal type of the unapply method
is an uncheckable type, wrap in the corresponding classtag extractor (if available)
don't trigger rewrite on non-toplevel unchecked types
(i.e., only look at type constructor part of T when looking for unchecked types)
TODO: find outer match to figure out if we're supposed to be unchecked
would like to give users a chance to opt-out from the wrapping,
but finding the match to which this pattern belongs turned out to be tricky...
Diffstat (limited to 'test/files')
-rw-r--r-- | test/files/run/patmat_unapp_abstype-new.scala | 21 | ||||
-rw-r--r-- | test/files/run/virtpatmat_typetag.check | 10 | ||||
-rw-r--r-- | test/files/run/virtpatmat_typetag.scala | 36 |
3 files changed, 53 insertions, 14 deletions
diff --git a/test/files/run/patmat_unapp_abstype-new.scala b/test/files/run/patmat_unapp_abstype-new.scala index 45496f08a2..1141177504 100644 --- a/test/files/run/patmat_unapp_abstype-new.scala +++ b/test/files/run/patmat_unapp_abstype-new.scala @@ -1,10 +1,10 @@ +import reflect.{ClassTag, classTag} + // abstract types and extractors, oh my! trait TypesAPI { trait Type - // an alternative fix (implemented in the virtual pattern matcher, is to replace the isInstanceOf by a manifest-based run-time test) - // that's what typeRefMani is for - type TypeRef <: Type //; implicit def typeRefMani: Manifest[TypeRef] + type TypeRef <: Type val TypeRef: TypeRefExtractor; trait TypeRefExtractor { def apply(x: Int): TypeRef def unapply(x: TypeRef): Option[(Int)] @@ -19,11 +19,6 @@ trait TypesUser extends TypesAPI { def shouldNotCrash(tp: Type): Unit = { tp match { case TypeRef(x) => println("TypeRef") - // the above checks tp.isInstanceOf[TypeRef], which is erased to tp.isInstanceOf[Type] - // before calling TypeRef.unapply(tp), which will then crash unless tp.isInstanceOf[TypesImpl#TypeRef] (which is not implied by tp.isInstanceOf[Type]) - // tp.isInstanceOf[TypesImpl#TypeRef] is equivalent to classOf[TypesImpl#TypeRef].isAssignableFrom(tp.getClass) - // this is equivalent to manifest - // it is NOT equivalent to manifest[Type] <:< typeRefMani case MethodType(x) => println("MethodType") case _ => println("none of the above") } @@ -34,7 +29,6 @@ trait TypesImpl extends TypesAPI { object TypeRef extends TypeRefExtractor // this will have a bridged unapply(x: Type) = unapply(x.asInstanceOf[TypeRef]) case class TypeRef(n: Int) extends Type // this has a bridge from TypesAPI#Type to TypesImpl#TypeRef // --> the cast in the bridge will fail because the pattern matcher can't type test against the abstract types in TypesUser - //lazy val typeRefMani = manifest[TypeRef] } trait Foos { @@ -63,16 +57,15 @@ trait Intermed extends Foos { object TestUnappStaticallyKnownSynthetic extends TypesImpl with TypesUser { def test() = { - shouldNotCrash(TypeRef(10)) // should and does print "TypeRef" - // once #1697/#2337 are fixed, this should generate the correct output - shouldNotCrash(MethodType(10)) // should print "MethodType" but prints "none of the above" -- good one, pattern matcher! + shouldNotCrash(TypeRef(10)) // prints "TypeRef" + shouldNotCrash(MethodType(10)) // prints "MethodType" } } object TestUnappDynamicSynth extends RealFoos with Intermed { - case class FooToo(n: Int) extends Bar + case class NotAFoo(n: Int) extends Bar def test() = { - crash(FooToo(10)) + crash(NotAFoo(10)) crash(new Foo(5)) } } diff --git a/test/files/run/virtpatmat_typetag.check b/test/files/run/virtpatmat_typetag.check new file mode 100644 index 0000000000..f9800b84d0 --- /dev/null +++ b/test/files/run/virtpatmat_typetag.check @@ -0,0 +1,10 @@ +1 is not a ClassTag[int]; it's a class java.lang.Integer +1 is a ClassTag[class java.lang.Integer] +1 is not a ClassTag[class java.lang.String]; it's a class java.lang.Integer +true is a ClassTag[class java.lang.Object] +woele is a ClassTag[class java.lang.String] +1 is not a ClassTag[int]; it's a class java.lang.Integer +1 is a ClassTag[class java.lang.Integer] +1 is not a ClassTag[class java.lang.String]; it's a class java.lang.Integer +true is a ClassTag[class java.lang.Object] +woele is a ClassTag[class java.lang.String] diff --git a/test/files/run/virtpatmat_typetag.scala b/test/files/run/virtpatmat_typetag.scala new file mode 100644 index 0000000000..c1b1fd813a --- /dev/null +++ b/test/files/run/virtpatmat_typetag.scala @@ -0,0 +1,36 @@ +import reflect.{ClassTag, classTag} + +trait Extractors { + type T + implicit val tTag: ClassTag[T] + object ExtractT { + def unapply(x: T) = Some(x) + } + def apply(a: Any) = a match { + case ExtractT(x) => println(x +" is a "+ implicitly[ClassTag[T]]) + case _ => println(a+ " is not a "+ implicitly[ClassTag[T]] +"; it's a "+ a.getClass) + } +} + +object Test extends App { + def typeMatch[T: ClassTag](a: Any) = a match { + case x : T => println(x +" is a "+ implicitly[ClassTag[T]]) + case _ => println(a+ " is not a "+ implicitly[ClassTag[T]] +"; it's a "+ a.getClass) + } + + // the same match as typeMatch, but using an extractor + def extractorMatch[S: ClassTag](a: Any) = + (new Extractors { type T = S; val tTag = classTag[T] })(a) + + typeMatch[Int](1) + typeMatch[Integer](1) + typeMatch[String](1) + typeMatch[Any](true) + typeMatch[String]("woele") + + extractorMatch[Int](1) + extractorMatch[Integer](1) + extractorMatch[String](1) + extractorMatch[Any](true) + extractorMatch[String]("woele") +}
\ No newline at end of file |