mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
This commit is contained in:
parent
6db5781d53
commit
ca36284612
@ -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.typ.share().str() + ' ')
|
||||||
}
|
}
|
||||||
f.write_string(arg.name)
|
f.write_string(arg.name)
|
||||||
mut s := t.type_to_str(arg.typ.clear_flag(.shared_f))
|
arg_sym := t.sym(arg.typ)
|
||||||
if arg.is_mut {
|
if arg_sym.kind == .struct_ && (arg_sym.info as Struct).is_anon {
|
||||||
arg_sym := t.sym(arg.typ)
|
f.write_string(' struct {')
|
||||||
if s.starts_with('&') && ((!arg_sym.is_number() && arg_sym.kind != .bool)
|
struct_ := arg_sym.info as Struct
|
||||||
|| node.language != .v) {
|
for field in struct_.fields {
|
||||||
s = s[1..]
|
f.write_string(' $field.name ${t.type_to_str(field.typ)}')
|
||||||
|
if field.has_default_expr {
|
||||||
|
f.write_string(' = $field.default_expr')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
if struct_.fields.len > 0 {
|
||||||
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(' ')
|
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 {
|
if !is_last_arg {
|
||||||
f.write_string(', ')
|
f.write_string(', ')
|
||||||
|
@ -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 {
|
if node.pos.line_nr < node.pos.last_line || node.pre_comments.len > 0 {
|
||||||
single_line_fields = false
|
single_line_fields = false
|
||||||
}
|
}
|
||||||
if !use_short_args {
|
if !use_short_args || node.is_anon {
|
||||||
f.write('$name{')
|
f.write('$name{')
|
||||||
f.mark_import_as_used(name)
|
f.mark_import_as_used(name)
|
||||||
if single_line_fields {
|
if single_line_fields {
|
||||||
@ -315,7 +315,7 @@ pub fn (mut f Fmt) struct_init(node ast.StructInit) {
|
|||||||
if !single_line_fields {
|
if !single_line_fields {
|
||||||
f.indent--
|
f.indent--
|
||||||
}
|
}
|
||||||
if !use_short_args {
|
if !use_short_args || node.is_anon {
|
||||||
if single_line_fields {
|
if single_line_fields {
|
||||||
f.write(' ')
|
f.write(' ')
|
||||||
}
|
}
|
||||||
|
9
vlib/v/fmt/tests/anon_struct_as_param_expected.vv
Normal file
9
vlib/v/fmt/tests/anon_struct_as_param_expected.vv
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
fn func(arg struct { foo string = 'bar' }) {
|
||||||
|
println(arg.foo)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
func(struct {
|
||||||
|
foo: 'foo'
|
||||||
|
})
|
||||||
|
}
|
8
vlib/v/fmt/tests/anon_struct_as_param_input.vv
Normal file
8
vlib/v/fmt/tests/anon_struct_as_param_input.vv
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
fn func(arg struct{foo string='bar'}) {
|
||||||
|
println(arg.foo)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
func(struct {foo: 'foo'
|
||||||
|
})
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user