Skip to content

Commit cdcf91a

Browse files
author
EnzeXing
committed
Address comments
1 parent 3ddd193 commit cdcf91a

File tree

11 files changed

+63
-22
lines changed

11 files changed

+63
-22
lines changed

compiler/src/dotty/tools/dotc/transform/init/Objects.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,9 +1066,6 @@ class Objects(using Context @constructorOnly):
10661066
else if target.equals(defn.Predef_classOf) then
10671067
// Predef.classOf is a stub method in tasty and is replaced in backend
10681068
UnknownValue
1069-
else if ref.isInstanceOf[ObjectRef] && !ref.asObjectRef.isAfterSuperCall then
1070-
report.warning("Calling " + target + " of object " + ref.klass + " before the super constructor of the object finishes! " + Trace.show, Trace.position)
1071-
Bottom
10721069
else if target.hasSource then
10731070
val cls = target.owner.enclosingClass.asClass
10741071
val ddef = target.defTree.asInstanceOf[DefDef]
@@ -1458,7 +1455,11 @@ class Objects(using Context @constructorOnly):
14581455
/** Check an individual object */
14591456
private def accessObject(classSym: ClassSymbol)(using Context, State.Data, Trace, Heap.MutableData, EnvMap.EnvMapMutableData): ObjectRef = log("accessing " + classSym.show, printer, (_: Value).show) {
14601457
if classSym.hasSource then
1461-
State.checkObjectAccess(classSym)
1458+
val obj = State.checkObjectAccess(classSym)
1459+
if !obj.isAfterSuperCall then
1460+
report.warning("Accessing " + obj.klass + " before the super constructor of the object finishes! " + Trace.show, Trace.position)
1461+
end if
1462+
obj
14621463
else
14631464
val obj = ObjectRef(classSym)
14641465
obj.setAfterSuperCall()
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-- Warning: tests/init-global/warn/global-cycle10.scala:13:14 ----------------------------------------------------------
2+
13 | def foo() = new Inner // warn
3+
| ^^^^^^^^^
4+
| Accessing module class O$ before the super constructor of the object finishes! Calling trace:
5+
| ├── object O extends Base { // error [ global-cycle10.scala:7 ]
6+
| │ ^
7+
| ├── abstract class Base { [ global-cycle10.scala:1 ]
8+
| │ ^
9+
| ├── foo() [ global-cycle10.scala:4 ]
10+
| │ ^^^^^
11+
| └── def foo() = new Inner // warn [ global-cycle10.scala:13 ]
12+
| ^^^^^^^^^
13+
-- Warning: tests/init-global/warn/global-cycle10.scala:10:12 ----------------------------------------------------------
14+
10 | println(msg) // warn
15+
| ^^^
16+
| Accessing module class O$ before the super constructor of the object finishes! Calling trace:
17+
| ├── object O extends Base { // error [ global-cycle10.scala:7 ]
18+
| │ ^
19+
| ├── abstract class Base { [ global-cycle10.scala:1 ]
20+
| │ ^
21+
| ├── foo() [ global-cycle10.scala:4 ]
22+
| │ ^^^^^
23+
| ├── def foo() = new Inner // warn [ global-cycle10.scala:13 ]
24+
| │ ^^^^^^^^^
25+
| ├── class Inner { [ global-cycle10.scala:9 ]
26+
| │ ^
27+
| └── println(msg) // warn [ global-cycle10.scala:10 ]
28+
| ^^^

tests/init-global/pos/global-cycle10.scala renamed to tests/init-global/warn/global-cycle10.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ abstract class Base {
77
object O extends Base { // error
88

99
class Inner {
10-
println(msg)
10+
println(msg) // warn
1111
}
1212

13-
def foo() = new Inner
13+
def foo() = new Inner // warn
1414
}
1515

1616
@main

tests/init-global/warn/global-cycle7.check

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,3 @@
1818
| │ ^
1919
| └── val m: Int = A.n // warn [ global-cycle7.scala:6 ]
2020
| ^^^
21-
-- Warning: /Users/enzexing/IdeaProjects/dotty-staging/dotty/tests/init-global/warn/global-cycle7.scala:12:66
22-
12 | val tokenString, debugString: Array[String] = new Array[String](maxToken + 1)
23-
| ^^^^^^^^
24-
|Calling method maxToken of object module class JavaTokens$ before the super constructor of the object finishes! Calling trace:
25-
|├── object JavaTokens extends TokensCommon { [ global-cycle7.scala:15 ]
26-
|│ ^
27-
|├── abstract class TokensCommon { [ global-cycle7.scala:9 ]
28-
|│ ^
29-
|└── val tokenString, debugString: Array[String] = new Array[String](maxToken + 1) [ global-cycle7.scala:12 ]
30-
| ^^^^^^^^

tests/init-global/warn/global-cycle7.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object B {
99
abstract class TokensCommon {
1010
def maxToken: Int
1111

12-
val tokenString, debugString: Array[String] = new Array[String](maxToken + 1) // warn
12+
val tokenString, debugString: Array[String] = new Array[String](maxToken + 1)
1313
}
1414

1515
object JavaTokens extends TokensCommon {

tests/init-global/warn/i9176.check

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,11 @@
44
| Cyclic initialization: object A -> object B -> object A. Calling trace:
55
| ├── case object A extends Foo(B) // warn [ i9176.scala:2 ]
66
| │ ^
7-
| └── case object B extends Foo(A) [ i9176.scala:3 ]
7+
| └── case object B extends Foo(A) // warn [ i9176.scala:3 ]
88
| ^
9+
-- Warning: tests/init-global/warn/i9176.scala:3:26 --------------------------------------------------------------------
10+
3 |case object B extends Foo(A) // warn
11+
| ^
12+
| Accessing module class A$ before the super constructor of the object finishes! Calling trace:
13+
| └── case object B extends Foo(A) // warn [ i9176.scala:3 ]
14+
| ^

tests/init-global/warn/i9176.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Foo(val opposite: Foo)
22
case object A extends Foo(B) // warn
3-
case object B extends Foo(A)
3+
case object B extends Foo(A) // warn
44
object Test {
55
def main(args: Array[String]): Unit = {
66
println(A.opposite)

tests/init-global/warn/resolve-parent-this.check

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
-- Warning: tests/init-global/warn/resolve-parent-this.scala:3:18 ------------------------------------------------------
2+
3 | val f: O.type = O // warn
3+
| ^
4+
| Accessing module class O$ before the super constructor of the object finishes! Calling trace:
5+
| ├── object O extends Delegate { [ resolve-parent-this.scala:6 ]
6+
| │ ^
7+
| ├── class Delegate { [ resolve-parent-this.scala:1 ]
8+
| │ ^
9+
| └── val f: O.type = O // warn [ resolve-parent-this.scala:3 ]
10+
| ^
111
-- Warning: tests/init-global/warn/resolve-parent-this.scala:7:21 ------------------------------------------------------
212
7 | val a: Int = foo().a // warn
313
| ^^^^^^^

tests/init-global/warn/resolve-parent-this.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Delegate {
22
def foo() = f
3-
val f: O.type = O
3+
val f: O.type = O // warn
44
}
55

66
object O extends Delegate {

tests/init-global/warn/t9261.check

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,11 @@
44
| Cyclic initialization: object Buy -> object Sell -> object Buy. Calling trace:
55
| ├── case object Buy extends OrderType(Sell) // warn [ t9261.scala:2 ]
66
| │ ^^^^
7-
| └── case object Sell extends OrderType(Buy) [ t9261.scala:3 ]
7+
| └── case object Sell extends OrderType(Buy) // warn [ t9261.scala:3 ]
88
| ^^^
9+
-- Warning: tests/init-global/warn/t9261.scala:3:35 --------------------------------------------------------------------
10+
3 |case object Sell extends OrderType(Buy) // warn
11+
| ^^^
12+
| Accessing module class Buy$ before the super constructor of the object finishes! Calling trace:
13+
| └── case object Sell extends OrderType(Buy) // warn [ t9261.scala:3 ]
14+
| ^^^

0 commit comments

Comments
 (0)