mirror of
https://github.com/vlang/v.git
synced 2025-09-18 20:07:02 -04:00
checker, cgen: fix comptime method and field name checking (#18402)
This commit is contained in:
parent
af8df871d1
commit
83e30a8104
@ -220,6 +220,30 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if branch.cond.op in [.eq, .ne] && left is ast.SelectorExpr
|
||||||
|
&& right is ast.StringLiteral {
|
||||||
|
if left.expr.str() == c.comptime_for_field_var {
|
||||||
|
if left.field_name == 'name' {
|
||||||
|
is_comptime_type_is_expr = true
|
||||||
|
match branch.cond.op {
|
||||||
|
.eq {
|
||||||
|
skip_state = if c.comptime_for_field_value.name == right.val.str() {
|
||||||
|
ComptimeBranchSkipState.eval
|
||||||
|
} else {
|
||||||
|
ComptimeBranchSkipState.skip
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.ne {
|
||||||
|
skip_state = if c.comptime_for_field_value.name == right.val.str() {
|
||||||
|
ComptimeBranchSkipState.skip
|
||||||
|
} else {
|
||||||
|
ComptimeBranchSkipState.eval
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -555,22 +555,30 @@ fn (mut g Gen) comptime_if_cond(cond ast.Expr, pkg_exist bool) (bool, bool) {
|
|||||||
if selector.expr is ast.Ident && selector.field_name == 'name' {
|
if selector.expr is ast.Ident && selector.field_name == 'name' {
|
||||||
if g.comptime_for_method_var.len > 0
|
if g.comptime_for_method_var.len > 0
|
||||||
&& (selector.expr as ast.Ident).name == g.comptime_for_method_var {
|
&& (selector.expr as ast.Ident).name == g.comptime_for_method_var {
|
||||||
is_equal := g.comptime_for_method == cond.right.val
|
is_true := if cond.op == .eq {
|
||||||
if is_equal {
|
g.comptime_for_method == cond.right.val
|
||||||
|
} else {
|
||||||
|
g.comptime_for_method != cond.right.val
|
||||||
|
}
|
||||||
|
if is_true {
|
||||||
g.write('1')
|
g.write('1')
|
||||||
} else {
|
} else {
|
||||||
g.write('0')
|
g.write('0')
|
||||||
}
|
}
|
||||||
return is_equal, true
|
return is_true, true
|
||||||
} else if g.comptime_for_field_var.len > 0
|
} else if g.comptime_for_field_var.len > 0
|
||||||
&& (selector.expr as ast.Ident).name == g.comptime_for_field_var {
|
&& (selector.expr as ast.Ident).name == g.comptime_for_field_var {
|
||||||
is_equal := g.comptime_for_field_value.name == cond.right.val
|
is_true := if cond.op == .eq {
|
||||||
if is_equal {
|
g.comptime_for_field_value.name == cond.right.val
|
||||||
|
} else {
|
||||||
|
g.comptime_for_field_value.name != cond.right.val
|
||||||
|
}
|
||||||
|
if is_true {
|
||||||
g.write('1')
|
g.write('1')
|
||||||
} else {
|
} else {
|
||||||
g.write('0')
|
g.write('0')
|
||||||
}
|
}
|
||||||
return is_equal, true
|
return is_true, true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if cond.right is ast.IntegerLiteral {
|
} else if cond.right is ast.IntegerLiteral {
|
||||||
|
22
vlib/v/tests/comptime_field_name_check_test.v
Normal file
22
vlib/v/tests/comptime_field_name_check_test.v
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
struct Struct {
|
||||||
|
a int
|
||||||
|
txt string
|
||||||
|
array []string
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_string[T](s T) string {
|
||||||
|
$for field in T.fields {
|
||||||
|
$if field.name == 'txt' {
|
||||||
|
println(field.name)
|
||||||
|
return s.$(field.name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_main() {
|
||||||
|
s := Struct{
|
||||||
|
txt: 'hello'
|
||||||
|
}
|
||||||
|
assert get_string(s) == 'hello'
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user