From 9d2439b2d99d7990e6cbf04671533f5c294e9072 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Thu, 2 Dec 2021 17:50:58 +0000 Subject: [PATCH] Fail compilation of attempting a 0-arity product extraction It's reasonable to desire this to work, but for now let's just not crash. Co-authored-by: Seth Tisue --- .../src/dotty/tools/dotc/typer/Applications.scala | 2 +- tests/neg/i13960.check | 6 ++++++ tests/neg/i13960.scala | 15 +++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 tests/neg/i13960.check create mode 100644 tests/neg/i13960.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Applications.scala b/compiler/src/dotty/tools/dotc/typer/Applications.scala index 1c346ac7af20..d8e9c413decb 100644 --- a/compiler/src/dotty/tools/dotc/typer/Applications.scala +++ b/compiler/src/dotty/tools/dotc/typer/Applications.scala @@ -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`. diff --git a/tests/neg/i13960.check b/tests/neg/i13960.check new file mode 100644 index 000000000000..ce280f48be4c --- /dev/null +++ b/tests/neg/i13960.check @@ -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` diff --git a/tests/neg/i13960.scala b/tests/neg/i13960.scala new file mode 100644 index 000000000000..54ae513598f1 --- /dev/null +++ b/tests/neg/i13960.scala @@ -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 + } +}