Skip to content

Commit

Permalink
all: remove inline sum types completely
Browse files Browse the repository at this point in the history
  • Loading branch information
medvednikov committed Nov 8, 2024
1 parent 59c8f6b commit a190415
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 146 deletions.
13 changes: 0 additions & 13 deletions vlib/v/fmt/tests/inline_sum_type_keep.vv

This file was deleted.

50 changes: 3 additions & 47 deletions vlib/v/parser/parse_type.v
Original file line number Diff line number Diff line change
Expand Up @@ -409,50 +409,9 @@ fn (mut p Parser) parse_language() ast.Language {
// parse_inline_sum_type parses the type and registers it in case the type is an anonymous sum type.
// It also takes care of inline sum types where parse_type only parses a standalone type.
fn (mut p Parser) parse_inline_sum_type() ast.Type {
if !p.pref.is_fmt {
p.warn(
'inline sum types have been deprecated and will be removed on January 1, 2023 due ' +
'to complicating the language and the compiler too much; define named sum types with `type Foo = Bar | Baz` instead')
}
variants := p.parse_sum_type_variants()
if variants.len > 1 {
if variants.len > maximum_inline_sum_type_variants {
pos := variants[0].pos.extend(variants.last().pos)
p.warn_with_pos('an inline sum type expects a maximum of ${maximum_inline_sum_type_variants} types (${variants.len} were given)',
pos)
}
mut variant_names := []string{}
for variant in variants {
if variant.typ == 0 {
p.error_with_pos('unknown type for variant: ${variant}', variant.pos)
return ast.no_type
}
variant_names << p.table.sym(variant.typ).name
}
variant_names.sort()
// deterministic name
name := '_v_anon_sum_type_${variant_names.join('_')}'
variant_types := variants.map(it.typ)
prepend_mod_name := p.prepend_mod(name)
mut idx := p.table.find_type_idx(prepend_mod_name)
if idx > 0 {
return ast.new_type(idx)
}
idx = p.table.register_sym(ast.TypeSymbol{
kind: .sum_type
name: prepend_mod_name
cname: util.no_dots(prepend_mod_name)
mod: p.mod
info: ast.SumType{
is_anon: true
variants: variant_types
}
})
return ast.new_type(idx)
} else if variants.len == 1 {
return variants[0].typ
}
return ast.no_type
p.error('inline sum types have been deprecated and will be removed on January 1, 2023 due ' +
'to complicating the language and the compiler too much; define named sum types with `type Foo = Bar | Baz` instead')
return ast.void_type
}

// parse_sum_type_variants parses several types separated with a pipe and returns them as a list with at least one node.
Expand Down Expand Up @@ -589,9 +548,6 @@ fn (mut p Parser) parse_type() ast.Type {
p.error_with_pos('missing concrete type on generic type', option_pos.extend(p.prev_tok.pos()))
}
}
if is_option && sym.info is ast.SumType && sym.info.is_anon {
p.error_with_pos('an inline sum type cannot be an Option', option_pos.extend(p.prev_tok.pos()))
}

