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

checker: disallow using unsafe { none } (#16431)

This commit is contained in:
Swastik Baranwal 2022-11-15 22:25:07 +05:30 committed by GitHub
parent fe2db64384
commit dc81d755e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 34 additions and 2 deletions

View File

@ -54,7 +54,13 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
} }
} }
if mut right is ast.None { if mut right is ast.None {
c.error('you can not assign a `none` value to a variable', right.pos) c.error('cannot assign a `none` value to a variable', right.pos)
}
// Handle `left_name := unsafe { none }`
if mut right is ast.UnsafeExpr {
if mut right.expr is ast.None {
c.error('cannot use `none` in `unsafe` blocks', right.expr.pos)
}
} }
} }
if node.left.len != right_len { if node.left.len != right_len {

View File

@ -42,6 +42,12 @@ pub fn (mut c Checker) return_stmt(mut node ast.Return) {
if typ == 0 { if typ == 0 {
return return
} }
// Handle `return unsafe { none }`
if expr is ast.UnsafeExpr {
if expr.expr is ast.None {
c.error('cannot return `none` in unsafe block', expr.expr.pos)
}
}
if typ == ast.void_type { if typ == ast.void_type {
c.error('`${expr}` used as value', node.pos) c.error('`${expr}` used as value', node.pos)
return return

View File

@ -1,4 +1,4 @@
vlib/v/checker/tests/assing_none.vv:2:9: error: you can not assign a `none` value to a variable vlib/v/checker/tests/assing_none.vv:2:9: error: cannot assign a `none` value to a variable
1 | fn main() { 1 | fn main() {
2 | val := none 2 | val := none
| ~~~~ | ~~~~

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/unsafe_assign_none_err.vv:2:20: error: cannot use `none` in `unsafe` blocks
1 | fn f() {
2 | mut s := unsafe { none }
| ~~~~
3 | println(s)
4 | }

View File

@ -0,0 +1,6 @@
fn f() {
mut s := unsafe { none }
println(s)
}
f()

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/unsafe_none_return_err.vv:2:18: error: cannot return `none` in unsafe block
1 | fn f() ?int {
2 | return unsafe { none }
| ~~~~
3 | }

View File

@ -0,0 +1,3 @@
fn f() ?int {
return unsafe { none }
}