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:
parent
531c145ae7
commit
06764bc559
@ -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 {
|
||||||
|
13
vlib/v/checker/tests/optional_fields_addr_err.out
Normal file
13
vlib/v/checker/tests/optional_fields_addr_err.out
Normal 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 | }
|
13
vlib/v/checker/tests/optional_fields_addr_err.vv
Normal file
13
vlib/v/checker/tests/optional_fields_addr_err.vv
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
module main
|
||||||
|
|
||||||
|
struct Wrapper {
|
||||||
|
value ?u8
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
x := Wrapper{}
|
||||||
|
if _ := &x.value {
|
||||||
|
}
|
||||||
|
|
||||||
|
_ := &x.value
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user