mirror of
https://github.com/vlang/v.git
synced 2025-09-08 14:51:53 -04:00
all: remove inline sum types completely
This commit is contained in:
parent
59c8f6b07b
commit
a1904154d3
@ -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
|
||||
}
|
@ -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)
|
||||
|
@ -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
|
||||
| ~~~~~~
|
||||
|
@ -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
|
||||
| ~~~~~~
|
||||
|
@ -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 | }
|
@ -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 | }
|
||||
|
@ -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 | }
|
@ -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'
|
||||
})
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user