Skip to content

Fix #4451: Set flag in the correct order #4817

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
Jul 20, 2018
Merged

Fix #4451: Set flag in the correct order #4817

merged 1 commit into from
Jul 20, 2018

Conversation

Medowhill
Copy link
Contributor

No description provided.

Copy link
Member

@dottybot dottybot left a comment

Choose a reason for hiding this comment

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

Hello, and thank you for opening this PR! 🎉

All contributors have signed the CLA, thank you! ❤️

Have an awesome day! ☀️

try foo.foo
catch {
case _: AssertionError =>
println(foo.foo) // prints 0, should be 42
Copy link
Contributor

Choose a reason for hiding this comment

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

I would use an assertion here and remove the check file. Also since you will add tests for other type of lazy vals, you could do something like:

class InitError extends Exception

class Lazy(x: Int) {
  private[this] var init = false;
  lazy val value = {
    if (!init) {
      init = true
      throw new InitError
    }
    x
  }
}

class LazyVolatile(x: Int) {
  private[this] var init = false;
  @volatile lazy val value = {
    if (!init) {
      init = true
      throw new InitError
    }
    x
  }
}

def tryTwice(op: () => Int): Int =
  try { op(); assert(false) }
  catch { _: InitError => op() }

val l1 = new Lazy(42)
val l2 = new LazyVolatile(42)
assert(tryTwice(() => l1.value) == 42)
assert(tryTwice(() => l2.value) == 42)
...

@@ -0,0 +1,21 @@
class Foo(x: Int) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add tests for lazy vals in methods as well as @volatile lazy vals

val init = Block(setFlag :: setTargetAndNullable, target.ensureApplied)
var setTargetFlagNullables = setFlag :: nullOut(nullables)
if (!isWildcardArg(rhs)) setTargetFlagNullables = target.becomes(rhs) :: setTargetFlagNullables
val init = Block(setTargetFlagNullables, target.ensureApplied)
Copy link
Contributor

@allanrenucci allanrenucci Jul 20, 2018

Choose a reason for hiding this comment

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

I would rewrite as:

val stats = new mutable.ListBuffer[Tree]
if (!isWildcardArg(rhs)) stats += target.becomes(rhs)
stats += flag.becomes(Literal(Constant(true)))
stats += nullOut(nullables)
val init = Block(stats.toList, target.ensureApplied)

Copy link
Contributor

@allanrenucci allanrenucci left a comment

Choose a reason for hiding this comment

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

LGTM. Can you squash your commits

@allanrenucci allanrenucci merged commit 89ab499 into scala:master Jul 20, 2018
@Medowhill Medowhill deleted the issue4451 branch July 20, 2018 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants