Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen: fix interface method list ordering to make test buildable with g++ #23870

Merged
merged 2 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -7533,7 +7533,8 @@ fn (mut g Gen) interface_table() string {
methods_struct_name := 'struct _${interface_name}_interface_methods'
mut methods_struct_def := strings.new_builder(100)
methods_struct_def.writeln('${methods_struct_name} {')
inter_methods := inter_info.get_methods()
mut inter_methods := inter_info.get_methods()
inter_methods.sort(a < b)
mut methodidx := map[string]int{}
for k, method_name in inter_methods {
method := isym.find_method_with_generic_parent(method_name) or { continue }
Expand Down Expand Up @@ -7664,7 +7665,9 @@ static inline __shared__${interface_name} ${shared_fn_name}(__shared__${cctype}*
methods_struct.writeln('\t{')
}
if st == ast.voidptr_type || st == ast.nil_type {
for mname, _ in methodidx {
mut mnames := methodidx.keys()
mnames.sort(a < b)
for mname in mnames {
if g.pref.build_mode != .build_module {
methods_struct.writeln('\t\t._method_${c_fn_name(mname)} = (void*) 0,')
}
Expand Down Expand Up @@ -7721,7 +7724,9 @@ static inline __shared__${interface_name} ${shared_fn_name}(__shared__${cctype}*
}
}

for method in methods {
mut ordered_methods := methods.clone()
ordered_methods.sort(a.name < b.name)
for method in ordered_methods {
mut name := method.name
if method.generic_names.len > 0 && inter_info.parent_type.has_flag(.generic) {
parent_sym := g.table.sym(inter_info.parent_type)
Expand Down
18 changes: 18 additions & 0 deletions vlib/v/gen/c/testdata/iface_method_order.c.must_have
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
struct _main__Foo_interface_methods {
void (*_method_a)(void* _);
void (*_method_b)(void* _);
};
struct _main__Foo_interface_methods main__Foo_name_table[3] = {
{
._method_a = (void*) main__Bar_a_Interface_main__Foo_method_wrapper,
._method_b = (void*) main__Bar_b_Interface_main__Foo_method_wrapper,
},
{
._method_a = (void*) 0,
._method_b = (void*) 0,
},
{
._method_a = (void*) main__Baz_a_Interface_main__Foo_method_wrapper,
._method_b = (void*) main__Baz_b_Interface_main__Foo_method_wrapper,
},
};
18 changes: 18 additions & 0 deletions vlib/v/gen/c/testdata/iface_method_order.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
interface Foo {
a()
b()
}

struct Bar implements Foo {
}

fn (b Bar) b() {}

fn (b Bar) a() {}

struct Baz implements Foo {
}

fn (b Baz) b() {}

fn (b Baz) a() {}
Loading