Skip to content

Commit

Permalink
checker: optimize error messages for must specify the generic type na…
Browse files Browse the repository at this point in the history
…mes (fix #20362) (#20382)
  • Loading branch information
shove70 committed Jan 4, 2024
1 parent 6e95f41 commit 7d29afe
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
7 changes: 4 additions & 3 deletions vlib/v/checker/fn.v
Expand Up @@ -232,26 +232,27 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
c.error('Result type argument is not supported currently', param.type_pos)
}
arg_typ_sym := c.table.sym(param.typ)
pure_sym_name := arg_typ_sym.embed_name()
if arg_typ_sym.info is ast.Struct {
if !param.typ.is_ptr() && arg_typ_sym.info.is_heap { // set auto_heap to promote value parameter
mut v := node.scope.find_var(param.name) or { continue }
v.is_auto_heap = true
}
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 `${arg_typ_sym.name}` in fn declaration must specify the generic type names, e.g. ${arg_typ_sym.name}[T]',
c.error('generic struct `${pure_sym_name}` in fn declaration must specify the generic type names, e.g. ${pure_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 `${arg_typ_sym.name}` in fn declaration must specify the generic type names, e.g. ${arg_typ_sym.name}[T]',
c.error('generic interface `${pure_sym_name}` in fn declaration must specify the generic type names, e.g. ${pure_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 `${arg_typ_sym.name}` in fn declaration must specify the generic type names, e.g. ${arg_typ_sym.name}[T]',
c.error('generic sumtype `${pure_sym_name}` in fn declaration must specify the generic type names, e.g. ${pure_sym_name}[T]',
param.type_pos)
}
}
Expand Down
17 changes: 12 additions & 5 deletions vlib/v/checker/tests/generics_method_receiver_type_err_b.out
@@ -1,7 +1,14 @@
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]
6 | used_resources map[T]Resources
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]
7 | }
8 | pub fn (cr &ConsumableResources) get_total_resources() Resources {
8 |
9 | pub fn (cr &ConsumableResources) get_total_resources() Resources {
| ~~~~~~~~~~~~~~~~~~~~
9 | return cr.total_resources
10 | }
10 | return cr.total_resources
11 | }
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]
17 | }
18 |
19 | pub fn (f Foo) method() {
| ~~~
20 | }
21 |
14 changes: 12 additions & 2 deletions vlib/v/checker/tests/generics_method_receiver_type_err_b.vv
@@ -1,13 +1,23 @@
pub struct Resources{}
pub struct Resources {}

pub struct ConsumableResources[T] {
mut:
total_resources Resources
used_resources map[T]Resources
used_resources map[T]Resources
}

pub fn (cr &ConsumableResources) get_total_resources() Resources {
return cr.total_resources
}

// for issue 20362
struct Foo[T] {}

pub fn new_foo[F](arg F) !Foo[F] {
}

pub fn (f Foo) method() {
}

fn main() {
}

0 comments on commit 7d29afe

Please sign in to comment.