-
Notifications
You must be signed in to change notification settings - Fork 471
Add Parsetree-level Option stdlib optimizations (forEach/map/flatMap) #7918
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
cknitt
wants to merge
3
commits into
master
Choose a base branch
from
options-stdlib-opt
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
open Parsetree | ||
open Longident | ||
|
||
(* | ||
Optimise calls to Option.forEach/map/flatMap so they produce the same switch | ||
structure as handwritten code. We only rewrite calls whose callback is a | ||
simple literal lambda or identifier; more complex callbacks are left intact | ||
to preserve ReScript's call-by-value semantics. | ||
*) | ||
|
||
let value_name = "__res_option_value" | ||
|
||
type option_call = ForEach | Map | FlatMap | ||
|
||
(* Inlineable callbacks are bare identifiers (possibly wrapped in coercions or | ||
type annotations). Those can be applied directly inside the emitted switch | ||
without introducing a let-binding that might change evaluation behaviour. *) | ||
let rec callback_is_inlineable expr = | ||
match expr.pexp_desc with | ||
| Pexp_ident _ -> true | ||
| Pexp_constraint (inner, _) | Pexp_coerce (inner, _, _) -> | ||
callback_is_inlineable inner | ||
| _ -> false | ||
|
||
(* Detect literal lambdas (ignoring type annotations) so we can reuse their | ||
argument binder in the rewritten switch. *) | ||
let rec inline_lambda expr = | ||
match expr.pexp_desc with | ||
| Pexp_constraint (inner, _) | Pexp_coerce (inner, _, _) -> | ||
inline_lambda inner | ||
| Pexp_fun {arg_label = Asttypes.Nolabel; lhs; rhs; async = false} -> | ||
Some (lhs, rhs) | ||
| _ -> None | ||
|
||
let transform (expr : Parsetree.expression) : Parsetree.expression = | ||
match expr.pexp_desc with | ||
| Pexp_apply | ||
{ | ||
funct = | ||
{ | ||
pexp_desc = | ||
Pexp_ident | ||
{txt = Ldot (Lident ("Option" | "Stdlib_Option"), fname)}; | ||
}; | ||
args = [(_, opt_expr); (_, func_expr)]; | ||
} -> ( | ||
let call_kind = | ||
match fname with | ||
| "forEach" -> Some ForEach | ||
| "map" -> Some Map | ||
| "flatMap" -> Some FlatMap | ||
| _ -> None | ||
in | ||
match call_kind with | ||
| None -> expr | ||
| Some call_kind -> ( | ||
let loc_ghost = {expr.pexp_loc with loc_ghost = true} in | ||
let emit_option_match value_pat result_expr = | ||
let some_rhs = | ||
match call_kind with | ||
| ForEach | FlatMap -> result_expr | ||
| Map -> | ||
Ast_helper.Exp.construct ~loc:loc_ghost | ||
{txt = Lident "Some"; loc = loc_ghost} | ||
(Some result_expr) | ||
in | ||
let none_rhs = | ||
match call_kind with | ||
| ForEach -> | ||
Ast_helper.Exp.construct ~loc:loc_ghost | ||
{txt = Lident "()"; loc = loc_ghost} | ||
None | ||
| Map | FlatMap -> | ||
Ast_helper.Exp.construct ~loc:loc_ghost | ||
{txt = Lident "None"; loc = loc_ghost} | ||
None | ||
in | ||
let mk_case ctor payload rhs = | ||
{ | ||
Parsetree.pc_bar = None; | ||
pc_lhs = | ||
Ast_helper.Pat.construct ~loc:loc_ghost | ||
{txt = Lident ctor; loc = loc_ghost} | ||
payload; | ||
pc_guard = None; | ||
pc_rhs = rhs; | ||
} | ||
in | ||
let some_case = mk_case "Some" (Some value_pat) some_rhs in | ||
let none_case = mk_case "None" None none_rhs in | ||
let transformed = | ||
Ast_helper.Exp.match_ ~loc:loc_ghost opt_expr [some_case; none_case] | ||
in | ||
{ | ||
transformed with | ||
pexp_loc = expr.pexp_loc; | ||
pexp_attributes = expr.pexp_attributes; | ||
} | ||
in | ||
match inline_lambda func_expr with | ||
(* Literal lambda with a simple binder: reuse the binder directly inside | ||
the generated switch, so the body runs exactly once with the option's | ||
payload. *) | ||
| Some ({ppat_desc = Parsetree.Ppat_var {txt}}, body) -> | ||
let value_pat = | ||
Ast_helper.Pat.var ~loc:loc_ghost {txt; loc = loc_ghost} | ||
in | ||
emit_option_match value_pat body | ||
(* Callback is a simple identifier (possibly annotated). Apply it inside | ||
the switch so evaluation order matches handwritten code. *) | ||
| _ when callback_is_inlineable func_expr -> | ||
let value_pat = | ||
Ast_helper.Pat.var ~loc:loc_ghost {txt = value_name; loc = loc_ghost} | ||
in | ||
let value_ident = | ||
Ast_helper.Exp.ident ~loc:loc_ghost | ||
{txt = Lident value_name; loc = loc_ghost} | ||
in | ||
let apply_callback = | ||
Ast_helper.Exp.apply ~loc:loc_ghost func_expr | ||
[(Asttypes.Nolabel, value_ident)] | ||
in | ||
emit_option_match value_pat apply_callback | ||
(* Complex callbacks are left as-is so we don't change when they run. *) | ||
| _ -> expr)) | ||
| _ -> expr |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
val transform : Parsetree.expression -> Parsetree.expression |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Guard rewrite against user-defined
Option
modulesThe optimization currently rewrites any application whose head is syntactically
Option.*
orStdlib_Option.*
(see theLdot (Lident ("Option" | "Stdlib_Option"), fname)
pattern). Because this runs before name resolution, it will also trigger for locally defined modules namedOption
orStdlib_Option
. If a project defines its own module shadowing these names (for example a customOption.map
with different semantics), the pass will silently replace the call with a match onSome
/None
, miscompiling the user’s code. The transformation needs to confirm it is targeting the stdlib Option module—e.g. by running after type checking or by explicitly checking the module path after resolution—otherwise it is unsound.Useful? React with 👍 / 👎.