Skip to content

Commit

Permalink
cgen: fix infix op when handling comptime selector (#19691)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Oct 29, 2023
1 parent 00165d1 commit 4a99b1e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
16 changes: 13 additions & 3 deletions vlib/v/gen/c/infix.v
Expand Up @@ -96,8 +96,18 @@ fn (mut g Gen) infix_expr_arrow_op(node ast.InfixExpr) {

// infix_expr_eq_op generates code for `==` and `!=`
fn (mut g Gen) infix_expr_eq_op(node ast.InfixExpr) {
left := g.unwrap(node.left_type)
right := g.unwrap(node.right_type)
left_type := if node.left is ast.ComptimeSelector {
g.get_comptime_var_type(node.left)
} else {
node.left_type
}
right_type := if node.right is ast.ComptimeSelector {
g.get_comptime_var_type(node.right)
} else {
node.right_type
}
left := g.unwrap(left_type)
right := g.unwrap(right_type)
mut has_defined_eq_operator := false
mut eq_operator_expects_ptr := false
if m := g.table.find_method(left.sym, '==') {
Expand All @@ -110,7 +120,7 @@ fn (mut g Gen) infix_expr_eq_op(node ast.InfixExpr) {
g.gen_plain_infix_expr(node)
return
}
is_none_check := node.left_type.has_flag(.option) && node.right is ast.None
is_none_check := left_type.has_flag(.option) && node.right is ast.None
if is_none_check {
g.gen_is_none_check(node)
} else if (left.typ.is_ptr() && right.typ.is_int())
Expand Down
34 changes: 34 additions & 0 deletions vlib/v/tests/comptime_eq_test.v
@@ -0,0 +1,34 @@
struct Abc {
id int
name string
letter string
}

fn join[T](mut old T, new T) {
$if T is $struct {
default := T{}
$for field in T.fields {
if new.$(field.name) != default.$(field.name) {
old.$(field.name) = new.$(field.name)
}
}
}
}

fn test_main() {
mut a := Abc{
name: 'Peter'
letter: 'a'
}
b := Abc{
id: 1
letter: 'b'
}
join(mut a, b)

assert a == Abc{
name: 'Peter'
id: 1
letter: 'b'
}
}

0 comments on commit 4a99b1e

Please sign in to comment.