Skip to content

Commit aa34047

Browse files
authored
cgen: fix fn mut argument of sumtype reference (#21874)
1 parent 64430f9 commit aa34047

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

vlib/v/gen/c/fn.v

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2608,8 +2608,14 @@ fn (mut g Gen) ref_or_deref_arg(arg ast.CallArg, expected_type ast.Type, lang as
26082608
g.write('&/*mut*/')
26092609
} else if arg.is_mut && arg_typ.is_ptr() && expected_type.is_ptr()
26102610
&& g.table.sym(arg_typ).kind == .struct_ && expected_type == arg_typ.ref() {
2611-
g.write('&/*mut*/')
2612-
g.expr(arg.expr)
2611+
if arg.expr is ast.PrefixExpr && arg.expr.op == .amp {
2612+
g.arg_no_auto_deref = true
2613+
g.expr(arg.expr)
2614+
g.arg_no_auto_deref = false
2615+
} else {
2616+
g.write('&/*mut*/')
2617+
g.expr(arg.expr)
2618+
}
26132619
return
26142620
} else if exp_is_ptr && !arg_is_ptr && !(arg_sym.kind == .alias
26152621
&& g.table.unaliased_type(arg_typ).is_pointer() && expected_type.is_pointer()) {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module main
2+
3+
pub struct Expr1 {}
4+
5+
pub struct Expr2 {}
6+
7+
pub type Expr = Expr1 | Expr2
8+
9+
pub struct Gen {
10+
}
11+
12+
fn (mut g Gen) foo(mut expr &Expr1) string {
13+
return '${expr}'
14+
}
15+
16+
pub fn (mut g Gen) bar(mut expr Expr) string {
17+
return match mut expr {
18+
Expr1 { g.foo(mut &expr) }
19+
Expr2 { '' }
20+
}
21+
}
22+
23+
fn test_fn_mut_arg_of_sumtype_ref() {
24+
mut expr := &Expr1{}
25+
mut g := Gen{}
26+
ret := g.bar(mut expr)
27+
println(ret)
28+
assert ret == 'Expr1{}'
29+
}

0 commit comments

Comments
 (0)