parser: assure explicit `map` init contains no parameters (#8299)
parent
ae1c7de604
commit
79b4b0e6c8
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 | }
|
|
@ -0,0 +1,4 @@
|
|||
fn main() {
|
||||
a := map[string]int{}
|
||||
b := map[string]f64{cap: 10}
|
||||
}
|
|
@ -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 | }
|
|
@ -0,0 +1,3 @@
|
|||
fn main() {
|
||||
m := map[string]{}
|
||||
}
|
|
@ -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 | }
|
|
@ -0,0 +1,7 @@
|
|||
fn f(m map[string]) {
|
||||
println('illegal function')
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println('Hello world')
|
||||
}
|
Loading…
Reference in New Issue