From 61c1c4a690a57bfb2300fc9a6f411a1312cb5941 Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Tue, 28 Jul 2020 01:04:04 +1000 Subject: [PATCH] checker: add error for invalid sum type cast `MySt{1}` --- vlib/v/checker/checker.v | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index adb0c7950e..a92bf1505b 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -358,6 +358,11 @@ pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) table.Type { struct_init.typ = c.expected_type } type_sym := c.table.get_type_symbol(struct_init.typ) + if type_sym.kind == .sum_type && struct_init.fields.len == 1 { + sexpr := struct_init.fields[0].expr.str() + c.error('cast to sum type using `${type_sym.name}($sexpr)` not `$type_sym.name{$sexpr}`', + struct_init.pos) + } if type_sym.kind == .interface_ { c.error('cannot instantiate interface `$type_sym.name`', struct_init.pos) }