Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

- Reanalyze: make optional args analysis liveness-aware, preventing false positives when functions are only called from dead code. https://github.com/rescript-lang/rescript/pull/8082
- Fix: do not warn for "editor" field in `rescript.json`. https://github.com/rescript-lang/rescript/pull/8084
- Fix `let?` unwrap to use actual variable names from pattern instead of hardcoded "x"/"e". When using `let? Some(myVar) = ...`, the variable name `myVar` is now properly propagated in early returns. https://github.com/rescript-lang/rescript/issues/8085

#### :memo: Documentation

Expand Down
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