Skip to content

Commit

Permalink
cgen: fix interface casting in anon fn (fix #23530) (#23533)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 authored Jan 20, 2025
1 parent 839d526 commit 6d40bb8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
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
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
}

0 comments on commit 6d40bb8

Please sign in to comment.