1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix comptime ptr comparison generated code (#18048)

This commit is contained in:
Felipe Pena 2023-04-26 14:40:28 -03:00 committed by GitHub
parent 4bfe270c41
commit 28f85371b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 1 deletions

View File

@ -100,7 +100,9 @@ fn (mut g Gen) infix_expr_eq_op(node ast.InfixExpr) {
is_none_check := node.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()) || (right.typ.is_ptr() && left.typ.is_int()) {
} else if (left.typ.is_ptr() && right.typ.is_int())
|| (right.typ.is_ptr() && left.typ.is_int())
|| (left.typ.is_ptr() && right.typ == ast.nil_type) {
g.gen_plain_infix_expr(node)
} else if (left.typ.idx() == ast.string_type_idx || (!has_defined_eq_operator
&& left.unaliased.idx() == ast.string_type_idx)) && node.right is ast.StringLiteral

View File

@ -0,0 +1,35 @@
struct Encoder {}
struct Writer {}
struct StructTypePointer[T] {
mut:
val &T
}
pub fn (e &Encoder) encode_value[T](val T, mut wr Writer) ! {
assert e.encode_struct[T](val, 1, mut wr)! == 'a'
}
fn (e &Encoder) encode_struct[U](val U, level int, mut wr Writer) !string {
$for field in U.fields {
if val.$(field.name) != unsafe { nil } {
$if field.indirections > 0 {
assert field.indirections == 1
return 'a'
} $else {
return 'b'
}
}
}
return 'z'
}
fn test_check() {
e := Encoder{}
mut sb := Writer{}
mut string_initialized_with_reference := 'ads'
e.encode_value(StructTypePointer[string]{ val: &string_initialized_with_reference }, mut
sb) or {}
}