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

fmt: fix anonymous struct in parameter with invalid type name. fix #15696 (#15711)

This commit is contained in:
shove 2022-09-09 19:44:49 +08:00 committed by GitHub
parent 6db5781d53
commit ca36284612
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 18 deletions

View File

@ -137,26 +137,41 @@ fn stringify_fn_after_name(node &FnDecl, mut f strings.Builder, t &Table, cur_mo
f.write_string(arg.typ.share().str() + ' ')
}
f.write_string(arg.name)
mut s := t.type_to_str(arg.typ.clear_flag(.shared_f))
if arg.is_mut {
arg_sym := t.sym(arg.typ)
if s.starts_with('&') && ((!arg_sym.is_number() && arg_sym.kind != .bool)
|| node.language != .v) {
s = s[1..]
arg_sym := t.sym(arg.typ)
if arg_sym.kind == .struct_ && (arg_sym.info as Struct).is_anon {
f.write_string(' struct {')
struct_ := arg_sym.info as Struct
for field in struct_.fields {
f.write_string(' $field.name ${t.type_to_str(field.typ)}')
if field.has_default_expr {
f.write_string(' = $field.default_expr')
}
}
}
s = util.no_cur_mod(s, cur_mod)
for mod, alias in m2a {
s = s.replace(mod, alias)
}
if should_add_type {
if !is_type_only {
if struct_.fields.len > 0 {
f.write_string(' ')
}
if node.is_variadic && is_last_arg {
f.write_string('...')
f.write_string('}')
} else {
mut s := t.type_to_str(arg.typ.clear_flag(.shared_f))
if arg.is_mut {
if s.starts_with('&') && ((!arg_sym.is_number() && arg_sym.kind != .bool)
|| node.language != .v) {
s = s[1..]
}
}
s = util.no_cur_mod(s, cur_mod)
for mod, alias in m2a {
s = s.replace(mod, alias)
}
if should_add_type {
if !is_type_only {
f.write_string(' ')
}
if node.is_variadic && is_last_arg {
f.write_string('...')
}
f.write_string(s)
}
f.write_string(s)
}
if !is_last_arg {
f.write_string(', ')

View File

@ -253,7 +253,7 @@ pub fn (mut f Fmt) struct_init(node ast.StructInit) {
if node.pos.line_nr < node.pos.last_line || node.pre_comments.len > 0 {
single_line_fields = false
}
if !use_short_args {
if !use_short_args || node.is_anon {
f.write('$name{')
f.mark_import_as_used(name)
if single_line_fields {
@ -315,7 +315,7 @@ pub fn (mut f Fmt) struct_init(node ast.StructInit) {
if !single_line_fields {
f.indent--
}
if !use_short_args {
if !use_short_args || node.is_anon {
if single_line_fields {
f.write(' ')
}

View File

@ -0,0 +1,9 @@
fn func(arg struct { foo string = 'bar' }) {
println(arg.foo)
}
fn main() {
func(struct {
foo: 'foo'
})
}

View File

@ -0,0 +1,8 @@
fn func(arg struct{foo string='bar'}) {
println(arg.foo)
}
fn main() {
func(struct {foo: 'foo'
})
}