From c53d859fbd4bb94c8609623da9ef3668711a753b Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 14 Apr 2026 12:45:26 +0300 Subject: [PATCH] parser: All fields within a structure param should be public by default (fixes #24234) --- vlib/v/parser/struct.v | 4 +++- .../anon_struct_param_public_fields_test.v | 7 +++++++ .../modules/anon_struct_param_public_fields/foo/foo.v | 5 +++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/modules/anon_struct_param_public_fields/anon_struct_param_public_fields_test.v create mode 100644 vlib/v/tests/modules/anon_struct_param_public_fields/foo/foo.v diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index 765b94db8..0ed7d35cc 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -118,7 +118,9 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl { mut global_pos := -1 mut module_pos := -1 mut is_field_mut := language == .c - mut is_field_pub := language == .c + // Anonymous struct parameter fields are part of the function's call surface, + // so callers in other modules must be able to initialize them. + mut is_field_pub := language == .c || (is_anon && p.inside_fn_param) mut is_field_global := false mut is_implements := false mut implements_types := []ast.TypeNode{cap: 3} // ast.void_type diff --git a/vlib/v/tests/modules/anon_struct_param_public_fields/anon_struct_param_public_fields_test.v b/vlib/v/tests/modules/anon_struct_param_public_fields/anon_struct_param_public_fields_test.v new file mode 100644 index 000000000..45717def7 --- /dev/null +++ b/vlib/v/tests/modules/anon_struct_param_public_fields/anon_struct_param_public_fields_test.v @@ -0,0 +1,7 @@ +module main + +import anon_struct_param_public_fields.foo + +fn test_anon_struct_parameter_fields_are_public() { + assert foo.bar(name: 'Foo!') == 'Foo!' +} diff --git a/vlib/v/tests/modules/anon_struct_param_public_fields/foo/foo.v b/vlib/v/tests/modules/anon_struct_param_public_fields/foo/foo.v new file mode 100644 index 000000000..7a982c0b7 --- /dev/null +++ b/vlib/v/tests/modules/anon_struct_param_public_fields/foo/foo.v @@ -0,0 +1,5 @@ +module foo + +pub fn bar(params struct { name string }) string { + return params.name +} -- 2.39.5