Skip to content

Fix LABEL inside block silently dropped (#2)#5

Open
p0dalirius wants to merge 1 commit intomainfrom
bugfix-label-top-level-only
Open

Fix LABEL inside block silently dropped (#2)#5
p0dalirius wants to merge 1 commit intomainfrom
bugfix-label-top-level-only

Conversation

@p0dalirius
Copy link
Copy Markdown
Contributor

Linked Issue

Closes #2

Root Cause

Label resolution in ScriptParser::parse() iterates only root->children, so any LABEL node that has been appended to a nested block's children (because it appeared inside IF / WHILE / REPEAT / DEF) is silently dropped. The language reference already documents LABEL as top-level only, and DEF is already enforced to be top-level at parse time — LABEL simply lacked the equivalent guard. When a subsequent GOTO referenced the dropped label, the only user-visible error pointed at the GOTO, not at the misplaced LABEL.

Fix Description

Mirror the existing DEF top-level check immediately after it in ScriptParser::parse(): when the first token of a line is LABEL and blockStack is non-empty, emit "LABEL must be at the top level (not inside IF/WHILE/REPEAT/DEF)" on that line and skip the node so it is never appended to the block. This keeps the error attached to the offending line and consistent in wording with the existing DEF diagnostic.

How Verified

  • Tests: added testLabelInsideBlockError covering LABEL inside IF ... ENDIF and asserting the error line number and message. It verifies the error is reported on the LABEL line (line 2), not on the subsequent GOTO.
  • Static: verified the new check at src/script_parser.cpp immediately following the DEF guard; the continue ensures the nested LABEL is not appended to the enclosing block, so the later recursive GOTO validation cannot be misled by a phantom label entry either.
  • Existing tests: full parser and lexer test suites still pass (ctest green).

Test Coverage

Added: tests/test_script_parser.cpp::testLabelInsideBlockError — asserts that a nested LABEL produces a top-level-only error on its own line.

Scope of Change

  • Files changed: src/script_parser.cpp, tests/test_script_parser.cpp
  • Submodule pointer updated: no
  • Behavioral changes outside the bug fix: none

Risk and Rollout

Local change. Previously-valid scripts (where LABEL already appeared only at the top level) are unaffected. Scripts that unintentionally nested a LABEL will now see a clear error on the correct line instead of a misdirected GOTO target '<name>' not found message; this is the intended improvement.

@p0dalirius p0dalirius self-assigned this Apr 18, 2026
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.

LABEL inside a nested block is silently dropped, producing a misleading 'GOTO target not found' error

1 participant