Skip to content

Commit d26a863

Browse files
authored
parser: add error for map incomplete declaration (#27956)
1 parent f20fe19 commit d26a863

19 files changed

Lines changed: 101 additions & 2 deletions

vlib/v/parser/containers.v

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ fn (mut p Parser) parse_fixed_array_literal_elem_type() ast.Type {
3535
p.chan_type_error()
3636
return 0
3737
}
38+
if elem_type.idx() == ast.map_type_idx {
39+
p.map_type_error()
40+
return 0
41+
}
3842
return elem_type
3943
}
4044

@@ -187,6 +191,10 @@ fn (mut p Parser) array_init(is_option bool, alias_array_type ast.Type) ast.Arra
187191
has_type = true
188192
} else {
189193
elem_type = p.parse_type()
194+
if elem_type.idx() == ast.map_type_idx {
195+
p.map_type_error()
196+
return ast.ArrayInit{}
197+
}
190198
// this is set here because it's a known type, others could be the
191199
// result of expr so we do those in checker
192200
if elem_type != 0 {

vlib/v/parser/messages.v

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,7 @@ fn (mut p Parser) chan_type_error() {
255255
p.error_with_pos('`chan` has no type specified. Use `chan Type` instead of `chan`',
256256
p.prev_tok.pos())
257257
}
258+
259+
fn (mut p Parser) map_type_error() {
260+
p.error_with_pos('cannot use the map type without key and value definition', p.prev_tok.pos())
261+
}

vlib/v/parser/parse_type.v

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ fn (mut p Parser) parse_array_type(expecting token.Kind, is_option bool) ast.Typ
145145
p.chan_type_error()
146146
return 0
147147
}
148+
if elem_type.idx() == ast.map_type_idx {
149+
p.map_type_error()
150+
return 0
151+
}
148152
// has been explicitly resolved, but size is 0
149153
if fixed_size <= 0 && !size_unresolved {
150154
p.error_with_pos('fixed size cannot be zero or negative', size_expr.pos())
@@ -172,6 +176,10 @@ fn (mut p Parser) parse_array_type(expecting token.Kind, is_option bool) ast.Typ
172176
p.chan_type_error()
173177
return 0
174178
}
179+
if elem_type.idx() == ast.map_type_idx {
180+
p.map_type_error()
181+
return 0
182+
}
175183
if elem_type.idx() == ast.thread_type_idx {
176184
p.register_auto_import('sync.threads')
177185
}
@@ -190,8 +198,7 @@ fn (mut p Parser) parse_map_type() ast.Type {
190198
p.next()
191199
if p.tok.kind != .lsbr {
192200
if p.inside_struct_field_decl {
193-
p.error_with_pos('cannot use the map type without key and value definition',
194-
p.prev_tok.pos())
201+
p.map_type_error()
195202
return 0
196203
}
197204
return ast.map_type
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
vlib/v/parser/tests/array_init_map_elem_type_err.vv:2:9: error: cannot use the map type without key and value definition
2+
1 | fn main() {
3+
2 | a := []map{}
4+
| ~~~
5+
3 | println(a)
6+
4 | }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
a := []map{}
3+
println(a)
4+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
vlib/v/parser/tests/array_init_option_map_elem_type_err.vv:2:10: error: cannot use the map type without key and value definition
2+
1 | fn main() {
3+
2 | a := []?map{}
4+
| ~~~
5+
3 | println(a)
6+
4 | }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
a := []?map{}
3+
println(a)
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
vlib/v/parser/tests/array_map_elem_type_err.vv:1:14: error: cannot use the map type without key and value definition
2+
1 | type Arr = []map
3+
| ~~~
4+
2 |
5+
3 | fn main() {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type Arr = []map
2+
3+
fn main() {
4+
_ := Arr{}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
vlib/v/parser/tests/array_option_map_elem_type_err.vv:1:15: error: cannot use the map type without key and value definition
2+
1 | type Arr = []?map
3+
| ~~~
4+
2 |
5+
3 | fn main() {

0 commit comments

Comments
 (0)