You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
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
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.)
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.
Activity
adeerkhan commentedon Oct 26, 2026
Correction after re-verifying each spelling in isolation (my first table conflated a type error of mine with the let):
IO.print("a")thenx = pure_of())expected : a pattern (a binder or a constructor), caret on theIO.printline;x : Nat = pure_of(), first or after an effect)DoLet ::= Name ":" Term "=" Termand it runsSo the grammar is fine and my "typed pure let → expected : U32" row was my own bug (
U32.showon aNat). The real gap is exactly one thing: the untyped let — the form that works everywhere else in the language — is refused inside adoblock, and nothing in the message says that a type annotation is what's missing. After thexparses as a statement term, the leftover= vis re-parsed as the next statement, which is why the caret and theIO.bindobservation land on the previous line.That sharpens the proposal: in
parse_term_do_stmt, when a statement isVar "=" …(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.fix(parser): refuse a stray = after a do block's last statement (#1889)