diff --git a/CHANGELOG.md b/CHANGELOG.md index b0524761c0b..7991c353904 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/compiler/syntax/src/res_comments_table.ml b/compiler/syntax/src/res_comments_table.ml index 0df924ee2ad..1d34e711610 100644 --- a/compiler/syntax/src/res_comments_table.ml +++ b/compiler/syntax/src/res_comments_table.ml @@ -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 = diff --git a/compiler/syntax/src/res_core.ml b/compiler/syntax/src/res_core.ml index ed44181a74f..4082ceca33e 100644 --- a/compiler/syntax/src/res_core.ml +++ b/compiler/syntax/src/res_core.ml @@ -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."; @@ -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 []) @@ -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; diff --git a/compiler/syntax/src/res_parens.ml b/compiler/syntax/src/res_parens.ml index 35d21c8b3e6..170255de64d 100644 --- a/compiler/syntax/src/res_parens.ml +++ b/compiler/syntax/src/res_parens.ml @@ -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 _}); diff --git a/compiler/syntax/src/res_parsetree_viewer.ml b/compiler/syntax/src/res_parsetree_viewer.ml index b3515e3690b..c8a9387f109 100644 --- a/compiler/syntax/src/res_parsetree_viewer.ml +++ b/compiler/syntax/src/res_parsetree_viewer.ml @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/compiler/syntax/src/res_parsetree_viewer.mli b/compiler/syntax/src/res_parsetree_viewer.mli index 6261a0d0e8c..b771688c181 100644 --- a/compiler/syntax/src/res_parsetree_viewer.mli +++ b/compiler/syntax/src/res_parsetree_viewer.mli @@ -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 : @@ -77,11 +73,8 @@ 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 -> @@ -89,9 +82,6 @@ val collect_ternary_parts : 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 @@ -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 diff --git a/compiler/syntax/src/res_printer.ml b/compiler/syntax/src/res_printer.ml index e4ce9f5b9b7..c652ea29e05 100644 --- a/compiler/syntax/src/res_printer.ml +++ b/compiler/syntax/src/res_printer.ml @@ -2594,12 +2594,10 @@ and print_value_binding ~state ~rec_flag (vb : Parsetree.value_binding) cmt_tbl match vb.pvb_expr with | {pexp_attributes = []; pexp_desc = Pexp_ternary (if_expr, _, _)} -> Parsetree_viewer.is_binary_expression if_expr - || Parsetree_viewer.has_attributes if_expr.pexp_attributes + || if_expr.pexp_attributes <> [] | {pexp_desc = Pexp_tagged_template _} -> false | {pexp_desc = Pexp_jsx_element _} -> true - | e -> - Parsetree_viewer.has_attributes e.pexp_attributes - || Parsetree_viewer.is_array_access e) + | e -> e.pexp_attributes <> [] || Parsetree_viewer.is_array_access e) in Doc.group (Doc.concat @@ -3131,53 +3129,31 @@ and print_if_chain ~state pexp_attributes ifs else_expr cmt_tbl = (fun i (outer_loc, if_expr, then_expr) -> let if_txt = if i > 0 then Doc.text "else if " else Doc.text "if " in let doc = - match if_expr with - | Parsetree_viewer.If if_expr -> - let condition = - if Parsetree_viewer.is_block_expr if_expr then - print_expression_block ~state ~braces:true if_expr cmt_tbl - else - let doc = - print_expression_with_comments ~state if_expr cmt_tbl - in - match Parens.expr if_expr with - | Parens.Parenthesized -> add_parens doc - | Braced braces -> print_braces doc if_expr braces - | Nothing -> Doc.if_breaks (add_parens doc) doc - in - Doc.concat - [ - if_txt; - Doc.group condition; - Doc.space; - (let then_expr = - match Parsetree_viewer.process_braces then_expr with - (* This case only happens when coming from Reason, we strip braces *) - | Some _, expr -> expr - | _ -> then_expr - in - print_expression_block ~state ~braces:true then_expr cmt_tbl); - ] - | IfLet (pattern, condition_expr) -> - let condition_doc = + let condition = + if Parsetree_viewer.is_block_expr if_expr then + print_expression_block ~state ~braces:true if_expr cmt_tbl + else let doc = - print_expression_with_comments ~state condition_expr cmt_tbl + print_expression_with_comments ~state if_expr cmt_tbl in - match Parens.expr condition_expr with + match Parens.expr if_expr with | Parens.Parenthesized -> add_parens doc - | Braced braces -> print_braces doc condition_expr braces - | Nothing -> doc - in - Doc.concat - [ - if_txt; - Doc.text "let "; - print_pattern ~state pattern cmt_tbl; - Doc.text " = "; - condition_doc; - Doc.space; - print_expression_block ~state ~braces:true then_expr cmt_tbl; - ] + | Braced braces -> print_braces doc if_expr braces + | Nothing -> Doc.if_breaks (add_parens doc) doc + in + Doc.concat + [ + if_txt; + Doc.group condition; + Doc.space; + (let then_expr = + match Parsetree_viewer.process_braces then_expr with + (* This case only happens when coming from Reason, we strip braces *) + | Some _, expr -> expr + | _ -> then_expr + in + print_expression_block ~state ~braces:true then_expr cmt_tbl); + ] in print_leading_comments doc cmt_tbl.leading outer_loc) ifs) @@ -3193,10 +3169,8 @@ and print_if_chain ~state pexp_attributes ifs else_expr cmt_tbl = print_expression_block ~state ~braces:true expr cmt_tbl; ] in - let attrs = - Parsetree_viewer.filter_fragile_match_attributes pexp_attributes - in - Doc.concat [print_attributes ~state attrs cmt_tbl; if_docs; else_doc] + Doc.concat + [print_attributes ~state pexp_attributes cmt_tbl; if_docs; else_doc] and print_assignment_rhs ~operator rhs rhs_doc = (* Delimited expressions handle their own indentation. Binary expressions @@ -3206,7 +3180,7 @@ and print_assignment_rhs ~operator rhs rhs_doc = && (Parsetree_viewer.is_binary_expression rhs || match rhs.pexp_desc with - | Pexp_match _ -> not (Parsetree_viewer.is_if_let_expr rhs) + | Pexp_match _ -> true | _ -> false) in Doc.concat @@ -3778,9 +3752,6 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl = Doc.text " catch "; print_cases ~state cases cmt_tbl; ] - | Pexp_match (_, [_; _]) when Parsetree_viewer.is_if_let_expr e -> - let ifs, else_expr = Parsetree_viewer.collect_if_expressions e in - print_if_chain ~state e.pexp_attributes ifs else_expr cmt_tbl | Pexp_match (expr, cases) -> let expr_doc = let doc = print_expression_with_comments ~state expr cmt_tbl in @@ -3830,7 +3801,6 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl = | Pexp_apply _ | Pexp_fun _ | Pexp_setfield _ | Pexp_ifthenelse _ | Pexp_ternary _ -> true - | Pexp_match _ when Parsetree_viewer.is_if_let_expr e -> true | Pexp_jsx_element _ -> true | _ -> false in @@ -4074,7 +4044,7 @@ and print_binary_expression ~state ~force_pipe_breaks } -> if Parsetree_viewer.flattenable_operators parent_operator operator - && not (Parsetree_viewer.has_attributes expr.pexp_attributes) + && expr.pexp_attributes = [] then let left_printed = flatten ~is_lhs:true ~is_multiline left operator @@ -4568,10 +4538,8 @@ and print_pexp_apply ~state expr cmt_tbl = match target_expr with | {pexp_attributes = []; pexp_desc = Pexp_ternary (if_expr, _, _)} -> Parsetree_viewer.is_binary_expression if_expr - || Parsetree_viewer.has_attributes if_expr.pexp_attributes - | e -> - Parsetree_viewer.has_attributes e.pexp_attributes - || Parsetree_viewer.is_array_access e + || if_expr.pexp_attributes <> [] + | e -> e.pexp_attributes <> [] || Parsetree_viewer.is_array_access e in let target_expr = let doc = print_expression_with_comments ~state target_expr cmt_tbl in diff --git a/tests/syntax_tests/data/parsing/errors/expressions/expected/ifLet.res.txt b/tests/syntax_tests/data/parsing/errors/expressions/expected/ifLet.res.txt index 7c23f147c2a..39899c2e06f 100644 --- a/tests/syntax_tests/data/parsing/errors/expressions/expected/ifLet.res.txt +++ b/tests/syntax_tests/data/parsing/errors/expressions/expected/ifLet.res.txt @@ -35,14 +35,10 @@ switch result { | _ => () } -;;((match result with - | Some x -> Console.log {js|The sky is blue|js} - | _ -> ())[@res.iflet ][@warning {js|-4|js}]) -;;((match result with - | Error x -> Console.log {js|The sky is red|js} - | _ -> - (((match result with - | Ok y -> Console.log {js|The sky is blue|js} - | _ -> ())) - [@res.iflet ][@warning {js|-4|js}])) - [@res.iflet ][@warning {js|-4|js}]) \ No newline at end of file +;;match result with | Some x -> Console.log {js|The sky is blue|js} | _ -> () +;;match result with + | Error x -> Console.log {js|The sky is red|js} + | _ -> + (match result with + | Ok y -> Console.log {js|The sky is blue|js} + | _ -> ()) \ No newline at end of file diff --git a/tests/syntax_tests/data/printer/comments/expected/ifLet.res.txt b/tests/syntax_tests/data/printer/comments/expected/ifLet.res.txt deleted file mode 100644 index 3be53725ba7..00000000000 --- a/tests/syntax_tests/data/printer/comments/expected/ifLet.res.txt +++ /dev/null @@ -1,24 +0,0 @@ -/* -if /* c0 */ let /* c1 */ Some(/* c2 */ x /* c3 */) /* c4 */ = /* c5 */ makeOpt() /* c6 */ { - /* c7 */ foo() /* c8 */ -} else { - /* c9 */ bar() /* c10 */ -} - -let getZ2 = nested => - if let /* c-4 */ Some(point) /* c-3 */ = /* c-2 */ nested.origin /* c-1 */ { - if let /* c0 */ Some( /* c1 */ z /* c2 */ ) =/* c3 */ point.z /* c4 */ { - let pi /* inside */ = 3.13 *. point.z // trailing1 - let e /* inside1 */= 42.3 // trailing2 - pi *. e // trailing3 - } else { - let pi /* inside2 */= 3.13 // trailing4 - let e /* inside3 */= 42.3 // trailing5 - pi *. e // trailing6 - } - } else { - let pi /* inside4 */ = 3.13 // trailing7 - let e /* inside5 */ = 42.3 // trailing8 - pi *. e // trailing9 - } - */ diff --git a/tests/syntax_tests/data/printer/comments/ifLet.res b/tests/syntax_tests/data/printer/comments/ifLet.res deleted file mode 100644 index d6e294c17a2..00000000000 --- a/tests/syntax_tests/data/printer/comments/ifLet.res +++ /dev/null @@ -1,24 +0,0 @@ -/* -if /* c0 */ let /* c1 */ Some(/* c2 */ x /* c3 */) /* c4 */ = /* c5 */ makeOpt() /* c6 */ { - /* c7 */ foo() /* c8 */ -} else { - /* c9 */ bar() /* c10 */ -} - -let getZ2 = nested => - if let /* c-4 */ Some(point) /* c-3 */ = /* c-2 */ nested.origin /* c-1 */ { - if let /* c0 */ Some( /* c1 */ z /* c2 */ ) =/* c3 */ point.z /* c4 */ { - let pi /* inside */ = 3.13 *. point.z // trailing1 - let e /* inside1 */= 42.3 // trailing2 - pi *. e // trailing3 - } else { - let pi /* inside2 */= 3.13 // trailing4 - let e /* inside3 */= 42.3 // trailing5 - pi *. e // trailing6 - } - } else { - let pi /* inside4 */ = 3.13 // trailing7 - let e /* inside5 */ = 42.3 // trailing8 - pi *. e // trailing9 - } - */ diff --git a/tests/syntax_tests/data/printer/expr/expected/if.res.txt b/tests/syntax_tests/data/printer/expr/expected/if.res.txt index c438c1f8e6d..bd629c26551 100644 --- a/tests/syntax_tests/data/printer/expr/expected/if.res.txt +++ b/tests/syntax_tests/data/printer/expr/expected/if.res.txt @@ -46,186 +46,10 @@ if inclusions[index] = (uid, url) { onChange(inclusions) } -// Basic -// if let Some(x) = foo() { -// doSomethingWithX(x) -// } - -// Else branch -// if let Some(x) = foo(){ -// doSomethingWithX(x) -// } else { -// doSomethingElse() -// } - -// Else-if support -// if let Some(x) = foo(){ -// doSomethingWithX(x) -// } else if let Some(y) = bar(){ -// doSomethingWithY(y) -// } else { -// doSomethingElse() -// } - -// Mixed conditions, pattern start -// if let Some(x) = foo(){ -// doSomethingWithX(x) -// } else if n > 10 { -// doSomethingWithForN() -// } else { -// doSomethingElse() -// } - -// Mixed conditions, condition start -// if n > 10 { -// doSomethingWithForN() -// } else if let Some(x) = foo() { -// doSomethingWithX(x) -// } else { -// doSomethingElse() -// } - -// Maintains attrs correctly -// if n > 10 { -// @aa -// doSomethingWithForN() -// } else if let @bb Some(@cc x) = @dd foo() { -// @ee -// doSomethingWithY(x) -// } else { -// @ff -// doSomethingElse() -// } - -// if let Some(x) = foo() { -// doSomethingWithX(x) -// } else if let Some(y) = bar() { -// doSomethingWithY(y) -// } else if let Some(z) = baz() { -// doSomethingWithZ(z) -// } - -// full destructuring -// if let Some(Thing(With({ -// many: Internal([Components, q]), -// }))) as p = foo() { -// doSomethingWithE(e) -// } - -// Assignment -// let a = if let Some(x) = foo() { -// x -// } else { -// 123 -// } - -// Nesting -// let getZ = nested => -// if let Some(point) = nested.origin { -// if let Some(z) = point.z { -// z -// } else { -// 0 -// } -// } else { -// 0 -// } - -// Deep nesting -// let getZ = nested => -// if let Some(nested) = nested.a { -// if let Some(nested) = nested.b { -// if let Some(nested) = nested.c { -// if let Some(nested) = nested.d { -// if let Some(nested) = nested.e { -// if let Some(nested) = nested.f { -// if let Some(nested) = nested.g { -// z -// } else { -// 0 -// } -// } else { -// 0 -// } -// } else if let Some(nested) = nested.g { -// z -// } else { -// 0 -// } -// } else { -// a ? b : c -// } -// } else { -// 0 -// } -// } else { -// 0 -// } -// } else { -// 0 -// } - -// Break testing -// if let Some(suuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuperLongName) = anotherSuuuuuuuuuuuuuuuuuuuuuuuuuuuperLongName() { -// foo() -// } - -// if let Some(suuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuperLongName) = anotherSuuuuuuuuuuuuuuuuuuuuuuuuuuuperLongNameanotherSuuuuuuuuuuuuuuuuuuuuuuuuuuuperLongName(withSomeArgsThatAreAlsoLarge, withManyArgsThatAreAlsoLarge) { -// foo() -// } - -// if let { suuuuuuuuuuuuuuuuuuuuuuuuuuuperLongName, suuuuuuuuuuuuuuuuuuuuuuuuuuuperLongName2, suuuuuuuuuuuuuuuuuuuuuuuuuuuperLongName3 } = buildMyRecord() { -// foo() -// } - -// Complex nesting -// if let Some(Thing(With({many: Internal([Components, q])}))) as p = foo() { -// if let Other(Thing(With({many: Internal([Components, q])}))) as p = foo() { -// doSomethingElse(e) -// } -// doSomethingWithE(e) -// } else if let Some(Thing(With({ -// many: Internal([Components, q]), -// }))) as p = foo() { -// doSomethingWithE(e) -// } else if let Some(Thing(With({ -// many: Internal([Components, q]), -// }))) as p = foo() { -// doSomethingWithE(e) -// } - -// Ternary -// let a = b -// ? if let Some(x) = foo() { -// 1 -// } else { -// 2 -// } -// : 3 - -// let a = b -// ? 1 -// : if let Some(x) = foo() { -// 1 -// } else { -// 2 -// } - -// let a = b -// ? if let Some(x) = foo() { -// 1 -// } else { -// 2 -// } -// : if let Some(x) = foo() { -// 1 -// } else { -// 2 -// } - -// print parens correct -// if let Some(x) = (3: option) { -// (x: int) -// } else { -// (20: int) -// } +let x = + @warning("-4") + if a { + b + } else { + c + } diff --git a/tests/syntax_tests/data/printer/expr/if.res b/tests/syntax_tests/data/printer/expr/if.res index 14214eec8bf..2f9ebeb8972 100644 --- a/tests/syntax_tests/data/printer/expr/if.res +++ b/tests/syntax_tests/data/printer/expr/if.res @@ -40,187 +40,8 @@ if inclusions[index] = (uid, url) { onChange(inclusions) } - -// Basic -// if let Some(x) = foo() { - // doSomethingWithX(x) -// } - -// Else branch -// if let Some(x) = foo(){ - // doSomethingWithX(x) -// } else { - // doSomethingElse() -// } - -// Else-if support -// if let Some(x) = foo(){ - // doSomethingWithX(x) -// } else if let Some(y) = bar(){ - // doSomethingWithY(y) -// } else { - // doSomethingElse() -// } - -// Mixed conditions, pattern start -// if let Some(x) = foo(){ - // doSomethingWithX(x) -// } else if n > 10 { - // doSomethingWithForN() -// } else { - // doSomethingElse() -// } - -// Mixed conditions, condition start -// if n > 10 { - // doSomethingWithForN() -// } else if let Some(x) = foo() { - // doSomethingWithX(x) -// } else { - // doSomethingElse() -// } - -// Maintains attrs correctly -// if n > 10 { - // @aa - // doSomethingWithForN() -// } else if let @bb Some(@cc x) = @dd foo() { - // @ee - // doSomethingWithY(x) -// } else { - // @ff - // doSomethingElse() -// } - -// if let Some(x) = foo() { - // doSomethingWithX(x) -// } else if let Some(y) = bar() { - // doSomethingWithY(y) -// } else if let Some(z) = baz() { - // doSomethingWithZ(z) -// } - -// full destructuring -// if let Some(Thing(With({ - // many: Internal([Components, q]), -// }))) as p = foo() { - // doSomethingWithE(e) -// } - -// Assignment -// let a = if let Some(x) = foo() { - // x -// } else { - // 123 -// } - -// Nesting -// let getZ = nested => - // if let Some(point) = nested.origin { - // if let Some(z) = point.z { - // z - // } else { - // 0 - // } - // } else { - // 0 - // } - -// Deep nesting -// let getZ = nested => - // if let Some(nested) = nested.a { - // if let Some(nested) = nested.b { - // if let Some(nested) = nested.c { - // if let Some(nested) = nested.d { - // if let Some(nested) = nested.e { - // if let Some(nested) = nested.f { - // if let Some(nested) = nested.g { - // z - // } else { - // 0 - // } - // } else { - // 0 - // } - // } else if let Some(nested) = nested.g { - // z - // } else { - // 0 - // } - // } else { - // a ? b : c - // } - // } else { - // 0 - // } - // } else { - // 0 - // } - // } else { - // 0 - // } - -// Break testing -// if let Some(suuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuperLongName) = anotherSuuuuuuuuuuuuuuuuuuuuuuuuuuuperLongName() { - // foo() -// } - -// if let Some(suuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuperLongName) = anotherSuuuuuuuuuuuuuuuuuuuuuuuuuuuperLongNameanotherSuuuuuuuuuuuuuuuuuuuuuuuuuuuperLongName(withSomeArgsThatAreAlsoLarge, withManyArgsThatAreAlsoLarge) { - // foo() -// } - -// if let { suuuuuuuuuuuuuuuuuuuuuuuuuuuperLongName, suuuuuuuuuuuuuuuuuuuuuuuuuuuperLongName2, suuuuuuuuuuuuuuuuuuuuuuuuuuuperLongName3 } = buildMyRecord() { - // foo() -// } - -// Complex nesting -// if let Some(Thing(With({many: Internal([Components, q])}))) as p = foo() { - // if let Other(Thing(With({many: Internal([Components, q])}))) as p = foo() { - // doSomethingElse(e) - // } - // doSomethingWithE(e) -// } else if let Some(Thing(With({ - // many: Internal([Components, q]), -// }))) as p = foo() { - // doSomethingWithE(e) -// } else if let Some(Thing(With({ - // many: Internal([Components, q]), -// }))) as p = foo() { - // doSomethingWithE(e) -// } - -// Ternary -// let a = b - // ? if let Some(x) = foo() { - // 1 - // } else { - // 2 - // } - // : 3 - -// let a = b - // ? 1 - // : if let Some(x) = foo() { - // 1 - // } else { - // 2 - // } - -// let a = b - // ? if let Some(x) = foo() { - // 1 - // } else { - // 2 - // } - // : if let Some(x) = foo() { - // 1 - // } else { - // 2 - // } - -// print parens correct -// if let Some(x) = (3: option) { - // (x: int) -// } else { - // (20: int) -// } +let x = @warning("-4") if a { + b +} else { + c +}