Skip to content

Commit 6a9e84a

Browse files
cknittclaude
andcommitted
Remove the unused res.iflet encoding of if let
The parser always rejects `if let` with a syntax error that suggests the equivalent `switch`, and the formatter refuses files with syntax errors. Still, the parser encoded `if let` as a `switch` marked with `res.iflet` and `@warning("-4")`, and the viewer, printer and comment table carried code to print it back. Its printer tests had been commented out. Parse `if let` into a plain `switch` for recovery, keep the error, and remove the encoding: `has_if_let_attribute`, `is_if_let_expr`, the `IfLet` condition kind, `filter_fragile_match_attributes`, the comment-table case, and `res.iflet` in `is_parsing_attr`. Without the `res.iflet` exception, `has_attributes` meant "has any attribute" and is inlined. The commented-out `if let` printer tests are removed. `filter_fragile_match_attributes` also dropped a `@warning("-4")` written on an ordinary `if` when formatting; it's now kept. Signed-off-by: Christoph Knittel <christoph@knittel.cc> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1f3b6d6 commit 6a9e84a

12 files changed

Lines changed: 58 additions & 649 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#### :bug: Bug fix
2525

26+
- Keep `@warning("-4")` on `if` expressions when formatting; the formatter dropped it. PR_LINK
2627
- Fix the formatter dropping a `@res.ternary` attribute written in source, e.g. `let b = @res.ternary x`, and print `@JSX` and `@res.ternary` with the same parens as other attributes, e.g. `(@JSX x) + 1`. https://github.com/rescript-lang/rescript/pull/8740
2728
- Respect `@warning` attributes on the module in `module type of`, e.g. `module type of @warning("-3") DeprecatedModule`, which still reported the warning. https://github.com/rescript-lang/rescript/pull/8738
2829
- Fix the formatter dropping or moving attributes and `await` on module expressions, such as `module M = @attr F(X)`, and printing invalid code for module constraints and functors in some positions, such as `module M: T = (X: S)`. https://github.com/rescript-lang/rescript/pull/8735
@@ -46,6 +47,7 @@
4647

4748
#### :house: Internal
4849

50+
- Remove the unused `res.iflet` encoding of `if let`, which the parser always rejects with a syntax error suggesting a `switch`. The error is unchanged. PR_LINK
4951
- Remove the obsolete `res.ternary` and `JSX` attributes from the formatter's internal attribute filters and merge the filters into one. https://github.com/rescript-lang/rescript/pull/8740
5052
- Represent `await` on module expressions with a dedicated `Pmod_await` parsetree node instead of a `res.await` attribute, which remains only in the PPX wire representation. https://github.com/rescript-lang/rescript/pull/8738
5153
- Remove dead compiler code: warning 24, the `J.String_index` JS IR node, the `Pisint` and `Parraysets` Lambda primitives with their unused `%` primitive names (`%obj_is_int`, `%obj_dup`, `%obj_field`, `%array_safe_set`, `%null_to_opt`, `%nullable_to_opt`), the no-op `-pp` bsc flag and unread `Clflags` fields (`-unsafe` stays accepted as an explicit no-op), and the unused `unit` slot of `Pexp_coerce`. https://github.com/rescript-lang/rescript/pull/8731

‎compiler/syntax/src/res_comments_table.ml‎

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,56 +1381,6 @@ and walk_expression expr t comments =
13811381
attach t.leading mod_expr.pmod_loc before;
13821382
walk_module_expr mod_expr t inside;
13831383
attach t.trailing mod_expr.pmod_loc after
1384-
| Pexp_match (expr1, [case; else_branch])
1385-
when Res_parsetree_viewer.has_if_let_attribute expr.pexp_attributes ->
1386-
let before, inside, after =
1387-
partition_by_loc comments case.pc_lhs.ppat_loc
1388-
in
1389-
attach t.leading case.pc_lhs.ppat_loc before;
1390-
walk_pattern case.pc_lhs t inside;
1391-
let after_pat, rest =
1392-
partition_adjacent_trailing case.pc_lhs.ppat_loc after
1393-
in
1394-
attach t.trailing case.pc_lhs.ppat_loc after_pat;
1395-
let before, inside, after = partition_by_loc rest expr1.pexp_loc in
1396-
attach t.leading expr1.pexp_loc before;
1397-
walk_expression expr1 t inside;
1398-
let after_expr, rest = partition_adjacent_trailing expr1.pexp_loc after in
1399-
attach t.trailing expr1.pexp_loc after_expr;
1400-
let before, inside, after = partition_by_loc rest case.pc_rhs.pexp_loc in
1401-
let after =
1402-
if is_block_expr case.pc_rhs then (
1403-
let after_expr, rest =
1404-
partition_adjacent_trailing case.pc_rhs.pexp_loc after
1405-
in
1406-
walk_expression case.pc_rhs t (List.concat [before; inside; after_expr]);
1407-
rest)
1408-
else (
1409-
attach t.leading case.pc_rhs.pexp_loc before;
1410-
walk_expression case.pc_rhs t inside;
1411-
after)
1412-
in
1413-
let after_expr, rest =
1414-
partition_adjacent_trailing case.pc_rhs.pexp_loc after
1415-
in
1416-
attach t.trailing case.pc_rhs.pexp_loc after_expr;
1417-
let before, inside, after =
1418-
partition_by_loc rest else_branch.pc_rhs.pexp_loc
1419-
in
1420-
let after =
1421-
if is_block_expr else_branch.pc_rhs then (
1422-
let after_expr, rest =
1423-
partition_adjacent_trailing else_branch.pc_rhs.pexp_loc after
1424-
in
1425-
walk_expression else_branch.pc_rhs t
1426-
(List.concat [before; inside; after_expr]);
1427-
rest)
1428-
else (
1429-
attach t.leading else_branch.pc_rhs.pexp_loc before;
1430-
walk_expression else_branch.pc_rhs t inside;
1431-
after)
1432-
in
1433-
attach t.trailing else_branch.pc_rhs.pexp_loc after
14341384
| Pexp_match (expr, cases) | Pexp_try (expr, cases) ->
14351385
let before, inside, after = partition_by_loc comments expr.pexp_loc in
14361386
let after =

