From c10c8ff9e40c291eca0a86902de0812bb2d5098e Mon Sep 17 00:00:00 2001 From: shove Date: Fri, 26 Aug 2022 12:01:50 +0800 Subject: [PATCH] parser: fix anon struct name conflict (#15517) --- vlib/v/ast/table.v | 2 ++ vlib/v/parser/parser.v | 1 - vlib/v/parser/struct.v | 4 ++-- vlib/v/parser/v_parser_test.v | 4 ++++ 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index 98009ea522..d1775488f0 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -47,6 +47,8 @@ pub mut: // cache for type_to_str_using_aliases cached_type_to_str map[u64]string anon_struct_names map[string]int // anon struct name -> struct sym idx + // counter for anon struct, avoid name conflicts. + anon_struct_counter int } // used by vls to avoid leaks diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 9e9452c1e5..f446bf49ca 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -91,7 +91,6 @@ mut: if_cond_comments []ast.Comment script_mode bool script_mode_start_token token.Token - anon_struct_counter int pub mut: scanner &scanner.Scanner errors []errors.Error diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index b8069ac0d7..8617a400b1 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -41,8 +41,8 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl { return ast.StructDecl{} } mut name := if is_anon { - p.anon_struct_counter++ - '_VAnonStruct$p.anon_struct_counter' + p.table.anon_struct_counter++ + '_VAnonStruct$p.table.anon_struct_counter' } else { p.check_name() } diff --git a/vlib/v/parser/v_parser_test.v b/vlib/v/parser/v_parser_test.v index 8c660add1e..1fc31d2cbb 100644 --- a/vlib/v/parser/v_parser_test.v +++ b/vlib/v/parser/v_parser_test.v @@ -261,3 +261,7 @@ fn test_fn_is_html_open_tag() { b = is_html_open_tag('style', s) assert b == false } + +// For issue #15516 +fn test_anon_struct() { +}