From f59b771c765b8be0b5265331c76a8a7de1294105 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=C3=A4schle?= Date: Fri, 18 Sep 2020 01:04:56 +0200 Subject: [PATCH] parser: fix nested amp (#6402) --- vlib/v/gen/cgen.v | 7 +------ vlib/v/parser/parser.v | 4 ++++ vlib/v/parser/pratt.v | 2 +- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index e37dfe755b..d153683fb8 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -1921,6 +1921,7 @@ fn (mut g Gen) expr(node ast.Expr) { // &Foo(0) => ((Foo*)0) g.out.go_back(1) } + g.is_amp = false sym := g.table.get_type_symbol(node.typ) if sym.kind == .string && !node.typ.is_ptr() { // `string(x)` needs `tos()`, but not `&string(x) @@ -1950,14 +1951,8 @@ fn (mut g Gen) expr(node ast.Expr) { g.expr(node.expr) g.write('))') } else { - // styp := g.table.Type_to_str(it.typ) styp := g.typ(node.typ) - // g.write('($styp)(') g.write('(($styp)(') - // if g.is_amp { - // g.write('*') - // } - // g.write(')(') g.expr(node.expr) if node.expr is ast.IntegerLiteral && node.typ in [table.u64_type, table.u32_type, table.u16_type] { diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 62ecf52645..783660730f 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1061,6 +1061,10 @@ pub fn (mut p Parser) name_expr() ast.Expr { // Handle `&Foo(0)` to_typ = to_typ.to_ptr() } + // this prevents inner casts to also have an `&` + // example: &Foo(malloc(int(num))) + // without the next line int would result in int* + p.is_amp = false p.check(.lpar) mut expr := ast.Expr{} mut arg := ast.Expr{} diff --git a/vlib/v/parser/pratt.v b/vlib/v/parser/pratt.v index b4812722ac..8b9af1b129 100644 --- a/vlib/v/parser/pratt.v +++ b/vlib/v/parser/pratt.v @@ -340,7 +340,7 @@ fn (mut p Parser) prefix_expr() ast.PrefixExpr { p.next() mut right := if op == .minus { p.expr(token.Precedence.call) } else { p.expr(token.Precedence.prefix) } p.is_amp = false - if mut right is ast.CastExpr { + if right is ast.CastExpr { right.in_prexpr = true } mut or_stmts := []ast.Stmt{}