Skip to content

Commit e5100a8

Browse files
authored
cgen: allow alias sumtype smartcasting (fix #25085) (#25096)
1 parent b7cd854 commit e5100a8

File tree

5 files changed

+20
-5
lines changed

5 files changed

+20
-5
lines changed

vlib/v/checker/if.v

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -707,14 +707,15 @@ fn (mut c Checker) smartcast_if_conds(mut node ast.Expr, mut scope ast.Scope, co
707707
right_sym := c.table.sym(right_type)
708708
mut expr_type := c.unwrap_generic(node.left_type)
709709
left_sym := c.table.sym(expr_type)
710+
left_final_sym := c.table.final_sym(expr_type)
710711
if left_sym.kind == .aggregate {
711712
expr_type = (left_sym.info as ast.Aggregate).sum_type
712713
}
713714
if left_sym.kind == .interface {
714715
if right_sym.kind != .interface {
715716
c.type_implements(right_type, expr_type, node.pos)
716717
}
717-
} else if !c.check_types(right_type, expr_type) && left_sym.kind != .sum_type {
718+
} else if !c.check_types(right_type, expr_type) && left_final_sym.kind != .sum_type {
718719
expect_str := c.table.type_to_str(right_type)
719720
expr_str := c.table.type_to_str(expr_type)
720721
c.error('cannot use type `${expect_str}` as type `${expr_str}`', node.pos)
@@ -740,7 +741,7 @@ fn (mut c Checker) smartcast_if_conds(mut node ast.Expr, mut scope ast.Scope, co
740741
node.left.pos)
741742
}
742743
}
743-
if left_sym.kind in [.interface, .sum_type] {
744+
if left_final_sym.kind in [.interface, .sum_type] {
744745
c.smartcast(mut node.left, node.left_type, right_type, mut
745746
scope, is_comptime, false)
746747
}

vlib/v/checker/infix.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
736736
if typ != ast.none_type_idx {
737737
c.error('`${op}` can only be used to test for none in sql', node.pos)
738738
}
739-
} else if left_sym.kind !in [.interface, .sum_type]
739+
} else if left_final_sym.kind !in [.interface, .sum_type]
740740
&& !c.comptime.is_comptime(node.left) {
741741
c.error('`${op}` can only be used with interfaces and sum types',
742742
node.pos) // can be used in sql too, but keep err simple

vlib/v/gen/c/cgen.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5341,7 +5341,7 @@ fn (mut g Gen) ident(node ast.Ident) {
53415341
}
53425342
is_option = is_option || node.obj.orig_type.has_flag(.option)
53435343
if node.obj.smartcasts.len > 0 {
5344-
obj_sym := g.table.sym(g.unwrap_generic(node.obj.typ))
5344+
obj_sym := g.table.final_sym(g.unwrap_generic(node.obj.typ))
53455345
if !prevent_sum_type_unwrapping_once {
53465346
nested_unwrap := node.obj.smartcasts.len > 1
53475347
unwrap_sumtype := is_option && nested_unwrap && obj_sym.kind == .sum_type

vlib/v/gen/c/infix.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ fn (mut g Gen) infix_expr_in_optimization(left ast.Expr, left_type ast.Type, rig
845845

846846
// infix_expr_is_op generates code for `is` and `!is`
847847
fn (mut g Gen) infix_expr_is_op(node ast.InfixExpr) {
848-
mut left_sym := g.table.sym(g.unwrap_generic(g.type_resolver.get_type_or_default(node.left,
848+
mut left_sym := g.table.final_sym(g.unwrap_generic(g.type_resolver.get_type_or_default(node.left,
849849
node.left_type)))
850850
is_aggregate := node.left is ast.Ident && g.comptime.get_ct_type_var(node.left) == .aggregate
851851
right_sym := g.table.sym(node.right_type)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
type Sum = int | string
2+
type SumAlias = Sum
3+
4+
fn test_alias_sumtype_smartcast() {
5+
a_int := SumAlias(Sum(10))
6+
if a_int is int {
7+
assert a_int == 10
8+
}
9+
10+
a_str := SumAlias('foo')
11+
if a_str is string {
12+
assert a_str == 'foo'
13+
}
14+
}

0 commit comments

Comments
 (0)