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 recursion from inside value defs #20145

Closed
LucySMartin opened this issue Apr 9, 2024 · 3 comments · Fixed by #20652
Closed

Tail recursion from inside value defs #20145

LucySMartin opened this issue Apr 9, 2024 · 3 comments · Fixed by #20652

Comments

@LucySMartin
Copy link

LucySMartin commented Apr 9, 2024

Compiler version

3.4.1

Minimized code

import scala.annotation.tailrec
  @tailrec
  def foo(i: Int): Int = {
    if (i > 10000000) {
      i
    } else {
      val bar: String = {
        return foo(i + 1)
        "foo"
      }
      -1
    }
  }
println(foo(0))

Output

Stack overflow error on the line return foo(i + 1)

Expectation

10000001 should be prited to the console - or if it is determined this is not a tail location - it shoud fail to compile.

@LucySMartin LucySMartin added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Apr 9, 2024
@LucySMartin
Copy link
Author

LucySMartin commented Apr 9, 2024

Noticed this while resolving merge conflicts on #20143 its an easy fix.

@som-snytt
Copy link
Contributor

I think there was a Scala 3 tweak to consider returns as tail pos? This errors on 2.13.

@LucySMartin
Copy link
Author

LucySMartin commented Apr 10, 2024

Either wat it shouldn't stack overflow.
Either rewrite to a loop, or fail because it can't.
I'll fix this once we either merge or reject #20143. No need to create extra merge conflicts.

@Gedochao Gedochao added area:annotations itype:soundness Soundness bug (it lets us compile code that crashes at runtime with a ClassCastException) and removed stat:needs triage Every issue needs to have an "area" and "itype" label itype:soundness Soundness bug (it lets us compile code that crashes at runtime with a ClassCastException) labels Apr 10, 2024
LucySMartin pushed a commit to LucySMartin/scala3 that referenced this issue Jun 19, 2024
…l definition, previously this would neither optimise nor fail.
LucySMartin pushed a commit to LucySMartin/scala3 that referenced this issue Jun 19, 2024
…l definition, previously this would neither optimise nor fail.
LucySMartin pushed a commit to LucySMartin/scala3 that referenced this issue Jun 19, 2024
…l definition, previously this would neither optimise nor fail.
LucySMartin pushed a commit to LucySMartin/scala3 that referenced this issue Jun 20, 2024
…l definition, previously this would neither optimise nor fail.
@sjrd sjrd closed this as completed in 56e6f9f Jun 20, 2024
@Kordyjan Kordyjan added this to the 3.5.1 milestone Jul 3, 2024
WojciechMazur pushed a commit that referenced this issue Jul 9, 2024
…inition, previously this would neither optimise nor fail.

[Cherry-picked 036b86b]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment