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

checker: disallow none type in struct fields (#16425)

This commit is contained in:
Swastik Baranwal 2022-11-14 20:18:00 +05:30 committed by GitHub
parent 91ecfb917c
commit 58cee6ccc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 0 deletions

View File

@ -84,6 +84,10 @@ pub fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
c.error('cannot use multi return as field type', field.type_pos) c.error('cannot use multi return as field type', field.type_pos)
} }
if sym.kind == .none_ {
c.error('cannot use `none` as field type', field.type_pos)
}
if field.has_default_expr { if field.has_default_expr {
c.expected_type = field.typ c.expected_type = field.typ
default_expr_type := c.expr(field.default_expr) default_expr_type := c.expr(field.default_expr)

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/struct_none_field_type_err.vv:2:7: error: cannot use `none` as field type
1 | struct Jaca {
2 | lala none
| ~~~~
3 | }
4 |

View File

@ -0,0 +1,12 @@
struct Jaca {
lala none
}
fn f() {
mut s := Jaca{
lala: none
}
println(s)
}
f()