Skip to content

Parser: a body introducer ':' at end of line followed by an unindented line takes that line as the body (§29.13) #1391

Closed

Description

@ehartford

§29.13: "A colon at end of line with nothing following (no indented block) is a syntax error." Parser.parse_block_or_expr (src/Parser.w) takes the block's column from whatever statement follows the newline and never compares it with the introducing construct's indentation, so a following statement at the same (or a shallower) column becomes the body.

fn h(c: bool) -> i32:
    if c:
    return 1
    2
fn main:
    print(f"{h(true)}")

Actual (seed and stage1 of a7e2004): the rest of the function is parsed as the if body, and the diagnostics are about the consequences, not the cause:

error: unreachable code
error: if expression requires an else branch unless the then branch diverges
error: missing return

while c: / for / fn bodies behave the same. Expected: one parse error at the colon, "expected an indented block". The let-else else: form had the same shape and there it was silent (let Some(v) = o else: followed by return v at the let's column compiled and ran); #1382's PR gives let-else the check locally (parse_let_else_body), which is where the rest of the constructs need it too — parse_block_or_expr needs the introducing construct's column to do it in one place.

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