Skip to content

Commit

Permalink
cgen: fix interface casting in anon fn
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Jan 20, 2025
1 parent f9d3bd3 commit 89bb85c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
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 89bb85c

Please sign in to comment.