From 79b4b0e6c87c3caf9871322e9a9bedd5c2bba701 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kr=C3=BCger?= <45282134+UweKrueger@users.noreply.github.com> Date: Sun, 24 Jan 2021 00:06:43 +0100 Subject: [PATCH] parser: assure explicit `map` init contains no parameters (#8299) --- vlib/v/parser/parse_type.v | 4 ++++ vlib/v/parser/parser.v | 8 ++++++-- vlib/v/parser/tests/map_init.out | 6 ++++++ vlib/v/parser/tests/map_init.vv | 4 ++++ vlib/v/parser/tests/map_init_void.out | 5 +++++ vlib/v/parser/tests/map_init_void.vv | 3 +++ vlib/v/parser/tests/map_init_void2.out | 5 +++++ vlib/v/parser/tests/map_init_void2.vv | 7 +++++++ 8 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 vlib/v/parser/tests/map_init.out create mode 100644 vlib/v/parser/tests/map_init.vv create mode 100644 vlib/v/parser/tests/map_init_void.out create mode 100644 vlib/v/parser/tests/map_init_void.vv create mode 100644 vlib/v/parser/tests/map_init_void2.out create mode 100644 vlib/v/parser/tests/map_init_void2.vv diff --git a/vlib/v/parser/parse_type.v b/vlib/v/parser/parse_type.v index 59b045f866..fd2338f782 100644 --- a/vlib/v/parser/parse_type.v +++ b/vlib/v/parser/parse_type.v @@ -90,6 +90,10 @@ pub fn (mut p Parser) parse_map_type() table.Type { // error is reported in parse_type return 0 } + if value_type.idx() == table.void_type_idx { + p.error_with_pos('map value type cannot be void', p.tok.position()) + return 0 + } idx := p.table.find_or_register_map(key_type, value_type) return table.new_type(idx) } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index a8c93cd484..e8cbcfe2c0 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1140,9 +1140,13 @@ pub fn (mut p Parser) name_expr() ast.Expr { // `map[string]int` initialization if p.tok.lit == 'map' && p.peek_tok.kind == .lsbr { map_type := p.parse_map_type() - if p.tok.kind == .lcbr && p.peek_tok.kind == .rcbr { - p.next() + if p.tok.kind == .lcbr { p.next() + if p.tok.kind == .rcbr { + p.next() + } else { + p.error('`}` expected; explicit `map` initialization does not support parameters') + } } return ast.MapInit{ typ: map_type diff --git a/vlib/v/parser/tests/map_init.out b/vlib/v/parser/tests/map_init.out new file mode 100644 index 0000000000..71852660c1 --- /dev/null +++ b/vlib/v/parser/tests/map_init.out @@ -0,0 +1,6 @@ +vlib/v/parser/tests/map_init.vv:3:22: error: `}` expected; explicit `map` initialization does not support parameters + 1 | fn main() { + 2 | a := map[string]int{} + 3 | b := map[string]f64{cap: 10} + | ~~~ + 4 | } diff --git a/vlib/v/parser/tests/map_init.vv b/vlib/v/parser/tests/map_init.vv new file mode 100644 index 0000000000..2ab972d113 --- /dev/null +++ b/vlib/v/parser/tests/map_init.vv @@ -0,0 +1,4 @@ +fn main() { + a := map[string]int{} + b := map[string]f64{cap: 10} +} diff --git a/vlib/v/parser/tests/map_init_void.out b/vlib/v/parser/tests/map_init_void.out new file mode 100644 index 0000000000..fd76d7ee0f --- /dev/null +++ b/vlib/v/parser/tests/map_init_void.out @@ -0,0 +1,5 @@ +vlib/v/parser/tests/map_init_void.vv:2:18: error: map value type cannot be void + 1 | fn main() { + 2 | m := map[string]{} + | ^ + 3 | } diff --git a/vlib/v/parser/tests/map_init_void.vv b/vlib/v/parser/tests/map_init_void.vv new file mode 100644 index 0000000000..5ad2727fb2 --- /dev/null +++ b/vlib/v/parser/tests/map_init_void.vv @@ -0,0 +1,3 @@ +fn main() { + m := map[string]{} +} diff --git a/vlib/v/parser/tests/map_init_void2.out b/vlib/v/parser/tests/map_init_void2.out new file mode 100644 index 0000000000..81031dc5e5 --- /dev/null +++ b/vlib/v/parser/tests/map_init_void2.out @@ -0,0 +1,5 @@ +vlib/v/parser/tests/map_init_void2.vv:1:19: error: expecting type declaration + 1 | fn f(m map[string]) { + | ^ + 2 | println('illegal function') + 3 | } diff --git a/vlib/v/parser/tests/map_init_void2.vv b/vlib/v/parser/tests/map_init_void2.vv new file mode 100644 index 0000000000..0b9a8e43a0 --- /dev/null +++ b/vlib/v/parser/tests/map_init_void2.vv @@ -0,0 +1,7 @@ +fn f(m map[string]) { + println('illegal function') +} + +fn main() { + println('Hello world') +}