Skip to content

Commit 091f3ba

Browse files
committed
Merge pull request scala#2992 from retronym/ticket/7877
Only look for unapplies in term trees
2 parents 4b89ce6 + d290d0d commit 091f3ba

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

src/compiler/scala/tools/nsc/typechecker/Typers.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3245,7 +3245,8 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
32453245
if (!tree.isErrorTyped) setError(tree) else tree
32463246
// @H change to setError(treeCopy.Apply(tree, fun, args))
32473247

3248-
case ExtractorType(unapply) if mode.inPatternMode =>
3248+
// SI-7877 `isTerm` needed to exclude `class T[A] { def unapply(..) }; ... case T[X] =>`
3249+
case HasUnapply(unapply) if mode.inPatternMode && fun.isTerm =>
32493250
if (unapply == QuasiquoteClass_api_unapply) macroExpandUnapply(this, tree, fun, unapply, args, mode, pt)
32503251
else doTypedUnapply(tree, fun0, fun, args, mode, pt)
32513252

src/compiler/scala/tools/nsc/typechecker/Unapplies.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ trait Unapplies extends ast.TreeDSL {
3939
*/
4040
def unapplyMember(tp: Type): Symbol = directUnapplyMember(tp) filter (sym => !hasMultipleNonImplicitParamLists(sym))
4141

42-
object ExtractorType {
42+
object HasUnapply {
4343
def unapply(tp: Type): Option[Symbol] = unapplyMember(tp).toOption
4444
}
4545

test/files/neg/t7877.check

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
t7877.scala:6: error: not found: value Y
2+
case Y() => () // not allowed
3+
^
4+
t7877.scala:7: error: OnNext[Any] does not take parameters
5+
case OnNext[Any]() => () // should *not* be allowed, but was.
6+
^
7+
two errors found

test/files/neg/t7877.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Test {
2+
val X: OnNext[Any] = null
3+
def Y: OnNext[Any] = null
4+
(null: Any) match {
5+
case X() => () // allowed
6+
case Y() => () // not allowed
7+
case OnNext[Any]() => () // should *not* be allowed, but was.
8+
}
9+
}
10+
11+
class OnNext[+T] {
12+
def unapply(x: Any) = false
13+
}

0 commit comments

Comments
 (0)