Skip to content

Commit 772d210

Browse files
authored
cgen: fix codegen for assigning from infixexpr with generic operand (fix #23560) (#23561)
1 parent da5bb68 commit 772d210

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

vlib/v/gen/c/assign.v

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,9 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
376376
}
377377
} else if val is ast.InfixExpr && val.op in [.plus, .minus, .mul, .div, .mod]
378378
&& val.left_ct_expr {
379-
ctyp := g.unwrap_generic(g.type_resolver.get_type(val.left))
379+
ctyp := g.type_resolver.promote_type(g.unwrap_generic(g.type_resolver.get_type(val.left)),
380+
g.unwrap_generic(g.type_resolver.get_type_or_default(val.right,
381+
val.right_type)))
380382
if ctyp != ast.void_type {
381383
ct_type_var := g.comptime.get_ct_type_var(val.left)
382384
if ct_type_var in [.key_var, .value_var] {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
fn lerp[T](x T) T {
2+
return x
3+
}
4+
5+
struct Foo[T] {
6+
mut:
7+
value T
8+
}
9+
10+
fn (mut t Foo[T]) r[T](dt f64) {
11+
mut value := t.value + dt
12+
lerp(value)
13+
}
14+
15+
fn test_main() {
16+
mut t2 := Foo[f32]{}
17+
t2.r(2.1)
18+
assert true
19+
}

vlib/v/type_resolver/type_resolver.v

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ fn (t &TypeResolver) error(s string, pos token.Pos) {
8989
exit(1)
9090
}
9191

92+
// promote_type resolves the final type of different generic/comptime operand types
93+
pub fn (t &TypeResolver) promote_type(left_type ast.Type, right_type ast.Type) ast.Type {
94+
if left_type == ast.f32_type && right_type == ast.f64_type {
95+
return right_type
96+
}
97+
return left_type
98+
}
99+
92100
// get_type_or_default retries the comptime value if the AST node is related to comptime otherwise default_typ is returned
93101
@[inline]
94102
pub fn (mut t TypeResolver) get_type_or_default(node ast.Expr, default_typ ast.Type) ast.Type {

0 commit comments

Comments
 (0)