Skip to content

Commit

Permalink
Fix scala#1828: add warning when patternmatching on generics
Browse files Browse the repository at this point in the history
  • Loading branch information
felixmulder committed Dec 22, 2016
1 parent 404ce76 commit 758db16
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions tests/repl/errmsgs.check
Original file line number Diff line number Diff line change
Expand Up @@ -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: <console> ----------------------------------------------------------
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 .<init>(<console>:6)
at .<clinit>(<console>)
at RequestResult$.<init>(<console>:3)
at RequestResult$.<clinit>(<console>)
at RequestResult$result(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.jav...
scala> :quit

0 comments on commit 758db16

Please sign in to comment.