diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 091d31082d..f5680cdd9c 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -492,7 +492,13 @@ pub fn (mut c Checker) sum_type_decl(node ast.SumTypeDecl) { c.error('sum type cannot hold an interface', variant.pos) } else if sym.kind == .struct_ && sym.language == .js { c.error('sum type cannot hold an JS struct', variant.pos) + } else if mut sym.info is ast.Struct { + if sym.info.is_generic && !variant.typ.has_flag(.generic) { + c.error('generic struct `$sym.name` must specify generic type names, e.g. Foo', + variant.pos) + } } + if sym.name.trim_string_left(sym.mod + '.') == node.name { c.error('sum type cannot hold itself', variant.pos) } diff --git a/vlib/v/checker/tests/generic_sumtype_decl_err.out b/vlib/v/checker/tests/generic_sumtype_decl_err.out new file mode 100644 index 0000000000..7d3ce8f664 --- /dev/null +++ b/vlib/v/checker/tests/generic_sumtype_decl_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/generic_sumtype_decl_err.vv:7:24: error: generic struct `Just` must specify generic type names, e.g. Foo + 5 | struct Nothing {} + 6 | + 7 | type Maybe = Nothing | Just + | ~~~~ + 8 | + 9 | fn main() { diff --git a/vlib/v/checker/tests/generic_sumtype_decl_err.vv b/vlib/v/checker/tests/generic_sumtype_decl_err.vv new file mode 100644 index 0000000000..3c606bf4f9 --- /dev/null +++ b/vlib/v/checker/tests/generic_sumtype_decl_err.vv @@ -0,0 +1,10 @@ +struct Just { + value T +} + +struct Nothing {} + +type Maybe = Nothing | Just + +fn main() { +}