cgen: fix a big with &Foo{} init

pull/4801/head
Alexander Medvednikov 2020-05-09 15:23:48 +02:00
parent c64f8b0d1f
commit dea9ca2491
2 changed files with 5 additions and 14 deletions

View File

@ -838,15 +838,9 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
} }
mut return_type := table.void_type mut return_type := table.void_type
match assign_stmt.right[0] { match assign_stmt.right[0] {
ast.CallExpr { ast.CallExpr { return_type = it.return_type }
return_type = it.return_type ast.IfExpr { return_type = it.typ }
} ast.MatchExpr { return_type = it.return_type }
ast.IfExpr {
return_type = it.typ
}
ast.MatchExpr {
return_type = it.return_type
}
else {} else {}
} }
mut is_multi := false mut is_multi := false
@ -2147,6 +2141,7 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) {
sym := g.table.get_type_symbol(struct_init.typ) sym := g.table.get_type_symbol(struct_init.typ)
styp := g.typ(struct_init.typ) styp := g.typ(struct_init.typ)
is_amp := g.is_amp is_amp := g.is_amp
g.is_amp = false // reset the flag immediately so that other struct inits in this expr are handled correctly
if is_amp { if is_amp {
g.out.go_back(1) // delete the `&` already generated in `prefix_expr() g.out.go_back(1) // delete the `&` already generated in `prefix_expr()
g.write('($styp*)memdup(&($styp){') g.write('($styp*)memdup(&($styp){')

View File

@ -37,7 +37,7 @@ mut:
global_scope &ast.Scope global_scope &ast.Scope
imports map[string]string imports map[string]string
ast_imports []ast.Import ast_imports []ast.Import
is_amp bool is_amp bool // for generating the right code for `&Foo{}`
returns bool returns bool
inside_match bool // to separate `match A { }` from `Struct{}` inside_match bool // to separate `match A { }` from `Struct{}`
inside_match_case bool // to separate `match_expr { }` from `Struct{}` inside_match_case bool // to separate `match_expr { }` from `Struct{}`
@ -311,7 +311,6 @@ pub fn (mut p Parser) top_stmt() ast.Stmt {
} }
.lsbr { .lsbr {
attrs := p.attributes() attrs := p.attributes()
if attrs.len > 1 { if attrs.len > 1 {
p.error('multiple attributes detected') p.error('multiple attributes detected')
} }
@ -488,7 +487,6 @@ pub fn (mut p Parser) stmt() ast.Stmt {
fn (mut p Parser) attributes() []ast.Attr { fn (mut p Parser) attributes() []ast.Attr {
mut attrs := []ast.Attr{} mut attrs := []ast.Attr{}
p.check(.lsbr) p.check(.lsbr)
for p.tok.kind != .rsbr { for p.tok.kind != .rsbr {
attr := p.parse_attr() attr := p.parse_attr()
@ -499,12 +497,10 @@ fn (mut p Parser) attributes() []ast.Attr {
p.next() p.next()
break break
} }
p.error('unexpected `${p.tok.kind.str()}`, expecting `${expected.str()}`') p.error('unexpected `${p.tok.kind.str()}`, expecting `${expected.str()}`')
} }
p.next() p.next()
} }
return attrs return attrs
} }