Skip to content

Commit 42f8369

Browse files
Merge pull request #3508 from gosubpl/wip/1589-unapply-invalid-number-args-2
Add error message for wrong number of argument patterns
2 parents 7f32798 + e5e9270 commit 42f8369

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ public enum ErrorMessageID {
114114
TraitIsExpectedID,
115115
TraitRedefinedFinalMethodFromAnyRefID,
116116
PackageNameAlreadyDefinedID,
117+
UnapplyInvalidNumberOfArgumentsID,
117118
;
118119

119120
public int errorNumber() {

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,4 +1929,17 @@ object messages {
19291929
val explanation =
19301930
hl"An ${"object"} cannot have the same name as an existing ${"package"}. Rename either one of them."
19311931
}
1932+
1933+
case class UnapplyInvalidNumberOfArguments(qual: untpd.Tree, argTypes: List[Type])(implicit ctx: Context)
1934+
extends Message(UnapplyInvalidNumberOfArgumentsID) {
1935+
val kind = "Syntax"
1936+
val msg = hl"Wrong number of argument patterns for $qual; expected: ($argTypes%, %)"
1937+
val explanation =
1938+
hl"""The Unapply method of $qual was used with incorrect number of arguments.
1939+
|Expected usage would be something like:
1940+
|case $qual(${argTypes.map(_ => '_')}%, %) => ...
1941+
|
1942+
|where subsequent arguments would have following types: ($argTypes%, %).
1943+
|""".stripMargin
1944+
}
19321945
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package dotc
33
package typer
44

55
import core._
6-
import ast.{Trees, untpd, tpd, TreeInfo}
6+
import ast.{TreeInfo, Trees, tpd, untpd}
77
import util.Positions._
88
import util.Stats.track
99
import Trees.Untyped
@@ -26,13 +26,14 @@ import EtaExpansion._
2626
import Inferencing._
2727

2828
import collection.mutable
29-
import config.Printers.{typr, unapp, overload}
29+
import config.Printers.{overload, typr, unapp}
3030
import TypeApplications._
3131

3232
import language.implicitConversions
3333
import reporting.diagnostic.Message
3434
import reporting.trace
3535
import Constants.{Constant, IntTag, LongTag}
36+
import dotty.tools.dotc.reporting.diagnostic.messages.UnapplyInvalidNumberOfArguments
3637

3738
import scala.collection.mutable.ListBuffer
3839

@@ -969,7 +970,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
969970
case _ => args
970971
}
971972
if (argTypes.length != bunchedArgs.length) {
972-
ctx.error(em"wrong number of argument patterns for $qual; expected: ($argTypes%, %)", tree.pos)
973+
ctx.error(UnapplyInvalidNumberOfArguments(qual, argTypes), tree.pos)
973974
argTypes = argTypes.take(args.length) ++
974975
List.fill(argTypes.length - args.length)(WildcardType)
975976
}

compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,6 @@ class ErrorMessagesTests extends ErrorMessagesTest {
11891189
assertEquals("method wait", method.show)
11901190
}
11911191

1192-
11931192
@Test def packageNameAlreadyDefined =
11941193
checkMessagesAfter("frontend") {
11951194
"""
@@ -1203,4 +1202,26 @@ class ErrorMessagesTests extends ErrorMessagesTest {
12031202
val PackageNameAlreadyDefined(pkg) = messages.head
12041203
assertEquals(pkg.show, "object bar")
12051204
}
1205+
1206+
@Test def unapplyInvalidNumberOfArguments =
1207+
checkMessagesAfter("frontend") {
1208+
"""
1209+
|case class Boo(a: Int, b: String)
1210+
|
1211+
|object autoTuplingNeg2 {
1212+
| val z = Boo(1, "foo")
1213+
|
1214+
| z match {
1215+
| case Boo(a, b, c) => a
1216+
| }
1217+
|}
1218+
""".stripMargin
1219+
}
1220+
.expect { (ictx, messages) =>
1221+
implicit val ctx: Context = ictx
1222+
assertMessageCount(1, messages)
1223+
val UnapplyInvalidNumberOfArguments(qual, argTypes) :: Nil = messages
1224+
assertEquals("Boo", qual.show)
1225+
assertEquals("(class Int, class String)", argTypes.map(_.typeSymbol).mkString("(", ", ", ")"))
1226+
}
12061227
}

0 commit comments

Comments
 (0)