@@ -146,7 +146,7 @@ fn (mut c Checker) comptime_call(mut node ast.ComptimeCall) ast.Type {
146
146
147
147
fn (mut c Checker) comptime_selector (mut node ast.ComptimeSelector) ast.Type {
148
148
node.left_type = c.expr (node.left)
149
- expr_type := c.unwrap_generic (c.expr (node.field_expr))
149
+ mut expr_type := c.unwrap_generic (c.expr (node.field_expr))
150
150
expr_sym := c.table.sym (expr_type)
151
151
if expr_type != ast.string_type {
152
152
c.error ('expected `string` instead of `${expr_sym.name} ` (e.g. `field.name`)' ,
@@ -158,6 +158,10 @@ fn (mut c Checker) comptime_selector(mut node ast.ComptimeSelector) ast.Type {
158
158
c.error ('compile time field access can only be used when iterating over `T.fields`' ,
159
159
left_pos)
160
160
}
161
+ expr_type = c.get_comptime_selector_type (node, ast.void_type)
162
+ if expr_type != ast.void_type {
163
+ return expr_type
164
+ }
161
165
expr_name := node.field_expr.expr.str ()
162
166
if expr_name in c.comptime_fields_type {
163
167
return c.comptime_fields_type[expr_name]
@@ -755,6 +759,18 @@ fn (mut c Checker) comptime_if_branch(cond ast.Expr, pos token.Pos) ComptimeBran
755
759
return .unknown
756
760
}
757
761
762
+ // get_comptime_selector_type retrieves the var.$(field.name) type when field_name is 'name' otherwise default_type is returned
763
+ [inline ]
764
+ fn (mut c Checker) get_comptime_selector_type (node ast.ComptimeSelector, default_type ast.Type) ast.Type {
765
+ if node.field_expr is ast.SelectorExpr
766
+ && c.check_comptime_is_field_selector (node.field_expr as ast.SelectorExpr )
767
+ && (node.field_expr as ast.SelectorExpr ).field_name == 'name' {
768
+ return c.unwrap_generic (c.comptime_fields_default_type)
769
+ }
770
+ return default_type
771
+ }
772
+
773
+ // check_comptime_is_field_selector checks if the SelectorExpr is related to $for variable
758
774
[inline ]
759
775
fn (mut c Checker) check_comptime_is_field_selector (node ast.SelectorExpr) bool {
760
776
if c.inside_comptime_for_field && node.expr is ast.Ident {
@@ -763,6 +779,7 @@ fn (mut c Checker) check_comptime_is_field_selector(node ast.SelectorExpr) bool
763
779
return false
764
780
}
765
781
782
+ // check_comptime_is_field_selector_bool checks if the SelectorExpr is related to field.is_* boolean fields
766
783
[inline ]
767
784
fn (mut c Checker) check_comptime_is_field_selector_bool (node ast.SelectorExpr) bool {
768
785
if c.check_comptime_is_field_selector (node) {
@@ -772,6 +789,7 @@ fn (mut c Checker) check_comptime_is_field_selector_bool(node ast.SelectorExpr)
772
789
return false
773
790
}
774
791
792
+ // get_comptime_selector_bool_field evaluates the bool value for field.is_* fields
775
793
fn (mut c Checker) get_comptime_selector_bool_field (field_name string ) bool {
776
794
field := c.comptime_for_field_value
777
795
field_typ := c.comptime_fields_default_type
0 commit comments