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

cgen, parser: fix option as possible match case for sumtype #21079

Merged
merged 4 commits into from
Mar 24, 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
4 changes: 3 additions & 1 deletion vlib/v/gen/c/auto_eq_methods.v
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ fn (mut g Gen) gen_sumtype_equality_fn(left_type ast.Type) string {
left_arg := g.read_field(left_type, name, 'a')
right_arg := g.read_field(left_type, name, 'b')

if variant.sym.kind == .string {
if variant.typ.has_flag(.option) {
fn_builder.writeln('\t\treturn ((*${left_arg}).state == 2 && (*${right_arg}).state == 2) || !memcmp(&(*${left_arg}).data, &(*${right_arg}).data, sizeof(${g.base_type(variant.typ)}));')
} else if variant.sym.kind == .string {
fn_builder.writeln('\t\treturn string__eq(*${left_arg}, *${right_arg});')
} else if variant.sym.kind == .sum_type && !typ.is_ptr() {
eq_fn := g.gen_sumtype_equality_fn(typ)
Expand Down
5 changes: 3 additions & 2 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -6433,13 +6433,14 @@ fn (mut g Gen) write_types(symbols []&ast.TypeSymbol) {
g.type_definitions.writeln('\tunion {')
for variant in sym.info.variants {
variant_sym := g.table.sym(variant)
mut var := variant.ref()
mut var := if variant.has_flag(.option) { variant } else { variant.ref() }
if variant_sym.info is ast.FnType {
if variant_sym.info.is_anon {
var = variant
}
}
g.type_definitions.writeln('\t\t${g.typ(var)} _${variant_sym.cname};')
var_type := if variant.has_flag(.option) { '${g.typ(var)}*' } else { g.typ(var) }
g.type_definitions.writeln('\t\t${var_type} _${variant_sym.cname};')
}
g.type_definitions.writeln('\t};')
g.type_definitions.writeln('\tint _typ;')
Expand Down
17 changes: 13 additions & 4 deletions vlib/v/parser/if_match.v
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,18 @@ fn (mut p Parser) is_only_array_type() bool {
return false
}

fn (mut p Parser) is_match_sumtype_type() bool {
is_option := p.tok.kind == .question
name_tok := if is_option { p.peek_tok } else { p.tok }
next_tok_kind := if is_option { p.peek_token(2).kind } else { p.peek_tok.kind }
next_next_tok := if is_option { p.peek_token(3) } else { p.peek_token(2) }

return name_tok.kind == .name && !(name_tok.lit == 'C' && next_tok_kind == .dot)
&& (((ast.builtin_type_names_matcher.matches(name_tok.lit) || name_tok.lit[0].is_capital())
&& next_tok_kind != .lpar) || (next_tok_kind == .dot && next_next_tok.lit.len > 0
&& next_next_tok.lit[0].is_capital()))
}
spytheman marked this conversation as resolved.
Show resolved Hide resolved

fn (mut p Parser) match_expr() ast.MatchExpr {
match_first_pos := p.tok.pos()
mut else_count := 0
Expand All @@ -248,10 +260,7 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
is_else = true
else_count += 1
p.next()
} else if (p.tok.kind == .name && !(p.tok.lit == 'C' && p.peek_tok.kind == .dot)
&& (((ast.builtin_type_names_matcher.matches(p.tok.lit) || p.tok.lit[0].is_capital())
&& p.peek_tok.kind != .lpar) || (p.peek_tok.kind == .dot && p.peek_token(2).lit.len > 0
&& p.peek_token(2).lit[0].is_capital()))) || p.is_only_array_type()
} else if p.is_match_sumtype_type() || p.is_only_array_type()
|| p.tok.kind == .key_fn
|| (p.tok.kind == .lsbr && p.peek_token(2).kind == .amp) {
mut types := []ast.Type{}
Expand Down
17 changes: 17 additions & 0 deletions vlib/v/tests/option_generic_sumtype_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
struct Undefined {}

type Foo[T] = ?T | Undefined

@[params]
struct Bar {
x Foo[bool] = Undefined{}
}

fn f(b Bar) Bar {
return dump(b)
}

fn test_main() {
a := f()
assert a == Bar{}
}
23 changes: 23 additions & 0 deletions vlib/v/tests/option_sum_type_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
fn is_opt(f Foo) bool {
return match f {
Baz {
false
}
// vfmt off
?Bar {
true
}
// vfmt on
}
}

fn test_main() {
dump(is_opt(Foo(?Bar(none))))
dump(is_opt(Foo(Baz{})))
}

struct Baz {}

struct Bar {}

type Foo = ?Bar | Baz