if is_option && sym.info is ast.Alias && sym.info.parent_type.has_flag(.option) {
alias_type_str := p.table.type_to_str(typ)
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/parser/tests/anon_sum_type_interface.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
vlib/v/parser/tests/anon_sum_type_interface.vv:2:6: warning: inline sum types have been deprecated and will be removed on January 1, 2023 due to complicating the language and the compiler too much; define named sum types with `type Foo = Bar | Baz` instead
vlib/v/parser/tests/anon_sum_type_interface.vv:2:6: error: inline sum types have been deprecated and will be removed on January 1, 2023 due to complicating the language and the compiler too much; define named sum types with `type Foo = Bar | Baz` instead
1 | interface Foo {
2 | bar string|int
| ~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/parser/tests/anon_sum_type_struct.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
vlib/v/parser/tests/anon_sum_type_struct.vv:2:6: warning: inline sum types have been deprecated and will be removed on January 1, 2023 due to complicating the language and the compiler too much; define named sum types with `type Foo = Bar | Baz` instead
vlib/v/parser/tests/anon_sum_type_struct.vv:2:6: error: inline sum types have been deprecated and will be removed on January 1, 2023 due to complicating the language and the compiler too much; define named sum types with `type Foo = Bar | Baz` instead
1 | struct Foo {
2 | bar string|int
| ~~~~~~
Expand Down
7 changes: 1 addition & 6 deletions vlib/v/parser/tests/inline_sum_type_option_err.out
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
vlib/v/parser/tests/inline_sum_type_option_err.vv:1:11: warning: inline sum types have been deprecated and will be removed on January 1, 2023 due to complicating the language and the compiler too much; define named sum types with `type Foo = Bar | Baz` instead
vlib/v/parser/tests/inline_sum_type_option_err.vv:1:11: error: inline sum types have been deprecated and will be removed on January 1, 2023 due to complicating the language and the compiler too much; define named sum types with `type Foo = Bar | Baz` instead
1 | fn foo() ?string | int {
| ~~~~~~
2 | return 0
3 | }
vlib/v/parser/tests/inline_sum_type_option_err.vv:1:10: error: an inline sum type cannot be an Option
1 | fn foo() ?string | int {
| ~~~~~~~~~~~~~
2 | return 0
3 | }
Original file line number Diff line number Diff line change
@@ -1,42 +1,7 @@
vlib/v/parser/tests/inline_sum_type_return_type_too_many_variants.vv:4:6: warning: inline sum types have been deprecated and will be removed on January 1, 2023 due to complicating the language and the compiler too much; define named sum types with `type Foo = Bar | Baz` instead
2 |
vlib/v/parser/tests/inline_sum_type_return_type_too_many_variants.vv:4:6: error: inline sum types have been deprecated and will be removed on January 1, 2023 due to complicating the language and the compiler too much; define named sum types with `type Foo = Bar | Baz` instead
2 |
3 | struct Foo {
4 | bar int|string|token.Pos|bool|u32
| ~~~
5 | }
6 |
vlib/v/parser/tests/inline_sum_type_return_type_too_many_variants.vv:4:6: warning: an inline sum type expects a maximum of 3 types (5 were given)
2 |
3 | struct Foo {
4 | bar int|string|token.Pos|bool|u32
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 | }
6 |
vlib/v/parser/tests/inline_sum_type_return_type_too_many_variants.vv:7:12: warning: inline sum types have been deprecated and will be removed on January 1, 2023 due to complicating the language and the compiler too much; define named sum types with `type Foo = Bar | Baz` instead
5 | }
6 |
7 | fn foo(arg int|string|token.Pos|bool|u32) int|string|token.Pos|bool|u32 {
| ~~~
8 | return 1
9 | }
vlib/v/parser/tests/inline_sum_type_return_type_too_many_variants.vv:7:12: warning: an inline sum type expects a maximum of 3 types (5 were given)
5 | }
6 |
7 | fn foo(arg int|string|token.Pos|bool|u32) int|string|token.Pos|bool|u32 {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 | return 1
9 | }
vlib/v/parser/tests/inline_sum_type_return_type_too_many_variants.vv:7:43: warning: inline sum types have been deprecated and will be removed on January 1, 2023 due to complicating the language and the compiler too much; define named sum types with `type Foo = Bar | Baz` instead
5 | }
6 |
7 | fn foo(arg int|string|token.Pos|bool|u32) int|string|token.Pos|bool|u32 {
| ~~~
8 | return 1
9 | }
vlib/v/parser/tests/inline_sum_type_return_type_too_many_variants.vv:7:43: warning: an inline sum type expects a maximum of 3 types (5 were given)
5 | }
6 |
7 | fn foo(arg int|string|token.Pos|bool|u32) int|string|token.Pos|bool|u32 {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 | return 1
9 | }
7 changes: 1 addition & 6 deletions vlib/v/parser/tests/option_sum_type_return_err.out
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
vlib/v/parser/tests/option_sum_type_return_err.vv:1:22: warning: inline sum types have been deprecated and will be removed on January 1, 2023 due to complicating the language and the compiler too much; define named sum types with `type Foo = Bar | Baz` instead
vlib/v/parser/tests/option_sum_type_return_err.vv:1:22: error: inline sum types have been deprecated and will be removed on January 1, 2023 due to complicating the language and the compiler too much; define named sum types with `type Foo = Bar | Baz` instead
1 | fn option_sumtype() ?string | int {
| ~~~~~~
2 | return 0
3 | }
vlib/v/parser/tests/option_sum_type_return_err.vv:1:21: error: an inline sum type cannot be an Option
1 | fn option_sumtype() ?string | int {
| ~~~~~~~~~~~~~
2 | return 0
3 | }
35 changes: 0 additions & 35 deletions vlib/v/slow_tests/inout/printing_sumtype_with_none.vv

This file was deleted.

0 comments on commit a190415

Please sign in to comment.