@@ -114,7 +114,10 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
114114 c.cur_ct_id++
115115 branch.id = c.cur_ct_id
116116 }
117- idx_str := comptime_branch_context_str + '|id=${branch.id} |'
117+ mut idx_str := comptime_branch_context_str + '|id=${branch.id} |'
118+ if c.comptime.inside_comptime_for && c.comptime.comptime_for_field_var != '' {
119+ idx_str + = '|field_type=${c.comptime.comptime_for_field_type} |'
120+ }
118121 c.comptime.inside_comptime_if = true
119122 mut sb := strings.new_builder (256 )
120123 comptime_if_result , comptime_if_multi_pass_branch = c.comptime_if_cond (mut branch.cond, mut
@@ -186,7 +189,10 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
186189 c.cur_ct_id++
187190 branch.id = c.cur_ct_id
188191 }
189- idx_str := comptime_branch_context_str + '|id=${branch.id} |'
192+ mut idx_str := comptime_branch_context_str + '|id=${branch.id} |'
193+ if c.comptime.inside_comptime_for && c.comptime.comptime_for_field_var != '' {
194+ idx_str + = '|field_type=${c.comptime.comptime_for_field_type} |'
195+ }
190196 c.table.comptime_is_true[idx_str] = ast.ComptTimeCondResult{
191197 val: comptime_if_result
192198 c_str: ''
@@ -517,13 +523,35 @@ fn (mut c Checker) smartcast_if_conds(mut node ast.Expr, mut scope ast.Scope, co
517523 || (node.left is ast.SelectorExpr && node.left.is_mut) {
518524 c.fail_if_immutable (mut node.left)
519525 }
520- if node.left is ast.Ident && c.comptime.get_ct_type_var (node.left) == .smartcast {
521- node.left_type = c.type_resolver.get_type (node.left)
522- c.smartcast (mut node.left, node.left_type, node.left_type.clear_flag (.option), mut
523- scope, true , true )
526+ if c.comptime.inside_comptime_for && c.comptime.comptime_for_field_var != ''
527+ && node.left is ast.Ident {
528+ if mut node.left is ast.Ident {
529+ if mut node.left.obj is ast.Var {
530+ if node.left.obj.ct_type_var == .field_var {
531+ scope.register (ast.Var{
532+ name: node.left.name
533+ typ: node.left_type
534+ pos: node.left.pos
535+ is_used: true
536+ is_mut: node.left.is_mut
537+ is_inherited: node.left.obj.is_inherited
538+ is_unwrapped: true
539+ orig_type: node.left_type
540+ ct_type_var: .field_var
541+ ct_type_unwrapped: true
542+ })
543+ }
544+ }
545+ }
524546 } else {
525- c.smartcast (mut node.left, node.left_type, node.left_type.clear_flag (.option), mut
526- scope, false , true )
547+ if node.left is ast.Ident && c.comptime.get_ct_type_var (node.left) == .smartcast {
548+ node.left_type = c.type_resolver.get_type (node.left)
549+ c.smartcast (mut node.left, node.left_type, node.left_type.clear_flag (.option), mut
550+ scope, true , true )
551+ } else {
552+ c.smartcast (mut node.left, node.left_type, node.left_type.clear_flag (.option), mut
553+ scope, false , true )
554+ }
527555 }
528556 } else if node.op == .key_is {
529557 if node.left is ast.Ident && node.left.ct_expr {
@@ -593,9 +621,15 @@ fn (mut c Checker) smartcast_if_conds(mut node ast.Expr, mut scope ast.Scope, co
593621 node.left.pos)
594622 }
595623 }
596- if left_final_sym.kind in [.interface , .sum_type] {
624+ is_option_unwrap := node.left_type.has_flag (.option)
625+ && ! right_type.has_flag (.option)
626+ skip_smartcast := c.comptime.inside_comptime_for
627+ && c.comptime.comptime_for_field_var != '' && node.left is ast.Ident
628+ && (node.left as ast.Ident ).name == c.comptime.comptime_for_field_var
629+ if ! skip_smartcast
630+ && (left_final_sym.kind in [.interface , .sum_type] || is_option_unwrap) {
597631 c.smartcast (mut node.left, node.left_type, right_type, mut
598- scope, is_comptime, false )
632+ scope, is_comptime, is_option_unwrap )
599633 }
600634 }
601635 }
0 commit comments