Skip to content

Commit f556b04

Browse files
authored
Merge pull request #2447 from ennru/ennru_VarValParametersMayNotBeCallByName
Move 'var/val parameter may not be call-by-name' to error case class
2 parents 8fbe087 + 10339d1 commit f556b04

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1767,7 +1767,7 @@ object Parsers {
17671767
} else {
17681768
accept(COLON)
17691769
if (in.token == ARROW && owner.isTypeName && !(mods is Local))
1770-
syntaxError(s"${if (mods is Mutable) "`var'" else "`val'"} parameters may not be call-by-name")
1770+
syntaxError(VarValParametersMayNotBeCallByName(name, mods is Mutable))
17711771
paramType()
17721772
}
17731773
val default =

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public enum ErrorMessageID {
6262
ReassignmentToValID,
6363
TypeDoesNotTakeParametersID,
6464
ParameterizedTypeLacksArgumentsID,
65+
VarValParametersMayNotBeCallByNameID,
6566
;
6667

6768
public int errorNumber() {

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,4 +1336,18 @@ object messages {
13361336
|out the parameter list when extending it.
13371337
|"""
13381338
}
1339+
1340+
case class VarValParametersMayNotBeCallByName(name: Names.TermName, mutable: Boolean)(implicit ctx: Context)
1341+
extends Message(VarValParametersMayNotBeCallByNameID) {
1342+
val msg = s"${if (mutable) "`var'" else "`val'"} parameters may not be call-by-name"
1343+
val kind = "Syntax"
1344+
val explanation =
1345+
hl"""${"var"} and ${"val"} parameters of classes and traits may no be call-by-name. In case you
1346+
|want the parameter to be evaluated on demand, consider making it just a parameter
1347+
|and a ${"def"} in the class such as
1348+
| ${s"class MyClass(${name}Tick: => String) {"}
1349+
| ${s" def $name() = ${name}Tick"}
1350+
| ${"}"}
1351+
|"""
1352+
}
13391353
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,4 +473,15 @@ class ErrorMessagesTests extends ErrorMessagesTest {
473473
assertEquals("trait WithParams", symbol.show)
474474
}
475475

476+
@Test def varValParametersMayNotBeCallByName =
477+
checkMessagesAfter("frontend") {
478+
"trait Trait(val noNoNo: => String)"
479+
}
480+
.expect { (ictx, messages) =>
481+
implicit val ctx: Context = ictx
482+
assertMessageCount(1, messages)
483+
val VarValParametersMayNotBeCallByName(name, false) :: Nil = messages
484+
assertEquals("noNoNo", name.show)
485+
}
486+
476487
}

0 commit comments

Comments
 (0)