Skip to content

Commit 921cab5

Browse files
committed
vfmt: preserve parens around parenthesized function calls
parser fix (fixes #22652) unwraps a ParExpr wrapping a call's left, so a call like (add)(2, 4) was parsed but vfmt could no longer see the parens and rewrote it to add(2, 4) — defeating the regression test. Add is_paren_wrapped_call flag on CallExpr, set by the parser when it unwraps the ParExpr, and use it in fmt.call_expr to write '(' name ')'.
1 parent 7a83f2e commit 921cab5

3 files changed

Lines changed: 14 additions & 10 deletions

File tree

vlib/v/ast/ast.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,7 @@ pub mut:
940940
//
941941
is_expand_simple_interpolation bool // true, when the function/method is marked as @[expand_simple_interpolation]
942942
is_unwrapped_fn_selector bool // true, when the call is from an unwrapped selector (e.g. if t.foo != none { t.foo() })
943+
is_paren_wrapped_call bool // true, when the callee was wrapped in parentheses: `(f)(x)` — used by vfmt to preserve the parens
943944
// Calls to it with an interpolation argument like `b.f('x ${y}')`, will be converted to `b.f('x ')` followed by `b.f(y)`.
944945
// The same type, has to support also a .write_decimal(n i64) method.
945946
}

vlib/v/fmt/fmt.v

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2213,6 +2213,8 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
22132213
name := f.short_module(node.name)
22142214
if node.is_static_method {
22152215
f.write_static_method(node.name, name)
2216+
} else if node.is_paren_wrapped_call {
2217+
f.write('(${name})')
22162218
} else {
22172219
f.write(name)
22182220
}

vlib/v/parser/expr.v

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -882,16 +882,17 @@ fn (mut p Parser) call_expr_with_left(left ast.Expr) ast.CallExpr {
882882
kind = p.call_kind(fn_name)
883883
}
884884
return ast.CallExpr{
885-
name: name
886-
name_pos: name_pos
887-
mod: mod
888-
kind: kind
889-
left: unwrapped_left
890-
args: args
891-
pos: pos
892-
scope: p.scope
893-
or_block: or_block
894-
is_return_used: p.expecting_value
885+
name: name
886+
name_pos: name_pos
887+
mod: mod
888+
kind: kind
889+
left: unwrapped_left
890+
args: args
891+
pos: pos
892+
scope: p.scope
893+
or_block: or_block
894+
is_return_used: p.expecting_value
895+
is_paren_wrapped_call: left is ast.ParExpr
895896
}
896897
}
897898

0 commit comments

Comments
 (0)