mirror of
https://github.com/vlang/v.git
synced 2025-09-17 19:36:35 -04:00
checker: check for duplicate interface names in the implements
parts of struct declarations (#22230)
This commit is contained in:
parent
acf6b344f7
commit
5f495e23a4
@ -333,6 +333,7 @@ fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
|
||||
// XTODO2
|
||||
// cgen error if I use `println(sym)` without handling the option with `or{}`
|
||||
struct_type := c.table.find_type_idx(node.name) // or { panic(err) }
|
||||
mut names_used := []string{}
|
||||
for t in node.implements_types {
|
||||
t_sym := c.table.sym(t.typ)
|
||||
if t_sym.info is ast.Interface {
|
||||
@ -350,6 +351,12 @@ fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
|
||||
}
|
||||
}
|
||||
}
|
||||
variant_name := c.table.type_to_str(t.typ)
|
||||
if variant_name in names_used {
|
||||
c.error('struct type ${node.name} cannot implement interface `${t_sym.name} more than once`',
|
||||
t.pos)
|
||||
}
|
||||
names_used << variant_name
|
||||
} else {
|
||||
c.error('`${t_sym.name}` is not an interface type', t.pos)
|
||||
}
|
||||
|
@ -0,0 +1,14 @@
|
||||
vlib/v/checker/tests/struct_implements_interface_more_than_once_err.vv:3:31: error: struct type Abc cannot implement interface `io.Writer more than once`
|
||||
1 | import io { Writer }
|
||||
2 |
|
||||
3 | struct Abc implements Writer, Writer, Writer {
|
||||
| ~~~~~~
|
||||
4 | aaa string
|
||||
5 | }
|
||||
vlib/v/checker/tests/struct_implements_interface_more_than_once_err.vv:12:31: error: struct type Def cannot implement interface `io.Writer more than once`
|
||||
10 |
|
||||
11 | // vfmt off
|
||||
12 | struct Def implements Writer, io.Writer {
|
||||
| ~~
|
||||
13 | aaa string
|
||||
14 | }
|
@ -0,0 +1,19 @@
|
||||
import io { Writer }
|
||||
|
||||
struct Abc implements Writer, Writer, Writer {
|
||||
aaa string
|
||||
}
|
||||
|
||||
fn (a Abc) write(buf []u8) !int {
|
||||
return 42
|
||||
}
|
||||
|
||||
// vfmt off
|
||||
struct Def implements Writer, io.Writer {
|
||||
aaa string
|
||||
}
|
||||
// vfmt on
|
||||
|
||||
fn (a Def) write(buf []u8) !int {
|
||||
return 42
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user