Skip to content

Commit 4737d4f

Browse files
author
EnzeXing
committed
Only conduct check on methods with source
1 parent af62be6 commit 4737d4f

File tree

5 files changed

+31
-29
lines changed

5 files changed

+31
-29
lines changed

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

Lines changed: 13 additions & 12 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]
@@ -1082,16 +1079,20 @@ class Objects(using Context @constructorOnly):
10821079
val outerEnvs = Env.resolveEnvByMethod(enclosingMethod, ref, summon[Scope])
10831080
(outerEnvs.joinThisV, outerEnvs)
10841081

1085-
val env2 = Env.ofDefDef(ddef, args.map(_.value), thisV, outerEnv)
1086-
extendTrace(ddef) {
1087-
given Scope = env2
1088-
cache.cachedEval(ref, ddef.rhs, cacheResult = true) { expr =>
1089-
Returns.installHandler(meth)
1090-
val res = cases(expr, thisV, cls)
1091-
val returns = Returns.popHandler(meth)
1092-
res.join(returns)
1082+
if ref.isInstanceOf[ObjectRef] && !ref.asObjectRef.isAfterSuperCall then
1083+
report.warning("Calling " + target + " of object " + ref.klass + " before the super constructor of the object finishes! " + Trace.show, Trace.position)
1084+
Bottom
1085+
else
1086+
val env2 = Env.ofDefDef(ddef, args.map(_.value), thisV, outerEnv)
1087+
extendTrace(ddef) {
1088+
given Scope = env2
1089+
cache.cachedEval(ref, ddef.rhs, cacheResult = true) { expr =>
1090+
Returns.installHandler(meth)
1091+
val res = cases(expr, thisV, cls)
1092+
val returns = Returns.popHandler(meth)
1093+
res.join(returns)
1094+
}
10931095
}
1094-
}
10951096
else
10961097
UnknownValue
10971098
else if target.exists then
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
abstract class Foo[T](defaultValue: => T, arg1: Int = 1, arg2: Int = 2):
2-
def getValue: T = defaultValue
1+
class C(i: Int = 42, j: Int = 27) {
2+
val f = X.foo() // warn
3+
}
34

4-
enum Baz:
5-
case E1, E2 // warn
5+
object X extends C(j = 5):
6+
def foo() = 5
67

7-
object Baz extends Foo[Baz](Baz.E1, arg2 = 2) // warn
8-
9-
@main def test = println(Baz.getValue)
8+
@main def test = println:
9+
X

tests/init-global/warn/call-before-super4.scala

Lines changed: 0 additions & 9 deletions
This file was deleted.

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,13 @@
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)
12+
val tokenString, debugString: Array[String] = new Array[String](maxToken + 1) // warn
1313
}
1414

1515
object JavaTokens extends TokensCommon {

0 commit comments

Comments
 (0)