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

ci: allow unsafe { a := nil }, add separate nil case in pointer_ops.vv, to test the nil checker errors independently from the voidptr ones

This commit is contained in:
Delyan Angelov 2022-07-21 22:52:38 +03:00
parent 697d546d46
commit 474033c055
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
3 changed files with 115 additions and 53 deletions

View File

@ -220,9 +220,14 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
c.error('invalid use of reserved type `$left.name` as a variable name', c.error('invalid use of reserved type `$left.name` as a variable name',
left.pos) left.pos)
} }
if right is ast.Nil { if right is ast.Nil && !c.inside_unsafe {
// `x := unsafe { nil }` is allowed // `x := unsafe { nil }` is allowed,
c.error('use of untyped nil in assignment (use `unsafe`)', // as well as:
// `unsafe {
// x := nil
// println(x)
// }`
c.error('use of untyped nil in assignment (use `unsafe` | $c.inside_unsafe)',
right.pos()) right.pos())
} }
} }

View File

@ -1,56 +1,98 @@
vlib/v/checker/tests/pointer_ops.vv:5:7: error: `+` cannot be used with `voidptr` vlib/v/checker/tests/pointer_ops.vv:7:7: error: `+` cannot be used with `voidptr`
3 | unsafe { 5 | unsafe {
4 | mut p := voidptr(0) 6 | mut p := voidptr(u64(0))
5 | _ = p + 1 7 | _ = p + 1
| ^ | ^
6 | p++ 8 | p++
7 | p += 3 9 | p += 3
vlib/v/checker/tests/pointer_ops.vv:6:4: error: invalid operation: ++ (non-numeric type `voidptr`) vlib/v/checker/tests/pointer_ops.vv:8:4: error: invalid operation: ++ (non-numeric type `voidptr`)
4 | mut p := voidptr(0) 6 | mut p := voidptr(u64(0))
5 | _ = p + 1 7 | _ = p + 1
6 | p++ 8 | p++
| ~~ | ~~
7 | p += 3 9 | p += 3
8 | _ = p - 1 10 | _ = p - 1
vlib/v/checker/tests/pointer_ops.vv:7:3: error: operator `+=` not defined on left operand type `voidptr` vlib/v/checker/tests/pointer_ops.vv:9:3: error: operator `+=` not defined on left operand type `voidptr`
5 | _ = p + 1 7 | _ = p + 1
6 | p++ 8 | p++
7 | p += 3 9 | p += 3
| ^ | ^
8 | _ = p - 1 10 | _ = p - 1
9 | p-- 11 | p--
vlib/v/checker/tests/pointer_ops.vv:8:7: error: `-` cannot be used with `voidptr` vlib/v/checker/tests/pointer_ops.vv:10:7: error: `-` cannot be used with `voidptr`
6 | p++ 8 | p++
7 | p += 3 9 | p += 3
8 | _ = p - 1 10 | _ = p - 1
| ^ | ^
9 | p-- 11 | p--
10 | p -= 3 12 | p -= 3
vlib/v/checker/tests/pointer_ops.vv:9:4: error: invalid operation: -- (non-numeric type `voidptr`) vlib/v/checker/tests/pointer_ops.vv:11:4: error: invalid operation: -- (non-numeric type `voidptr`)
7 | p += 3 9 | p += 3
8 | _ = p - 1 10 | _ = p - 1
9 | p-- 11 | p--
| ~~ | ~~
10 | p -= 3 12 | p -= 3
11 | _ = p[3] 13 | _ = p[3]
vlib/v/checker/tests/pointer_ops.vv:10:3: error: operator `-=` not defined on left operand type `voidptr` vlib/v/checker/tests/pointer_ops.vv:12:3: error: operator `-=` not defined on left operand type `voidptr`
8 | _ = p - 1 10 | _ = p - 1
9 | p-- 11 | p--
10 | p -= 3 12 | p -= 3
| ^ | ^
11 | _ = p[3] 13 | _ = p[3]
12 | mut foo := &Foo{} 14 | mut foo := &Foo{}
vlib/v/checker/tests/pointer_ops.vv:11:8: error: type `voidptr` does not support indexing vlib/v/checker/tests/pointer_ops.vv:13:8: error: type `voidptr` does not support indexing
9 | p-- 11 | p--
10 | p -= 3 12 | p -= 3
11 | _ = p[3] 13 | _ = p[3]
| ~~~ | ~~~
12 | mut foo := &Foo{} 14 | mut foo := &Foo{}
13 | foo % 3 15 | foo % 3
vlib/v/checker/tests/pointer_ops.vv:13:3: error: invalid operator `%` to `&Foo` and `int literal` vlib/v/checker/tests/pointer_ops.vv:15:3: error: invalid operator `%` to `&Foo` and `int literal`
11 | _ = p[3] 13 | _ = p[3]
12 | mut foo := &Foo{} 14 | mut foo := &Foo{}
13 | foo % 3 15 | foo % 3
| ~~~~~~~ | ~~~~~~~
14 | } 16 | }
15 | } 17 | }
vlib/v/checker/tests/pointer_ops.vv:23:4: error: invalid operation: ++ (non-numeric type `voidptr`)
21 | mut p := nil
22 | _ = p + 1
23 | p++
| ~~
24 | p += 3
25 | _ = p - 1
vlib/v/checker/tests/pointer_ops.vv:24:3: error: operator `+=` not defined on left operand type `nil`
22 | _ = p + 1
23 | p++
24 | p += 3
| ^
25 | _ = p - 1
26 | p--
vlib/v/checker/tests/pointer_ops.vv:26:4: error: invalid operation: -- (non-numeric type `voidptr`)
24 | p += 3
25 | _ = p - 1
26 | p--
| ~~
27 | p -= 3
28 | _ = p[3]
vlib/v/checker/tests/pointer_ops.vv:27:3: error: operator `-=` not defined on left operand type `nil`
25 | _ = p - 1
26 | p--
27 | p -= 3
| ^
28 | _ = p[3]
29 | mut foo := &Foo{}
vlib/v/checker/tests/pointer_ops.vv:28:8: error: type `nil` does not support indexing
26 | p--
27 | p -= 3
28 | _ = p[3]
| ~~~
29 | mut foo := &Foo{}
30 | foo % 3
vlib/v/checker/tests/pointer_ops.vv:30:3: error: invalid operator `%` to `&Foo` and `int literal`
28 | _ = p[3]
29 | mut foo := &Foo{}
30 | foo % 3
| ~~~~~~~
31 | }
32 | }

View File

@ -1,5 +1,22 @@
struct Foo {}
// void* arithmetic is not allowed in MSVC, so ban it // void* arithmetic is not allowed in MSVC, so ban it
fn test_voidptr() { fn test_voidptr() {
unsafe {
mut p := voidptr(u64(0))
_ = p + 1
p++
p += 3
_ = p - 1
p--
p -= 3
_ = p[3]
mut foo := &Foo{}
foo % 3
}
}
fn test_nil() {
unsafe { unsafe {
mut p := nil mut p := nil
_ = p + 1 _ = p + 1
@ -13,5 +30,3 @@ fn test_voidptr() {
foo % 3 foo % 3
} }
} }
struct Foo {}