1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix short struct init with mut (#8384)

This commit is contained in:
Tim Basel 2021-01-28 09:05:09 +01:00 committed by GitHub
parent 5a1f3cd394
commit 5fc7eadd8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 0 deletions

View File

@ -1122,6 +1122,9 @@ fn (mut c Checker) fail_if_immutable(expr ast.Expr) (string, token.Position) {
ast.ArrayInit { ast.ArrayInit {
return '', pos return '', pos
} }
ast.StructInit {
return '', pos
}
else { else {
c.error('unexpected expression `$expr.type_name()`', expr.position()) c.error('unexpected expression `$expr.type_name()`', expr.position())
} }

View File

@ -4710,6 +4710,13 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) {
} else { } else {
g.write('($styp*)memdup(&($styp){') 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 { } else {
if g.is_shared { if g.is_shared {
g.writeln('{.val = {') g.writeln('{.val = {')

View File

@ -231,6 +231,7 @@ fn test_fixed_field() {
} }
*/ */
struct Config { struct Config {
mut:
n int n int
def int = 10 def int = 10
} }
@ -241,6 +242,11 @@ fn foo_config(def int, c Config) {
fn bar_config(c Config, def int) { fn bar_config(c Config, def int) {
assert c.def == def 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) {} fn foo_user(u User) {}
@ -256,6 +262,10 @@ fn test_struct_literal_args() {
bar_config({}, 10) bar_config({}, 10)
bar_config({def:4}, 4) bar_config({def:4}, 4)
c := mut_bar_config(mut {def: 10}, 10)
assert c.n == 10
assert c.def == 10
foo_user({ foo_user({
name: 'Peter' name: 'Peter'
}) })