Skip to content
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

Tail calls within catch blocks not optimized #1672

Closed
scabug opened this issue Jan 27, 2009 · 7 comments
Closed

Tail calls within catch blocks not optimized #1672

scabug opened this issue Jan 27, 2009 · 7 comments
Assignees
Labels
Milestone

Comments

@scabug
Copy link

scabug commented Jan 27, 2009

scala> def foo = {
     |   def bar : Nothing = {
     |     try {
     |  throw new RuntimeException 
     |     } catch {
     |         case _ => bar
     |    }
     |   }
     |   bar
     | }
foo: Nothing

scala> foo
java.lang.StackOverflowError
	at .bar$$1(<console>:9)
	at .bar$$1(<console>:9)
	at .bar$$1(<console>:9)
	at .bar$$1(<console>:9)
	at .bar$$1(<console>:9)
	at .bar$$1(<console>:9)
	at .bar$$1(<console>:9)
	at .bar$$1(<console>:9)
	at .bar$$1(<console>:9)
	at .bar$$1(<console>:9)
	at .bar$$1(<console>:9)
	at .bar$$1(<console>:9)
	at .bar$$1(<console>:9)
	at .bar$$1(<console>:9)
	at .bar$$1(<console>:9...
scala> 
@scabug
Copy link
Author

scabug commented Jan 27, 2009

Imported From: https://issues.scala-lang.org/browse/SI-1672?orig=1
Reporter: @eengbrec
Affected Versions: 2.9.2, 2.10.0-RC5
Attachments:

@scabug
Copy link
Author

scabug commented Jan 27, 2009

@eengbrec said:
I'll attach a test case for partest tomorrow...

@scabug
Copy link
Author

scabug commented Mar 24, 2009

@dragos said:
This needs a bit more thinking, it turns out such recursive calls /were/ optimized some time ago, but a later commit 'fixed' it by skipping them. I'll need to find out why the exception was added.

@scabug
Copy link
Author

scabug commented May 11, 2010

@dragos said:
I think this is safe as long as there is no finally. Will do.

@scabug
Copy link
Author

scabug commented Dec 5, 2012

@retronym said:
scala/scala#1711

@scabug scabug closed this as completed Dec 10, 2012
@scabug
Copy link
Author

scabug commented Mar 6, 2014

Erik Allik (eallik) said (edited on Mar 6, 2014 5:03:33 AM UTC):
Should this be handled by the fix?

  @tailrec
  def retryOn[T](thCls: Class[_ <: Throwable], maxTimes: Int)(block: => T): T = {
    try block
    catch {
      case t: Throwable if thCls.isInstance(t) && maxTimes > 0 =>
        retryOn(thCls, maxTimes - 1)(block)
    }
  }

...because currently I'm getting a compile error.

It works when the guard is turned into {code}case ... => if (...) ... else throw t{code}

@scabug
Copy link
Author

scabug commented Mar 6, 2014

@adriaanm said:
You cases in the catch should be restricted to type tests. More complicated patterns lead to a different compilation strategy that I'm not sure we can tailcall optimize. You can see the trees (as source code) with the -Xprint:patmat option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants