Skip to content

A do-block bind or let written without its type gets a note on its error - #1807

Merged
nicolas-abril merged 5 commits into
bendlang:mainfrom
HamedShams:fix/do-stmt-typed-bind
Oct 26, 2026
Merged

nicolas-abril merged 5 commits into
bendlang:mainfrom
HamedShams:fix/do-stmt-typed-bind

Conversation

@HamedShams

@HamedShams HamedShams commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1787.

parse_term_do_stmt reads a statement's first term before it knows what the statement is, and only a : after a variable makes it a bind or a let. So a bind or a let written without its type is read as something else:

  • x <- m becomes the exec form A <- m, with x as the type. That reports "a defined name" for a new x, and "expected Type, observed U32" when x is a U32 parameter.
  • x = v becomes the block's result, and the = then fails as "a pattern (a binder or a constructor)".

None of these errors mentioned the annotation.

Change. This is an error report only: the parser and checker are unchanged, and every program that parsed and checked before still does. err_show adds a note to an error at a statement's bare first name followed by <- or =, and only when it is the error an untyped bind or let produces:

- expected : a defined name
- observed : x
Location: main
7>|     x <- IO.pure(U32, 1)
  |     ^
Note: in a do-block, x <- m runs m at type x; to bind its result, write x : A <- m.
  • <-: the error is "a defined name" or an expected Type.
  • =: the error is "a pattern (a binder or a constructor)", and the note reads Note: in a do-block, a let names its type: x : A = v. An ordinary x = v never hits that error, since a bare name is a valid pattern.

History. The first two commits refused these statements at parse time: first by the case of the name, then by its scope (a vals set of the def's value binders). Both rejected valid programs, such as the exec form at a type declared later in the file, and the first also refused the exec form at a lowercase type variable. 7fce6e4 replaces them with the note; see the comment thread.

Tests (tests/parse/):

  • do_bind_untyped, do_let_untyped and do_bind_untyped_param are the three cases from the issue. Each pins main's error plus the note.
  • do_exec_type_var (an exec form at a type variable) and do_exec_lower_type (at a lowercase type declared after its use) print ok.

Gates: test 1702/1702, repo 54/54, ping 72/72. perf is noise.

… the exec form

parse_term_do_stmt read a statement's first term before deciding what
the statement was, and only a ":" after a variable made it a bind or a
let. So `x <- m` was the exec form with x as the type ("a defined
name" for a new x, "expected Type, observed U32" for a parameter), and
`x = v` was the block's result followed by a stray "=". A lowercase
name followed by "<-" or "=" now fails with "a typed bind (x : A <- m)
or let (x : A = v)", pointing at the statement. `Unit <- m` and
`U32 <- m` still read as the exec form. tests/parse/do_bind_continuation
wrote its trailing bind without a type; it binds with one now and keeps
its expected output.

Closes bendlang#1787.

@YidaWeng YidaWeng left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked the three cases from #1787 and the new output; this is the right diagnostic, and the do_bind_continuation edit keeps that test's intent.

One regression to settle before merge: /^[a-z]/ rejects valid exec statements whose type name starts lowercase. On main, both of these check and run (print ok):

import Base

def f(-a: Type, x: a) -> IO(Unit):
  do IO<Unit>:
    a <- IO.pure(a, x)
    IO.print("ok")

def main() -> IO(Unit):
  f(U32, 1)
import Base

type foo is Type:
  Mkfoo{}

def main() -> IO(Unit):
  do IO<Unit>:
    foo <- IO.pure(foo, Mkfoo{})
    IO.print("ok")

Both fail here with expected : a typed bind ...; (a) <- ... fails too. for -a: Type appears in the tree (tests/parse/rewrite_motive_annotation_000.bend) and type foo is legal, so this is not only a style question. Also, a : Type <- m is not a workaround: it binds a Type result, while a <- m is the exec form with a as the result type.

A refinement that keeps the message without reading intent from case:

  • x = v: fire unconditionally — a single = after a bare variable is never exec syntax.
  • x <- m: fire only when x is known not to be a type. A declared name (book.tlds / book.ctrs) or a local binder whose annotation is Typ stays an exec; a known value binder (n: U32) or an unbound name fires; a binder of unknown type keeps the old behavior.
  • Track the known value binders in parse_def: from the def telescope, or from the filled law's leading All domains for a law-backed def.

The core check is:

if (!typed && t.$ === "Var") {
  const q = parse_reso(p, t.k);
  const loc = p.stk.some(([, i]) => i === t.i);
  const ty = q in p.book.tlds || q in p.book.ctrs || (loc && !p.vals.has(t.i));
  if ((parse_at(p, "=") && !parse_at(p, "==")) || (parse_at(p, "<-") && !ty)) {
    const eol = p.str.indexOf("\n", p.pos);
    parse_fail(p, "a typed bind (x : A <- m) or let (x : A = v)", beg, eol === -1 ? p.str.length : eol);
  }
}

I prototyped the full patch on this branch: the three #1787 tests still print the new message; the two programs above check and run; existing tests/parse outputs are unchanged (after the same do_bind_continuation edit); bend.ts is 44,042 / 48,000 ttok.

Refined diff against this branch
--- a/bend2/bend.ts
+++ b/bend2/bend.ts
@@ -334,7 +334,7 @@
 
 // Parser
 export type File  = { str: string; ns: string; al: Record<Name, Name>; };
-export type Parse = File & { book: Book; dir: string; pos: number; stk: Array<[Name, number]>; frs: number; };
+export type Parse = File & { book: Book; dir: string; pos: number; stk: Array<[Name, number]>; frs: number; vals: Set<number>; };
 export type Span  = { file: File; beg: number; end: number; };
 
 // Machine
@@ -2255,17 +2255,19 @@
   const t = parse_term(p);
   parse_skip(p);
   const typed = t.$ === "Var" && parse_take(p, ":");
+  if (!typed && t.$ === "Var") {
+    const q = parse_reso(p, t.k);
+    const loc = p.stk.some(([, i]) => i === t.i);
+    const ty = q in p.book.tlds || q in p.book.ctrs || (loc && !p.vals.has(t.i));
+    if ((parse_at(p, "=") && !parse_at(p, "==")) || (parse_at(p, "<-") && !ty)) {
+      const eol = p.str.indexOf("\n", p.pos);
+      parse_fail(p, "a typed bind (x : A <- m) or let (x : A = v)", beg, eol === -1 ? p.str.length : eol);
+    }
+  }
   const step  = !typed && parse_more(p, col);
   const A     = typed ? parse_term(p, 1) : step ? parse_var(p, "Unit", t.s) : t;
   parse_skip(p);
   const asg = typed && !parse_at(p, "==") && parse_take(p, "=");
-  // A lowercase name followed by `<-` or `=` is a bind or a let that lost its
-  // type: read as the exec form, the name would be the type, and the error
-  // would be about that instead.
-  if (!typed && t.$ === "Var" && /^[a-z]/.test(t.k) && (parse_at(p, "<-") || (parse_at(p, "=") && !parse_at(p, "==")))) {
-    const eol = p.str.indexOf("\n", p.pos);
-    parse_fail(p, "a typed bind (x : A <- m) or let (x : A = v)", beg, eol === -1 ? p.str.length : eol);
-  }
   if (typed && !asg) {
     parse_eat(p, "<-");
   } else if (!typed && !step && !parse_take(p, "<-")) {
@@ -2468,6 +2470,17 @@
     def = book.tlds[k] = { $: "Def", n: tele.length, x: tk.length, T: term_higher(tele_bind(tele, parse_term(p))), v: null, m: p.ns };
   }
   def.u ||= un;
+  if (law) {
+    let T = law.T;
+    for (let i = 0; T.$ === "All"; i++) {
+      if (T.A.$ !== "Typ") p.vals.add(i);
+      T = T.B(Var(T.k, i));
+    }
+  } else {
+    for (const cell of tele) {
+      if (cell[3].$ !== "Typ") p.vals.add(cell[2]);
+    }
+  }
   parse_eat(p, ":");
   if (parse_at_word(p, "import")) {
     if (def.x > 0) {
@@ -2494,7 +2507,7 @@
 }
 
 export function parse_book(book: Book, dir: string, src: string, ns: string, al: Record<Name, Name>): void {
-  const p: Parse = { book, dir, str: src, pos: 0, stk: [], frs: 0, ns, al };
+  const p: Parse = { book, dir, str: src, pos: 0, stk: [], frs: 0, ns, al, vals: new Set() };
   while (true) {
     parse_skip(p);
     if (p.pos >= p.str.length) {
@@ -2502,6 +2515,7 @@
     }
     p.frs = 0;
     parse_close(p, 0);
+    p.vals.clear();
     if (parse_take(p, "@")) {
       if (!parse_word(p, "unsafe")) {
         parse_fail(p, "'unsafe' (the one decorator)");

Happy to push a branch or add the positive tests (do_exec_lower_typevar.bend, do_exec_lower_alias.bend) if that is easier.

…ec form

The check in d342750 read the case of a bare name before `<-` as
intent, so `a <- IO.pure(a, x)` with a parameter `a: Type`, and
`foo <- m` with a declared `type foo`, were refused as binds that lost
their type; both check and run on main. The name's scope decides now:
`=` after a bare name is never exec syntax and still fails, and `<-`
fails only after a name that cannot be a type, a value binder of the
def or an unbound lowercase name. parse_def records the def's value
binders for that, from its telescope or from the filled law's leading
domains. An unbound uppercase name keeps "a defined name".
tests/parse/do_exec_type_var and do_exec_lower_type pin the two exec
forms; the three bendlang#1787 tests are unchanged. Refinement proposed by
@YidaWeng in review.
@HamedShams

Copy link
Copy Markdown
Contributor Author

@YidaWeng Good catch, thank you. Both programs run on main and my check refused them: I read the case of the name as intent, and a type variable or a lowercase type is a type all the same.

Took your refinement: = fires after any bare name, <- only after a name that cannot be a type (a value binder of the def, or an unbound one), with the def's value binders tracked as in your diff. One thing I kept from the old rule: an unbound uppercase name, Foo <- m with no Foo anywhere, still gets a defined name. Both readings are errors there, and that one names the actual problem.

Your two programs are in as tests/parse/do_exec_type_var and do_exec_lower_type, the three #1787 tests keep the new message, bend.ts is 44,054. Pushed as a second commit.

nicolas-abril and others added 3 commits October 26, 2026 18:43
…s a declared type

The previous commit told a lost annotation from the exec form by the
binder's scope: parse_def recorded the def's value binders in a Set on
the Parse, so a parameter typed Type stayed the exec form. That case,
the exec form at a lowercase type variable (`a <- m`), has no use in the
tree or in BendHub, and `_ : a <- m` says the same thing. So the rule is
now local to parse_term_do_stmt: a bare name before `=`, or a lowercase
one that names no declared type before `<-`, fails with "a typed bind
(x : A <- m) or let (x : A = v)". `Unit <- m`, `A <- m` and `foo <- m`
with a declared `type foo` stay the exec form. The vals Set, its filling
in parse_def and its clearing in parse_book are gone.

tests/parse/do_exec_type_var now writes `_ : a <- m` and prints ok;
do_exec_type_var_bare pins the message for `a <- m`. Every other file
under tests/ prints what the previous commit printed.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
The previous commits decided at parse time that a bare name before `<-`
or `=` was a lost annotation and failed there. That made a new parse
rule, and it rejected valid programs: an exec form at a type declared
later in the file (the book does not have it yet) or at a lowercase type
variable. This is meant to be an error report only, so the parser is
back to main's, and the hint comes from errors the checker and parser
already raise:

- `x <- m` with x no defined name, or no Type, is the exec form at type
  x: its error gets "Note: in a do-block, x <- m runs m at type x; to
  bind its result, write x : A <- m."
- `x = v` fails as "a pattern (a binder or a constructor)": its error
  gets "Note: in a do-block, a let names its type: x : A = v."

err_lost_type adds the note only for those errors, at a statement's
bare first name followed by `<-` or `=`, so a let's binder in an
ordinary body never gets it. The vals Set and the case rule are gone.

Tests: the three bendlang#1787 files pin the original error plus the note;
do_exec_type_var and do_exec_lower_type print ok, the latter now with
its type declared after main; do_bind_continuation is main's again.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@nicolas-abril

Copy link
Copy Markdown
Collaborator

Thanks @HamedShams, and @YidaWeng for the review. I pushed two commits (27fe13e, then 7fce6e4 and 3e7bf5b) that change the approach, so here's why.

The goal of #1787 is a better error, so the fix should never change which programs parse. Both parse-time rules did change that:

  • A type declared later in the file. With type foo declared after main, foo <- IO.pure(foo, Mkfoo{}) checks and runs on main. With the scope rule (and with my 27fe13e), it fails with "a typed bind", because book.tlds doesn't have foo yet while main is parsed.
  • A lowercase type variable. 27fe13e also refused a <- m with a : Type.

So the parser is main's again. The hint is now a note on errors the checker and parser already raise. It is added only for an error at a statement's bare first name followed by <- or =, and only when the error is the one an untyped bind or let produces:

  • x <- m: the error says x is no defined name, or no Type. That is the exec form at type x failing, and it gets Note: in a do-block, x <- m runs m at type x; to bind its result, write x : A <- m.
  • x = v: "a pattern (a binder or a constructor)", which is how the block's result read as a let pattern fails. It gets Note: in a do-block, a let names its type: x : A = v. An ordinary x = v never hits that error, since a bare name is a valid pattern.

I first matched on the source text alone. The test gate then caught the note landing on 8 unrelated errors at ordinary lets, which is why it now checks the error kind too.

The three #1787 tests pin main's error plus the note. do_exec_type_var and do_exec_lower_type print ok; the latter now declares its type after main, to cover the first case above. do_bind_continuation is main's again. The vals set and the case rule are gone, and the net change to bend.ts is err_lost_type (18 lines) plus one line in err_show.

Gates: test 1702/1702, repo 54/54, ping 72/72 (run on its own). perf is noise.

@nicolas-abril nicolas-abril changed the title A do-block bind or let that lost its type is reported as that, not as the exec form A do-block bind or let written without its type gets a note on its error Oct 26, 2026
@nicolas-abril
nicolas-abril merged commit c184171 into bendlang:main Oct 26, 2026
1 check passed
@HamedShams

Copy link
Copy Markdown
Contributor Author

Thanks @nicolas-abril. A type declared after main was a case neither rule saw coming. Putting the hint on the error the checker already gives is the cleaner way, and it keeps the parser untouched. Glad this one landed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A do-block bind or let without its type annotation reports an unrelated error

3 participants