Skip to content

let-else: a multi-statement indented else block does not parse as a block ("requires a diverging else branch" on its first statement) #1382

Closed

Description

@ehartford

The else branch of let PAT = subject else: may be any diverging body (§9.7: "The else branch must diverge (return, break, continue, panic)"). An indented multi-statement block ending in return is rejected: only its first statement is taken as the else body.

fn int_or_err(k: i32) -> Result[i32, str]:
    if k == 0: return Err("none".clone())
    k
fn h(k: i32) -> i32:
    let Ok(v) = int_or_err(k) else:
        print("miss")
        return -1
    v
fn main:
    print(f"{h(0)} {h(1)}")

Actual (v0.15.2.1 and origin/main 8c859a7):

error: let ... else requires a diverging else branch
 --> m4.w:6:9
6 |         print("miss")

Expected: miss then -1 1.

Likely site: Parser.parse_let_pattern_rest (src/Parser.w ~7105) — if TK_COLON: advance(); skip_newlines(); parse_block_or_expr(): skipping the newline before the indented block makes parse_block_or_expr see the first statement as an expression. The other NK_LET_ELSE producers (7160/7171/7212/7224) should be checked for the same shape.

Found while building the #1365 let-else drop matrix.

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