Skip to content

Parser: inner if-chain ending in 'else if' swallows the outer chain's next else at the same column #629

Closed

Description

@ehartford

Summary

A dangling-else variant: an inner if-chain whose LAST arm is else if captures the outer chain's following else if/else at the outer column, silently miscompiling. Found while writing compiler code (worked around with a helper). Related to the §7.5 dangling-else fix but a distinct shape (inner chain ends in else-if, not a bare inner if).

Spec §7.5 (dangling-else, indentation-sensitive)

Repro

Being reduced; the shape is: outer if a: ... else if b: (inner: if c: X else if d: Y) else if e: Z — Z binds to the inner chain. Probe files exist under scratchpad/tartest/norm{e,f,h,i}.w (fail) vs d (works). Will attach a minimal .w.

Activity

  1. ehartford commented on Jul 4, 2026

    @ehartford
    ContributorAuthor

    Fixed in 508659f; build + fixpoint + full test suite green. Root cause: parse_if_expr computed is_stmt_if/if_col from each arm's own if token, so an else if arm (not first-on-line, column-shifted by else ) bypassed the §7.5 dangling-else rejection and captured the outer chain's next else. The chain now threads the original if's column/statement-ness through the recursion.

    Because the compiler is self-hosted, an instrumented sweep (old-vs-new parse decision diff over the whole tree) found 10 flipped sites — in every one the new parse matches the written indentation intent, and the old parse had silently disabled the outer arms. Notable repairs that ride along: lib/std/build.w tool_path_normalize was completely broken ('src/x.w' → '.'), now correct (fixes ToolFs containment comparisons); build/sdk.w SDK packaging's bin/ tool-selection arm was dead; SemaCheck function/closure tail-return checks and the comprehension expected-type diagnostic were dead (one fixture updated to the now-live intended diagnostic). Full list in the commit message. Pinned by test/behavior/behav_dangling_else_if_chain.w.

  2. added 2 commits that reference this issue on Jul 4, 2026
    508659f
    011ec31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions