Skip to content

Diagnostics: a pure let inside a do block is refused with "expected: a pattern (a binder or a constructor)" pointing at the previous statement #1868

Closed

Description

@adeerkhan

What happened

A definition cannot sit inside a do block — fine, if that is the rule — but the refusal names neither the rule nor the line at fault. The error points at the statement before the let, with a message about patterns:

SOME PROOFS FAIL
Error:
- expected : a pattern (a binder or a constructor)
- observed : IO.bind(Unit, Unit, IO.print("a"), _ => {x : IO(Unit)})
Location:
7 |   do IO<Unit>:
8>|     IO.print("a")
  |     ^^^^^^^^^^^^^
9 |     x = pure_of()

The offender is line 9; the caret sits on line 8, and neither the message nor the location suggests that the problem is the x = ... at all (I first suspected the IO.print call). Hoisting the binding before the do works — that is the shape MLBend's examples use — but nothing says so.

How to reproduce

import Base

def pure_of() -> Nat:
  5n

def bad() -> IO(Unit):
  do IO<Unit>:
    IO.print("a")
    x = pure_of()
    IO.print(U32.show(x))
    return Unit{}

def main() -> IO(Unit):
  bad()
$ bend repro.bend        # 2.0.38 (and the same on 2.0.39)

All four spellings are refused the same way (bend 2.0.38):

variant refusal
pure let after an effect expected : a pattern (a binder or a constructor)
pure let first in the block expected : a pattern (a binder or a constructor)
typed pure let (x : Nat = ...) expected : U32 (equally opaque)
effect statement with ; before the let expected : a pattern (a binder or a constructor)

Why it matters

Writing a new ML program, this cost a debugging round: the first suspect was the IO.print/bind desugaring (the message shows an IO.bind), not the let. A program author hitting this has no way to learn "a let may not sit inside a do block; hoist it or use x <- eff" from the compiler's words.

Proposal

  • The message names the rule: a definition cannot appear inside a do block (a let is a def-body form): bind the value before the block, or use x <- eff for an effect, and
  • the Location points at the let line, not the previous statement.

(Allowing the let there would of course also fix it — but if the grammar stays as is, the diagnostics should say so.)

Activity

  1. adeerkhan commented on Oct 26, 2026

    @adeerkhan
    ContributorAuthor

    Correction after re-verifying each spelling in isolation (my first table conflated a type error of mine with the let):

    variant verdict on 2.0.38
    untyped let after an effect (IO.print("a") then x = pure_of()) refused — expected : a pattern (a binder or a constructor), caret on the IO.print line
    untyped let first in the block refused, same message
    untyped let after ; refused, same message
    typed let (x : Nat = pure_of(), first or after an effect) works — this is the grammar's DoLet ::= Name ":" Term "=" Term and it runs

    So the grammar is fine and my "typed pure let → expected : U32" row was my own bug (U32.show on a Nat). The real gap is exactly one thing: the untyped let — the form that works everywhere else in the language — is refused inside a do block, and nothing in the message says that a type annotation is what's missing. After the x parses as a statement term, the leftover = v is re-parsed as the next statement, which is why the caret and the IO.bind observation land on the previous line.

    That sharpens the proposal: in parse_term_do_stmt, when a statement is Var "=" … (untyped, = not ==), fail with the form that works — a do-block let needs its type: write "x : T = ..." (or bind the value before the block) — located on the let's own line. PR to follow on this issue.

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