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

parser: disallow all array usage outside of builtin (#18222)

This commit is contained in:
Swastik Baranwal 2023-05-21 18:51:57 +05:30 committed by GitHub
parent abcbba1e81
commit 4b22ea7803
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 28 additions and 9 deletions

View File

@ -103,11 +103,6 @@ fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
c.warn('arrays of references need to be initialized right away, therefore `len:` cannot be used (unless inside `unsafe`)',
node.pos)
}
if node.elem_type.idx() == ast.array_type && !c.is_builtin_mod {
c.error('`array` is an internal type, it cannot be used directly. Use `[]int`, `[]Foo` etc',
node.pos)
}
return node.typ
}

View File

@ -1,4 +0,0 @@
vlib/v/checker/tests/array_decl_type_err.vv:1:10: error: `array` is an internal type, it cannot be used directly. Use `[]int`, `[]Foo` etc
1 | mut a := []array{}
| ~~~~~~~~
2 | a.insert(4, '1')

View File

@ -489,6 +489,11 @@ pub fn (mut p Parser) parse_type() ast.Type {
if is_atomic {
typ = typ.set_flag(.atomic_f)
}
if typ.idx() == ast.array_type && !p.builtin_mod && p.mod !in ['os', 'strconv', 'sync']
&& !p.inside_unsafe {
p.error_with_pos('`array` is an internal type, it cannot be used directly. Use `[]int`, `[]Foo` etc',
pos)
}
if nr_muls > 0 {
typ = typ.set_nr_muls(nr_muls)
if is_array && nr_amps > 0 {

View File

@ -0,0 +1,3 @@
vlib/v/parser/tests/array_decl_type_alias_err.vv:1:12: error: `array` is an internal type, it cannot be used directly. Use `[]int`, `[]Foo` etc
1 | type Arr = array
| ~~~~~

View File

@ -0,0 +1 @@
type Arr = array

View File

@ -0,0 +1,4 @@
vlib/v/parser/tests/array_decl_type_err.vv:1:12: error: `array` is an internal type, it cannot be used directly. Use `[]int`, `[]Foo` etc
1 | mut a := []array{}
| ~~~~~
2 | a.insert(4, '1')

View File

@ -0,0 +1,7 @@
vlib/v/parser/tests/array_decl_type_struct_field_err.vv:6:20: error: `array` is an internal type, it cannot be used directly. Use `[]int`, `[]Foo` etc
4 | max_size string
5 | filter_until_hour int = 240 // 10 days
6 | prune_list array = ['all']
| ~~~~~
7 | time_interval int = 300 // 5 min
8 | }

View File

@ -0,0 +1,8 @@
struct Config {
mut:
max_size string
filter_until_hour int = 240 // 10 days
prune_list array = ['all']
time_interval int = 300 // 5 min
}