Skip to content

Commit bfe8279

Browse files
committed
Fix let? unwrap to use actual variable names instead of hardcoded ones
When using `let? Some(myVar) = ...`, the variable name was hardcoded as "x" instead of using the actual pattern variable name "myVar". This fix extracts the variable name from the pattern and uses it in the early return cases, ensuring proper variable propagation and avoiding unnecessary runtime allocations. Fixes #8085 Signed-Off-By: [Huijung Yoon] <[markup3604@gmail.com]>
1 parent 14721cb commit bfe8279

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
- 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
2424
- Fix: do not warn for "editor" field in `rescript.json`. https://github.com/rescript-lang/rescript/pull/8084
25+
- 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
2526

2627
#### :memo: Documentation
2728

compiler/frontend/bs_builtin_ppx.ml

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,15 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
216216
}
217217
in
218218
let loc = {pvb_pat.ppat_loc with loc_ghost = true} in
219+
(* Extract the variable name from the pattern (e.g., myVar from Some(myVar)) *)
220+
let var_name =
221+
match pvb_pat.ppat_desc with
222+
| Ppat_construct (_, Some inner_pat) -> (
223+
match Ast_pat.is_single_variable_pattern_conservative inner_pat with
224+
| Some name when name <> "" -> name
225+
| _ -> "x")
226+
| _ -> "x"
227+
in
219228
let early_case =
220229
match variant with
221230
(* Result: continue on Ok(_), early-return on Error(e) *)
@@ -227,9 +236,9 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
227236
(Ast_helper.Pat.construct ~loc
228237
{txt = Lident "Error"; loc}
229238
(Some (Ast_helper.Pat.any ~loc ())))
230-
{txt = "e"; loc};
239+
{txt = var_name; loc};
231240
pc_guard = None;
232-
pc_rhs = Ast_helper.Exp.ident ~loc {txt = Lident "e"; loc};
241+
pc_rhs = Ast_helper.Exp.ident ~loc {txt = Lident var_name; loc};
233242
}
234243
(* Result: continue on Error(_), early-return on Ok(x) *)
235244
| `Result_Error ->
@@ -239,9 +248,9 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
239248
Ast_helper.Pat.alias
240249
(Ast_helper.Pat.construct ~loc {txt = Lident "Ok"; loc}
241250
(Some (Ast_helper.Pat.any ~loc ())))
242-
{txt = "x"; loc};
251+
{txt = var_name; loc};
243252
pc_guard = None;
244-
pc_rhs = Ast_helper.Exp.ident ~loc {txt = Lident "x"; loc};
253+
pc_rhs = Ast_helper.Exp.ident ~loc {txt = Lident var_name; loc};
245254
}
246255
(* Option: continue on Some(_), early-return on None *)
247256
| `Option_Some ->
@@ -250,9 +259,9 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
250259
pc_lhs =
251260
Ast_helper.Pat.alias
252261
(Ast_helper.Pat.construct ~loc {txt = Lident "None"; loc} None)
253-
{txt = "x"; loc};
262+
{txt = var_name; loc};
254263
pc_guard = None;
255-
pc_rhs = Ast_helper.Exp.ident ~loc {txt = Lident "x"; loc};
264+
pc_rhs = Ast_helper.Exp.ident ~loc {txt = Lident var_name; loc};
256265
}
257266
(* Option: continue on None, early-return on Some(x) *)
258267
| `Option_None ->
@@ -262,9 +271,9 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
262271
Ast_helper.Pat.alias
263272
(Ast_helper.Pat.construct ~loc {txt = Lident "Some"; loc}
264273
(Some (Ast_helper.Pat.any ~loc ())))
265-
{txt = "x"; loc};
274+
{txt = var_name; loc};
266275
pc_guard = None;
267-
pc_rhs = Ast_helper.Exp.ident ~loc {txt = Lident "x"; loc};
276+
pc_rhs = Ast_helper.Exp.ident ~loc {txt = Lident var_name; loc};
268277
}
269278
in
270279
default_expr_mapper self

0 commit comments

Comments
 (0)