1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: assure explicit map init contains no parameters (#8299)

This commit is contained in:
Uwe Krüger 2021-01-24 00:06:43 +01:00 committed by GitHub
parent ae1c7de604
commit 79b4b0e6c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 40 additions and 2 deletions

View File

@ -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)
}

View File

@ -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

View File

@ -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 | }

View File

@ -0,0 +1,4 @@
fn main() {
a := map[string]int{}
b := map[string]f64{cap: 10}
}

View File

@ -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 | }

View File

@ -0,0 +1,3 @@
fn main() {
m := map[string]{}
}

View File

@ -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 | }

View File

@ -0,0 +1,7 @@
fn f(m map[string]) {
println('illegal function')
}
fn main() {
println('Hello world')
}