File tree Expand file tree Collapse file tree 4 files changed +36
-2
lines changed
test/dotty/tools/dotc/reporting Expand file tree Collapse file tree 4 files changed +36
-2
lines changed Original file line number Diff line number Diff line change @@ -142,7 +142,8 @@ public enum ErrorMessageID {
142
142
StaticOverridingNonStaticMembersID ,
143
143
OverloadInRefinementID ,
144
144
NoMatchingOverloadID ,
145
- StableIdentPatternID
145
+ StableIdentPatternID ,
146
+ StaticFieldsShouldPrecedeNonStaticID
146
147
;
147
148
148
149
public int errorNumber () {
Original file line number Diff line number Diff line change @@ -1936,6 +1936,24 @@ object messages {
1936
1936
hl " ${" @static" } members are only allowed inside objects. "
1937
1937
}
1938
1938
1939
+ case class StaticFieldsShouldPrecedeNonStatic (member : Symbol )(implicit ctx : Context ) extends Message (StaticFieldsShouldPrecedeNonStaticID ) {
1940
+ val msg : String = hl " ${" @static" } $member in ${member.owner} must be defined before non-static fields. "
1941
+ val kind : String = " Syntax"
1942
+ val explanation : String = {
1943
+ val codeExample = """ object Foo {
1944
+ | @static val foo = 1
1945
+ | val bar = 2
1946
+ |}"""
1947
+ hl """ The fields annotated with @static should precede any non-@static fields.
1948
+ |This ensures that we do not introduce surprises for users in initialization order of this class.
1949
+ | $codeExample
1950
+ |
1951
+ |Static field are initialized when class loading the code of Foo.
1952
+ |Non static fields are only initialized the first time that Foo is accessed.
1953
+ | """
1954
+ }
1955
+ }
1956
+
1939
1957
case class CyclicInheritance (symbol : Symbol , addendum : String )(implicit ctx : Context ) extends Message (CyclicInheritanceID ) {
1940
1958
val kind : String = " Syntax"
1941
1959
val msg : String = hl " Cyclic inheritance: $symbol extends itself $addendum"
Original file line number Diff line number Diff line change @@ -37,7 +37,7 @@ class CheckStatic extends MiniPhase {
37
37
}
38
38
39
39
if (defn.isInstanceOf [ValDef ] && hadNonStaticField) {
40
- ctx.error(" @static fields should precede non-static ones " , defn.pos)
40
+ ctx.error(StaticFieldsShouldPrecedeNonStatic (defn.symbol) , defn.pos)
41
41
}
42
42
43
43
val companion = ctx.owner.companionClass
Original file line number Diff line number Diff line change @@ -1393,6 +1393,21 @@ class ErrorMessagesTests extends ErrorMessagesTest {
1393
1393
assertEquals(field.show, " method bar" )
1394
1394
}
1395
1395
1396
+ @ Test def staticShouldPrecedeNonStatic =
1397
+ checkMessagesAfter(CheckStatic .name) {
1398
+ """
1399
+ |class Foo
1400
+ |object Foo {
1401
+ | val foo = 1
1402
+ | @annotation.static val bar = 2
1403
+ |}
1404
+ """ .stripMargin
1405
+ }.expect { (ictx, messages) =>
1406
+ implicit val ctx : Context = ictx
1407
+ val StaticFieldsShouldPrecedeNonStatic (field) = messages.head
1408
+ assertEquals(field.show, " value bar" )
1409
+ }
1410
+
1396
1411
@ Test def cyclicInheritance =
1397
1412
checkMessagesAfter(FrontEnd .name) {
1398
1413
" class A extends A"
You can’t perform that action at this time.
0 commit comments