‎compiler/syntax/src/res_core.ml‎

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,7 @@ module Error_messages = struct
174174
"A polymorphic variant (e.g. #id) must start with an alphabetical letter \
175175
or be a number (e.g. #742)"
176176

177-
let experimental_if_let expr =
178-
let switch_expr = {expr with Parsetree.pexp_attributes = []} in
177+
let experimental_if_let switch_expr =
179178
Doc.concat
180179
[
181180
Doc.text "If-let is currently highly experimental.";
@@ -265,17 +264,8 @@ module Error_messages = struct
265264
"Spreading JSX children is no longer supported."
266265
end
267266

268-
let if_let_attr = (Location.mknoloc "res.iflet", Parsetree.PStr [])
269-
let suppress_fragile_match_warning_attr =
270-
( Location.mknoloc "warning",
271-
Parsetree.PStr
272-
[
273-
Ast_helper.Str.eval
274-
(Ast_helper.Exp.constant (Ast_helper.Const.string "-4"));
275-
] )
276267
let make_pat_variant_spread_attr =
277268
(Location.mknoloc "res.patVariantSpread", Parsetree.PStr [])
278-
279269
let spread_attr = (Location.mknoloc "res.spread", Parsetree.PStr [])
280270
let dict_spread_attr = (Location.mknoloc "res.dictSpread", Parsetree.PStr [])
281271

@@ -3610,9 +3600,7 @@ and parse_if_let_expr start_pos p =
36103600
(Location.mkloc [] loc)
36113601
in
36123602
let loc = mk_loc start_pos (Parser.position p) in
3613-
Ast_helper.Exp.match_
3614-
~attrs:[if_let_attr; suppress_fragile_match_warning_attr]
3615-
~loc condition_expr
3603+
Ast_helper.Exp.match_ ~loc condition_expr
36163604
[
36173605
Ast_helper.Exp.case pattern then_expr;
36183606
Ast_helper.Exp.case (Ast_helper.Pat.any ()) else_expr;

‎compiler/syntax/src/res_parens.ml‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ let structure_expr expr =
7070
| None -> (
7171
match expr with
7272
| {pexp_desc = Pexp_jsx_element _} -> Nothing
73-
| _ when Parsetree_viewer.has_attributes expr.pexp_attributes ->
74-
Parenthesized
73+
| _ when expr.pexp_attributes <> [] -> Parenthesized
7574
| {
7675
Parsetree.pexp_desc =
7776
Pexp_constraint ({pexp_desc = Pexp_pack _}, {ptyp_desc = Ptyp_package _});

‎compiler/syntax/src/res_parsetree_viewer.ml‎

Lines changed: 3 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,7 @@ let is_parsing_attr (attr : Parsetree.attribute) =
223223
match attr with
224224
| ( {
225225
Location.txt =
226-
( "res.iflet" | "res.patVariantSpread" | "res.dictPattern"
227-
| "res.dictSpread" );
226+
"res.patVariantSpread" | "res.dictPattern" | "res.dictSpread";
228227
},
229228
_ ) ->
230229
true
@@ -365,39 +364,6 @@ let flattenable_operators parent_operator child_operator =
365364
&& is_equality_operator child_operator)
366365
else false
367366

368-
let rec has_if_let_attribute attrs =
369-
match attrs with
370-
| [] -> false
371-
| ({Location.txt = "res.iflet"}, _) :: _ -> true
372-
| _ :: attrs -> has_if_let_attribute attrs
373-
374-
let is_if_let_expr expr =
375-
match expr with
376-
| {pexp_attributes = attrs; pexp_desc = Pexp_match _}
377-
when has_if_let_attribute attrs ->
378-
true
379-
| _ -> false
380-
381-
let has_attributes attrs =
382-
List.exists
383-
(fun attr ->
384-
match attr with
385-
| {Location.txt = "res.iflet"}, _ -> false
386-
(* Remove the fragile pattern warning for iflet expressions *)
387-
| ( {Location.txt = "warning"},
388-
PStr
389-
[
390-
{
391-
pstr_desc =
392-
Pstr_eval
393-
({pexp_desc = Pexp_constant (Pconst_string payload)}, _);
394-
};
395-
] ) ->
396-
String_literal.string_semantic payload <> "-4"
397-
|| not (has_if_let_attribute attrs)
398-
| _ -> true)
399-
attrs
400-
401367
let is_array_access expr =
402368
match expr.pexp_desc with
403369
| Pexp_apply
@@ -413,43 +379,15 @@ let is_array_access expr =
413379
true
414380
| _ -> false
415381

416-
type if_condition_kind =
417-
| If of Parsetree.expression
418-
| IfLet of Parsetree.pattern * Parsetree.expression
419-
420382
let collect_if_expressions expr =
421383
let rec collect acc expr =
422384
let expr_loc = expr.pexp_loc in
423385
match expr.pexp_desc with
424386
| Pexp_ifthenelse (if_expr, then_expr, Some else_expr) ->
425-
collect ((expr_loc, If if_expr, then_expr) :: acc) else_expr
387+
collect ((expr_loc, if_expr, then_expr) :: acc) else_expr
426388
| Pexp_ifthenelse (if_expr, then_expr, (None as else_expr)) ->
427-
let ifs = List.rev ((expr_loc, If if_expr, then_expr) :: acc) in
389+
let ifs = List.rev ((expr_loc, if_expr, then_expr) :: acc) in
428390
(ifs, else_expr)
429-
| Pexp_match
430-
( condition,
431-
[
432-
{pc_lhs = pattern; pc_guard = None; pc_rhs = then_expr};
433-
{
434-
pc_rhs =
435-
{pexp_desc = Pexp_construct ({txt = Longident.Lident "()"}, _)};
436-
};
437-
] )
438-
when is_if_let_expr expr ->
439-
let ifs =
440-
List.rev ((expr_loc, IfLet (pattern, condition), then_expr) :: acc)
441-
in
442-
(ifs, None)
443-
| Pexp_match
444-
( condition,
445-
[
446-
{pc_lhs = pattern; pc_guard = None; pc_rhs = then_expr};
447-
{pc_rhs = else_expr};
448-
] )
449-
when is_if_let_expr expr ->
450-
collect
451-
((expr_loc, IfLet (pattern, condition), then_expr) :: acc)
452-
else_expr
453391
| _ -> (List.rev acc, Some expr)
454392
in
455393
collect [] expr
@@ -475,23 +413,6 @@ let parameters_should_hug parameters =
475413
true
476414
| _ -> false
477415

478-
let filter_fragile_match_attributes attrs =
479-
List.filter
480-
(fun attr ->
481-
match attr with
482-
| ( {Location.txt = "warning"},
483-
PStr
484-
[
485-
{
486-
pstr_desc =
487-
Pstr_eval
488-
({pexp_desc = Pexp_constant (Pconst_string payload)}, _);
489-
};
490-
] ) ->
491-
String_literal.string_semantic payload <> "-4"
492-
| _ -> true)
493-
attrs
494-
495416
let should_indent_binary_expr expr =
496417
let same_precedence_sub_expression operator sub_expression =
497418
match sub_expression with

‎compiler/syntax/src/res_parsetree_viewer.mli‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,12 @@ type dict_expr_part =
2323
| DictExprRows of Parsetree.expression
2424
| DictExprSpread of Parsetree.expression
2525

26-
type if_condition_kind =
27-
| If of Parsetree.expression
28-
| IfLet of Parsetree.pattern * Parsetree.expression
29-
3026
(* if ... else if ... else ... is represented as nested expressions: if ... else { if ... }
3127
* The purpose of this function is to flatten nested ifs into one sequence.
3228
* Basically compute: ([if, else if, else if, else if], else) *)
3329
val collect_if_expressions :
3430
Parsetree.expression ->
35-
(Location.t * if_condition_kind * Parsetree.expression) list
31+
(Location.t * Parsetree.expression * Parsetree.expression) list
3632
* Parsetree.expression option
3733

3834
val collect_array_expressions :
@@ -77,21 +73,15 @@ val is_equality_operator : string -> bool
7773

7874
val flattenable_operators : string -> string -> bool
7975

80-
val has_attributes : Parsetree.attributes -> bool
81-
8276
val is_array_access : Parsetree.expression -> bool
8377
val is_ternary_expr : Parsetree.expression -> bool
84-
val is_if_let_expr : Parsetree.expression -> bool
8578

8679
val collect_ternary_parts :
8780
Parsetree.expression ->
8881
(Parsetree.expression * Parsetree.expression) list * Parsetree.expression
8982

9083
val parameters_should_hug : fun_param_kind list -> bool
9184

92-
val filter_fragile_match_attributes :
93-
Parsetree.attributes -> Parsetree.attributes
94-
9585
val should_indent_binary_expr : Parsetree.expression -> bool
9686
val should_inline_rhs_binary_expr : Parsetree.expression -> bool
9787
val has_printable_attributes : Parsetree.attributes -> bool
@@ -159,8 +149,6 @@ val rewrite_underscore_apply_in_pipe :
159149
(* (__x) => f(a, __x, c) -----> f(a, _, c) *)
160150
val is_underscore_apply_sugar : Parsetree.expression -> bool
161151

162-
val has_if_let_attribute : Parsetree.attributes -> bool
163-
164152
val is_rewritten_underscore_apply_sugar : Parsetree.expression -> bool
165153

166154
val is_fun_expr : Parsetree.expression -> bool

0 commit comments

Comments
 (0)