Skip to content

Performance tweak for initialization check #11928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions compiler/src/dotty/tools/dotc/transform/init/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,18 @@ object Checking {
safePromoted: mutable.Set[Potential], // Potentials that can be safely promoted
env: Env
) {
def withOwner(sym: Symbol): State = copy(env = env.withOwner(sym))
def withOwner[T](sym: Symbol)(op: State ?=> T): T =
val state = this.copy(env = env.withOwner(sym))
val res = op(using state)
this.visited = state.visited
res


def visit[T](eff: Effect)(op: State ?=> T): T =
val state: State = this.copy(path = path :+ eff.source, visited = this.visited + eff)
val res = op(using state)
this.visited = state.visited
res

def test(op: State ?=> Errors): Errors = {
val savedVisited = visited
Expand All @@ -58,15 +69,14 @@ object Checking {
traceIndented("Already checked " + eff.show, init)
Errors.empty
}
else {
state.visited = state.visited + eff
val state2: State = state.copy(path = state.path :+ eff.source)
eff match {
case eff: Promote => Checking.checkPromote(eff)(using state2)
case eff: FieldAccess => Checking.checkFieldAccess(eff)(using state2)
case eff: MethodCall => Checking.checkMethodCall(eff)(using state2)
else
state.visit(eff) {
eff match {
case eff: Promote => Checking.checkPromote(eff)
case eff: FieldAccess => Checking.checkFieldAccess(eff)
case eff: MethodCall => Checking.checkMethodCall(eff)
}
}
}
}
}

Expand Down Expand Up @@ -118,11 +128,11 @@ object Checking {
def checkConstructor(ctor: Symbol, tp: Type, source: Tree)(using state: State): Unit = traceOp("checking " + ctor.show, init) {
val cls = ctor.owner
val classDef = cls.defTree
if (!classDef.isEmpty) {
given State = state.withOwner(cls)
if (ctor.isPrimaryConstructor) checkClassBody(classDef.asInstanceOf[TypeDef])
else checkSecondaryConstructor(ctor)
}
if (!classDef.isEmpty)
state.withOwner(cls) {
if (ctor.isPrimaryConstructor) checkClassBody(classDef.asInstanceOf[TypeDef])
else checkSecondaryConstructor(ctor)
}
}

def checkSecondaryConstructor(ctor: Symbol)(using state: State): Unit = traceOp("checking " + ctor.show, init) {
Expand Down
2 changes: 1 addition & 1 deletion tests/init/neg/function1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Foo {
val fun2: Int => Int = n => 1 + n + list.size
fun2(5)

List(5, 9).map(n => 2 + n + list.size) // error
List(5, 9).map(n => 2 + n + list.size)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this an error before? To what I understand final val is a "constant" (not assignable) value, so it gets initialized in a different order?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only final vals of constant types (such as false, 2, etc.) have different behavior due to const-folding.

We don't have a warning now, because at line 5, we already check the effect this.list!, thus the access this.list! is skipped here. If we comment out line 5, we will have a warning at this line.


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

Expand Down