Skip to content

parse_if_let fabricates else: 0 for a missing else #1179

Closed

Description

@ehartford

Part of #1178 / D43. Unconditional under every ruling.

Where. src/Parser.w, parse_if_let: else_body = self.pool.add_node(NodeKind.NK_INT_LIT, start, start, 0, 0, 0).

Bug. An if let with no else is desugared to else: 0. The value is fabricated:

  • if let Some(v) = f(): seen = v types as i32 vs i32 and compiles by accident.
  • if let Some(v) = f(): assert(v == 7) fails as i32 vs Unit although the user wrote no i32. lldb at Sema.merge_contextual_owned_join_types: w1 = 14 (Unit), w2 = 3 (i32).
  • An unannotated function whose whole body is an else-less if let with an assignment body infers i32 and returns 0 when the pattern does not match (run: prints 0).

This is a silent fallback (CLAUDE.md, No Silent Fallbacks).

Fix. Desugar a missing else to an empty block, as the guard-let path in the same file already does. No value is fabricated; what a missing else means is D43's and §9.1's business.

Must not land alone. With only this change, a single-statement unannotated body if let P = e: x = v stops compiling (the literal 0 was joining with the i32-typed assignment). It lands in the same batch as D43's missing-arm rule.

Tests.

  • Else-less if let tail with a Unit body on an unannotated fn: function is Unit; both match and no-match paths run.
  • Else-less if let tail with a value/assignment body: function is Unit; a caller that consumes the result is a compile error; the no-match path does not produce 0.
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