Repository navigation
A do-block bind or let written without its type gets a note on its error - #1807
Conversation
… 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
left a comment
There was a problem hiding this comment.
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 whenxis known not to be a type. A declared name (book.tlds/book.ctrs) or a local binder whose annotation isTypstays 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 leadingAlldomains 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.
|
@YidaWeng Good catch, thank you. Both programs run on Took your refinement: Your two programs are in as |
…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]>
|
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:
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
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. Gates: test 1702/1702, repo 54/54, ping 72/72 (run on its own). perf is noise. |
|
Thanks @nicolas-abril. A type declared after |
Closes #1787.
parse_term_do_stmtreads 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 <- mbecomes the exec formA <- m, withxas the type. That reports "a defined name" for a newx, and "expected Type, observed U32" whenxis aU32parameter.x = vbecomes 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_showadds 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:<-: the error is "a defined name" or an expectedType.=: the error is "a pattern (a binder or a constructor)", and the note readsNote: in a do-block, a let names its type: x : A = v.An ordinaryx = vnever 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
valsset 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_untypedanddo_bind_untyped_paramare 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) anddo_exec_lower_type(at a lowercase type declared after its use) printok.Gates: test 1702/1702, repo 54/54, ping 72/72. perf is noise.