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

Refering to a parameter/local var in an initializer of a {.global.} var results in invalid C code #9333

Closed
recloser opened this issue Oct 12, 2018 · 2 comments

Comments

@recloser
Copy link
Contributor

compiling

proc foo(arg: int) =
    var g {.global.} = arg + 1
    echo g

foo(123)

produces

Error: execution of an external compiler program 'gcc -c  -w  -I'~/.choosenim/toolchains/nim-#devel/lib' -o ~/.cache/nim/bug_d/bug.c.o ~/.cache/nim/bug_d/bug.c' failed with exit code: 1

~/.cache/nim/bug_d/bug.c: In function 'NimMainModule':
~/.cache/nim/bug_d/bug.c:168:41: error: 'arg' undeclared (first use in this function)
  TM_rmH9bpP9b3ZIjFP4QhigizlA_2 = addInt(arg, ((NI) 1));
                                         ^~~
~/.cache/nim/bug_d/bug.c:168:41: note: each undeclared identifier is reported only once for each function it appears in
@alaviss
Copy link
Collaborator

alaviss commented Oct 14, 2018

If I understand this correctly, you want g to be initialized on the first call only, so you'd have to do this:

proc foo(arg: int) =
    var g {.global.} = 0
    once:
        g = arg + 1
    echo g

This is because {.global.}s are initialized during program startup, which of course, arg wouldn't be available then since it only exists within the proc.

The compiler should raise a proper error for this case however.

@Araq
Copy link
Member

Araq commented Oct 15, 2018

Duplicate of #3505

@Araq Araq marked this as a duplicate of #3505 Oct 15, 2018
@Araq Araq closed this as completed Oct 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants