Skip to content

Commit 3ebeaa9

Browse files
authored
Merge pull request #11928 from dotty-staging/improve-init
Performance tweak for initialization check
2 parents e1aeeca + 7bed891 commit 3ebeaa9

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

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

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,18 @@ object Checking {
3939
safePromoted: mutable.Set[Potential], // Potentials that can be safely promoted
4040
env: Env
4141
) {
42-
def withOwner(sym: Symbol): State = copy(env = env.withOwner(sym))
42+
def withOwner[T](sym: Symbol)(op: State ?=> T): T =
43+
val state = this.copy(env = env.withOwner(sym))
44+
val res = op(using state)
45+
this.visited = state.visited
46+
res
47+
48+
49+
def visit[T](eff: Effect)(op: State ?=> T): T =
50+
val state: State = this.copy(path = path :+ eff.source, visited = this.visited + eff)
51+
val res = op(using state)
52+
this.visited = state.visited
53+
res
4354

4455
def test(op: State ?=> Errors): Errors = {
4556
val savedVisited = visited
@@ -58,15 +69,14 @@ object Checking {
5869
traceIndented("Already checked " + eff.show, init)
5970
Errors.empty
6071
}
61-
else {
62-
state.visited = state.visited + eff
63-
val state2: State = state.copy(path = state.path :+ eff.source)
64-
eff match {
65-
case eff: Promote => Checking.checkPromote(eff)(using state2)
66-
case eff: FieldAccess => Checking.checkFieldAccess(eff)(using state2)
67-
case eff: MethodCall => Checking.checkMethodCall(eff)(using state2)
72+
else
73+
state.visit(eff) {
74+
eff match {
75+
case eff: Promote => Checking.checkPromote(eff)
76+
case eff: FieldAccess => Checking.checkFieldAccess(eff)
77+
case eff: MethodCall => Checking.checkMethodCall(eff)
78+
}
6879
}
69-
}
7080
}
7181
}
7282

@@ -118,11 +128,11 @@ object Checking {
118128
def checkConstructor(ctor: Symbol, tp: Type, source: Tree)(using state: State): Unit = traceOp("checking " + ctor.show, init) {
119129
val cls = ctor.owner
120130
val classDef = cls.defTree
121-
if (!classDef.isEmpty) {
122-
given State = state.withOwner(cls)
123-
if (ctor.isPrimaryConstructor) checkClassBody(classDef.asInstanceOf[TypeDef])
124-
else checkSecondaryConstructor(ctor)
125-
}
131+
if (!classDef.isEmpty)
132+
state.withOwner(cls) {
133+
if (ctor.isPrimaryConstructor) checkClassBody(classDef.asInstanceOf[TypeDef])
134+
else checkSecondaryConstructor(ctor)
135+
}
126136
}
127137

128138
def checkSecondaryConstructor(ctor: Symbol)(using state: State): Unit = traceOp("checking " + ctor.show, init) {

tests/init/neg/function1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class Foo {
44
val fun2: Int => Int = n => 1 + n + list.size
55
fun2(5)
66

7-
List(5, 9).map(n => 2 + n + list.size) // error
7+
List(5, 9).map(n => 2 + n + list.size)
88

99
final val list = List(1, 2, 3) // error
1010

0 commit comments

Comments
 (0)