From a1904154d3589a877885fe3eca879eadeda6f5fc Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 8 Nov 2024 22:07:51 +0300 Subject: [PATCH] all: remove inline sum types completely --- vlib/v/fmt/tests/inline_sum_type_keep.vv | 13 ----- vlib/v/parser/parse_type.v | 50 ++----------------- .../parser/tests/anon_sum_type_interface.out | 2 +- vlib/v/parser/tests/anon_sum_type_struct.out | 2 +- .../tests/inline_sum_type_option_err.out | 7 +-- ...sum_type_return_type_too_many_variants.out | 39 +-------------- .../tests/option_sum_type_return_err.out | 7 +-- .../inout/printing_sumtype_with_none.vv | 35 ------------- 8 files changed, 9 insertions(+), 146 deletions(-) delete mode 100644 vlib/v/fmt/tests/inline_sum_type_keep.vv delete mode 100644 vlib/v/slow_tests/inout/printing_sumtype_with_none.vv diff --git a/vlib/v/fmt/tests/inline_sum_type_keep.vv b/vlib/v/fmt/tests/inline_sum_type_keep.vv deleted file mode 100644 index 3bd9b8d44f..0000000000 --- a/vlib/v/fmt/tests/inline_sum_type_keep.vv +++ /dev/null @@ -1,13 +0,0 @@ -import v.token - -struct Foo { - bar string|int -} - -interface Egg { - milk string|int -} - -fn foo(bar string|int) int|string|token.Pos { - return 1 -} diff --git a/vlib/v/parser/parse_type.v b/vlib/v/parser/parse_type.v index 378751b78d..2f2352168d 100644 --- a/vlib/v/parser/parse_type.v +++ b/vlib/v/parser/parse_type.v @@ -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. @@ -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) diff --git a/vlib/v/parser/tests/anon_sum_type_interface.out b/vlib/v/parser/tests/anon_sum_type_interface.out index 6003242746..be2b75e494 100644 --- a/vlib/v/parser/tests/anon_sum_type_interface.out +++ b/vlib/v/parser/tests/anon_sum_type_interface.out @@ -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 | ~~~~~~ diff --git a/vlib/v/parser/tests/anon_sum_type_struct.out b/vlib/v/parser/tests/anon_sum_type_struct.out index ceeebba4a7..cda0e5f2dd 100644 --- a/vlib/v/parser/tests/anon_sum_type_struct.out +++ b/vlib/v/parser/tests/anon_sum_type_struct.out @@ -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 | ~~~~~~ diff --git a/vlib/v/parser/tests/inline_sum_type_option_err.out b/vlib/v/parser/tests/inline_sum_type_option_err.out index f635d1e6e6..0e34c91442 100644 --- a/vlib/v/parser/tests/inline_sum_type_option_err.out +++ b/vlib/v/parser/tests/inline_sum_type_option_err.out @@ -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 | } \ No newline at end of file diff --git a/vlib/v/parser/tests/inline_sum_type_return_type_too_many_variants.out b/vlib/v/parser/tests/inline_sum_type_return_type_too_many_variants.out index ee35ee43e0..eeeb91c878 100644 --- a/vlib/v/parser/tests/inline_sum_type_return_type_too_many_variants.out +++ b/vlib/v/parser/tests/inline_sum_type_return_type_too_many_variants.out @@ -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 | } diff --git a/vlib/v/parser/tests/option_sum_type_return_err.out b/vlib/v/parser/tests/option_sum_type_return_err.out index 5eea675337..083a95ee2e 100644 --- a/vlib/v/parser/tests/option_sum_type_return_err.out +++ b/vlib/v/parser/tests/option_sum_type_return_err.out @@ -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 | } \ No newline at end of file diff --git a/vlib/v/slow_tests/inout/printing_sumtype_with_none.vv b/vlib/v/slow_tests/inout/printing_sumtype_with_none.vv deleted file mode 100644 index 8462527804..0000000000 --- a/vlib/v/slow_tests/inout/printing_sumtype_with_none.vv +++ /dev/null @@ -1,35 +0,0 @@ -module main - -struct None {} - -struct MyStruct { - my_first_field string|none - my_second_field string|none - my_third_field string|None -} - -fn main() { - s := MyStruct{ - my_first_field: 'blah' - my_second_field: none - my_third_field: None{} - } - - println(if s.my_first_field is string { - 'first field is a string' - } else { - 'first field is none' - }) - - println(if s.my_second_field is none { - 'second field is none' - } else { - 'second field is a string' - }) - - println(if s.my_third_field is None { - 'third field is None' - } else { - 'third field is a string' - }) -}