Skip to content

Commit

Permalink
Fail compilation of attempting a 0-arity product extraction
Browse files Browse the repository at this point in the history
It's reasonable to desire this to work, but for now let's just not
crash.

Co-authored-by: Seth Tisue <[email protected]>
  • Loading branch information
dwijnand and SethTisue committed Dec 2, 2021
1 parent 1003d7c commit 9d2439b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ object Applications {
getUnapplySelectors(getTp, args, pos)
else if (unapplyResult.widenSingleton isRef defn.BooleanClass)
Nil
else if (defn.isProductSubType(unapplyResult))
else if (defn.isProductSubType(unapplyResult) && productArity(unapplyResult, pos) != 0)
productSelectorTypes(unapplyResult, pos)
// this will cause a "wrong number of arguments in pattern" error later on,
// which is better than the message in `fail`.
Expand Down
6 changes: 6 additions & 0 deletions tests/neg/i13960.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- [E108] Declaration Error: tests/neg/i13960.scala:13:10 --------------------------------------------------------------
13 | case A() => // error
| ^^^
| A is not a valid result type of an unapply method of an extractor.

longer explanation available when compiling with `-explain`
15 changes: 15 additions & 0 deletions tests/neg/i13960.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class A() extends Product {
override def canEqual(that: Any) = true
override def productArity = 0
override def productElement(n: Int) = null
}

object A {
def unapply(a: A): A = a
}

object Main {
(new A) match {
case A() => // error
}
}

0 comments on commit 9d2439b

Please sign in to comment.