parser: cleanup `&Type(x)` more, add fmt regression test

pull/10787/head
Delyan Angelov 2021-07-13 13:12:37 +03:00
parent 5089eb4a84
commit bd0653abab
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
3 changed files with 34 additions and 5 deletions

View File

@ -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))
}
}

View File

@ -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)
}
}

View File

@ -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*