From 1b914d217e3d05c577eb62432c775b3e5530f07b Mon Sep 17 00:00:00 2001 From: Nick Treleaven Date: Fri, 21 Aug 2020 23:00:02 +0100 Subject: [PATCH] fmt: don't remove `mut` from `if mut` smart cast (#6188) --- vlib/v/ast/ast.v | 5 ++-- vlib/v/fmt/fmt.v | 37 +++++++++++--------------- vlib/v/fmt/tests/sum_smartcast_keep.vv | 5 +++- vlib/v/parser/if_match.v | 20 +++++++++----- 4 files changed, 36 insertions(+), 31 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 5347478aa1..b52edf0284 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -472,9 +472,10 @@ pub: pos token.Position body_pos token.Position comments []Comment + left_as_name string // `name` in `if cond is SumType as name` + mut_name bool // `if mut name is` pub mut: - smartcast bool // should only be true if cond is `x is sumtype`, it will be set in checker - if_expr - left_as_name string // only used in x is SumType check + smartcast bool // true when cond is `x is SumType`, set in checker.if_expr } pub struct UnsafeExpr { diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index ac55930b55..785ede90d4 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1357,35 +1357,30 @@ pub fn (mut f Fmt) if_expr(it ast.IfExpr) { } } if i == 0 { + // first `if` f.comments(branch.comments, {}) - f.write('if ') - f.expr(branch.cond) - if smartcast_as { - f.write(' as $branch.left_as_name') - } - f.write(' {') - } else if i < it.branches.len - 1 || !it.has_else { + } else { + // `else`, close previous branch if branch.comments.len > 0 { f.writeln('}') f.comments(branch.comments, {}) } else { f.write('} ') } - f.write('else if ') - f.expr(branch.cond) - if smartcast_as { - f.write(' as $branch.left_as_name') - } - f.write(' {') - } else if i == it.branches.len - 1 && it.has_else { - if branch.comments.len > 0 { - f.writeln('}') - f.comments(branch.comments, {}) - } else { - f.write('} ') - } - f.write('else {') + f.write('else ') } + if i < it.branches.len - 1 || !it.has_else { + f.write('if ') + if branch.mut_name { + f.write('mut ') + } + f.expr(branch.cond) + if smartcast_as { + f.write(' as $branch.left_as_name') + } + f.write(' ') + } + f.write('{') if single_line { f.write(' ') } else { diff --git a/vlib/v/fmt/tests/sum_smartcast_keep.vv b/vlib/v/fmt/tests/sum_smartcast_keep.vv index 9378122a5a..18d0bf208d 100644 --- a/vlib/v/fmt/tests/sum_smartcast_keep.vv +++ b/vlib/v/fmt/tests/sum_smartcast_keep.vv @@ -1,4 +1,6 @@ struct S1 { +mut: + i int } struct S2 { @@ -7,7 +9,8 @@ struct S2 { type Sum = S1 | S2 fn f(sum Sum) { - if sum is S1 { + if mut sum is S1 { + sum.i++ } if sum is S1 as s1 { } diff --git a/vlib/v/parser/if_match.v b/vlib/v/parser/if_match.v index 1c188419af..044f82c697 100644 --- a/vlib/v/parser/if_match.v +++ b/vlib/v/parser/if_match.v @@ -21,15 +21,11 @@ fn (mut p Parser) if_expr() ast.IfExpr { for p.tok.kind in [.key_if, .key_else] { p.inside_if = true start_pos := p.tok.position() - if p.tok.kind == .key_if { - p.next() - } else { + if p.tok.kind == .key_else { comments << p.eat_comments() p.check(.key_else) comments << p.eat_comments() - if p.tok.kind == .key_if { - p.next() - } else { + if p.tok.kind == .lcbr { // else { has_else = true p.inside_if = false @@ -64,9 +60,18 @@ fn (mut p Parser) if_expr() ast.IfExpr { break } } + // `if` or `else if` + p.check(.key_if) + comments << p.eat_comments() + // `if mut name is T` + mut mut_name := false + if p.tok.kind == .key_mut && p.peek_tok2.kind == .key_is { + mut_name = true + p.next() + comments << p.eat_comments() + } mut cond := ast.Expr{} mut is_guard := false - comments << p.eat_comments() // `if x := opt() {` if p.peek_tok.kind == .decl_assign { p.open_scope() @@ -121,6 +126,7 @@ fn (mut p Parser) if_expr() ast.IfExpr { body_pos: body_pos.extend(p.prev_tok.position()) comments: comments left_as_name: left_as_name + mut_name: mut_name } comments = p.eat_comments() if p.tok.kind != .key_else {