Skip to content

Commit 6ee06d8

Browse files
authored
cgen: fix codegen inconsistency handling nil param to arg expecting ptr (fix vlang#24491) (vlang#24503)
1 parent 511a644 commit 6ee06d8

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

vlib/v/gen/c/cgen.v

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2777,14 +2777,13 @@ fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp ast.Ty
27772777
is_sumtype_cast := !got_is_fn && fname.contains('_to_sumtype_')
27782778
is_comptime_variant := is_not_ptr_and_fn && expr is ast.Ident
27792779
&& g.comptime.is_comptime_variant_var(expr)
2780-
27812780
if exp.is_ptr() {
27822781
if $d('mutable_sumtype', false) && is_sumtype_cast && g.expected_arg_mut
27832782
&& expr is ast.Ident {
27842783
g.write('&(${exp_styp.trim_right('*')}){._${got_styp.trim_right('*')}=')
27852784
rparen_n = 0
27862785
mutable_idx = got.idx()
2787-
} else if expr is ast.UnsafeExpr && expr.expr is ast.Nil {
2786+
} else if (expr is ast.UnsafeExpr && expr.expr is ast.Nil) || got == ast.nil_type {
27882787
g.write('(void*)0')
27892788
return
27902789
} else {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module main
2+
3+
interface ISomeStruct {
4+
something string
5+
}
6+
7+
struct SomeStruct {
8+
something string
9+
}
10+
11+
fn test(s &ISomeStruct) {
12+
assert '${s}' == '&nil'
13+
if s == unsafe { nil } {
14+
assert true
15+
} else {
16+
assert false
17+
}
18+
}
19+
20+
fn test_main() {
21+
y := unsafe { nil }
22+
test(y)
23+
test(unsafe { nil })
24+
}

0 commit comments

Comments
 (0)