cgen: fix short struct init with `mut` (#8384)
parent
5a1f3cd394
commit
5fc7eadd8b
|
@ -1122,6 +1122,9 @@ fn (mut c Checker) fail_if_immutable(expr ast.Expr) (string, token.Position) {
|
|||
ast.ArrayInit {
|
||||
return '', pos
|
||||
}
|
||||
ast.StructInit {
|
||||
return '', pos
|
||||
}
|
||||
else {
|
||||
c.error('unexpected expression `$expr.type_name()`', expr.position())
|
||||
}
|
||||
|
|
|
@ -4710,6 +4710,13 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) {
|
|||
} else {
|
||||
g.write('($styp*)memdup(&($styp){')
|
||||
}
|
||||
} else if struct_init.typ.is_ptr() {
|
||||
basetyp := g.typ(struct_init.typ.set_nr_muls(0))
|
||||
if is_multiline {
|
||||
g.writeln('&($basetyp){')
|
||||
} else {
|
||||
g.write('&($basetyp){')
|
||||
}
|
||||
} else {
|
||||
if g.is_shared {
|
||||
g.writeln('{.val = {')
|
||||
|
|
|
@ -231,6 +231,7 @@ fn test_fixed_field() {
|
|||
}
|
||||
*/
|
||||
struct Config {
|
||||
mut:
|
||||
n int
|
||||
def int = 10
|
||||
}
|
||||
|
@ -241,6 +242,11 @@ fn foo_config(def int, c Config) {
|
|||
fn bar_config(c Config, def int) {
|
||||
assert c.def == def
|
||||
}
|
||||
fn mut_bar_config(mut c Config, def int) &Config {
|
||||
c.n = c.def
|
||||
assert c.n == def
|
||||
return c
|
||||
}
|
||||
|
||||
fn foo_user(u User) {}
|
||||
|
||||
|
@ -256,6 +262,10 @@ fn test_struct_literal_args() {
|
|||
bar_config({}, 10)
|
||||
bar_config({def:4}, 4)
|
||||
|
||||
c := mut_bar_config(mut {def: 10}, 10)
|
||||
assert c.n == 10
|
||||
assert c.def == 10
|
||||
|
||||
foo_user({
|
||||
name: 'Peter'
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue