@@ -363,7 +363,7 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
363
363
}
364
364
} else {
365
365
// smartcast sumtypes and interfaces when using `is`
366
- c.smartcast_if_conds (mut branch.cond, mut branch.scope)
366
+ c.smartcast_if_conds (mut branch.cond, mut branch.scope, node )
367
367
if node_is_expr {
368
368
c.stmts_ending_with_expression (mut branch.stmts, c.expected_or_type)
369
369
} else {
@@ -535,11 +535,11 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
535
535
return node.typ
536
536
}
537
537
538
- fn (mut c Checker) smartcast_if_conds (mut node ast.Expr, mut scope ast.Scope) {
538
+ fn (mut c Checker) smartcast_if_conds (mut node ast.Expr, mut scope ast.Scope, control_expr ast.Expr ) {
539
539
if mut node is ast.InfixExpr {
540
540
if node.op == .and {
541
- c.smartcast_if_conds (mut node.left, mut scope)
542
- c.smartcast_if_conds (mut node.right, mut scope)
541
+ c.smartcast_if_conds (mut node.left, mut scope, control_expr )
542
+ c.smartcast_if_conds (mut node.right, mut scope, control_expr )
543
543
} else if node.left is ast.Ident && node.op == .ne && node.right is ast.None {
544
544
if node.left is ast.Ident && c.comptime.get_ct_type_var (node.left) == .smartcast {
545
545
node.left_type = c.comptime.get_comptime_var_type (node.left)
@@ -624,7 +624,26 @@ fn (mut c Checker) smartcast_if_conds(mut node ast.Expr, mut scope ast.Scope) {
624
624
}
625
625
}
626
626
} else if mut node is ast.Likely {
627
- c.smartcast_if_conds (mut node.expr, mut scope)
627
+ c.smartcast_if_conds (mut node.expr, mut scope, control_expr)
628
+ } else if control_expr is ast.IfExpr && mut node is ast.NodeError { // IfExpr else branch
629
+ if control_expr.branches.len != 2 {
630
+ return
631
+ }
632
+ mut first_cond := control_expr.branches[0 ].cond
633
+ // handles unwrapping on if var == none { /**/ } else { /*unwrapped var*/ }
634
+ if mut first_cond is ast.InfixExpr {
635
+ if first_cond.left is ast.Ident && first_cond.op == .eq && first_cond.right is ast.None {
636
+ if first_cond.left is ast.Ident
637
+ && c.comptime.get_ct_type_var (first_cond.left) == .smartcast {
638
+ first_cond.left_type = c.comptime.get_comptime_var_type (first_cond.left)
639
+ c.smartcast (mut first_cond.left, first_cond.left_type, first_cond.left_type.clear_flag (.option), mut
640
+ scope, true )
641
+ } else {
642
+ c.smartcast (mut first_cond.left, first_cond.left_type, first_cond.left_type.clear_flag (.option), mut
643
+ scope, false )
644
+ }
645
+ }
646
+ }
628
647
}
629
648
}
630
649
0 commit comments