Skip to content

Commit 4eeae1c

Browse files
authored
cgen: fix codegen for unwrapping option comptime var (fix #23590) (#23591)
1 parent aa4c06c commit 4eeae1c

3 files changed

Lines changed: 52 additions & 21 deletions

File tree

‎vlib/v/checker/checker.v‎

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4312,7 +4312,9 @@ fn (mut c Checker) smartcast(mut expr ast.Expr, cur_type ast.Type, to_type_ ast.
43124312
mut orig_type := 0
43134313
mut is_inherited := false
43144314
mut ct_type_var := ast.ComptimeVarKind.no_comptime
4315+
mut is_ct_type_unwrapped := false
43154316
if mut expr.obj is ast.Var {
4317+
is_ct_type_unwrapped = expr.obj.ct_type_var != ast.ComptimeVarKind.no_comptime
43164318
is_mut = expr.obj.is_mut
43174319
smartcasts << expr.obj.smartcasts
43184320
is_already_casted = expr.obj.pos.pos == expr.pos.pos
@@ -4334,16 +4336,17 @@ fn (mut c Checker) smartcast(mut expr ast.Expr, cur_type ast.Type, to_type_ ast.
43344336
if cur_type.has_flag(.option) && !to_type.has_flag(.option) {
43354337
if !var.is_unwrapped {
43364338
scope.register(ast.Var{
4337-
name: expr.name
4338-
typ: cur_type
4339-
pos: expr.pos
4340-
is_used: true
4341-
is_mut: expr.is_mut
4342-
is_inherited: is_inherited
4343-
smartcasts: [to_type]
4344-
orig_type: orig_type
4345-
ct_type_var: ct_type_var
4346-
is_unwrapped: true
4339+
name: expr.name
4340+
typ: cur_type
4341+
pos: expr.pos
4342+
is_used: true
4343+
is_mut: expr.is_mut
4344+
is_inherited: is_inherited
4345+
smartcasts: [to_type]
4346+
orig_type: orig_type
4347+
ct_type_var: ct_type_var
4348+
ct_type_unwrapped: is_ct_type_unwrapped
4349+
is_unwrapped: true
43474350
})
43484351
} else {
43494352
scope.update_smartcasts(expr.name, to_type, true)
@@ -4355,16 +4358,17 @@ fn (mut c Checker) smartcast(mut expr ast.Expr, cur_type ast.Type, to_type_ ast.
43554358
}
43564359
}
43574360
scope.register(ast.Var{
4358-
name: expr.name
4359-
typ: cur_type
4360-
pos: expr.pos
4361-
is_used: true
4362-
is_mut: expr.is_mut
4363-
is_inherited: is_inherited
4364-
is_unwrapped: is_option_unwrap
4365-
smartcasts: smartcasts
4366-
orig_type: orig_type
4367-
ct_type_var: ct_type_var
4361+
name: expr.name
4362+
typ: cur_type
4363+
pos: expr.pos
4364+
is_used: true
4365+
is_mut: expr.is_mut
4366+
is_inherited: is_inherited
4367+
is_unwrapped: is_option_unwrap
4368+
smartcasts: smartcasts
4369+
orig_type: orig_type
4370+
ct_type_var: ct_type_var
4371+
ct_type_unwrapped: is_ct_type_unwrapped
43684372
})
43694373
} else if is_mut && !expr.is_mut {
43704374
c.smartcast_mut_pos = expr.pos

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5158,7 +5158,8 @@ fn (mut g Gen) ident(node ast.Ident) {
51585158
}
51595159
}
51605160
if i == 0 && node.obj.ct_type_var != .smartcast && node.obj.is_unwrapped {
5161-
dot := if !node.obj.orig_type.is_ptr() && obj_sym.is_heap() {
5161+
dot := if !node.obj.ct_type_unwrapped && !node.obj.orig_type.is_ptr()
5162+
&& obj_sym.is_heap() {
51625163
'->'
51635164
} else {
51645165
'.'
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
struct Foo {
2+
a int
3+
}
4+
5+
fn Foo.init[T](a T) {
6+
dump(a)
7+
}
8+
9+
@[heap]
10+
struct Bar {
11+
field int
12+
}
13+
14+
fn t[T](a ?T) T {
15+
mut b := a
16+
if b != none {
17+
Foo.init(b)
18+
return b
19+
}
20+
assert false
21+
return T{}
22+
}
23+
24+
fn test_main() {
25+
assert t(Bar{}) == Bar{}
26+
}

0 commit comments

Comments
 (0)