Skip to content

Constant if condition gets eliminated even when it has side-effects #2077

Closed
@smarter

Description

@smarter
object Test {
  if ({ println("hi"); true })
    1
  else
    2
}

Becomes, after RefChecks:

   Test$() extends Object() { 
    1
  }

There are two very wrong things here:

  • This sort of optimization should be done in its own mini-phase, it has nothing to do with RefChecks
  • The optimization is wrong because it only looks at the type of the condition (and check that it's a ConstantType), it should also check the purity of the tree. This should be done by sharing code with TreeInfo#constToLiteral

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions