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:
parent
4bfe270c41
commit
28f85371b1
@ -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
|
is_none_check := node.left_type.has_flag(.option) && node.right is ast.None
|
||||||
if is_none_check {
|
if is_none_check {
|
||||||
g.gen_is_none_check(node)
|
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)
|
g.gen_plain_infix_expr(node)
|
||||||
} else if (left.typ.idx() == ast.string_type_idx || (!has_defined_eq_operator
|
} 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
|
&& left.unaliased.idx() == ast.string_type_idx)) && node.right is ast.StringLiteral
|
||||||
|
35
vlib/v/tests/comptime_indirection_check_test.v
Normal file
35
vlib/v/tests/comptime_indirection_check_test.v
Normal 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 {}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user