cgen: fix interface method using struct embed (#12924)

pull/12928/head
yuyi 2021-12-22 01:16:05 +08:00 committed by GitHub
parent 794bdfdca7
commit de3665af69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 2 deletions

View File

@ -7383,11 +7383,12 @@ static inline $interface_name I_${cctype}_to_Interface_${interface_name}($cctype
false) false)
} }
} }
mut method_call := '${cctype}_$name' styp := g.cc_type(method.params[0].typ, true)
mut method_call := '${styp}_$name'
if !method.params[0].typ.is_ptr() { if !method.params[0].typ.is_ptr() {
// inline void Cat_speak_Interface_Animal_method_wrapper(Cat c) { return Cat_speak(*c); } // inline void Cat_speak_Interface_Animal_method_wrapper(Cat c) { return Cat_speak(*c); }
iwpostfix := '_Interface_${interface_name}_method_wrapper' iwpostfix := '_Interface_${interface_name}_method_wrapper'
methods_wrapper.write_string('static inline ${g.typ(method.return_type)} $method_call${iwpostfix}(') methods_wrapper.write_string('static inline ${g.typ(method.return_type)} ${cctype}_$name${iwpostfix}(')
// //
params_start_pos := g.out.len params_start_pos := g.out.len
mut params := method.params.clone() mut params := method.params.clone()

View File

@ -0,0 +1,34 @@
module main
pub interface IObject {
mut:
do_stuff()
do_something_with_int(int)
do_something_with_string(string)
}
pub struct BaseObject {
mut:
some_attr int
}
pub fn (mut base BaseObject) do_stuff() {}
pub fn (mut base BaseObject) do_something_with_int(n int) {}
pub fn (mut base BaseObject) do_something_with_string(s string) {}
pub fn (mut base BaseObject) method_thats_available_to_all_object() {}
pub struct GameObject {
BaseObject
pub mut:
some_attr_for_game int
}
fn test_interface_method_using_struct_embed() {
mut common_object := []IObject{}
common_object << GameObject{}
println(common_object)
assert common_object.len == 1
}