Skip to content

Scope Vim9 declarations to blocks - #207

Merged
mattn merged 1 commit into
masterfrom
vim9-block-scope
Nov 23, 2026
Merged

mattn merged 1 commit into
masterfrom
vim9-block-scope

Conversation

@mattn

@mattn mattn commented Nov 23, 2026

Copy link
Copy Markdown
Member

Follow-up to #206.

In Vim9 script a variable declared in :if, :while, :for, :try or a {} block is only visible in that block, also at the script level. The parser kept these names after the block, so a later put = 5 was parsed as an assignment while Vim runs it as :put.

  • Push a scope for these blocks in Vim9 script.
  • Reset the names at :elseif, :else, :catch and :finally, so a name declared in one branch is not visible in the next.
  • Declare :for loop variables inside the loop.

Added test/test_vim9declared_block.vim; the file compiles in Vim with :defcompile. Parsing a legacy script gives the same output as before.

In Vim9 script a variable declared in :if, :while, :for, :try or a {}
block is only visible in that block, also at the script level.  Names
declared in one branch of :if or :try are not visible in the next
branch, and :for loop variables are only visible inside the loop.
@mattn
mattn merged commit 215be72 into master Nov 23, 2026
4 checks passed
@mattn
mattn deleted the vim9-block-scope branch November 23, 2026 15:51
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

Successfully merging this pull request may close these issues.

1 participant