File tree 4 files changed +27
-1
lines changed
test/dotty/tools/dotc/reporting 4 files changed +27
-1
lines changed Original file line number Diff line number Diff line change @@ -1767,7 +1767,7 @@ object Parsers {
1767
1767
} else {
1768
1768
accept(COLON )
1769
1769
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 ))
1771
1771
paramType()
1772
1772
}
1773
1773
val default =
Original file line number Diff line number Diff line change @@ -62,6 +62,7 @@ public enum ErrorMessageID {
62
62
ReassignmentToValID ,
63
63
TypeDoesNotTakeParametersID ,
64
64
ParameterizedTypeLacksArgumentsID ,
65
+ VarValParametersMayNotBeCallByNameID ,
65
66
;
66
67
67
68
public int errorNumber () {
Original file line number Diff line number Diff line change @@ -1336,4 +1336,18 @@ object messages {
1336
1336
|out the parameter list when extending it.
1337
1337
| """
1338
1338
}
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
+ }
1339
1353
}
Original file line number Diff line number Diff line change @@ -473,4 +473,15 @@ class ErrorMessagesTests extends ErrorMessagesTest {
473
473
assertEquals(" trait WithParams" , symbol.show)
474
474
}
475
475
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
+
476
487
}
You can’t perform that action at this time.
0 commit comments