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

checker: disallow &nil (#17283)

This commit is contained in:
Felipe Pena 2023-02-13 09:24:11 -03:00 committed by GitHub
parent 04e00a46d4
commit fbcb1f3711
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 0 deletions

View File

@ -3643,6 +3643,9 @@ fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type {
c.inside_ref_lit = old_inside_ref_lit c.inside_ref_lit = old_inside_ref_lit
node.right_type = right_type node.right_type = right_type
if node.op == .amp { if node.op == .amp {
if node.right is ast.Nil {
c.error('invalid operation: cannot take address of nil', node.right.pos())
}
if mut node.right is ast.PrefixExpr { if mut node.right is ast.PrefixExpr {
if node.right.op == .amp { if node.right.op == .amp {
c.error('unexpected `&`, expecting expression', node.right.pos) c.error('unexpected `&`, expecting expression', node.right.pos)

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/invalid_nilref_err.vv:2:19: error: invalid operation: cannot take address of nil
1 | fn main() {
2 | var := unsafe { &nil }
| ~~~
3 | println(var)
4 | }

View File

@ -0,0 +1,4 @@
fn main() {
var := unsafe { &nil }
println(var)
}