Skip to content

Commit

Permalink
cgen: cast 'vstring'.str to char *
Browse files Browse the repository at this point in the history
  • Loading branch information
ttytm committed Nov 10, 2023
1 parent ab4c1f4 commit 4eae9c6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
19 changes: 11 additions & 8 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -2544,14 +2544,17 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_typ
g.write('->val')
return
}
if got_is_ptr && !expected_is_ptr && neither_void && exp_sym.kind != .placeholder
&& expr !is ast.InfixExpr {
got_deref_type := got_type.deref()
deref_sym := g.table.sym(got_deref_type)
deref_will_match := expected_type in [got_type, got_deref_type, deref_sym.parent_idx]
got_is_opt_or_res := got_type.has_flag(.option) || got_type.has_flag(.result)
if deref_will_match || got_is_opt_or_res || expr.is_auto_deref_var() {
g.write('*')
if got_is_ptr && neither_void && exp_sym.kind != .placeholder && expr !is ast.InfixExpr {
if !expected_is_ptr {
got_deref_type := got_type.deref()
deref_sym := g.table.sym(got_deref_type)
deref_will_match := expected_type in [got_type, got_deref_type, deref_sym.parent_idx]
got_is_opt_or_res := got_type.has_flag(.option) || got_type.has_flag(.result)
if deref_will_match || got_is_opt_or_res || expr.is_auto_deref_var() {
g.write('*')
}
} else if expr !is ast.StringLiteral && exp_sym.kind == .char && got_sym.kind == .u8 {
g.write('(${exp_styp})')
}
}
if expr is ast.IntegerLiteral {
Expand Down
1 change: 1 addition & 0 deletions vlib/v/gen/c/testdata/c_fn_args.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bar
8 changes: 8 additions & 0 deletions vlib/v/gen/c/testdata/c_fn_args.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
fn foo(str &char) {
println(unsafe { str.vstring() })
}

fn main() {
// Should compile with `-cc [gcc/clang] -cstrict`
foo('bar'.str)
}

0 comments on commit 4eae9c6

Please sign in to comment.