@@ -140,7 +140,6 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
140
140
if sym.kind == .placeholder || got_type.has_flag (.generic) {
141
141
c.error ('unknown type `${sym.name} `' , branch.cond.right.pos ())
142
142
}
143
-
144
143
if left is ast.SelectorExpr {
145
144
comptime_field_name = left.expr.str ()
146
145
c.comptime.type_map[comptime_field_name] = got_type
@@ -519,6 +518,9 @@ fn (mut c Checker) smartcast_if_conds(mut node ast.Expr, mut scope ast.Scope) {
519
518
c.smartcast (mut node.left, node.left_type, node.left_type.clear_flag (.option), mut
520
519
scope)
521
520
} else if node.op == .key_is {
521
+ if node.left_type == ast.Type (0 ) {
522
+ node.left_type = c.expr (mut node.left)
523
+ }
522
524
right_expr := node.right
523
525
right_type := match right_expr {
524
526
ast.TypeNode {
@@ -527,15 +529,24 @@ fn (mut c Checker) smartcast_if_conds(mut node ast.Expr, mut scope ast.Scope) {
527
529
ast.None {
528
530
ast.none_type_idx
529
531
}
532
+ ast.Ident {
533
+ if right_expr.name == c.comptime.comptime_for_variant_var {
534
+ println (c.comptime.type_map['${c.comptime.comptime_for_variant_var} .typ' ])
535
+ c.comptime.type_map['${c.comptime.comptime_for_variant_var} .typ' ]
536
+ } else {
537
+ c.error ('invalid type `${right_expr} `' , right_expr.pos)
538
+ ast.Type (0 )
539
+ }
540
+ }
530
541
else {
531
542
c.error ('invalid type `${right_expr} `' , right_expr.pos ())
532
543
ast.Type (0 )
533
544
}
534
545
}
535
546
if right_type != ast.Type (0 ) {
536
- left_sym := c.table.sym (node.left_type)
537
547
right_sym := c.table.sym (right_type)
538
548
mut expr_type := c.expr (mut node.left)
549
+ left_sym := c.table.sym (expr_type)
539
550
if left_sym.kind == .aggregate {
540
551
expr_type = (left_sym.info as ast.Aggregate ).sum_type
541
552
}
@@ -548,7 +559,8 @@ fn (mut c Checker) smartcast_if_conds(mut node ast.Expr, mut scope ast.Scope) {
548
559
expr_str := c.table.type_to_str (expr_type)
549
560
c.error ('cannot use type `${expect_str} ` as type `${expr_str} `' , node.pos)
550
561
}
551
- if node.left in [ast.Ident, ast.SelectorExpr] && node.right is ast.TypeNode {
562
+ if node.left in [ast.Ident, ast.SelectorExpr]
563
+ && node.right in [ast.ComptimeType, ast.TypeNode, ast.Ident] {
552
564
is_variable := if mut node.left is ast.Ident {
553
565
node.left.kind == .variable
554
566
} else {
0 commit comments