Skip to content

Commit 7d29afe

Browse files
authored
checker: optimize error messages for must specify the generic type names (fix #20362) (#20382)
1 parent 6e95f41 commit 7d29afe

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

vlib/v/checker/fn.v

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,26 +232,27 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
232232
c.error('Result type argument is not supported currently', param.type_pos)
233233
}
234234
arg_typ_sym := c.table.sym(param.typ)
235+
pure_sym_name := arg_typ_sym.embed_name()
235236
if arg_typ_sym.info is ast.Struct {
236237
if !param.typ.is_ptr() && arg_typ_sym.info.is_heap { // set auto_heap to promote value parameter
237238
mut v := node.scope.find_var(param.name) or { continue }
238239
v.is_auto_heap = true
239240
}
240241
if arg_typ_sym.info.generic_types.len > 0 && !param.typ.has_flag(.generic)
241242
&& arg_typ_sym.info.concrete_types.len == 0 {
242-
c.error('generic struct `${arg_typ_sym.name}` in fn declaration must specify the generic type names, e.g. ${arg_typ_sym.name}[T]',
243+
c.error('generic struct `${pure_sym_name}` in fn declaration must specify the generic type names, e.g. ${pure_sym_name}[T]',
243244
param.type_pos)
244245
}
245246
} else if arg_typ_sym.info is ast.Interface {
246247
if arg_typ_sym.info.generic_types.len > 0 && !param.typ.has_flag(.generic)
247248
&& arg_typ_sym.info.concrete_types.len == 0 {
248-
c.error('generic interface `${arg_typ_sym.name}` in fn declaration must specify the generic type names, e.g. ${arg_typ_sym.name}[T]',
249+
c.error('generic interface `${pure_sym_name}` in fn declaration must specify the generic type names, e.g. ${pure_sym_name}[T]',
249250
param.type_pos)
250251
}
251252
} else if arg_typ_sym.info is ast.SumType {
252253
if arg_typ_sym.info.generic_types.len > 0 && !param.typ.has_flag(.generic)
253254
&& arg_typ_sym.info.concrete_types.len == 0 {
254-
c.error('generic sumtype `${arg_typ_sym.name}` in fn declaration must specify the generic type names, e.g. ${arg_typ_sym.name}[T]',
255+
c.error('generic sumtype `${pure_sym_name}` in fn declaration must specify the generic type names, e.g. ${pure_sym_name}[T]',
255256
param.type_pos)
256257
}
257258
}
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
vlib/v/checker/tests/generics_method_receiver_type_err_b.vv:8:12: error: generic struct `ConsumableResources` in fn declaration must specify the generic type names, e.g. ConsumableResources[T]
2-
6 | used_resources map[T]Resources
1+
vlib/v/checker/tests/generics_method_receiver_type_err_b.vv:9:12: error: generic struct `ConsumableResources` in fn declaration must specify the generic type names, e.g. ConsumableResources[T]
32
7 | }
4-
8 | pub fn (cr &ConsumableResources) get_total_resources() Resources {
3+
8 |
4+
9 | pub fn (cr &ConsumableResources) get_total_resources() Resources {
55
| ~~~~~~~~~~~~~~~~~~~~
6-
9 | return cr.total_resources
7-
10 | }
6+
10 | return cr.total_resources
7+
11 | }
8+
vlib/v/checker/tests/generics_method_receiver_type_err_b.vv:19:11: error: generic struct `Foo` in fn declaration must specify the generic type names, e.g. Foo[T]
9+
17 | }
10+
18 |
11+
19 | pub fn (f Foo) method() {
12+
| ~~~
13+
20 | }
14+
21 |
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1-
pub struct Resources{}
1+
pub struct Resources {}
22

33
pub struct ConsumableResources[T] {
44
mut:
55
total_resources Resources
6-
used_resources map[T]Resources
6+
used_resources map[T]Resources
77
}
8+
89
pub fn (cr &ConsumableResources) get_total_resources() Resources {
910
return cr.total_resources
1011
}
1112

13+
// for issue 20362
14+
struct Foo[T] {}
15+
16+
pub fn new_foo[F](arg F) !Foo[F] {
17+
}
18+
19+
pub fn (f Foo) method() {
20+
}
21+
1222
fn main() {
1323
}

0 commit comments

Comments
 (0)