From b6d6d4b037c2b138812ef462008df5ffc43e8351 Mon Sep 17 00:00:00 2001 From: shove Date: Wed, 2 Aug 2023 00:10:52 +0800 Subject: [PATCH] parser: fix improper token advancement when parsing the types of struct thread type fields(fix #19029) (#19030) --- vlib/v/parser/parse_type.v | 21 +++++++++++++------ vlib/v/tests/parse_thread_type_test.v | 30 +++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 vlib/v/tests/parse_thread_type_test.v diff --git a/vlib/v/parser/parse_type.v b/vlib/v/parser/parse_type.v index e6e0909e14..a165616197 100644 --- a/vlib/v/parser/parse_type.v +++ b/vlib/v/parser/parse_type.v @@ -182,7 +182,7 @@ fn (mut p Parser) parse_thread_type() ast.Type { if is_opt { p.next() } - if p.peek_tok.kind !in [.name, .key_mut, .amp, .lsbr] { + if p.peek_tok.kind !in [.name, .key_pub, .key_mut, .amp, .lsbr] { p.next() if is_opt { mut ret_type := ast.void_type @@ -196,12 +196,21 @@ fn (mut p Parser) parse_thread_type() ast.Type { if !is_opt { p.next() } - ret_type := p.parse_type() - idx := p.table.find_or_register_thread(ret_type) - if ret_type.has_flag(.generic) { - return ast.new_type(idx).set_flag(.generic) + if is_opt || p.tok.kind in [.amp, .lsbr] + || (p.tok.lit.len > 0 && p.tok.lit[0].is_capital()) + || ast.builtin_type_names_matcher.matches(p.tok.lit) + || p.peek_tok.kind == .dot { + mut ret_type := p.parse_type() + if is_opt { + ret_type = ret_type.set_flag(.option) + } + idx := p.table.find_or_register_thread(ret_type) + if ret_type.has_flag(.generic) { + return ast.new_type(idx).set_flag(.generic) + } + return ast.new_type(idx) } - return ast.new_type(idx) + return ast.thread_type } fn (mut p Parser) parse_multi_return_type() ast.Type { diff --git a/vlib/v/tests/parse_thread_type_test.v b/vlib/v/tests/parse_thread_type_test.v new file mode 100644 index 0000000000..9d754b02b6 --- /dev/null +++ b/vlib/v/tests/parse_thread_type_test.v @@ -0,0 +1,30 @@ +struct Foo1 { + before_1 int + thr thread + after_1 int + after_2 int + after_3 int +} + +struct Foo2 { + thr thread + a ?int +} + +struct Foo3 { + thrs []thread + a []int +} + +struct Foo4 { + thrs []thread int + a []int +} + +fn test_parse_thread_type() { + _ = &Foo1{} + _ = &Foo2{} + _ = &Foo3{} + _ = &Foo4{} + assert true +}