Skip to content

Commit

Permalink
cgen: fix codegen for assigning from infixexpr with generic operand (fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Jan 24, 2025
1 parent da5bb68 commit 772d210
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion vlib/v/gen/c/assign.v
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,9 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) {
}
} else if val is ast.InfixExpr && val.op in [.plus, .minus, .mul, .div, .mod]
&& val.left_ct_expr {
ctyp := g.unwrap_generic(g.type_resolver.get_type(val.left))
ctyp := g.type_resolver.promote_type(g.unwrap_generic(g.type_resolver.get_type(val.left)),
g.unwrap_generic(g.type_resolver.get_type_or_default(val.right,
val.right_type)))
if ctyp != ast.void_type {
ct_type_var := g.comptime.get_ct_type_var(val.left)
if ct_type_var in [.key_var, .value_var] {
Expand Down
19 changes: 19 additions & 0 deletions vlib/v/tests/generics/generic_selector_infix_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
fn lerp[T](x T) T {
return x
}

struct Foo[T] {
mut:
value T
}

fn (mut t Foo[T]) r[T](dt f64) {
mut value := t.value + dt
lerp(value)
}

fn test_main() {
mut t2 := Foo[f32]{}
t2.r(2.1)
assert true
}
8 changes: 8 additions & 0 deletions vlib/v/type_resolver/type_resolver.v
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ fn (t &TypeResolver) error(s string, pos token.Pos) {
exit(1)
}

// promote_type resolves the final type of different generic/comptime operand types
pub fn (t &TypeResolver) promote_type(left_type ast.Type, right_type ast.Type) ast.Type {
if left_type == ast.f32_type && right_type == ast.f64_type {
return right_type
}
return left_type
}

// get_type_or_default retries the comptime value if the AST node is related to comptime otherwise default_typ is returned
@[inline]
pub fn (mut t TypeResolver) get_type_or_default(node ast.Expr, default_typ ast.Type) ast.Type {
Expand Down

0 comments on commit 772d210

Please sign in to comment.