Skip to content

Commit

Permalink
checker: show a more detailed error for invalid declarations of gener…
Browse files Browse the repository at this point in the history
…ic methods on generic structs.
  • Loading branch information
spytheman committed Aug 24, 2022
1 parent 4718b8b commit 3ad22eb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
11 changes: 9 additions & 2 deletions vlib/v/checker/fn.v
Expand Up @@ -63,8 +63,15 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
}
}
if need_generic_names {
c.error('generic function declaration must specify generic type names, e.g. foo<T>',
node.pos)
if node.is_method {
c.add_error_detail('use `fn (r SomeType<T>) foo<T>() {`, not just `fn (r SomeType<T>) foo() {`')
c.error('generic method declaration must specify generic type names',
node.pos)
} else {
c.add_error_detail('use `fn foo<T>(x T) {`, not just `fn foo(x T) {`')
c.error('generic function declaration must specify generic type names',
node.pos)
}
}
}
if node.language == .v && !c.is_builtin_mod && !node.is_anon {
Expand Down
21 changes: 15 additions & 6 deletions vlib/v/checker/tests/generic_fn_decl_without_generic_names_err.out
@@ -1,15 +1,24 @@
vlib/v/checker/tests/generic_fn_decl_without_generic_names_err.vv:26:1: error: generic function declaration must specify generic type names, e.g. foo<T>
vlib/v/checker/tests/generic_fn_decl_without_generic_names_err.vv:26:1: error: generic function declaration must specify generic type names
24 | }
25 |
25 |
26 | fn g_worker(g Generic<T>) {
| ~~~~~~~~~~~~~~~~~~~~~~~~~
27 | t := <-g.ch
28 | handle(t)
vlib/v/checker/tests/generic_fn_decl_without_generic_names_err.vv:32:1: error: generic function declaration must specify generic type names, e.g. foo<T>
Details: use `fn foo<T>(x T) {`, not just `fn foo(x T) {`
vlib/v/checker/tests/generic_fn_decl_without_generic_names_err.vv:32:1: error: generic function declaration must specify generic type names
30 | }
31 |
31 |
32 | fn handle(t T) {
| ~~~~~~~~~~~~~~
33 | println("hi")
33 | println('hi')
34 | }

Details: use `fn foo<T>(x T) {`, not just `fn foo(x T) {`
vlib/v/checker/tests/generic_fn_decl_without_generic_names_err.vv:40:1: error: generic method declaration must specify generic type names
38 | type MayBe<T> = None | T
39 |
40 | fn (m MayBe<T>) is_some() bool {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
41 | return m is T
42 | }
Details: use `fn (r SomeType<T>) foo<T>() {`, not just `fn (r SomeType<T>) foo() {`
Expand Up @@ -30,5 +30,13 @@ fn g_worker(g Generic<T>) {
}

fn handle(t T) {
println("hi")
println('hi')
}

struct None {}

type MayBe<T> = None | T

fn (m MayBe<T>) is_some() bool {
return m is T
}

0 comments on commit 3ad22eb

Please sign in to comment.