5.4.1.1 Statement Labels
Note
This section describes the implementation of MS-VBAL §5.4.1.1 Statement Labels.
Syntax
| AST node | Instruction kind(s) | Notes |
|---|---|---|
| LineLabelNode | — | A line label. Its name is a key of InstructionList.Labels. |
| LineNumberNode | — | A line number. Its number is a key of InstructionList.Labels. |
Statement labels and line numbers are captured as separate nodes. A jump statement's target is an expression naming or numbering a label, with no static link to the label node (RD-VBAL §3.4.2 Simple Statements).
Static Semantics
A procedure declaration must contain exactly one definition of each label value it declares (MS-VBAL §5.4.1.1). A procedure that jumps to a label must contain exactly one definition of that label.
A label belongs to its procedure, not to the block it is written in. A label defined inside a nested block (If,
Do, For, Select Case, With), however deeply nested, is defined for the whole procedure.
Label names are looked up case-insensitively, like every VBA identifier. Two labels differing only by case, such as
Top: and TOP:, are the same label.
A label is never a variable. A jump target is looked up among the labels the procedure defines, not among the symbols in scope.
| Condition | Diagnostic |
|---|---|
| A jump names a line label or line number the procedure does not define. | VBC09309 — Label not defined |
| A label is defined more than once in a procedure, including two labels differing only by case. | VBC09319 — Duplicate label definition |
For a repeated label definition, lowering keeps the first offset the label was defined at; every jump to the label resolves against that first definition (RD-VBAL §3.5.3 Lowering Block Statements).
Runtime Semantics
A jump's target is resolved at lowering time, not at run time. InstructionList.Labels (TryGetLabelOffset) maps a
line label or line number name to its offset
(RD-VBAL §3.5.1 InstructionList).
A label with nothing after it resolves to offset Items.Length. Execution there completes as if it had reached the
end of the procedure body.
A GoTo from anywhere in the procedure into the middle of a loop body or an If body resolves in the same way as any
other jump (RD-VBAL §5.4.2.12 GoTo Statement).
👉 Jumping into the middle of a loop body has a consequence for the loop's hidden per-activation state. A
Nextcloser reached without its opener having run in the activation raises run-time error 92 (RD-VBAL §5.4.2.3 For Statement, RD-VBAL §5.4.2.4 For Each Statement).
A named label never sets Erl, in either ErlLineNumbering mode. Only a label spelled as decimal digits sets Erl
(RD-VBAL §6.1.2.7 Information).
Implementation
InstructionListLowering builds the label
table of an InstructionList and reports both
diagnostics (RD-VBAL §2.6.2 Semantic Compilation Errors).
⏮️ RD-VBAL §5.4.1 Statement Blocks | ⏭️ RD-VBAL §5.4.1.2 Rem Statement