checker: check generic sumtype declare error (#14367)

master
yuyi 2022-05-11 22:39:58 +08:00 committed by GitHub
parent 34a252ef84
commit cd4fa041ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 0 deletions

View File

@ -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<T>',
variant.pos)
}
}
if sym.name.trim_string_left(sym.mod + '.') == node.name {
c.error('sum type cannot hold itself', variant.pos)
}

View File

@ -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<T>
5 | struct Nothing {}
6 |
7 | type Maybe = Nothing | Just
| ~~~~
8 |
9 | fn main() {

View File

@ -0,0 +1,10 @@
struct Just<T> {
value T
}
struct Nothing {}
type Maybe = Nothing | Just
fn main() {
}