Skip to content

Fix #2266: Do not replace constant type lazy vals with constant. #2267

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 4 commits into from
Apr 19, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/TreeInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
def constToLiteral(tree: Tree)(implicit ctx: Context): Tree = {
val tree1 = ConstFold(tree)
tree1.tpe.widenTermRefExpr match {
case ConstantType(value) if isIdempotentExpr(tree1) => Literal(value)
case ConstantType(value) if isIdempotentExpr(tree1) && !tree1.symbol.is(Lazy) => Literal(value)
Copy link
Contributor

@DarkDimius DarkDimius Apr 15, 2017

Choose a reason for hiding this comment

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

I don't think this is specific to lazy vals.

abstract class A {
  def s: Boolean = r
  def r: Boolean
  assert(Test.r == this.s)
}

object Test extends A {
   override val r: true = true
   def main(args: Array[String]): Unit = {}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

// updated example to make it more likely to trigger

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What is the issue you would expect with that example?

Copy link
Contributor

Choose a reason for hiding this comment

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

assertion will fail, while it shouldn't.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, forgot to update the example, though I've left the comment that I did update it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Now, updated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed, that one fails for a similar reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a fix for that issue.

case _ => tree1
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/run/inline-constant-lazy-val.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
X
Y
10 changes: 10 additions & 0 deletions tests/run/inline-constant-lazy-val.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

object Test {
lazy val x: true = { println("X"); true }

def main(args: Array[String]): Unit = {
lazy val y: true = { println("Y"); true }
x
y
}
}