Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v: fix sumtype option support #21101

Merged
merged 9 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/v_apps_and_modules_compile_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
echo "Clone https://github.com/DarpHome/discord.v/"
git clone https://github.com/DarpHome/discord.v/ discord && cd discord
echo "Checkout last known good commit"
git checkout 65448b687ae759e385c127c8739b97b6ac62d3e3
git checkout 533485c08f21df91ff62fea9477e7017d21f91c4
echo "Execute Tests"
v test .

Expand Down
1 change: 1 addition & 0 deletions vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,7 @@ pub:
pub mut:
is_arg bool // fn args should not be autofreed
is_auto_deref bool
is_unwrapped bool // ct type smartcast unwrapped
expr Expr
typ Type
orig_type Type // original sumtype type; 0 if it's not a sumtype
Expand Down
3 changes: 2 additions & 1 deletion vlib/v/ast/scope.v
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,11 @@ pub fn (mut s Scope) update_ct_var_kind(name string, kind ComptimeVarKind) {
}
}

pub fn (mut s Scope) update_smartcasts(name string, typ Type) {
pub fn (mut s Scope) update_smartcasts(name string, typ Type, is_unwrapped bool) {
mut obj := unsafe { s.objects[name] }
if mut obj is Var {
obj.smartcasts = [typ]
obj.is_unwrapped = is_unwrapped
}
}

Expand Down
3 changes: 2 additions & 1 deletion vlib/v/ast/str.v
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,8 @@ pub fn (x Expr) str() string {
return "'${x.val}'"
}
TypeNode {
return 'TypeNode(${global_table.type_str(x.typ)})'
opt_prefix := if x.typ.has_flag(.option) { '?' } else { '' }
return 'TypeNode(${opt_prefix}${global_table.type_str(x.typ)})'
}
TypeOf {
if x.is_type {
Expand Down
3 changes: 2 additions & 1 deletion vlib/v/ast/table.v
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,8 @@ fn (t &Table) sumtype_check_function_variant(parent_info SumType, variant Type,

fn (t &Table) sumtype_check_variant_in_type(parent_info SumType, variant Type, is_as bool) bool {
for v in parent_info.variants {
if v.idx() == variant.idx() && (!is_as || v.nr_muls() == variant.nr_muls()) {
if v.idx() == variant.idx() && variant.has_flag(.option) == v.has_flag(.option)
&& (!is_as || v.nr_muls() == variant.nr_muls()) {
return true
}
}
Expand Down
26 changes: 23 additions & 3 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,8 @@ fn (mut c Checker) sum_type_decl(node ast.SumTypeDecl) {
and use a reference to the sum type instead: `var := &${node.name}(${variant_name}${lb}val${rb})`')
c.error('sum type cannot hold a reference type', variant.pos)
}
if sym.name in names_used {
variant_name := c.table.type_to_str(variant.typ)
if variant_name in names_used {
c.error('sum type ${node.name} cannot hold the type `${sym.name}` more than once',
variant.pos)
} else if sym.kind in [.placeholder, .int_literal, .float_literal] {
Expand Down Expand Up @@ -696,7 +697,7 @@ and use a reference to the sum type instead: `var := &${node.name}(${variant_nam
if sym.name.trim_string_left(sym.mod + '.') == node.name {
c.error('sum type cannot hold itself', variant.pos)
}
names_used << sym.name
names_used << variant_name
}
}

Expand Down Expand Up @@ -3945,7 +3946,26 @@ fn (mut c Checker) smartcast(mut expr ast.Expr, cur_type ast.Type, to_type_ ast.
smartcasts << to_type
if var := scope.find_var(expr.name) {
if is_comptime && var.ct_type_var == .smartcast {
scope.update_smartcasts(expr.name, to_type)
if cur_type.has_flag(.option) && !to_type.has_flag(.option) {
if !var.is_unwrapped {
scope.register(ast.Var{
name: expr.name
typ: cur_type
pos: expr.pos
is_used: true
is_mut: expr.is_mut
is_inherited: is_inherited
smartcasts: [to_type]
orig_type: orig_type
ct_type_var: ct_type_var
is_unwrapped: true
})
} else {
scope.update_smartcasts(expr.name, to_type, true)
}
} else {
scope.update_smartcasts(expr.name, to_type, false)
}
return
}
}
Expand Down
10 changes: 8 additions & 2 deletions vlib/v/checker/if.v
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,14 @@ fn (mut c Checker) smartcast_if_conds(mut node ast.Expr, mut scope ast.Scope) {
c.smartcast_if_conds(mut node.left, mut scope)
c.smartcast_if_conds(mut node.right, mut scope)
} else if node.left is ast.Ident && node.op == .ne && node.right is ast.None {
c.smartcast(mut node.left, node.left_type, node.left_type.clear_flag(.option), mut
scope, false)
if node.left is ast.Ident && c.comptime.get_ct_type_var(node.left) == .smartcast {
node.left_type = c.comptime.get_comptime_var_type(node.left)
c.smartcast(mut node.left, node.left_type, node.left_type.clear_flag(.option), mut
scope, true)
} else {
c.smartcast(mut node.left, node.left_type, node.left_type.clear_flag(.option), mut
scope, false)
}
} else if node.op == .key_is {
if node.left is ast.Ident && c.comptime.is_comptime_var(node.left) {
node.left_type = c.comptime.get_comptime_var_type(node.left)
Expand Down
7 changes: 6 additions & 1 deletion vlib/v/comptime/comptimeinfo.v
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ pub fn (mut ct ComptimeInfo) get_comptime_var_type(node ast.Expr) ast.Type {
node.obj.typ
}
.smartcast {
ct.type_map['${ct.comptime_for_variant_var}.typ'] or { node.obj.typ }
ctyp := ct.type_map['${ct.comptime_for_variant_var}.typ'] or { node.obj.typ }
return if (node.obj as ast.Var).is_unwrapped {
ctyp.clear_flag(.option)
} else {
ctyp
}
}
.key_var, .value_var {
// key and value variables from normal for stmt
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/gen/c/auto_eq_methods.v
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ fn (mut g Gen) gen_sumtype_equality_fn(left_type ast.Type) string {
fn_builder.writeln('\tif (${left_typ} == ${right_typ} && ${right_typ} == 0) { return true; } // uninitialized')
for typ in info.variants {
variant := g.unwrap(typ)
fn_builder.writeln('\tif (${left_typ} == ${variant.typ.idx()}) {')
name := '_${variant.sym.cname}'
fn_builder.writeln('\tif (${left_typ} == ${int(variant.typ)}) {')
name := '_${g.get_sumtype_variant_name(variant.typ, variant.sym.cname)}'

left_arg := g.read_field(left_type, name, 'a')
right_arg := g.read_field(left_type, name, 'b')
Expand Down
10 changes: 5 additions & 5 deletions vlib/v/gen/c/auto_str_methods.v
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,10 @@ fn (mut g Gen) gen_str_for_union_sum_type(info ast.SumType, styp string, typ_str
{_SLIT("${clean_sum_type_v_type_name}(\'"), ${c.si_s_code}, {.d_s = ${val}}},
{_SLIT("\')"), 0, {.d_c = 0 }}
}))'
fn_builder.write_string('\t\tcase ${typ.idx()}: return ${res};\n')
fn_builder.write_string('\t\tcase ${int(typ)}: return ${res};\n')
} else {
mut val := '${func_name}(${deref}(${typ_name}*)x._${sym.cname}'
mut val := '${func_name}(${deref}(${typ_name}*)x._${g.get_sumtype_variant_name(typ,
sym.cname)}'
if should_use_indent_func(sym.kind) && !sym_has_str_method {
val += ', indent_count'
}
Expand All @@ -468,7 +469,7 @@ fn (mut g Gen) gen_str_for_union_sum_type(info ast.SumType, styp string, typ_str
{_SLIT("${clean_sum_type_v_type_name}("), ${c.si_s_code}, {.d_s = ${val}}},
{_SLIT(")"), 0, {.d_c = 0 }}
}))'
fn_builder.write_string('\t\tcase ${typ.idx()}: return ${res};\n')
fn_builder.write_string('\t\tcase ${int(typ)}: return ${res};\n')
}
}
fn_builder.writeln('\t\tdefault: return _SLIT("unknown sum type value");')
Expand Down Expand Up @@ -1077,8 +1078,7 @@ fn (mut g Gen) gen_str_for_struct(info ast.Struct, lang ast.Language, styp strin
}

// c_struct_ptr handles the C struct argument for .str() method
@[inline]
pub fn c_struct_ptr(sym &ast.TypeSymbol, typ ast.Type, expects_ptr bool) string {
fn c_struct_ptr(sym &ast.TypeSymbol, typ ast.Type, expects_ptr bool) string {
if sym.is_c_struct() {
if typ.has_flag(.option) {
return ''
Expand Down
Loading