diff --git a/vlib/v/fmt/tests/pointer_casts_keep.vv b/vlib/v/fmt/tests/pointer_casts_keep.vv new file mode 100644 index 0000000000..7cc646033e --- /dev/null +++ b/vlib/v/fmt/tests/pointer_casts_keep.vv @@ -0,0 +1,30 @@ +struct Struct {} + +fn main() { + unsafe { + pb := &byte(0) + ppb := &&byte(0) + pppb := &&&byte(0) + ppppb := &&&&byte(0) + dump(voidptr(pb)) + dump(voidptr(ppb)) + dump(voidptr(pppb)) + dump(voidptr(ppppb)) + pc := &char(0) + ppc := &&char(0) + pppc := &&&char(0) + ppppc := &&&&char(0) + dump(voidptr(pc)) + dump(voidptr(ppc)) + dump(voidptr(pppc)) + dump(voidptr(ppppc)) + ps := &Struct(0) + pps := &&Struct(0) + ppps := &&&Struct(0) + pppps := &&&&Struct(0) + dump(voidptr(ps)) + dump(voidptr(pps)) + dump(voidptr(ppps)) + dump(voidptr(pppps)) + } +} diff --git a/vlib/v/parser/expr.v b/vlib/v/parser/expr.v index 862eb062de..ef2b3d9ea9 100644 --- a/vlib/v/parser/expr.v +++ b/vlib/v/parser/expr.v @@ -170,7 +170,7 @@ pub fn (mut p Parser) check_expr(precedence int) ?ast.Expr { node = p.name_expr() } else if p.is_amp && p.peek_tok.kind == .rsbr && p.peek_token(3).kind != .lcbr { pos := p.tok.position() - typ := p.parse_type().to_ptr() + typ := p.parse_type() p.check(.lpar) expr := p.expr(0) p.check(.rpar) @@ -577,8 +577,11 @@ fn (mut p Parser) prefix_expr() ast.Expr { mut right := p.expr(int(token.Precedence.prefix)) p.is_amp = false if mut right is ast.CastExpr && op == .amp { + // Handle &Type(x), as well as &&Type(x) etc: + mut new_cast_type := right.typ.to_ptr() return ast.CastExpr{ ...right + typ: new_cast_type pos: pos.extend(right.pos) } } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 6a8ba0a484..0cddbf0495 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -2070,10 +2070,6 @@ pub fn (mut p Parser) name_expr() ast.Expr { // TODO handle C.stat() start_pos := p.tok.position() mut to_typ := p.parse_type() - if p.is_amp { - // 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*