1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: improve error messages of missing generic type names (#16610)

This commit is contained in:
yuyi 2022-12-07 17:29:18 +08:00 committed by GitHub
parent 48a7de643e
commit 4eb81d2f05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 26 additions and 26 deletions

View File

@ -497,11 +497,11 @@ fn (mut c Checker) sum_type_decl(node ast.SumTypeDecl) {
} else if mut sym.info is ast.Struct {
if sym.info.is_generic {
if !variant.typ.has_flag(.generic) {
c.error('generic struct `${sym.name}` must specify generic type names, e.g. Foo[T]',
c.error('generic struct `${sym.name}` must specify generic type names, e.g. ${sym.name}[T]',
variant.pos)
}
if node.generic_types.len == 0 {
c.error('generic sumtype `${node.name}` must specify generic type names, e.g. Foo[T]',
c.error('generic sumtype `${node.name}` must specify generic type names, e.g. ${node.name}[T]',
node.name_pos)
} else {
for typ in sym.info.generic_types {

View File

@ -17,10 +17,10 @@ fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
if elem_info.generic_types.len > 0 && elem_info.concrete_types.len == 0
&& !node.elem_type.has_flag(.generic) {
if c.table.cur_concrete_types.len == 0 {
c.error('generic struct must specify type parameter, e.g. Foo[int]',
c.error('generic struct `${elem_sym.name}` must specify type parameter, e.g. ${elem_sym.name}[int]',
node.elem_type_pos)
} else {
c.error('generic struct must specify type parameter, e.g. Foo[T]',
c.error('generic struct `${elem_sym.name}` must specify type parameter, e.g. ${elem_sym.name}[T]',
node.elem_type_pos)
}
}
@ -29,10 +29,10 @@ fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
if elem_info.generic_types.len > 0 && elem_info.concrete_types.len == 0
&& !node.elem_type.has_flag(.generic) {
if c.table.cur_concrete_types.len == 0 {
c.error('generic interface must specify type parameter, e.g. Foo[int]',
c.error('generic interface `${elem_sym.name}` must specify type parameter, e.g. ${elem_sym.name}[int]',
node.elem_type_pos)
} else {
c.error('generic interface must specify type parameter, e.g. Foo[T]',
c.error('generic interface `${elem_sym.name}` must specify type parameter, e.g. ${elem_sym.name}[T]',
node.elem_type_pos)
}
}
@ -41,10 +41,10 @@ fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
if elem_info.generic_types.len > 0 && elem_info.concrete_types.len == 0
&& !node.elem_type.has_flag(.generic) {
if c.table.cur_concrete_types.len == 0 {
c.error('generic sumtype must specify type parameter, e.g. Foo[int]',
c.error('generic sumtype `${elem_sym.name}` must specify type parameter, e.g. ${elem_sym.name}[int]',
node.elem_type_pos)
} else {
c.error('generic sumtype must specify type parameter, e.g. Foo[T]',
c.error('generic sumtype `${elem_sym.name}` must specify type parameter, e.g. ${elem_sym.name}[T]',
node.elem_type_pos)
}
}
@ -307,10 +307,10 @@ fn (mut c Checker) map_init(mut node ast.MapInit) ast.Type {
if val_info.generic_types.len > 0 && val_info.concrete_types.len == 0
&& !info.value_type.has_flag(.generic) {
if c.table.cur_concrete_types.len == 0 {
c.error('generic struct `${val_sym.name}` must specify type parameter, e.g. Foo[int]',
c.error('generic struct `${val_sym.name}` must specify type parameter, e.g. ${val_sym.name}[int]',
node.pos)
} else {
c.error('generic struct `${val_sym.name}` must specify type parameter, e.g. Foo[T]',
c.error('generic struct `${val_sym.name}` must specify type parameter, e.g. ${val_sym.name}[T]',
node.pos)
}
}

View File

@ -105,7 +105,7 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
gs := c.table.sym(node.return_type)
if gs.info is ast.Struct {
if gs.info.is_generic && !node.return_type.has_flag(.generic) {
c.error('return generic struct in fn declaration must specify the generic type names, e.g. Foo[T]',
c.error('return generic struct `${gs.name}` in fn declaration must specify the generic type names, e.g. ${gs.name}[T]',
node.return_type_pos)
}
}
@ -224,19 +224,19 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
}
if arg_typ_sym.info.generic_types.len > 0 && !param.typ.has_flag(.generic)
&& arg_typ_sym.info.concrete_types.len == 0 {
c.error('generic struct in fn declaration must specify the generic type names, e.g. Foo[T]',
c.error('generic struct `${arg_typ_sym.name}` in fn declaration must specify the generic type names, e.g. ${arg_typ_sym.name}[T]',
param.type_pos)
}
} else if arg_typ_sym.info is ast.Interface {
if arg_typ_sym.info.generic_types.len > 0 && !param.typ.has_flag(.generic)
&& arg_typ_sym.info.concrete_types.len == 0 {
c.error('generic interface in fn declaration must specify the generic type names, e.g. Foo[T]',
c.error('generic interface `${arg_typ_sym.name}` in fn declaration must specify the generic type names, e.g. ${arg_typ_sym.name}[T]',
param.type_pos)
}
} else if arg_typ_sym.info is ast.SumType {
if arg_typ_sym.info.generic_types.len > 0 && !param.typ.has_flag(.generic)
&& arg_typ_sym.info.concrete_types.len == 0 {
c.error('generic sumtype in fn declaration must specify the generic type names, e.g. Foo[T]',
c.error('generic sumtype `${arg_typ_sym.name}` in fn declaration must specify the generic type names, e.g. ${arg_typ_sym.name}[T]',
param.type_pos)
}
}

View File

@ -211,7 +211,7 @@ fn (mut c Checker) interface_decl(mut node ast.InterfaceDecl) {
}
}
if node.generic_types.len == 0 && has_generic_types {
c.error('generic interface declaration must specify the generic type names, e.g. Foo[T]',
c.error('generic interface `${node.name}` declaration must specify the generic type names, e.g. ${node.name}[T]',
node.pos)
}
}

View File

@ -165,7 +165,7 @@ fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
}
}
if node.generic_types.len == 0 && has_generic_types {
c.error('generic struct declaration must specify the generic type names, e.g. Foo[T]',
c.error('generic struct `${node.name}` declaration must specify the generic type names, e.g. ${node.name}[T]',
node.pos)
}
}

View File

@ -4,7 +4,7 @@ vlib/v/checker/tests/array_of_generic_struct_init_err.vv:6:6: warning: unused va
6 | mut arr := []Item{}
| ~~~
7 | }
vlib/v/checker/tests/array_of_generic_struct_init_err.vv:6:15: error: generic struct must specify type parameter, e.g. Foo[int]
vlib/v/checker/tests/array_of_generic_struct_init_err.vv:6:15: error: generic struct `Item` must specify type parameter, e.g. Item[int]
4 |
5 | fn main() {
6 | mut arr := []Item{}

View File

@ -1,4 +1,4 @@
vlib/v/checker/tests/generic_interface_missing_type_names_err.vv:16:17: error: generic interface must specify type parameter, e.g. Foo[int]
vlib/v/checker/tests/generic_interface_missing_type_names_err.vv:16:17: error: generic interface `Output` must specify type parameter, e.g. Output[int]
14 |
15 | fn main() {
16 | mut outs := []Output{}

View File

@ -1,4 +1,4 @@
vlib/v/checker/tests/generic_sumtype_decl_err_a.vv:7:24: error: generic struct `Just` must specify generic type names, e.g. Foo[T]
vlib/v/checker/tests/generic_sumtype_decl_err_a.vv:7:24: error: generic struct `Just` must specify generic type names, e.g. Just[T]
5 | struct Nothing {}
6 |
7 | type Maybe = Nothing | Just

View File

@ -1,4 +1,4 @@
vlib/v/checker/tests/generic_sumtype_decl_err_b.vv:7:6: error: generic sumtype `Maybe` must specify generic type names, e.g. Foo[T]
vlib/v/checker/tests/generic_sumtype_decl_err_b.vv:7:6: error: generic sumtype `Maybe` must specify generic type names, e.g. Maybe[T]
5 | struct Nothing {}
6 |
7 | type Maybe = Nothing | Just[T]

View File

@ -1,4 +1,4 @@
vlib/v/checker/tests/generics_fn_arg_type_err.vv:14:23: error: generic interface in fn declaration must specify the generic type names, e.g. Foo[T]
vlib/v/checker/tests/generics_fn_arg_type_err.vv:14:23: error: generic interface `List` in fn declaration must specify the generic type names, e.g. List[T]
12 | }
13 |
14 | fn do_list_thing(list List) { // <--- Error here

View File

@ -1,4 +1,4 @@
vlib/v/checker/tests/generics_fn_return_generic_struct_err.vv:13:32: error: return generic struct in fn declaration must specify the generic type names, e.g. Foo[T]
vlib/v/checker/tests/generics_fn_return_generic_struct_err.vv:13:32: error: return generic struct `GenericChannelStruct` in fn declaration must specify the generic type names, e.g. GenericChannelStruct[T]
11 | }
12 |
13 | pub fn new_channel_struct[T]() GenericChannelStruct {

View File

@ -1,4 +1,4 @@
vlib/v/checker/tests/generics_interface_declaration_err.vv:1:1: error: generic interface declaration must specify the generic type names, e.g. Foo[T]
vlib/v/checker/tests/generics_interface_declaration_err.vv:1:1: error: generic interface `Expr` declaration must specify the generic type names, e.g. Expr[T]
1 | interface Expr {
| ~~~~~~~~~~~~~~~~
2 | accept(v Visitor<R>) R

View File

@ -1,4 +1,4 @@
vlib/v/checker/tests/generics_method_receiver_type_err.vv:6:11: error: generic struct in fn declaration must specify the generic type names, e.g. Foo[T]
vlib/v/checker/tests/generics_method_receiver_type_err.vv:6:11: error: generic struct `Node` in fn declaration must specify the generic type names, e.g. Node[T]
4 | }
5 |
6 | pub fn (x Node) str() string {

View File

@ -1,4 +1,4 @@
vlib/v/checker/tests/generics_struct_declaration_err.vv:5:1: error: generic struct declaration must specify the generic type names, e.g. Foo[T]
vlib/v/checker/tests/generics_struct_declaration_err.vv:5:1: error: generic struct `MyGenericChannelStruct` declaration must specify the generic type names, e.g. MyGenericChannelStruct[T]
3 | }
4 |
5 | struct MyGenericChannelStruct {

View File

@ -4,7 +4,7 @@ vlib/v/checker/tests/map_of_generic_struct_init_err.vv:6:6: warning: unused vari
6 | mut m := map[string]Item{}
| ^
7 | }
vlib/v/checker/tests/map_of_generic_struct_init_err.vv:6:11: error: generic struct `Item` must specify type parameter, e.g. Foo[int]
vlib/v/checker/tests/map_of_generic_struct_init_err.vv:6:11: error: generic struct `Item` must specify type parameter, e.g. Item[int]
4 |
5 | fn main() {
6 | mut m := map[string]Item{}