File tree Expand file tree Collapse file tree 2 files changed +12
-8
lines changed
compiler/src/dotty/tools/dotc Expand file tree Collapse file tree 2 files changed +12
-8
lines changed Original file line number Diff line number Diff line change @@ -1936,20 +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 ) {
1939
+ case class StaticFieldsShouldPrecedeNonStatic (member : Symbol , defns : List [tpd. Tree ] )(implicit ctx : Context ) extends Message (StaticFieldsShouldPrecedeNonStaticID ) {
1940
1940
val msg : String = hl " ${" @static" } $member in ${member.owner} must be defined before non-static fields. "
1941
1941
val kind : String = " Syntax"
1942
+
1942
1943
val explanation : String = {
1943
- val codeExample = """ object Foo {
1944
- | @static val foo = 1
1945
- | val bar = 2
1944
+ val nonStatics = defns.takeWhile(_.symbol != member).take(3 ).filter(_.isInstanceOf [tpd.ValDef ])
1945
+ val codeExample = s """ object ${member.owner.name.firstPart} {
1946
+ | @static ${member} = ...
1947
+ | ${nonStatics.map(m => s " ${m.symbol} = ... " ).mkString(" \n " )}
1948
+ | ...
1946
1949
|} """
1947
- hl """ The fields annotated with @static should precede any non- @static fields.
1950
+ hl """ The fields annotated with @static should precede any non @static fields.
1948
1951
|This ensures that we do not introduce surprises for users in initialization order of this class.
1949
- | $codeExample
1950
- |
1951
1952
|Static field are initialized when class loading the code of Foo.
1952
1953
|Non static fields are only initialized the first time that Foo is accessed.
1954
+ |
1955
+ |The definition of ${member.name} should have been before the non ${" @static val" }s:
1956
+ | $codeExample
1953
1957
| """
1954
1958
}
1955
1959
}
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(StaticFieldsShouldPrecedeNonStatic (defn.symbol), defn.pos)
40
+ ctx.error(StaticFieldsShouldPrecedeNonStatic (defn.symbol, defns ), defn.pos)
41
41
}
42
42
43
43
val companion = ctx.owner.companionClass
You can’t perform that action at this time.
0 commit comments