Skip to content

Commit

Permalink
Merge pull request scala#7729 from EpamLifeSciencesTeam/issue-1589-er…
Browse files Browse the repository at this point in the history
…ror-mes-case-class-missing-non-implicit-param-list

Add detailed message for case class missing non-implicit parameter list
  • Loading branch information
OlivierBlanvillain authored Jan 17, 2020
2 parents 3e08142 + d3cf3a3 commit 4b9333a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ object desugar {
ListOfNil
}
else if (isCaseClass && originalVparamss.head.exists(_.mods.isOneOf(GivenOrImplicit))) {
ctx.error("Case classes should have a non-implicit parameter list", namePos)
ctx.error(CaseClassMissingNonImplicitParamList(cdef), namePos)
ListOfNil
}
else originalVparamss.nestedMap(toDefParam(_, keepAnnotations = false))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ enum ErrorMessageID extends java.lang.Enum[ErrorMessageID] {
IllegalCyclicTypeReferenceID,
MissingTypeParameterInTypeAppID,
ImplicitTypesCanOnlyBeFunctionTypesID,
ErasedTypesCanOnlyBeFunctionTypesID
ErasedTypesCanOnlyBeFunctionTypesID,
CaseClassMissingNonImplicitParamListID

def errorNumber = ordinal - 2
}
12 changes: 12 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2395,4 +2395,16 @@ object messages {
val msg: String = "Types with erased keyword can only be function types `erased (...) => ...`"
val explanation: String = ""
}

case class CaseClassMissingNonImplicitParamList(cdef: untpd.TypeDef)(implicit ctx: Context)
extends Message(CaseClassMissingNonImplicitParamListID) {
val kind: String = "Syntax"
val msg: String =
em"""|A ${hl("case class")} must have at least one non-implicit parameter list"""

val explanation: String =
em"""|${cdef.name} must have at least one non-implicit parameter list,
| if you're aiming to have a case class parametrized only by implicit ones, you should
| add an explicit ${hl("()")} as a parameter list to ${cdef.name}.""".stripMargin
}
}
28 changes: 28 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1710,4 +1710,32 @@ class ErrorMessagesTests extends ErrorMessagesTest {
val ErasedTypesCanOnlyBeFunctionTypes() :: Nil = messages
assertEquals("Types with erased keyword can only be function types `erased (...) => ...`", messages.head.msg)
}

@Test def caseClassMissingParamListSuccessful =
checkMessagesAfter(FrontEnd.name) ("case class Test()")
.expectNoErrors

@Test def caseClassMissingParamListFailed =
checkMessagesAfter(FrontEnd.name) ("case class Test")
.expect {
(ictx, messages) =>
implicit val ctx: Context = ictx
assertMessageCount(1, messages)
val CaseClassMissingParamList(tpe) :: Nil = messages
assertEquals("A case class must have at least one parameter list", messages.head.msg)
}

@Test def caseClassMissingNonImplicitParamListSuccessful =
checkMessagesAfter(FrontEnd.name) ("case class Test()(given foo: String)")
.expectNoErrors

@Test def caseClassMissingNonImplicitParamListFailed =
checkMessagesAfter(FrontEnd.name) ("case class Test(given foo: String)")
.expect {
(ictx, messages) =>
implicit val ctx: Context = ictx
assertMessageCount(1, messages)
val CaseClassMissingNonImplicitParamList(tpe) :: Nil = messages
assertEquals("A case class must have at least one non-implicit parameter list", messages.head.msg)
}
}

0 comments on commit 4b9333a

Please sign in to comment.