-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Conversation
There was a problem hiding this 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! ☀️
tests/run/i4451.scala
Outdated
try foo.foo | ||
catch { | ||
case _: AssertionError => | ||
println(foo.foo) // prints 0, should be 42 |
There was a problem hiding this comment.
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)
...
tests/run/i4451.scala
Outdated
@@ -0,0 +1,21 @@ | |||
class Foo(x: Int) { |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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)
There was a problem hiding this 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
No description provided.