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

No protection when stack resource is depleted (eg a bad recursion) #2677

Closed
SleepyRoy opened this issue Nov 7, 2019 · 3 comments
Closed
Assignees
Labels
Bug This tag is applied to issues which reports bugs. OS: Playground Bugs/feature requests, that are specific to play.vlang.io.
Milestone

Comments

@SleepyRoy
Copy link
Member

SleepyRoy commented Nov 7, 2019

V version:
V playground

OS:
N/A

What did you do?

// A trivial recursive function just for testing
// Return 1 if works normally
fn func(n int) int {
    if n>1{
    	return func(n-1)
    }
    else{
    	return 1
    }
}

fn main() {
    println(func(1000000))  // Make n really large so that stack is depleted
} 

What did you expect to see?
An error: a bad recursion.

What did you see instead?
You just found a bug. V can't compile this program, but it should. Please create a GitHub issue.

Some compilers set a default limit and allow users to change it when necessary. Should we also introduce some mechanism to solve this in V?

@SleepyRoy SleepyRoy added the Bug This tag is applied to issues which reports bugs. label Nov 7, 2019
@Metalymph
Copy link
Contributor

In this case i think two things can be applied:

  • a check for available stack memory after each recursive function call (which is identified by a bool is_rec for example). So, this check would be applied only for this kind of functions and would verify if stack memory limit (assigned by default, or manually assigned for example with a command option like v -sl 64000 file.v) is been exceeded than return error 'Stack Overflow'.
  • an optimization for tail recursions (only for future optimizer)

Could be a good solution ? @medvednikov

@dumblob
Copy link
Contributor

dumblob commented Nov 20, 2019

I'd leave this backend specific - each C backend has some limitations and V shouldn't try to unify that. Of course, for the direct x86 backend V plans to have at some point a clearly defined value is necessary, but that's not what this issue is about here.

In other words, V compiler shall understand from the backend compiler, that the backend compiler complained about stack depth and shall print a better message than the currently incorrect one:

You just found a bug. V can't compile this program, but it should. Please create a GitHub issue.

@M4SSD35TRUCT10N M4SSD35TRUCT10N added this to the Beta Release milestone Apr 6, 2020
@M4SSD35TRUCT10N M4SSD35TRUCT10N added the OS: Playground Bugs/feature requests, that are specific to play.vlang.io. label Apr 6, 2020
@medvednikov
Copy link
Member

Yeah, not something that can be handled by V.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. OS: Playground Bugs/feature requests, that are specific to play.vlang.io.
Projects
None yet
Development

No branches or pull requests

5 participants