mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: error, when same type is used multiple times in a sum type (#7432)
This commit is contained in:
parent
c831711a0e
commit
5cd2dffafb
@ -338,16 +338,21 @@ pub fn (mut c Checker) type_decl(node ast.TypeDecl) {
|
|||||||
}
|
}
|
||||||
ast.SumTypeDecl {
|
ast.SumTypeDecl {
|
||||||
c.check_valid_pascal_case(node.name, 'sum type', node.pos)
|
c.check_valid_pascal_case(node.name, 'sum type', node.pos)
|
||||||
|
mut names_used := []string{}
|
||||||
for variant in node.variants {
|
for variant in node.variants {
|
||||||
if variant.typ.is_ptr() {
|
if variant.typ.is_ptr() {
|
||||||
c.error('sum type cannot hold a reference type', variant.pos)
|
c.error('sum type cannot hold a reference type', variant.pos)
|
||||||
}
|
}
|
||||||
mut sym := c.table.get_type_symbol(variant.typ)
|
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)
|
c.error("type `$sym.name` doesn't exist", node.pos)
|
||||||
} else if sym.kind == .interface_ {
|
} else if sym.kind == .interface_ {
|
||||||
c.error('sum type cannot hold an interface', node.pos)
|
c.error('sum type cannot hold an interface', node.pos)
|
||||||
}
|
}
|
||||||
|
names_used << sym.name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
5
vlib/v/checker/tests/sum_type_multiple_type_define.out
Normal file
5
vlib/v/checker/tests/sum_type_multiple_type_define.out
Normal 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
|
||||||
|
| ~~~
|
3
vlib/v/checker/tests/sum_type_multiple_type_define.vv
Normal file
3
vlib/v/checker/tests/sum_type_multiple_type_define.vv
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
struct Foo {}
|
||||||
|
|
||||||
|
type FooType = Foo | Foo
|
Loading…
Reference in New Issue
Block a user