Skip to content

Commit

Permalink
cgen: fix fn mut argument of sumtype reference (#21874)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Jul 15, 2024
1 parent 64430f9 commit aa34047
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
10 changes: 8 additions & 2 deletions vlib/v/gen/c/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -2608,8 +2608,14 @@ fn (mut g Gen) ref_or_deref_arg(arg ast.CallArg, expected_type ast.Type, lang as
g.write('&/*mut*/')
} else if arg.is_mut && arg_typ.is_ptr() && expected_type.is_ptr()
&& g.table.sym(arg_typ).kind == .struct_ && expected_type == arg_typ.ref() {
g.write('&/*mut*/')
g.expr(arg.expr)
if arg.expr is ast.PrefixExpr && arg.expr.op == .amp {
g.arg_no_auto_deref = true
g.expr(arg.expr)
g.arg_no_auto_deref = false
} else {
g.write('&/*mut*/')
g.expr(arg.expr)
}
return
} else if exp_is_ptr && !arg_is_ptr && !(arg_sym.kind == .alias
&& g.table.unaliased_type(arg_typ).is_pointer() && expected_type.is_pointer()) {
Expand Down
29 changes: 29 additions & 0 deletions vlib/v/tests/fn_mut_arg_of_sumtype_ref_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module main

pub struct Expr1 {}

pub struct Expr2 {}

pub type Expr = Expr1 | Expr2

pub struct Gen {
}

fn (mut g Gen) foo(mut expr &Expr1) string {
return '${expr}'
}

pub fn (mut g Gen) bar(mut expr Expr) string {
return match mut expr {
Expr1 { g.foo(mut &expr) }
Expr2 { '' }
}
}

fn test_fn_mut_arg_of_sumtype_ref() {
mut expr := &Expr1{}
mut g := Gen{}
ret := g.bar(mut expr)
println(ret)
assert ret == 'Expr1{}'
}

0 comments on commit aa34047

Please sign in to comment.