1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

fmt: fix parens around reference module prefix expressions (#18416)

This commit is contained in:
Turiiya 2023-06-12 12:35:44 +02:00 committed by GitHub
parent 37386697a3
commit 7f178d4662
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 3 deletions

View File

@ -2629,9 +2629,9 @@ pub fn (mut f Fmt) postfix_expr(node ast.PostfixExpr) {
} }
pub fn (mut f Fmt) prefix_expr(node ast.PrefixExpr) { pub fn (mut f Fmt) prefix_expr(node ast.PrefixExpr) {
// !(a in b) => a !in b, !(a is b) => a !is b if node.right is ast.ParExpr {
if node.op == .not && node.right is ast.ParExpr { if node.op == .not && node.right.expr is ast.InfixExpr {
if node.right.expr is ast.InfixExpr { // !(a in b) => a !in b, !(a is b) => a !is b
if node.right.expr.op in [.key_in, .not_in, .key_is, .not_is] if node.right.expr.op in [.key_in, .not_in, .key_is, .not_is]
&& node.right.expr.right !is ast.InfixExpr { && node.right.expr.right !is ast.InfixExpr {
f.expr(node.right.expr.left) f.expr(node.right.expr.left)
@ -2645,6 +2645,14 @@ pub fn (mut f Fmt) prefix_expr(node ast.PrefixExpr) {
f.expr(node.right.expr.right) f.expr(node.right.expr.right)
return return
} }
} else if node.op == .amp && node.right.expr is ast.Ident {
// Reference prefix ParExpr. E.g.: `a := &(modname.constname)`
f.write(node.op.str())
f.write('(')
f.expr(node.right)
f.or_expr(node.or_block)
f.write(')')
return
} }
} }
f.write(node.op.str()) f.write(node.op.str())

View File

@ -0,0 +1,13 @@
module ast
struct MyChar {
c string
}
const a_char = MyChar{
c: 'a'
}
fn foo() {
mut b := &(ast.a_char)
}