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

test: add tests for disallowing map/array get element/key address (#6568)

This commit is contained in:
Swastik Baranwal 2020-10-05 21:10:51 +05:30 committed by GitHub
parent 4b410534dd
commit d77669da80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 54 additions and 28 deletions

View File

@ -182,17 +182,6 @@ fn test_delete() {
println('two' in m) // => true, on Linux and Windows <-- wrong !
}
/*
fn test_ref() {
m := { 'one': 1 }
// TODO "cannot take the address of m['one']"
mut one := &m['one']
one++
println(*one)
}
*/
fn test_delete_size() {
arr := ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
mut m := map[string]int

View File

@ -471,7 +471,7 @@ pub struct IndexExpr {
pub:
pos token.Position
left Expr
index Expr // [0] or RangeExpr [start..end]
index Expr // [0], RangeExpr [start..end] or map[key]
pub mut:
left_type table.Type // array, map, fixed array
is_setter bool

View File

@ -123,15 +123,13 @@ pub fn (mut c Checker) check_files(ast_files []ast.File) {
mut has_main_fn := false
mut files_from_main_module := []&ast.File{}
for i in 0 .. ast_files.len {
unsafe {
file := &ast_files[i]
c.check(file)
if file.mod.name == 'main' {
files_from_main_module << file
has_main_mod_file = true
if c.check_file_in_main(file) {
has_main_fn = true
}
file := unsafe {&ast_files[i]}
c.check(file)
if file.mod.name == 'main' {
files_from_main_module << file
has_main_mod_file = true
if c.check_file_in_main(file) {
has_main_fn = true
}
}
}

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/array_get_element_address_err.vv:3:20: error: cannot get address of array elements outside unsafe blocks
1 | fn main() {
2 | arr_int := [int(23), 45, 7, 8]
3 | ele := &arr_int[1]
| ~~~
4 | println(ele)
5 | }

View File

@ -0,0 +1,5 @@
fn main() {
arr_int := [int(23), 45, 7, 8]
ele := &arr_int[1]
println(ele)
}

View File

@ -0,0 +1,14 @@
vlib/v/checker/tests/map_get_value_address_err.vv:3:12: error: cannot get address of map values outside unsafe blocks
1 | fn main() {
2 | m := {'key' : 3}
3 | a := &m['key']
| ~~~~~~~
4 | b := &{'foo': 4}['foo']
5 | println(a)
vlib/v/checker/tests/map_get_value_address_err.vv:4:21: error: cannot get address of map values outside unsafe blocks
2 | m := {'key' : 3}
3 | a := &m['key']
4 | b := &{'foo': 4}['foo']
| ~~~~~~~
5 | println(a)
6 | println(b)

View File

@ -0,0 +1,7 @@
fn main() {
m := {'key' : 3}
a := &m['key']
b := &{'foo': 4}['foo']
println(a)
println(b)
}

View File

@ -30,18 +30,13 @@ fn (s S1) f() {
fn test_funcs() {
s := S1{}
unsafe {
s.f()
}
unsafe {s.f()}
_ = C.strerror(0) // [trusted] function prototype in builtin/cfns.c.v
}
fn test_if_expr_unsafe() {
i := 4
p := if true {
unsafe {&i}
}
else {unsafe {&i}}
p := if true { unsafe {&i} } else { unsafe {&i} }
assert *p == 4
}
@ -54,3 +49,14 @@ fn test_unsafe_if_stmt() int {
}
return i
}
fn test_map_address_index() {
m := {
'one': 1
}
unsafe {
mut one := &m['one']
one++
println(*one)
}
}