parser: suggest using type alias when defining methods on non-local types (#21803)

This commit is contained in:
Swastik Baranwal 2024-07-05 15:15:54 +05:30 committed by GitHub
parent 174257650f
commit dd93b56b68
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 4 additions and 4 deletions

View File

@ -503,7 +503,7 @@ run them via `v file.v` instead',
&& elem_type_sym.language == .v
}
if is_non_local {
p.error_with_pos('cannot define new methods on non-local type ${type_sym.name}',
p.error_with_pos('cannot define new methods on non-local type ${type_sym.name}. Define an alias and use that instead like `type AliasName = ${type_sym.name}` ',
rec.type_pos)
return ast.FnDecl{
scope: unsafe { nil }

View File

@ -1,4 +1,4 @@
vlib/v/parser/tests/method_decl_on_non_local_array.vv:1:7: error: cannot define new methods on non-local type []int
vlib/v/parser/tests/method_decl_on_non_local_array.vv:1:7: error: cannot define new methods on non-local type []int. Define an alias and use that instead like `type AliasName = []int`
1 | fn (a []int) get_number() int {
| ~~~~~
2 | return 1

View File

@ -1,4 +1,4 @@
vlib/v/parser/tests/method_decl_on_non_local_map.vv:1:7: error: cannot define new methods on non-local type map[string]string
vlib/v/parser/tests/method_decl_on_non_local_map.vv:1:7: error: cannot define new methods on non-local type map[string]string. Define an alias and use that instead like `type AliasName = map[string]string`
1 | fn (a map[string]string) get_number() int {
| ~~~~~~~~~~~~~~~~~
2 | return 1

View File

@ -1,4 +1,4 @@
vlib/v/parser/tests/method_decl_on_non_local_type.vv:1:7: error: cannot define new methods on non-local type int
vlib/v/parser/tests/method_decl_on_non_local_type.vv:1:7: error: cannot define new methods on non-local type int. Define an alias and use that instead like `type AliasName = int`
1 | fn (a int) get_number() int {
| ~~~
2 | return 1