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

checker: disallow taking address of optional fields for now (#16487)

This commit is contained in:
Swastik Baranwal 2022-11-20 01:33:39 +05:30 committed by GitHub
parent 531c145ae7
commit 06764bc559
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 2 deletions

View File

@ -3544,9 +3544,13 @@ pub fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type {
&& (node.right.typ == ast.bool_type_idx || (right_sym.kind == .enum_ && (node.right.typ == ast.bool_type_idx || (right_sym.kind == .enum_
&& !(right_sym.info as ast.Enum).is_flag && !(right_sym.info as ast.Enum).is_flag
&& !(right_sym.info as ast.Enum).uses_exprs)) { && !(right_sym.info as ast.Enum).uses_exprs)) {
c.error('cannot take address of field in struct `${c.table.type_to_str(node.right.expr_type)}`, which is tagged as `[minify]`', c.error('cannot take the address of field in struct `${c.table.type_to_str(node.right.expr_type)}`, which is tagged as `[minify]`',
node.pos.extend(node.right.pos)) node.pos.extend(node.right.pos))
} }
if node.right.typ.has_flag(.optional) {
c.error('cannot take the address of an optional field', node.pos.extend(node.right.pos))
}
} }
} }
// TODO: testing ref/deref strategy // TODO: testing ref/deref strategy
@ -3562,7 +3566,7 @@ pub fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type {
} }
if mut node.right is ast.Ident { if mut node.right is ast.Ident {
if node.right.kind == .constant && !c.inside_unsafe && c.pref.experimental { if node.right.kind == .constant && !c.inside_unsafe && c.pref.experimental {
c.warn('cannot take an address of const outside `unsafe`', node.right.pos) c.warn('cannot take the address of const outside `unsafe`', node.right.pos)
} }
} }
if node.right is ast.SelectorExpr { if node.right is ast.SelectorExpr {

View File

@ -0,0 +1,13 @@
vlib/v/checker/tests/optional_fields_addr_err.vv:9:10: error: cannot take the address of an optional field
7 | fn main() {
8 | x := Wrapper{}
9 | if _ := &x.value {
| ~~~~~~~~
10 | }
11 |
vlib/v/checker/tests/optional_fields_addr_err.vv:12:7: error: cannot take the address of an optional field
10 | }
11 |
12 | _ := &x.value
| ~~~~~~~~
13 | }

View File

@ -0,0 +1,13 @@
module main
struct Wrapper {
value ?u8
}
fn main() {
x := Wrapper{}
if _ := &x.value {
}
_ := &x.value
}