Skip to content

Commit ef9bb8e

Browse files
authored
cgen: fix codegen for assigning nil or 0 to option ptr field (fix #24447) (fix #24500) (#24502)
1 parent d433835 commit ef9bb8e

3 files changed

Lines changed: 51 additions & 1 deletion

File tree

‎vlib/v/gen/c/cgen.v‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2333,7 +2333,8 @@ fn (mut g Gen) expr_with_tmp_var(expr ast.Expr, expr_typ ast.Type, ret_typ ast.T
23332333
}
23342334
}
23352335
}
2336-
if ret_typ.nr_muls() > expr_typ.nr_muls() {
2336+
if !expr.is_literal() && expr_typ != ast.nil_type
2337+
&& ret_typ.nr_muls() > expr_typ.nr_muls() {
23372338
g.write('&'.repeat(ret_typ.nr_muls() - expr_typ.nr_muls()))
23382339
}
23392340
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
struct Foo {
2+
name ?&string
3+
}
4+
5+
struct Foo2 {
6+
mut:
7+
name ?&string
8+
}
9+
10+
fn test_1() {
11+
_ := Foo{
12+
name: unsafe { nil }
13+
}
14+
}
15+
16+
fn test_2() {
17+
mut foo := Foo2{}
18+
unsafe {
19+
foo.name = nil
20+
}
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Simple arena allocator
2+
struct ArenaChunk {
3+
mut:
4+
next ?&ArenaChunk
5+
used int
6+
cap int
7+
data byteptr
8+
}
9+
10+
struct Arena {
11+
mut:
12+
head ?&ArenaChunk
13+
}
14+
15+
fn arena_init(mut arena Arena, first_capacity int) {
16+
chunk := &ArenaChunk{
17+
next: 0
18+
used: 0
19+
cap: first_capacity
20+
data: unsafe { malloc(first_capacity) }
21+
}
22+
arena.head = chunk
23+
}
24+
25+
fn test_main() {
26+
mut green_arena := Arena{}
27+
arena_init(mut green_arena, 64 * 1024)
28+
}

0 commit comments

Comments
 (0)