Skip to content

Commit

Permalink
cgen: fix codegen for unwrapping option comptime var (fix vlang#23590) (
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Jan 27, 2025
1 parent aa4c06c commit 4eeae1c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 21 deletions.
44 changes: 24 additions & 20 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -4312,7 +4312,9 @@ fn (mut c Checker) smartcast(mut expr ast.Expr, cur_type ast.Type, to_type_ ast.
mut orig_type := 0
mut is_inherited := false
mut ct_type_var := ast.ComptimeVarKind.no_comptime
mut is_ct_type_unwrapped := false
if mut expr.obj is ast.Var {
is_ct_type_unwrapped = expr.obj.ct_type_var != ast.ComptimeVarKind.no_comptime
is_mut = expr.obj.is_mut
smartcasts << expr.obj.smartcasts
is_already_casted = expr.obj.pos.pos == expr.pos.pos
Expand All @@ -4334,16 +4336,17 @@ fn (mut c Checker) smartcast(mut expr ast.Expr, cur_type ast.Type, to_type_ ast.
if cur_type.has_flag(.option) && !to_type.has_flag(.option) {
if !var.is_unwrapped {
scope.register(ast.Var{
name: expr.name
typ: cur_type
pos: expr.pos
is_used: true
is_mut: expr.is_mut
is_inherited: is_inherited
smartcasts: [to_type]
orig_type: orig_type
ct_type_var: ct_type_var
is_unwrapped: true
name: expr.name
typ: cur_type
pos: expr.pos
is_used: true
is_mut: expr.is_mut
is_inherited: is_inherited
smartcasts: [to_type]
orig_type: orig_type
ct_type_var: ct_type_var
ct_type_unwrapped: is_ct_type_unwrapped
is_unwrapped: true
})
} else {
scope.update_smartcasts(expr.name, to_type, true)
Expand All @@ -4355,16 +4358,17 @@ fn (mut c Checker) smartcast(mut expr ast.Expr, cur_type ast.Type, to_type_ ast.
}
}
scope.register(ast.Var{
name: expr.name
typ: cur_type
pos: expr.pos
is_used: true
is_mut: expr.is_mut
is_inherited: is_inherited
is_unwrapped: is_option_unwrap
smartcasts: smartcasts
orig_type: orig_type
ct_type_var: ct_type_var
name: expr.name
typ: cur_type
pos: expr.pos
is_used: true
is_mut: expr.is_mut
is_inherited: is_inherited
is_unwrapped: is_option_unwrap
smartcasts: smartcasts
orig_type: orig_type
ct_type_var: ct_type_var
ct_type_unwrapped: is_ct_type_unwrapped
})
} else if is_mut && !expr.is_mut {
c.smartcast_mut_pos = expr.pos
Expand Down
3 changes: 2 additions & 1 deletion vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -5158,7 +5158,8 @@ fn (mut g Gen) ident(node ast.Ident) {
}
}
if i == 0 && node.obj.ct_type_var != .smartcast && node.obj.is_unwrapped {
dot := if !node.obj.orig_type.is_ptr() && obj_sym.is_heap() {
dot := if !node.obj.ct_type_unwrapped && !node.obj.orig_type.is_ptr()
&& obj_sym.is_heap() {
'->'
} else {
'.'
Expand Down
26 changes: 26 additions & 0 deletions vlib/v/tests/comptime/comptime_var_unwrap_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
struct Foo {
a int
}

fn Foo.init[T](a T) {
dump(a)
}

@[heap]
struct Bar {
field int
}

fn t[T](a ?T) T {
mut b := a
if b != none {
Foo.init(b)
return b
}
assert false
return T{}
}

fn test_main() {
assert t(Bar{}) == Bar{}
}

0 comments on commit 4eeae1c

Please sign in to comment.