mirror of
https://github.com/vlang/v.git
synced 2025-09-11 16:36:20 -04:00
cgen: fix comptime assign with generic result return type (#19192)
This commit is contained in:
parent
ccb14382b9
commit
f183ec3f69
@ -804,7 +804,7 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
|
|||||||
unwrapped_typ = unaliased_type.clear_flags(.option, .result)
|
unwrapped_typ = unaliased_type.clear_flags(.option, .result)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unwrapped_styp := g.typ(unwrapped_typ)
|
mut unwrapped_styp := g.typ(unwrapped_typ)
|
||||||
if g.infix_left_var_name.len > 0 {
|
if g.infix_left_var_name.len > 0 {
|
||||||
g.indent--
|
g.indent--
|
||||||
g.writeln('}')
|
g.writeln('}')
|
||||||
@ -814,6 +814,11 @@ fn (mut g Gen) call_expr(node ast.CallExpr) {
|
|||||||
g.write('\n ${cur_line}')
|
g.write('\n ${cur_line}')
|
||||||
} else if !g.inside_curry_call {
|
} else if !g.inside_curry_call {
|
||||||
if !g.inside_const_opt_or_res {
|
if !g.inside_const_opt_or_res {
|
||||||
|
if g.assign_ct_type != 0
|
||||||
|
&& node.or_block.kind in [.propagate_option, .propagate_result] {
|
||||||
|
unwrapped_styp = g.typ(g.assign_ct_type.derive(node.return_type).clear_flags(.option,
|
||||||
|
.result))
|
||||||
|
}
|
||||||
g.write('\n ${cur_line} (*(${unwrapped_styp}*)${tmp_opt}.data)')
|
g.write('\n ${cur_line} (*(${unwrapped_styp}*)${tmp_opt}.data)')
|
||||||
} else {
|
} else {
|
||||||
g.write('\n ${cur_line} ${tmp_opt}')
|
g.write('\n ${cur_line} ${tmp_opt}')
|
||||||
|
48
vlib/v/tests/comptime_generic_ret_test.v
Normal file
48
vlib/v/tests/comptime_generic_ret_test.v
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
struct Parent {
|
||||||
|
pub mut:
|
||||||
|
id int
|
||||||
|
name string
|
||||||
|
child Child
|
||||||
|
other Other
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Child {
|
||||||
|
pub mut:
|
||||||
|
id int
|
||||||
|
age int
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Other {
|
||||||
|
pub mut:
|
||||||
|
id int
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
interface IdInterface {
|
||||||
|
mut:
|
||||||
|
id int
|
||||||
|
}
|
||||||
|
|
||||||
|
fn insert_ids[T](val T) !T {
|
||||||
|
mut clone := val
|
||||||
|
|
||||||
|
$for field in T.fields {
|
||||||
|
$if field.typ is $struct {
|
||||||
|
clone.$(field.name) = insert_ids(val.$(field.name))!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$if T is IdInterface {
|
||||||
|
clone.id = 1
|
||||||
|
} $else {
|
||||||
|
return error('${T.name} does not have an id field!')
|
||||||
|
}
|
||||||
|
return clone
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_main() {
|
||||||
|
inserted := insert_ids(Parent{
|
||||||
|
name: 'test'
|
||||||
|
})!
|
||||||
|
assert inserted.child.id == 1
|
||||||
|
assert inserted.other.id == 1
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user