Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions compiler/frontend/bs_builtin_ppx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,15 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
}
in
let loc = {pvb_pat.ppat_loc with loc_ghost = true} in
(* Extract the variable name from the pattern (e.g., myVar from Some(myVar)) *)
let var_name =
match pvb_pat.ppat_desc with
| Ppat_construct (_, Some inner_pat) -> (
match Ast_pat.is_single_variable_pattern_conservative inner_pat with
| Some name when name <> "" -> name
| _ -> "x")
| _ -> "x"
in
let early_case =
match variant with
(* Result: continue on Ok(_), early-return on Error(e) *)
Expand All @@ -227,9 +236,9 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
(Ast_helper.Pat.construct ~loc
{txt = Lident "Error"; loc}
(Some (Ast_helper.Pat.any ~loc ())))
{txt = "e"; loc};
{txt = var_name; loc};
pc_guard = None;
pc_rhs = Ast_helper.Exp.ident ~loc {txt = Lident "e"; loc};
pc_rhs = Ast_helper.Exp.ident ~loc {txt = Lident var_name; loc};
}
(* Result: continue on Error(_), early-return on Ok(x) *)
| `Result_Error ->
Expand All @@ -239,9 +248,9 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
Ast_helper.Pat.alias
(Ast_helper.Pat.construct ~loc {txt = Lident "Ok"; loc}
(Some (Ast_helper.Pat.any ~loc ())))
{txt = "x"; loc};
{txt = var_name; loc};
pc_guard = None;
pc_rhs = Ast_helper.Exp.ident ~loc {txt = Lident "x"; loc};
pc_rhs = Ast_helper.Exp.ident ~loc {txt = Lident var_name; loc};
}
(* Option: continue on Some(_), early-return on None *)
| `Option_Some ->
Expand All @@ -250,9 +259,9 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
pc_lhs =
Ast_helper.Pat.alias
(Ast_helper.Pat.construct ~loc {txt = Lident "None"; loc} None)
{txt = "x"; loc};
{txt = var_name; loc};
pc_guard = None;
pc_rhs = Ast_helper.Exp.ident ~loc {txt = Lident "x"; loc};
pc_rhs = Ast_helper.Exp.ident ~loc {txt = Lident var_name; loc};
}
(* Option: continue on None, early-return on Some(x) *)
| `Option_None ->
Expand All @@ -262,9 +271,9 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
Ast_helper.Pat.alias
(Ast_helper.Pat.construct ~loc {txt = Lident "Some"; loc}
(Some (Ast_helper.Pat.any ~loc ())))
{txt = "x"; loc};
{txt = var_name; loc};
pc_guard = None;
pc_rhs = Ast_helper.Exp.ident ~loc {txt = Lident "x"; loc};
pc_rhs = Ast_helper.Exp.ident ~loc {txt = Lident var_name; loc};
}
in
default_expr_mapper self
Expand Down
Loading