all: make implements work with multiple interfaces

This commit is contained in:
Alexander Medvednikov 2024-09-09 05:03:27 +03:00
parent 3300e59a0f
commit ac6ca5f858
6 changed files with 61 additions and 25 deletions

View File

@ -418,7 +418,7 @@ pub:
embeds []Embed embeds []Embed
is_implements bool is_implements bool
implements_type Type implements_types []Type
pub mut: pub mut:
fields []StructField fields []StructField
} }

View File

@ -333,7 +333,9 @@ fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
// XTODO2 // XTODO2
// cgen error if I use `println(sym)` without handling the option with `or{}` // 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) } struct_type := c.table.find_type_idx(node.name) // or { panic(err) }
c.type_implements(struct_type, node.implements_type, node.pos) for t in node.implements_types {
c.type_implements(struct_type, t, node.pos)
}
} }
} }

View File

@ -5,3 +5,17 @@ vlib/v/checker/tests/implements_keyword.vv:5:1: error: `CustomError` doesn't imp
| ~~~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~~~
6 | } 6 | }
7 | 7 |
vlib/v/checker/tests/implements_keyword.vv:15:1: error: `CustomError2` doesn't implement method `print_error` of interface `MyError`
13 | }
14 |
15 | struct CustomError2 implements MyError, MyError2 {
| ~~~~~~~~~~~~~~~~~~~
16 | }
17 |
vlib/v/checker/tests/implements_keyword.vv:15:1: error: `CustomError2` doesn't implement method `print_error2` of interface `MyError2`
13 | }
14 |
15 | struct CustomError2 implements MyError, MyError2 {
| ~~~~~~~~~~~~~~~~~~~
16 | }
17 |

View File

@ -7,3 +7,13 @@ struct CustomError implements MyError {
fn (e CustomError) print_error2() { fn (e CustomError) print_error2() {
} }
interface MyError2 {
print_error2()
}
struct CustomError2 implements MyError, MyError2 {
}
fn (e CustomError2) print_error3() {
}

View File

@ -33,7 +33,12 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl, is_anon bool) {
mut field_types := []string{cap: node.fields.len} mut field_types := []string{cap: node.fields.len}
if node.is_implements { if node.is_implements {
f.write(' implements ') f.write(' implements ')
f.write(f.table.type_to_str_using_aliases(node.implements_type, f.mod2alias)) for i, t in node.implements_types {
f.write(f.table.type_to_str_using_aliases(t, f.mod2alias))
if i < node.implements_types.len - 1 {
f.write(', ')
}
}
} }
// Calculate the alignments first // Calculate the alignments first
f.calculate_alignment(node.fields, mut type_align, mut comment_align, mut default_expr_align, mut f.calculate_alignment(node.fields, mut type_align, mut comment_align, mut default_expr_align, mut

View File

@ -94,14 +94,19 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
mut is_field_pub := false mut is_field_pub := false
mut is_field_global := false mut is_field_global := false
mut is_implements := false mut is_implements := false
mut implements_type := ast.void_type mut implements_types := []ast.Type{cap: 3} // ast.void_type
mut last_line := p.prev_tok.pos().line_nr + 1 mut last_line := p.prev_tok.pos().line_nr + 1
mut end_comments := []ast.Comment{} mut end_comments := []ast.Comment{}
if !no_body { if !no_body {
if p.tok.kind == .key_implements { if p.tok.kind == .key_implements {
p.next()
implements_type = p.parse_type()
is_implements = true is_implements = true
for {
p.next()
implements_types << p.parse_type()
if p.tok.kind != .comma {
break
}
}
} }
p.check(.lcbr) p.check(.lcbr)
pre_comments << p.eat_comments() pre_comments << p.eat_comments()
@ -395,7 +400,7 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
generic_types: generic_types generic_types: generic_types
embeds: embeds embeds: embeds
is_implements: is_implements is_implements: is_implements
implements_type: implements_type implements_types: implements_types
} }
} }