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 casting in anon fn (fix #23530) #23533

Merged
merged 2 commits into from
Jan 20, 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
1 change: 0 additions & 1 deletion vlib/v/gen/c/auto_eq_methods.v
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,6 @@ fn (mut g Gen) gen_interface_equality_fn(left_type ast.Type) string {
left_arg := g.read_field(left_type, '_typ', 'a')
right_arg := g.read_field(left_type, '_typ', 'b')

fn_builder.writeln('${g.static_non_parallel}int v_typeof_interface_idx_${idx_fn}(int sidx); // for auto eq method')
fn_builder.writeln('${g.static_non_parallel}inline bool ${fn_name}_interface_eq(${ptr_styp} a, ${ptr_styp} b) {')
fn_builder.writeln('\tif (${left_arg} == ${right_arg}) {')
fn_builder.writeln('\t\tint idx = v_typeof_interface_idx_${idx_fn}(${left_arg});')
Expand Down
1 change: 1 addition & 0 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,7 @@ pub fn (mut g Gen) write_typeof_functions() {
g.writeln('\tif (sidx == _${sym.cname}_${sub_sym.cname}_index) return "${util.strip_main_name(sub_sym.name)}";')
}
g.writeln2('\treturn "unknown ${util.strip_main_name(sym.name)}";', '}')
g.definitions.writeln('int v_typeof_interface_idx_${sym.cname}(int sidx);')
g.writeln2('', 'int v_typeof_interface_idx_${sym.cname}(int sidx) {')
if g.pref.parallel_cc {
g.extern_out.writeln('extern int v_typeof_interface_idx_${sym.cname}(int sidx);')
Expand Down
26 changes: 26 additions & 0 deletions vlib/v/tests/interfaces/interface_as_cast_in_anon_fn_test.v
spytheman marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@[heap]
struct Foo {
mut:
val int
}

@[heap]
struct Bar {
mut:
val int
}

interface FooBar {
mut:
val int
}

fn test_interface_as_cast_in_anon_fn() {
mut fbs := []&FooBar{}
fbs << &Foo{1}
do_something := fn [mut fbs] () {
_ := fbs.last() as Foo // this line works outside of anon fn
}
do_something()
assert true
}
Loading