From d6aa85d059ad124e58b22a74d32f310d60f5c189 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 17 May 2022 17:05:10 +0800 Subject: [PATCH] parser: fix panic for parse invalid map type (#14431) --- vlib/v/parser/parse_type.v | 4 ++-- vlib/v/tests/parse_invalid_map_type_test.v | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/parse_invalid_map_type_test.v diff --git a/vlib/v/parser/parse_type.v b/vlib/v/parser/parse_type.v index dc6f045137..cc6fd76f42 100644 --- a/vlib/v/parser/parse_type.v +++ b/vlib/v/parser/parse_type.v @@ -113,12 +113,12 @@ pub fn (mut p Parser) parse_map_type() ast.Type { } p.check(.lsbr) key_type := p.parse_type() - key_sym := p.table.sym(key_type) - is_alias := key_sym.kind == .alias if key_type.idx() == 0 { // error is reported in parse_type return 0 } + key_sym := p.table.sym(key_type) + is_alias := key_sym.kind == .alias key_type_supported := key_type in [ast.string_type_idx, ast.voidptr_type_idx] || key_sym.kind in [.enum_, .placeholder, .any] || ((key_type.is_int() || key_type.is_float() || is_alias) && !key_type.is_ptr()) diff --git a/vlib/v/tests/parse_invalid_map_type_test.v b/vlib/v/tests/parse_invalid_map_type_test.v new file mode 100644 index 0000000000..1a562db02c --- /dev/null +++ b/vlib/v/tests/parse_invalid_map_type_test.v @@ -0,0 +1,13 @@ +import v.ast +import v.parser +import v.pref + +fn test_parser_map_type() { + result := parser.parse_text('a := map[*Node]bool', '', ast.new_table(), .parse_comments, + &pref.Preferences{ + output_mode: .silent + is_fmt: true + }) + println(result) + assert true +}