Skip to content

Conversation

cknitt
Copy link
Member

@cknitt cknitt commented Sep 18, 2025

Adds support for try..finally and try..catch..finally syntax to ReScript.
No breaking changes / full backwards compatibility.

Resolves #6974.

Copy link

pkg-pr-new bot commented Sep 18, 2025

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript-lang/rescript@7903

@rescript/darwin-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-arm64@7903

@rescript/darwin-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/darwin-x64@7903

@rescript/linux-arm64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-arm64@7903

@rescript/linux-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/linux-x64@7903

@rescript/runtime

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/runtime@7903

@rescript/win32-x64

npm i https://pkg.pr.new/rescript-lang/rescript/@rescript/win32-x64@7903

commit: 4eb58f6

@cknitt cknitt changed the title [PoC] try..finally Add try..finally syntax Sep 18, 2025
@cknitt cknitt marked this pull request as ready for review September 18, 2025 17:54
@cknitt cknitt requested review from cristianoc and zth September 18, 2025 17:54
@fhammerschmidt
Copy link
Member

Maybe add a try await example to the tests.

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) -> (
Copy link
Collaborator

Choose a reason for hiding this comment

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

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
Copy link
Collaborator

Choose a reason for hiding this comment

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

what's finally in the grammar? just an identified?
Not a keyword I guess

} catch {
| InnerError => Console.log("Inner caught")
}
} catch {
Copy link
Collaborator

Choose a reason for hiding this comment

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

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 *)
Copy link
Collaborator

Choose a reason for hiding this comment

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

why?

*)
| Texp_try of expression * case list
(** try E with P1 -> E1 | ... | PN -> EN *)
| Texp_try of expression * case list * expression option
Copy link
Collaborator

Choose a reason for hiding this comment

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

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) ->
Copy link
Collaborator

Choose a reason for hiding this comment

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

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
Copy link
Collaborator

Choose a reason for hiding this comment

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

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;
Copy link
Collaborator

Choose a reason for hiding this comment

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

curious why fin was already here

Copy link
Collaborator

@cristianoc cristianoc left a comment

Choose a reason for hiding this comment

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

The PR should have a good amount of text in the description explaining what this PR does at high level.

@cknitt cknitt marked this pull request as draft September 20, 2025 06:30
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.

Support try...finally
3 participants