Skip to content

Commit b52e727

Browse files
committed
[1589] Moved anonymous function missing parameter type error message to case class scheme.
1 parent c804620 commit b52e727

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
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
@@ -87,6 +87,7 @@ public enum ErrorMessageID {
8787
ValueClassParameterMayNotBeAVarID,
8888
ValueClassNeedsExactlyOneValParamID,
8989
OnlyCaseClassOrCaseObjectAllowedID,
90+
AnonymousFunctionMissingParamTypeID
9091
;
9192

9293
public int errorNumber() {

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,34 @@ object messages {
176176
|Or, add an explicit `()' as a parameter list to ${cdef.name}."""
177177
}
178178

179+
case class AnonymousFunctionMissingParamType(param: untpd.ValDef,
180+
args: List[untpd.Tree],
181+
tree: untpd.Function,
182+
pt: Type)
183+
(implicit ctx: Context)
184+
extends Message(AnonymousFunctionMissingParamTypeID) {
185+
val kind = "Syntax"
186+
187+
val msg = {
188+
val ofFun =
189+
if (MethodType.syntheticParamNames(args.length + 1) contains param.name)
190+
i" of expanded function $tree"
191+
else
192+
""
193+
194+
i"missing parameter type for parameter ${param.name}$ofFun, expected = $pt"
195+
}
196+
197+
val explanation =
198+
hl"""|Anonymous functions must define a type. For example, if you define a function like this one:
199+
|
200+
|val f = { case l@List(1,2,3) => Some(l) }
201+
|
202+
|Make sure you give it a type of what you expect to match and help the type inference system:
203+
|
204+
|val f: Seq[Int] => Option[List[Int]] = { case l@List(1,2,3) => Some(l) } """
205+
}
206+
179207

180208
// Type Errors ------------------------------------------------------------ //
181209
case class DuplicateBind(bind: untpd.Bind, tree: untpd.CaseDef)(implicit ctx: Context)

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -762,12 +762,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
762762
}
763763
case _ =>
764764
}
765-
val ofFun =
766-
if (MethodType.syntheticParamNames(args.length + 1) contains param.name)
767-
i" of expanded function $tree"
768-
else
769-
""
770-
errorType(i"missing parameter type for parameter ${param.name}$ofFun, expected = $pt", param.pos)
765+
errorType(AnonymousFunctionMissingParamType(param, args, tree, pt), param.pos)
771766
}
772767

773768
def protoFormal(i: Int): Type =

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,4 +792,22 @@ class ErrorMessagesTests extends ErrorMessagesTest {
792792
assertEquals(err, OnlyCaseClassOrCaseObjectAllowed())
793793
}
794794

795+
@Test def anonymousFunctionMissingParamType =
796+
checkMessagesAfter("refchecks") {
797+
"""
798+
|object AnonymousF {
799+
| val f = { case l@List(1,2,3) => Some(l) }
800+
|}""".stripMargin
801+
}
802+
.expect { (ictx, messages) =>
803+
implicit val ctx: Context = ictx
804+
val defn = ictx.definitions
805+
806+
assertMessageCount(1, messages)
807+
val AnonymousFunctionMissingParamType(param, args, _, pt) :: Nil = messages
808+
assertEquals("x$1", param.show)
809+
assertEquals(s"List(ValDef(${param.show},TypeTree,EmptyTree))", args.toString)
810+
assertEquals("?", pt.show)
811+
}
812+
795813
}

0 commit comments

Comments
 (0)