Skip to content

Commit 1ff9f04

Browse files
authored
all: check error of generic symbol and cleanup generic symbol (fix #23072) (#23178)
1 parent b01f482 commit 1ff9f04

18 files changed

+141
-123
lines changed

vlib/v/fmt/tests/import_selective_input.vv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ fn f(v FnArg, vv ...FnArgVariadic) FnRet {
7676
return FnRet{}
7777
}
7878

79-
fn f2(v Generic<FnArgTypeParam1, FnArgTypeParam2>) Generic<FnRetTypeParam1, FnRetTypeParam2> {}
79+
fn f2(v Generic[FnArgTypeParam1, FnArgTypeParam2]) Generic[FnRetTypeParam1, FnRetTypeParam2] {}
8080

8181
fn f_generic[T](v FnArgGeneric[T]) FnRetGeneric[T] {
8282
return FnRetGeneric[T]{}

vlib/v/parser/parse_type.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,9 @@ fn (mut p Parser) parse_generic_inst_type(name string) ast.Type {
873873
p.error('too many levels of Parser.parse_generic_inst_type() calls: ${p.generic_type_level}, probably due to too many layers embedded generic type')
874874
return ast.void_type
875875
}
876+
if p.tok.kind == .lt {
877+
p.error('The generic symbol `<>` is obsolete, please replace it with `[]`')
878+
}
876879
mut bs_name := name
877880
mut bs_cname := name
878881
start_pos := p.tok.pos()

vlib/v/parser/parser.v

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3442,10 +3442,13 @@ fn (mut p Parser) dot_expr(left ast.Expr) ast.Expr {
34423442
fn (mut p Parser) parse_generic_types() ([]ast.Type, []string) {
34433443
mut types := []ast.Type{}
34443444
mut param_names := []string{}
3445-
if p.tok.kind !in [.lt, .lsbr] {
3445+
if p.tok.kind == .lt {
3446+
p.error('The generic symbol `<>` is obsolete, please replace it with `[]`')
3447+
}
3448+
if p.tok.kind != .lsbr {
34463449
return types, param_names
34473450
}
3448-
end_kind := if p.tok.kind == .lt { token.Kind.gt } else { token.Kind.rsbr }
3451+
end_kind := token.Kind.rsbr
34493452
p.next()
34503453
mut first_done := false
34513454
mut count := 0
@@ -3492,15 +3495,18 @@ fn (mut p Parser) parse_generic_types() ([]ast.Type, []string) {
34923495

34933496
fn (mut p Parser) parse_concrete_types() []ast.Type {
34943497
mut types := []ast.Type{}
3495-
if p.tok.kind !in [.lt, .lsbr] {
3498+
if p.tok.kind == .lt {
3499+
p.error('The generic symbol `<>` is obsolete, please replace it with `[]`')
3500+
}
3501+
if p.tok.kind != .lsbr {
34963502
return types
34973503
}
34983504
p.inside_fn_concrete_type = true
34993505
defer {
35003506
p.inside_fn_concrete_type = false
35013507
}
3502-
end_kind := if p.tok.kind == .lt { token.Kind.gt } else { token.Kind.rsbr }
3503-
p.next() // `<`
3508+
end_kind := token.Kind.rsbr
3509+
p.next() // `[`
35043510
mut first_done := false
35053511
for p.tok.kind !in [.eof, end_kind] {
35063512
if first_done {
@@ -3509,7 +3515,7 @@ fn (mut p Parser) parse_concrete_types() []ast.Type {
35093515
types << p.parse_type()
35103516
first_done = true
35113517
}
3512-
p.check(end_kind) // `>`
3518+
p.check(end_kind) // `]`
35133519
return types
35143520
}
35153521

vlib/v/parser/struct.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
668668
is_mut = true
669669
mut_pos = fields.len
670670
}
671-
if p.peek_tok.kind in [.lt, .lsbr] && p.peek_tok.is_next_to(p.tok) {
671+
if p.peek_tok.kind == .lsbr && p.peek_tok.is_next_to(p.tok) {
672672
if generic_types.len == 0 {
673673
p.error_with_pos('non-generic interface `${interface_name}` cannot define a generic method',
674674
p.peek_tok.pos())
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
vlib/v/parser/tests/duplicated_generic_err.vv:1:12: error: duplicated generic parameter `A`
2-
1 | fn test<A, A>() {}
2+
1 | fn test[A, A]() {}
33
| ^
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
fn test<A, A>() {}
1+
fn test[A, A]() {}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
vlib/v/parser/tests/generic_lowercase_err.vv:1:22: error: generic parameter needs to be uppercase
2-
1 | fn lowercase_generic<a>() {}
2+
1 | fn lowercase_generic[a]() {}
33
| ^
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
fn lowercase_generic<a>() {}
1+
fn lowercase_generic[a]() {}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
vlib/v/parser/tests/generic_struct_type_decl_err.vv:1:13: error: generic parameter name needs to be exactly one char
2-
1 | struct GMap<Hashable<K>, V> {
2+
1 | struct GMap[Hashable[K], V] {
33
| ~~~~~~~~
44
2 | map_ map[int]V
55
3 | }
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
struct GMap<Hashable<K>, V> {
1+
struct GMap[Hashable[K], V] {
22
map_ map[int]V
33
}
44

5-
fn (t GMap<Hashable<K>, V>) contains(k K) bool {
5+
fn (t GMap[Hashable[K], V]) contains(k K) bool {
66
return true // TODO
77
}
88

9-
fn (t GMap<Hashable<K>, V>) set(k K, v V) {
9+
fn (t GMap[Hashable[K], V]) set(k K, v V) {
1010
// TODO
1111
}
1212

1313
fn main() {
14-
x := GMap<string, string>{}
14+
x := GMap[string, string]{}
1515
x.set("hello", "world")
1616
println(x.contains("hello"))
1717
}

0 commit comments

Comments
 (0)