Skip to content

Commit d6fe4b4

Browse files
authored
Merge pull request #14029 from dwijnand/ko-product-0-extractor
Fail compilation of attempting a 0-arity product extraction
2 parents c3ebebe + 9d2439b commit d6fe4b4

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

compiler/src/dotty/tools/dotc/typer/Applications.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ object Applications {
189189
getUnapplySelectors(getTp, args, pos)
190190
else if (unapplyResult.widenSingleton isRef defn.BooleanClass)
191191
Nil
192-
else if (defn.isProductSubType(unapplyResult))
192+
else if (defn.isProductSubType(unapplyResult) && productArity(unapplyResult, pos) != 0)
193193
productSelectorTypes(unapplyResult, pos)
194194
// this will cause a "wrong number of arguments in pattern" error later on,
195195
// which is better than the message in `fail`.

tests/neg/i13960.check

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- [E108] Declaration Error: tests/neg/i13960.scala:13:10 --------------------------------------------------------------
2+
13 | case A() => // error
3+
| ^^^
4+
| A is not a valid result type of an unapply method of an extractor.
5+
6+
longer explanation available when compiling with `-explain`

tests/neg/i13960.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class A() extends Product {
2+
override def canEqual(that: Any) = true
3+
override def productArity = 0
4+
override def productElement(n: Int) = null
5+
}
6+
7+
object A {
8+
def unapply(a: A): A = a
9+
}
10+
11+
object Main {
12+
(new A) match {
13+
case A() => // error
14+
}
15+
}

0 commit comments

Comments
 (0)