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:
parent
4b410534dd
commit
d77669da80
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
7
vlib/v/checker/tests/array_get_element_address_err.out
Normal file
7
vlib/v/checker/tests/array_get_element_address_err.out
Normal 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 | }
|
5
vlib/v/checker/tests/array_get_element_address_err.vv
Normal file
5
vlib/v/checker/tests/array_get_element_address_err.vv
Normal file
@ -0,0 +1,5 @@
|
||||
fn main() {
|
||||
arr_int := [int(23), 45, 7, 8]
|
||||
ele := &arr_int[1]
|
||||
println(ele)
|
||||
}
|
14
vlib/v/checker/tests/map_get_value_address_err.out
Normal file
14
vlib/v/checker/tests/map_get_value_address_err.out
Normal 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)
|
7
vlib/v/checker/tests/map_get_value_address_err.vv
Normal file
7
vlib/v/checker/tests/map_get_value_address_err.vv
Normal file
@ -0,0 +1,7 @@
|
||||
fn main() {
|
||||
m := {'key' : 3}
|
||||
a := &m['key']
|
||||
b := &{'foo': 4}['foo']
|
||||
println(a)
|
||||
println(b)
|
||||
}
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user