Repository navigation
Conversation
rescript
@rescript/darwin-arm64
@rescript/darwin-x64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
|
Maybe add a |
| attach t.trailing expr.pexp_loc after_expr; | ||
| walk_list (cases |> List.map (fun case -> Case case)) t rest | ||
| (* unary expression: todo use parsetreeviewer *) | ||
| | Pexp_try (expr, cases, finally_expr) -> ( |
There was a problem hiding this comment.
why was this case not handled at all before?
I would expect a change that only deals with finally, here
| else if | ||
| Parser.lookahead p (fun state -> | ||
| match state.Parser.token with | ||
| | Lident "finally" -> true |
There was a problem hiding this comment.
what's finally in the grammar? just an identified?
Not a keyword I guess
| } catch { | ||
| | InnerError => Console.log("Inner caught") | ||
| } | ||
| } catch { |
There was a problem hiding this comment.
Is this the indentation we want? Does TS look like this when formatted?
| let finally_typed = | ||
| type_expect ~context:None env expr Predef.type_unit | ||
| in | ||
| (* Finally blocks must return unit *) |
| *) | ||
| | Texp_try of expression * case list | ||
| (** try E with P1 -> E1 | ... | PN -> EN *) | ||
| | Texp_try of expression * case list * expression option |
There was a problem hiding this comment.
nit: get assistant to use a record in a hypothetical follow-up PR, if this goes ahead
| Sexp.list (map_empty ~f:case cases); | ||
| ] | ||
| | Pexp_try (expr, cases) -> | ||
| | Pexp_try (expr, cases, finally_expr) -> |
There was a problem hiding this comment.
I keep on forgetting why we have this debugger file. Gives maintenance burden but can't remember why we need it.
| | Lstaticraise of int * t list | ||
| | Lstaticcatch of t * (int * ident list) * t | ||
| | Ltrywith of t * ident * t | ||
| | Ltrywith of t * ident * t option * t option |
There was a problem hiding this comment.
opportunity: this file is entirely uncommented
a separate PR could assist in adding some explanation
More bigger / longer term assistance opportunity: why do we have both lambda and lam? Is that a historical artefact for what used to be backwards compatibility, or can they possibly be merged now?
| P.string f L.finally; | ||
| P.space f; | ||
| brace_block cxt f b)) | ||
| P.space f; |
There was a problem hiding this comment.
curious why fin was already here
cristianoc
left a comment
There was a problem hiding this comment.
The PR should have a good amount of text in the description explaining what this PR does at high level.
|
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Why:
JavaScript code cleans up with finally blocks: after an await, around
break and continue, and in generators, where cancellation is only seen
by finally. ReScript had no way to express them.
`try` takes `finally { ... }` after its body or after its catch. `finally`
stays an identifier: after a catch it only starts a finally block when a
brace follows. The block is a statement and must not break or continue out
of it. Supersedes #7903.
AST modified (Pexp_try):
Example:
let f = g =>
try g() catch {
| Not_found => 0
} finally {
Console.log("done")
}
Before (diagnostic): `finally` was not valid after a catch.
After (parse):
expression
Pexp_try
expression
Pexp_apply
expression
Pexp_ident "g"
…
[
<case>
pattern
Ppat_construct "Not_found"
expression
Pexp_constant PConst_int (0,None)
]
Some
expression
Pexp_apply
expression
Pexp_ident "Console.log"
…
Lambda added (Ltryfinally):
After (lambda):
(try
(try (apply g/1002 unit) with raw_exn/1062
…)
finally (seq (js_call[console.log] "done") unit))
JS added (try..catch..finally):
After (js):
function f(g) {
try {
return g();
} catch (raw_exn) {
let exn = Primitive_exceptions.internalToException(raw_exn);
if (exn.RE_EXN_ID === "Not_found") {
return 0;
}
throw exn;
} finally {
console.log("done");
}
}
Diagnostic added (break out of finally):
Example:
while true {
try {
()
} finally {
break
}
}
After (diagnostic):
`break` cannot leave a `finally` block. Only a loop inside the block can be broken out of.
Review: Why a separate Ltryfinally instead of an optional finally on
Ltrywith? Ltrywith keeps its meaning in every pass, a try without catch
needs no exception ident, and try..catch..finally is an Ltryfinally around
an Ltrywith that the JS backend prints as one statement.
Review: Why is the catch handler no longer in tail position? With a
finally, a self tail call in the handler would become a loop `continue`
that runs the finally before the recursive call instead of after it.
Review: Why reject break and continue in finally? JavaScript allows them,
but they silently drop a pending exception or return value.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Fc7LEuyEfynjaKCVEBPmTF
Signed-off-by: Christoph Knittel <ck@cca.io>
Adds support for
try..finallyandtry..catch..finallysyntax to ReScript.No breaking changes / full backwards compatibility.
Resolves #6974.