From 58cee6ccc8f307814a61790a889984e334793c5e Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Mon, 14 Nov 2022 20:18:00 +0530 Subject: [PATCH] checker: disallow none type in struct fields (#16425) --- vlib/v/checker/struct.v | 4 ++++ vlib/v/checker/tests/struct_none_field_type_err.out | 6 ++++++ vlib/v/checker/tests/struct_none_field_type_err.vv | 12 ++++++++++++ 3 files changed, 22 insertions(+) create mode 100644 vlib/v/checker/tests/struct_none_field_type_err.out create mode 100644 vlib/v/checker/tests/struct_none_field_type_err.vv diff --git a/vlib/v/checker/struct.v b/vlib/v/checker/struct.v index 1128fc8edd..e3cda72f16 100644 --- a/vlib/v/checker/struct.v +++ b/vlib/v/checker/struct.v @@ -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) } + if sym.kind == .none_ { + c.error('cannot use `none` as field type', field.type_pos) + } + if field.has_default_expr { c.expected_type = field.typ default_expr_type := c.expr(field.default_expr) diff --git a/vlib/v/checker/tests/struct_none_field_type_err.out b/vlib/v/checker/tests/struct_none_field_type_err.out new file mode 100644 index 0000000000..39e7872e04 --- /dev/null +++ b/vlib/v/checker/tests/struct_none_field_type_err.out @@ -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 | diff --git a/vlib/v/checker/tests/struct_none_field_type_err.vv b/vlib/v/checker/tests/struct_none_field_type_err.vv new file mode 100644 index 0000000000..a7c7f281d5 --- /dev/null +++ b/vlib/v/checker/tests/struct_none_field_type_err.vv @@ -0,0 +1,12 @@ +struct Jaca { + lala none +} + +fn f() { + mut s := Jaca{ + lala: none + } + println(s) +} + +f()