v/fmt: fix dropping `as ident` from `if sum is T` statement (#6049)
parent
66b3fabeef
commit
3e4df7f140
|
@ -53,6 +53,7 @@ jobs:
|
||||||
- name: v fmt
|
- name: v fmt
|
||||||
run: |
|
run: |
|
||||||
./v fmt -verify vlib/v/checker/checker.v
|
./v fmt -verify vlib/v/checker/checker.v
|
||||||
|
./v fmt -verify vlib/v/fmt/fmt.v
|
||||||
./v fmt -verify vlib/v/parser/parser.v
|
./v fmt -verify vlib/v/parser/parser.v
|
||||||
./v fmt -verify vlib/v/gen/cgen.v
|
./v fmt -verify vlib/v/gen/cgen.v
|
||||||
# - name: Test v binaries
|
# - name: Test v binaries
|
||||||
|
|
|
@ -1311,21 +1311,20 @@ pub fn (mut f Fmt) if_expr(it ast.IfExpr) {
|
||||||
(it.is_expr || f.is_assign)
|
(it.is_expr || f.is_assign)
|
||||||
f.single_line_if = single_line
|
f.single_line_if = single_line
|
||||||
for i, branch in it.branches {
|
for i, branch in it.branches {
|
||||||
// NOTE: taken from checker in if_expr(). used for smartcast
|
// Check `sum is T` smartcast
|
||||||
mut is_variable := false
|
mut smartcast_as := false
|
||||||
if branch.cond is ast.InfixExpr {
|
if branch.cond is ast.InfixExpr as infix {
|
||||||
infix := branch.cond as ast.InfixExpr
|
if infix.op == .key_is {
|
||||||
if infix.op == .key_is &&
|
// left_as_name is either empty, infix.left.str() or the `as` name
|
||||||
(infix.left is ast.Ident || infix.left is ast.SelectorExpr) && infix.right is ast.Type {
|
smartcast_as = branch.left_as_name.len > 0 &&
|
||||||
// right_expr := infix.right as ast.Type
|
infix.left.str() != branch.left_as_name
|
||||||
is_variable = if infix.left is ast.Ident { (infix.left as ast.Ident).kind == .variable } else { true }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
f.comments(branch.comments, {})
|
f.comments(branch.comments, {})
|
||||||
f.write('if ')
|
f.write('if ')
|
||||||
f.expr(branch.cond)
|
f.expr(branch.cond)
|
||||||
if is_variable && branch.left_as_name.len > 0 {
|
if smartcast_as {
|
||||||
f.write(' as $branch.left_as_name')
|
f.write(' as $branch.left_as_name')
|
||||||
}
|
}
|
||||||
f.write(' {')
|
f.write(' {')
|
||||||
|
@ -1338,7 +1337,7 @@ pub fn (mut f Fmt) if_expr(it ast.IfExpr) {
|
||||||
}
|
}
|
||||||
f.write('else if ')
|
f.write('else if ')
|
||||||
f.expr(branch.cond)
|
f.expr(branch.cond)
|
||||||
if is_variable && branch.left_as_name.len > 0 {
|
if smartcast_as {
|
||||||
f.write(' as $branch.left_as_name')
|
f.write(' as $branch.left_as_name')
|
||||||
}
|
}
|
||||||
f.write(' {')
|
f.write(' {')
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
struct S1 {
|
||||||
|
}
|
||||||
|
|
||||||
|
struct S2 {
|
||||||
|
}
|
||||||
|
|
||||||
|
type Sum = S1 | S2
|
||||||
|
|
||||||
|
fn f(sum Sum) {
|
||||||
|
if sum is S1 {
|
||||||
|
}
|
||||||
|
if sum is S1 as s1 {
|
||||||
|
}
|
||||||
|
a := [sum]
|
||||||
|
if a[0] is S2 {
|
||||||
|
}
|
||||||
|
if a[0] is S2 as s2 {
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue