Skip to content

Returning a proc named result is accepted by the compiler but segfaults #18564

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

Closed
deech opened this issue Jul 23, 2021 · 4 comments
Closed

Returning a proc named result is accepted by the compiler but segfaults #18564

deech opened this issue Jul 23, 2021 · 4 comments

Comments

@deech
Copy link
Contributor

deech commented Jul 23, 2021

Example

proc p(): proc() =
  proc result = echo "hello world"

(p())()

Current Output

nim c -r --verbosity\:0 --hint\[Processing\]\:off --excessiveStackTrace\:on /home/deech/Nim/functest.nim
/home/deech/Nim/functest.nim(2, 8) Hint: 'result' is declared but not used [XDeclaredButNotUsed]
Traceback (most recent call last)
/home/deech/Nim/functest.nim(4) functest
SIGSEGV: Illegal storage access. (Attempt to read from nil?)
Segmentation fault
Error: execution of an external program failed: '/home/deech/Nim/functest '

Expected Output

hello world

Additional Information

Changing it to this works but the compiler should catch the above:

proc p(): proc() =
  result = proc () = echo "hello world"

(p())()
Nim Compiler Version 1.5.1 [Linux: amd64]
Compiled at 2021-07-12
Copyright (c) 2006-2021 by Andreas Rumpf

git hash: 195300a938c7c8dd4ec7db3ca7cc76dddeab953e
active boot switches: -d:release
@Araq
Copy link
Member

Araq commented Jul 23, 2021

No bug here, you don't return the inner proc at all so you return a "nil" proc which crashes when you try to call it.

@Araq Araq closed this as completed Jul 23, 2021
@Araq
Copy link
Member

Araq commented Jul 23, 2021

See also nim-lang/RFCs#378 (upvote it) :-)

@deech
Copy link
Contributor Author

deech commented Jul 23, 2021

I think the bug here is that in the first example result is manually initialized but the compiler doesn't see that proc result() ... and result = proc () ... are equivalent.

@Araq
Copy link
Member

Araq commented Jul 23, 2021

that proc result() ... and result = proc () ... are equivalent.

They are not equivalent at all. In one case you declare a proc named result (i.e. the builtin result is shadowed), in the other case you assign an anon proc to result.

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

No branches or pull requests

2 participants