Skip to content

Commit 74997fd

Browse files
authored
ast, cgen: fix as cast with call expr (fix #19453) (#19462)
1 parent b5f71df commit 74997fd

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed

cmd/tools/vtest-self.v

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ const (
255255
'do_not_remove',
256256
'vlib/v/tests/const_fixed_array_containing_references_to_itself_test.v', // error C2099: initializer is not a constant
257257
'vlib/v/tests/const_and_global_with_same_name_test.v', // error C2099: initializer is not a constant
258-
'vlib/v/tests/sumtype_as_cast_test.v', // error: cannot support compound statement expression ({expr; expr; expr;})
258+
'vlib/v/tests/sumtype_as_cast_1_test.v', // error: cannot support compound statement expression ({expr; expr; expr;})
259+
'vlib/v/tests/sumtype_as_cast_2_test.v', // error: cannot support compound statement expression ({expr; expr; expr;})
259260
'vlib/v/tests/project_with_cpp_code/compiling_cpp_files_with_a_cplusplus_compiler_test.v', // TODO
260261
]
261262
skip_on_windows = [

vlib/v/ast/ast.v

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,6 +2079,15 @@ pub fn (e &Expr) is_lockable() bool {
20792079
}
20802080
}
20812081

2082+
// returns if an expression has call expr`
2083+
pub fn (e &Expr) has_fn_call() bool {
2084+
return match e {
2085+
CallExpr { true }
2086+
SelectorExpr { e.expr.has_fn_call() }
2087+
else { false }
2088+
}
2089+
}
2090+
20822091
// CTempVar is used in cgen only, to hold nodes for temporary variables
20832092
pub struct CTempVar {
20842093
pub:

vlib/v/gen/c/cgen.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6564,7 +6564,7 @@ fn (mut g Gen) as_cast(node ast.AsCast) {
65646564
mut expr_type_sym := g.table.sym(g.unwrap_generic(node.expr_type))
65656565
if mut expr_type_sym.info is ast.SumType {
65666566
dot := if node.expr_type.is_ptr() { '->' } else { '.' }
6567-
if node.expr is ast.CallExpr && !g.is_cc_msvc {
6567+
if node.expr.has_fn_call() && !g.is_cc_msvc {
65686568
tmp_var := g.new_tmp_var()
65696569
expr_styp := g.typ(node.expr_type)
65706570
g.write('({ ${expr_styp} ${tmp_var} = ')

vlib/v/tests/sumtype_as_cast_2_test.v

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
type Numbers = int | string
2+
3+
struct Values {
4+
value Numbers
5+
}
6+
7+
struct Wrapper {
8+
element []Values
9+
mut:
10+
index int
11+
}
12+
13+
fn (mut instance Wrapper) get() Values {
14+
instance.index++
15+
return instance.element[0]
16+
}
17+
18+
fn test_sumtype_as_cast() {
19+
mut wrapper := Wrapper{
20+
element: [
21+
Values{
22+
value: 1
23+
},
24+
Values{
25+
value: '2'
26+
},
27+
]
28+
}
29+
println(wrapper.get().value as int)
30+
assert wrapper.index == 1
31+
}

0 commit comments

Comments
 (0)