diff --git a/vlib/v/checker/tests/pointer_arithmetic_should_be_checked.out b/vlib/v/checker/tests/pointer_arithmetic_should_be_checked.out new file mode 100644 index 0000000000..ab7d9c8fe0 --- /dev/null +++ b/vlib/v/checker/tests/pointer_arithmetic_should_be_checked.out @@ -0,0 +1,34 @@ +vlib/v/checker/tests/pointer_arithmetic_should_be_checked.v:4:6: error: pointer arithmetic is only allowed in `unsafe` blocks + 2 | v := 5 + 3 | mut p := &v + 4 | p++ + | ~~ + 5 | p += 2 + 6 | _ := v +vlib/v/checker/tests/pointer_arithmetic_should_be_checked.v:5:7: error: pointer arithmetic is only allowed in `unsafe` blocks + 3 | mut p := &v + 4 | p++ + 5 | p += 2 + | ~~ + 6 | _ := v + 7 | } +vlib/v/checker/tests/pointer_arithmetic_should_be_checked.v:11:14: error: pointer arithmetic is only allowed in `unsafe` blocks + 9 | fn test_ptr_infix() { + 10 | v := 4 + 11 | mut q := &v - 1 + | ^ + 12 | q = q + 3 + 13 | _ := q +vlib/v/checker/tests/pointer_arithmetic_should_be_checked.v:12:9: error: pointer arithmetic is only allowed in `unsafe` blocks + 10 | v := 4 + 11 | mut q := &v - 1 + 12 | q = q + 3 + | ^ + 13 | _ := q + 14 | _ := v +vlib/v/checker/tests/pointer_arithmetic_should_be_checked.v:24:7: error: method `S1.f` must be called from an `unsafe` block + 22 | fn test_funcs() { + 23 | s := S1{} + 24 | s.f() + | ~~~ + 25 | } diff --git a/vlib/v/tests/unsafe.v b/vlib/v/checker/tests/pointer_arithmetic_should_be_checked.vv similarity index 100% rename from vlib/v/tests/unsafe.v rename to vlib/v/checker/tests/pointer_arithmetic_should_be_checked.vv diff --git a/vlib/v/tests/unsafe.out b/vlib/v/tests/unsafe.out deleted file mode 100644 index c7a93b2900..0000000000 --- a/vlib/v/tests/unsafe.out +++ /dev/null @@ -1,34 +0,0 @@ -unsafe.v:4:6: warning: pointer arithmetic is only allowed in `unsafe` blocks - 2 | v := 5 - 3 | mut p := &v - 4 | p++ - | ~~ - 5 | p += 2 - 6 | _ := v -unsafe.v:5:7: warning: pointer arithmetic is only allowed in `unsafe` blocks - 3 | mut p := &v - 4 | p++ - 5 | p += 2 - | ~~ - 6 | _ := v - 7 | } -unsafe.v:11:14: warning: pointer arithmetic is only allowed in `unsafe` blocks - 9 | fn test_ptr_infix() { - 10 | v := 4 - 11 | mut q := &v - 1 - | ^ - 12 | q = q + 3 - 13 | _ := q -unsafe.v:12:9: warning: pointer arithmetic is only allowed in `unsafe` blocks - 10 | v := 4 - 11 | mut q := &v - 1 - 12 | q = q + 3 - | ^ - 13 | _ := q - 14 | _ := v -unsafe.v:24:7: warning: method `S1.f` must be called from an `unsafe` block - 22 | fn test_funcs() { - 23 | s := S1{} - 24 | s.f() - | ~~~ - 25 | } diff --git a/vlib/v/tests/unsafe_test.v b/vlib/v/tests/unsafe_test.v new file mode 100644 index 0000000000..19443958eb --- /dev/null +++ b/vlib/v/tests/unsafe_test.v @@ -0,0 +1,31 @@ +fn test_ptr_assign() { + v := [int(5), 6, 77, 1] + mut p := &v[0] + unsafe { (*p)++ } + unsafe { p++ } // p now points to v[1] + unsafe { (*p) += 2 } + unsafe { p += 2 } // p now points to v[3] + unsafe { *p = 31 } + assert v[0] == 6 + assert v[1] == 8 + assert v[2] == 77 + assert v[3] == 31 +} + +fn test_ptr_infix() { + v := 4 + mut q := unsafe{ &v - 1 } + q = unsafe {q + 3} + _ := q + _ := v +} + +struct S1 {} + +[unsafe_fn] +fn (s S1) f(){} + +fn test_funcs() { + s := S1{} + unsafe { s.f() } +}