From e2de04c1ccbab83adb610d1af59e04956a010f76 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 14 Apr 2026 12:45:30 +0300 Subject: [PATCH] parser: vfmt "breaks" invalid code with map of `[]u8` with `cap:` instead of giving error (fixes #24055) --- vlib/v/fmt/tests/maps_expected.vv | 4 ---- vlib/v/fmt/tests/maps_input.vv | 6 +----- vlib/v/parser/map_init_fmt_regression_test.v | 21 ++++++++++++++++++++ vlib/v/parser/parser.v | 5 ----- 4 files changed, 22 insertions(+), 14 deletions(-) create mode 100644 vlib/v/parser/map_init_fmt_regression_test.v diff --git a/vlib/v/fmt/tests/maps_expected.vv b/vlib/v/fmt/tests/maps_expected.vv index 78450b5f5..97ed7fad7 100644 --- a/vlib/v/fmt/tests/maps_expected.vv +++ b/vlib/v/fmt/tests/maps_expected.vv @@ -15,10 +15,6 @@ numbers := { explicit_init := map[string]string{} -explicit_init_with_value := { - 'abc': 0 -} - headers := http.new_header_from_map({ .content_type: 'application/json' .authorization: 'Bearer abcdef' diff --git a/vlib/v/fmt/tests/maps_input.vv b/vlib/v/fmt/tests/maps_input.vv index 549d0a376..f21c0d7f4 100644 --- a/vlib/v/fmt/tests/maps_input.vv +++ b/vlib/v/fmt/tests/maps_input.vv @@ -17,10 +17,6 @@ numbers := { explicit_init := map[string]string{} -explicit_init_with_value := map[string]int{ - 'abc': 0 -} - headers := http.new_header_from_map({ .content_type: 'application/json', .authorization: 'Bearer abcdef' @@ -52,4 +48,4 @@ utf8_keys := { 'В начале': 'В начале' '最初有道': '最初有道' 'לפניכם': 'לפניכם' -} \ No newline at end of file +} diff --git a/vlib/v/parser/map_init_fmt_regression_test.v b/vlib/v/parser/map_init_fmt_regression_test.v new file mode 100644 index 000000000..9bedee7a9 --- /dev/null +++ b/vlib/v/parser/map_init_fmt_regression_test.v @@ -0,0 +1,21 @@ +import v.ast +import v.parser +import v.pref + +fn test_typed_map_init_is_rejected_in_fmt_mode() { + source_text := 'module main + +const c_cap = 10 + +fn main() { + _ := map[string][]u8{cap: c_cap} +} +' + mut table := ast.new_table() + prefs := &pref.Preferences{ + is_fmt: true + output_mode: .silent + } + prog := parser.parse_text(source_text, '', mut table, .skip_comments, prefs) + assert prog.errors.any(it.message == '`}` expected; explicit `map` initialization does not support parameters') +} diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 5550c2197..fc96616db 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1597,11 +1597,6 @@ fn (mut p Parser) name_expr() ast.Expr { pos = pos.extend(p.tok.pos()) p.next() } else { - if p.pref.is_fmt { - map_init := p.map_init() - p.check(.rcbr) - return map_init - } p.error('`}` expected; explicit `map` initialization does not support parameters') } } -- 2.39.5