diff --git a/vlib/v/checker/assign.v b/vlib/v/checker/assign.v index e1571617a5..7da4f9d0d7 100644 --- a/vlib/v/checker/assign.v +++ b/vlib/v/checker/assign.v @@ -54,7 +54,13 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) { } } 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 { diff --git a/vlib/v/checker/return.v b/vlib/v/checker/return.v index a1715dd1f5..c16f68c40c 100644 --- a/vlib/v/checker/return.v +++ b/vlib/v/checker/return.v @@ -42,6 +42,12 @@ pub fn (mut c Checker) return_stmt(mut node ast.Return) { if typ == 0 { 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 { c.error('`${expr}` used as value', node.pos) return diff --git a/vlib/v/checker/tests/assing_none.out b/vlib/v/checker/tests/assing_none.out index d913002ccc..767f70d293 100644 --- a/vlib/v/checker/tests/assing_none.out +++ b/vlib/v/checker/tests/assing_none.out @@ -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() { 2 | val := none | ~~~~ diff --git a/vlib/v/checker/tests/unsafe_assign_none_err.out b/vlib/v/checker/tests/unsafe_assign_none_err.out new file mode 100644 index 0000000000..099e2e72eb --- /dev/null +++ b/vlib/v/checker/tests/unsafe_assign_none_err.out @@ -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 | } diff --git a/vlib/v/checker/tests/unsafe_assign_none_err.vv b/vlib/v/checker/tests/unsafe_assign_none_err.vv new file mode 100644 index 0000000000..eda2e86924 --- /dev/null +++ b/vlib/v/checker/tests/unsafe_assign_none_err.vv @@ -0,0 +1,6 @@ +fn f() { + mut s := unsafe { none } + println(s) +} + +f() diff --git a/vlib/v/checker/tests/unsafe_none_return_err.out b/vlib/v/checker/tests/unsafe_none_return_err.out new file mode 100644 index 0000000000..9e6e3e09d8 --- /dev/null +++ b/vlib/v/checker/tests/unsafe_none_return_err.out @@ -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 | } diff --git a/vlib/v/checker/tests/unsafe_none_return_err.vv b/vlib/v/checker/tests/unsafe_none_return_err.vv new file mode 100644 index 0000000000..54bc0da99d --- /dev/null +++ b/vlib/v/checker/tests/unsafe_none_return_err.vv @@ -0,0 +1,3 @@ +fn f() ?int { + return unsafe { none } +}