checker: error, when same type is used multiple times in a sum type (#7432)

pull/7460/head
Lukas Neubert 2020-12-21 21:05:50 +01:00 committed by GitHub
parent c831711a0e
commit 5cd2dffafb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 1 deletions

View File

@ -338,16 +338,21 @@ pub fn (mut c Checker) type_decl(node ast.TypeDecl) {
}
ast.SumTypeDecl {
c.check_valid_pascal_case(node.name, 'sum type', node.pos)
mut names_used := []string{}
for variant in node.variants {
if variant.typ.is_ptr() {
c.error('sum type cannot hold a reference type', variant.pos)
}
mut sym := c.table.get_type_symbol(variant.typ)
if sym.kind == .placeholder {
if sym.name in names_used {
c.error('sum type $node.name cannot hold the type `$sym.name` more than once',
variant.pos)
} else if sym.kind == .placeholder {
c.error("type `$sym.name` doesn't exist", node.pos)
} else if sym.kind == .interface_ {
c.error('sum type cannot hold an interface', node.pos)
}
names_used << sym.name
}
}
}

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/sum_type_multiple_type_define.vv:3:22: error: sum type FooType cannot hold the type `Foo` more than once
1 | struct Foo {}
2 |
3 | type FooType = Foo | Foo
| ~~~

View File

@ -0,0 +1,3 @@
struct Foo {}
type FooType = Foo | Foo