luke-gru / riml

Riml is a subset of VimL with some nice added features. It compiles to plain VimL.
MIT License
224 stars 6 forks source link

`for` statement in global scope pollutes global variables #27

Closed rhysd closed 11 years ago

rhysd commented 11 years ago

When I compiled below riml code,

for i in range(1, 3)
  echo i
end

I got below Vim script code.

for i in range(1, 3)
  echo i
endfor

There is a problem in this Vim script code. i for items in for statement is global scope. It pollutes global variables if for is used in global scope.

let g:i = 42

for i in range(1, 3)
  echo i
endfor

" oops
echo g:i

However, local variable cannot be used in global scope. So, at least, script local variable should be used like below if for is used in global scope.

let g:i = 42

for s:i in range(1, 3)
  echo s:i
endfor

" yey!
echo g:i
luke-gru commented 11 years ago

Ah, great catch. This is an issue with me not knowing enough about Vim variable scoping, I must confess. Also, there is another issue, which is that your final example (the code that should be generated) is not compilable in Riml. It throws an unexpected SCOPE_MODIFIER parse error.

Both of these fixes are pretty quick, and I'll mark this as going into the 0.3.6 release, which will happen this weekend.

Thanks for the report! Keep them coming :smile:

luke-gru commented 11 years ago

This is in version 0.3.6, which I just released. Thanks again for reporting.

rhysd commented 11 years ago

I've confirmed that the bug is fixed. Thank you for quick fix!