Skip to content
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#### :bug: Bug fix

- Keep `@warning("-4")` on `if` expressions when formatting; the formatter dropped it. https://github.com/rescript-lang/rescript/pull/8741
- 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
- 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
- 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
Expand All @@ -46,6 +47,7 @@

#### :house: Internal

- 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. https://github.com/rescript-lang/rescript/pull/8741
- 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
- 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
- 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
Expand Down
50 changes: 0 additions & 50 deletions compiler/syntax/src/res_comments_table.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1381,56 +1381,6 @@ and walk_expression expr t comments =
attach t.leading mod_expr.pmod_loc before;
walk_module_expr mod_expr t inside;
attach t.trailing mod_expr.pmod_loc after
| Pexp_match (expr1, [case; else_branch])
when Res_parsetree_viewer.has_if_let_attribute expr.pexp_attributes ->
let before, inside, after =
partition_by_loc comments case.pc_lhs.ppat_loc
in
attach t.leading case.pc_lhs.ppat_loc before;
walk_pattern case.pc_lhs t inside;
let after_pat, rest =
partition_adjacent_trailing case.pc_lhs.ppat_loc after
in
attach t.trailing case.pc_lhs.ppat_loc after_pat;
let before, inside, after = partition_by_loc rest expr1.pexp_loc in
attach t.leading expr1.pexp_loc before;
walk_expression expr1 t inside;
let after_expr, rest = partition_adjacent_trailing expr1.pexp_loc after in
attach t.trailing expr1.pexp_loc after_expr;
let before, inside, after = partition_by_loc rest case.pc_rhs.pexp_loc in
let after =
if is_block_expr case.pc_rhs then (
let after_expr, rest =
partition_adjacent_trailing case.pc_rhs.pexp_loc after
in
walk_expression case.pc_rhs t (List.concat [before; inside; after_expr]);
rest)
else (
attach t.leading case.pc_rhs.pexp_loc before;
walk_expression case.pc_rhs t inside;
after)
in
let after_expr, rest =
partition_adjacent_trailing case.pc_rhs.pexp_loc after
in
attach t.trailing case.pc_rhs.pexp_loc after_expr;
let before, inside, after =
partition_by_loc rest else_branch.pc_rhs.pexp_loc
in
let after =
if is_block_expr else_branch.pc_rhs then (
let after_expr, rest =
partition_adjacent_trailing else_branch.pc_rhs.pexp_loc after
in
walk_expression else_branch.pc_rhs t
(List.concat [before; inside; after_expr]);
rest)
else (
attach t.leading else_branch.pc_rhs.pexp_loc before;
walk_expression else_branch.pc_rhs t inside;
after)
in
attach t.trailing else_branch.pc_rhs.pexp_loc after
| Pexp_match (expr, cases) | Pexp_try (expr, cases) ->
let before, inside, after = partition_by_loc comments expr.pexp_loc in
let after =
Expand Down
16 changes: 2 additions & 14 deletions compiler/syntax/src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ module Error_messages = struct
"A polymorphic variant (e.g. #id) must start with an alphabetical letter \
or be a number (e.g. #742)"

