From 758db16649ef569122354f44f10c85303e6f268a Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Mon, 19 Dec 2016 15:22:32 +0100 Subject: [PATCH] Fix #1828: add warning when patternmatching on generics --- .../dotc/transform/IsInstanceOfEvaluator.scala | 7 +++++-- tests/repl/errmsgs.check | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/transform/IsInstanceOfEvaluator.scala b/compiler/src/dotty/tools/dotc/transform/IsInstanceOfEvaluator.scala index 8bc4a2aa91ea..35de182676cc 100644 --- a/compiler/src/dotty/tools/dotc/transform/IsInstanceOfEvaluator.scala +++ b/compiler/src/dotty/tools/dotc/transform/IsInstanceOfEvaluator.scala @@ -147,8 +147,11 @@ class IsInstanceOfEvaluator extends MiniPhaseTransform { thisTransformer => val inMatch = s.qualifier.symbol is Case - if (valueClassesOrAny) tree - else if (knownStatically) + if (valueClassesOrAny) { + if (selector eq defn.ObjectType) + ctx.warning(i"abstract type pattern is unchecked since it is eliminated by erasure", tree.pos) + tree + } else if (knownStatically) handleStaticallyKnown(s, scrutinee, selector, inMatch, tree.pos) else if (falseIfUnrelated && scrutinee <:< selector) // scrutinee is a subtype of the selector, safe to rewrite diff --git a/tests/repl/errmsgs.check b/tests/repl/errmsgs.check index f0ccdf53f675..0199bb369ff4 100644 --- a/tests/repl/errmsgs.check +++ b/tests/repl/errmsgs.check @@ -78,4 +78,19 @@ scala> class Foo() { def bar: Int = 1 }; val foo = new Foo(); foo.barr 4 |class Foo() { def bar: Int = 1 }; val foo = new Foo(); foo.barr | ^^^^^^^^ | value `barr` is not a member of Foo(foo) - did you mean `foo.bar`? +scala> def unsafeCast[S](a: Any) = a match { case s: S => s; case _ => ??? } +-- Warning: ---------------------------------------------------------- +4 |def unsafeCast[S](a: Any) = a match { case s: S => s; case _ => ??? } + | ^ + | abstract type pattern is unchecked since it is eliminated by erasure +def unsafeCast[S](a: Any): [S] => (a: Any)S +scala> unsafeCast[String](1) +java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String + at .(:6) + at .() + at RequestResult$.(:3) + at RequestResult$.() + at RequestResult$result() + at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) + at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.jav... scala> :quit