Skip to content

check whether a value class parameter is call-by-name #5176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ public enum ErrorMessageID {
ImportRenamedTwiceID,
TypeTestAlwaysSucceedsID,
TermMemberNeedsNeedsResultTypeForImplicitSearchID,
CaseClassCannotExtendEnumID
CaseClassCannotExtendEnumID,
ValueClassParameterMayNotBeCallByNameID
;

public int errorNumber() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1666,6 +1666,13 @@ object messages {
val explanation = ""
}

case class ValueClassParameterMayNotBeCallByName(valueClass: Symbol, param: Symbol)(implicit ctx: Context)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should reuse VarValParametersMayNotBeCallByName instead and maybe addapt a bit it's explanation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my first idea, but I thought it might be confusing, because it's not really a val and doesn't seem to behave like a case class parameter, which implicitly is a val, either.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right

extends Message(ValueClassParameterMayNotBeCallByNameID) {
val msg = s"value class parameter `${param.name}` may not be call-by-name"
val kind = "Syntax"
val explanation = ""
}

case class OnlyCaseClassOrCaseObjectAllowed()(implicit ctx: Context)
extends Message(OnlyCaseClassOrCaseObjectAllowedID) {
val msg = "only `case class` or `case object` allowed"
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,8 @@ object Checking {
case param :: params =>
if (param.is(Mutable))
ctx.error(ValueClassParameterMayNotBeAVar(clazz, param), param.pos)
if (param.info.isInstanceOf[ExprType])
ctx.error(ValueClassParameterMayNotBeCallByName(clazz, param), param.pos)
if (param.is(Erased))
ctx.error("value class first parameter cannot be `erased`", param.pos)
else {
Expand Down
12 changes: 12 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,18 @@ class ErrorMessagesTests extends ErrorMessagesTest {
assertEquals("class MyValue", valueClass.show)
}

@Test def valueClassParameterMayNotBeCallByName =
checkMessagesAfter(RefChecks.name) {
"""class MyValue(a: => Int) extends AnyVal"""
}
.expect { (ictx, messages) =>
implicit val ctx: Context = ictx
assertMessageCount(1, messages)
val ValueClassParameterMayNotBeCallByName(valueClass, param) :: Nil = messages
assertEquals("class MyValue", valueClass.show)
assertEquals("value a", param.show)
}

@Test def onlyCaseClassOrCaseObjectAllowed =
checkMessagesAfter(FrontEnd.name) {
"""case Foobar"""
Expand Down