let experimental_if_let expr =
let switch_expr = {expr with Parsetree.pexp_attributes = []} in
let experimental_if_let switch_expr =
Doc.concat
[
Doc.text "If-let is currently highly experimental.";
Expand Down Expand Up @@ -265,17 +264,8 @@ module Error_messages = struct
"Spreading JSX children is no longer supported."
end

let if_let_attr = (Location.mknoloc "res.iflet", Parsetree.PStr [])
let suppress_fragile_match_warning_attr =
( Location.mknoloc "warning",
Parsetree.PStr
[
Ast_helper.Str.eval
(Ast_helper.Exp.constant (Ast_helper.Const.string "-4"));
] )
let make_pat_variant_spread_attr =
(Location.mknoloc "res.patVariantSpread", Parsetree.PStr [])

let spread_attr = (Location.mknoloc "res.spread", Parsetree.PStr [])
let dict_spread_attr = (Location.mknoloc "res.dictSpread", Parsetree.PStr [])

Expand Down Expand Up @@ -3610,9 +3600,7 @@ and parse_if_let_expr start_pos p =
(Location.mkloc [] loc)
in
let loc = mk_loc start_pos (Parser.position p) in
Ast_helper.Exp.match_
~attrs:[if_let_attr; suppress_fragile_match_warning_attr]
~loc condition_expr
Ast_helper.Exp.match_ ~loc condition_expr
[
Ast_helper.Exp.case pattern then_expr;
Ast_helper.Exp.case (Ast_helper.Pat.any ()) else_expr;
Expand Down
3 changes: 1 addition & 2 deletions compiler/syntax/src/res_parens.ml
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ let structure_expr expr =
| None -> (
match expr with
| {pexp_desc = Pexp_jsx_element _} -> Nothing
| _ when Parsetree_viewer.has_attributes expr.pexp_attributes ->
Parenthesized
| _ when expr.pexp_attributes <> [] -> Parenthesized
| {
Parsetree.pexp_desc =
Pexp_constraint ({pexp_desc = Pexp_pack _}, {ptyp_desc = Ptyp_package _});
Expand Down
85 changes: 3 additions & 82 deletions compiler/syntax/src/res_parsetree_viewer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@ let is_parsing_attr (attr : Parsetree.attribute) =
match attr with
| ( {
Location.txt =
( "res.iflet" | "res.patVariantSpread" | "res.dictPattern"
| "res.dictSpread" );
"res.patVariantSpread" | "res.dictPattern" | "res.dictSpread";
},
_ ) ->
true
Expand Down Expand Up @@ -365,39 +364,6 @@ let flattenable_operators parent_operator child_operator =
&& is_equality_operator child_operator)
else false

let rec has_if_let_attribute attrs =
match attrs with
| [] -> false
| ({Location.txt = "res.iflet"}, _) :: _ -> true
| _ :: attrs -> has_if_let_attribute attrs

let is_if_let_expr expr =
match expr with
| {pexp_attributes = attrs; pexp_desc = Pexp_match _}
when has_if_let_attribute attrs ->
true
| _ -> false

let has_attributes attrs =
List.exists
(fun attr ->
match attr with
| {Location.txt = "res.iflet"}, _ -> false
(* Remove the fragile pattern warning for iflet expressions *)
| ( {Location.txt = "warning"},
PStr
[
{
pstr_desc =
Pstr_eval
({pexp_desc = Pexp_constant (Pconst_string payload)}, _);
};
] ) ->
String_literal.string_semantic payload <> "-4"
|| not (has_if_let_attribute attrs)
| _ -> true)
attrs

let is_array_access expr =
match expr.pexp_desc with
| Pexp_apply
Expand All @@ -413,43 +379,15 @@ let is_array_access expr =
true
| _ -> false

type if_condition_kind =
| If of Parsetree.expression
| IfLet of Parsetree.pattern * Parsetree.expression

let collect_if_expressions expr =
let rec collect acc expr =
let expr_loc = expr.pexp_loc in
match expr.pexp_desc with
| Pexp_ifthenelse (if_expr, then_expr, Some else_expr) ->
collect ((expr_loc, If if_expr, then_expr) :: acc) else_expr
collect ((expr_loc, if_expr, then_expr) :: acc) else_expr
| Pexp_ifthenelse (if_expr, then_expr, (None as else_expr)) ->
let ifs = List.rev ((expr_loc, If if_expr, then_expr) :: acc) in
let ifs = List.rev ((expr_loc, if_expr, then_expr) :: acc) in
(ifs, else_expr)
| Pexp_match
( condition,
[
{pc_lhs = pattern; pc_guard = None; pc_rhs = then_expr};
{
pc_rhs =
{pexp_desc = Pexp_construct ({txt = Longident.Lident "()"}, _)};
};
] )
when is_if_let_expr expr ->
let ifs =
List.rev ((expr_loc, IfLet (pattern, condition), then_expr) :: acc)
in
(ifs, None)
| Pexp_match
( condition,
[
{pc_lhs = pattern; pc_guard = None; pc_rhs = then_expr};
{pc_rhs = else_expr};
] )
when is_if_let_expr expr ->
collect
((expr_loc, IfLet (pattern, condition), then_expr) :: acc)
else_expr
| _ -> (List.rev acc, Some expr)
in
collect [] expr
Expand All @@ -475,23 +413,6 @@ let parameters_should_hug parameters =
true
| _ -> false

let filter_fragile_match_attributes attrs =
List.filter
(fun attr ->
match attr with
| ( {Location.txt = "warning"},
PStr
[
{
pstr_desc =
Pstr_eval
({pexp_desc = Pexp_constant (Pconst_string payload)}, _);
};
] ) ->
String_literal.string_semantic payload <> "-4"
| _ -> true)
attrs

let should_indent_binary_expr expr =
let same_precedence_sub_expression operator sub_expression =
match sub_expression with
Expand Down
14 changes: 1 addition & 13 deletions compiler/syntax/src/res_parsetree_viewer.mli
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,12 @@ type dict_expr_part =
| DictExprRows of Parsetree.expression
| DictExprSpread of Parsetree.expression

type if_condition_kind =
| If of Parsetree.expression
| IfLet of Parsetree.pattern * Parsetree.expression

(* if ... else if ... else ... is represented as nested expressions: if ... else { if ... }
* The purpose of this function is to flatten nested ifs into one sequence.
* Basically compute: ([if, else if, else if, else if], else) *)
val collect_if_expressions :
Parsetree.expression ->
(Location.t * if_condition_kind * Parsetree.expression) list
(Location.t * Parsetree.expression * Parsetree.expression) list
* Parsetree.expression option

val collect_array_expressions :
Expand Down Expand Up @@ -77,21 +73,15 @@ val is_equality_operator : string -> bool

val flattenable_operators : string -> string -> bool

val has_attributes : Parsetree.attributes -> bool

val is_array_access : Parsetree.expression -> bool
val is_ternary_expr : Parsetree.expression -> bool
val is_if_let_expr : Parsetree.expression -> bool

val collect_ternary_parts :
Parsetree.expression ->
(Parsetree.expression * Parsetree.expression) list * Parsetree.expression

val parameters_should_hug : fun_param_kind list -> bool

val filter_fragile_match_attributes :
Parsetree.attributes -> Parsetree.attributes

val should_indent_binary_expr : Parsetree.expression -> bool
val should_inline_rhs_binary_expr : Parsetree.expression -> bool
val has_printable_attributes : Parsetree.attributes -> bool
Expand Down Expand Up @@ -159,8 +149,6 @@ val rewrite_underscore_apply_in_pipe :
(* (__x) => f(a, __x, c) -----> f(a, _, c) *)
val is_underscore_apply_sugar : Parsetree.expression -> bool

val has_if_let_attribute : Parsetree.attributes -> bool

val is_rewritten_underscore_apply_sugar : Parsetree.expression -> bool

val is_fun_expr : Parsetree.expression -> bool
Expand Down
Loading
Loading