Skip to content

Fix #4866: do not lift try with no catch block #4872

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 31, 2018

Conversation

abeln
Copy link
Contributor

@abeln abeln commented Jul 30, 2018

Forward port of scala/scala#922

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.

Can you also add a test to i1692.scala. This prevented an optimisation on lazy vals that should be applicable now:

class LazyNullable {
  ...
  private[this] val e = "E"
  lazy val l4 = try e finally () // null out e
}

object Test {
  ...
  def nullableTests() = {
    ...
    assert(lz.l4 == "E")
    assertNull("e")
    ...
  }
  ...
}

* Lifting is needed only for try-catch expressions that are evaluated in a context
* where the stack might not be empty. `finally` does not attempt to continue evaluation
* after an exception, so the fact that values on the stack are 'lost' does not matter
* (copied from https://github.com/scala/scala/pull/922).
Copy link
Contributor

Choose a reason for hiding this comment

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

Indentation of the lines above is off by one character

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -0,0 +1,27 @@
// Test that try-finally aren't lifted, but try-catch are.
Copy link
Contributor

Choose a reason for hiding this comment

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

Rename i4866.scala

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

var lifted = 0

for (f <- o.getClass.getDeclaredMethods) {
f.setAccessible(true)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copypasta. Simplified.

def numLifted(o: Object): Int = {
var lifted = 0

for (f <- o.getClass.getDeclaredMethods) {
Copy link
Contributor

Choose a reason for hiding this comment

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

def isLifted(method: Method) = method.getName.startsWith("lifted")
o.getClass.getDeclaredMethods.count(isLifted)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. Used it.

}

println("Foo #lifted: %d".format(numLifted(new Foo(new Object))))
println("FooLifted #lifted: %d".format(numLifted(new FooLifted(new Object))))
Copy link
Contributor

Choose a reason for hiding this comment

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

Why format?

"Foo #lifted: " + numLifted(new Foo(new Object))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed format.

@@ -0,0 +1,27 @@
// Test that try-finally aren't lifted, but try-catch are.

class Foo(param: => Object) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need a by-name parameter? Let's minimise feature interaction in tests:

class Foo {
 def foo: Int = try ??? finally ()
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't. Removed it.

val field = try param finally ()
}

class FooLifted(param: => Object) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@abeln
Copy link
Contributor Author

abeln commented Jul 31, 2018

PTAL

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! Thanks @abeln!

@allanrenucci allanrenucci merged commit b570776 into scala:master Jul 31, 2018
@abeln abeln deleted the lift-try branch July 31, 2018 11:39
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.

2 participants