Skip to content

An if without else as the last statement of a with ... as mut body is rejected, though §7.2 discards the body's value #1298

Closed

Description

@ehartford

Found rewriting `examples/service` under #1150 ruling 4 (`describe_changes` and the postgres `update` SET-clause builder).

§7.2: `with expr as mut x:` "always returns `x` (the builder), regardless of the type of the body's last expression". The checker still types the body's last statement as the block value, so a trailing `if` without `else` — the natural last line of a conditional builder — is an error:

```
fn describe(x: i32) -> str:
let changes = with Vec.new() as mut c:
if x > 1:
c.push("gt1")
if x > 2:
c.push("gt2")
changes.join(", ")
```

```
error: if expression requires an else branch unless the then branch diverges
--> p8.w:5:9
```

Single-line `if x > 2: c.push("gt2")` is rejected the same way. `src/MirLower.w:6196` already special-cases "a statement-if reached in value mode (`with … as mut c:` bodies)", so the lowering knows the body is a statement; Sema's join does not. Expected: the body of a Form 2 block is checked as statements, the block's type is the builder's. Installed `with v0.15.2.1-g478b26481`.

The example uses the `var` desugaring (§7.2:2906) for those two builders until this lands.

Activity

  1. ehartford commented on Sep 26, 2026

    @ehartford
    ContributorAuthor

    Verified fixed on main 55739d4 (release binary): trailing if without else in a with … as mut body compiles.

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