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

'stmt' adds symbols too early #3482

Closed
Araq opened this issue Oct 28, 2015 · 1 comment · Fixed by #18094
Closed

'stmt' adds symbols too early #3482

Araq opened this issue Oct 28, 2015 · 1 comment · Fixed by #18094

Comments

@Araq
Copy link
Member

Araq commented Oct 28, 2015

The following doesn't produce a Nim error, but a C error as due to the 'body: stmt' body is sem'checked twice. 'body: untyped' produces a proper Nim error.

import os

template cd*(dir: string, body: stmt) =
  ## Sets the current dir to ``dir``, executes ``body`` and restores the
  ## previous working dir.
  let lastDir = getCurrentDir()
  setCurrentDir(dir)
  try:
    body
  finally:
    setCurrentDir(lastDir)

proc execProcess(cmd: string): string = discard

proc test =
  cd "foo":
    var output = execProcess("git tag")
  if output.len > 0:
    echo "I feel sorry"

test()
@timotheecour
Copy link
Member

timotheecour commented Oct 29, 2018

minimized:

template foo*(body: typed) =
  if true:
    body

proc test =
  foo:
    var output = ""
  echo output.len

test()

Error: execution of an external compiler program 'clang -c -w -I/Users/timothee/git_clone/nim/Nim/lib -o /tmp/nim/nimcache/timn_issue_3482.c.o /tmp/nim/nimcache/timn_issue_3482.c' failed with exit code: 1

/tmp/nim/nimcache/timn_issue_3482.c:103:24: error: use of undeclared identifier 'output'
T5_[0] = nimIntToStr((output ? output->Sup.len : 0));
^
/tmp/nim/nimcache/timn_issue_3482.c:103:33: error: use of undeclared identifier 'output'
T5_[0] = nimIntToStr((output ? output->Sup.len : 0));

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

Successfully merging a pull request may close this issue.

3 participants