parser: support casting to a pointer to array (#7861)
parent
22085041f1
commit
b9c6011602
|
@ -831,10 +831,9 @@ pub fn (mut f Fmt) enum_decl(node ast.EnumDecl) {
|
|||
|
||||
pub fn (mut f Fmt) prefix_expr_cast_expr(fexpr ast.Expr) {
|
||||
mut is_pe_amp_ce := false
|
||||
mut ce := ast.CastExpr{}
|
||||
if fexpr is ast.PrefixExpr {
|
||||
if fexpr.right is ast.CastExpr && fexpr.op == .amp {
|
||||
ce = fexpr.right as ast.CastExpr
|
||||
mut ce := fexpr.right as ast.CastExpr
|
||||
ce.typname = f.table.get_type_symbol(ce.typ).name
|
||||
is_pe_amp_ce = true
|
||||
f.expr(ce)
|
||||
|
|
|
@ -1,19 +1,15 @@
|
|||
const (
|
||||
x = &Test{}
|
||||
y = []&Test{}
|
||||
z = &[]&Test{}
|
||||
xx = &&Test{}
|
||||
yy = []&&Test{}
|
||||
zz = &&[]&&Test{}
|
||||
)
|
||||
|
||||
fn test_type_ptr() {
|
||||
_ := &Test{}
|
||||
_ := []&Test{}
|
||||
_ := &[]&Test{}
|
||||
_ := &&Test{}
|
||||
_ := []&&Test{}
|
||||
_ := &&[]&&Test{}
|
||||
}
|
||||
|
||||
struct Test {
|
||||
|
|
|
@ -127,6 +127,17 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr {
|
|||
if p.expecting_type {
|
||||
// parse json.decode type (`json.decode([]User, s)`)
|
||||
node = p.name_expr()
|
||||
} else if p.is_amp && p.peek_tok.kind == .rsbr {
|
||||
pos := p.tok.position()
|
||||
typ := p.parse_type().to_ptr()
|
||||
p.check(.lpar)
|
||||
expr := p.expr(0)
|
||||
p.check(.rpar)
|
||||
node = ast.CastExpr{
|
||||
typ: typ
|
||||
expr: expr
|
||||
pos: pos
|
||||
}
|
||||
} else {
|
||||
node = p.array_init()
|
||||
}
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
fn test_array_cast() {
|
||||
mut keys := ['']
|
||||
unsafe {
|
||||
vp := voidptr(&keys)
|
||||
mut p := &[]string(vp)
|
||||
(*p)[0] = 'hi'
|
||||
assert *p == ['hi']
|
||||
}
|
||||
assert keys[0] == 'hi'
|
||||
}
|
||||
|
||||
fn test_int() {
|
||||
mut arr := [2.3,3]
|
||||
unsafe {
|
||||
vp := voidptr(&arr)
|
||||
p := &[]f64(vp)
|
||||
assert *p == arr
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue