Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen: fix codegen for unwrapping option comptime var #23591

Merged
merged 2 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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{}
}
Loading