checker: add an error for `x.method({})` calls, when method expects a struct

pull/12519/head
Delyan Angelov 2021-11-19 13:21:21 +02:00
parent 9eac656e55
commit fa995ca537
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
6 changed files with 37 additions and 5 deletions

View File

@ -7,6 +7,9 @@ import v.ast
import v.token
pub fn (mut c Checker) check_expected_call_arg(got ast.Type, expected_ ast.Type, language ast.Language, arg ast.CallArg) ? {
if got == 0 {
return error('unexpected 0 type')
}
mut expected := expected_
// variadic
if expected.has_flag(.variadic) {

View File

@ -7605,8 +7605,14 @@ pub fn (mut c Checker) map_init(mut node ast.MapInit) ast.Type {
node.value_type = info.value_type
return node.typ
} else {
c.error('invalid empty map initilization syntax, use e.g. map[string]int{} instead',
node.pos)
if sym.kind == .struct_ {
c.error('`{}` can not be used for initialising empty structs any more. Use `${c.table.type_to_str(c.expected_type)}{}` instead.',
node.pos)
} else {
c.error('invalid empty map initialisation syntax, use e.g. map[string]int{} instead',
node.pos)
}
return ast.void_type
}
}
// `x := map[string]string` - set in parser

View File

@ -1,4 +1,4 @@
vlib/v/checker/tests/assign_to_typeless_variable_err.vv:2:10: error: invalid empty map initilization syntax, use e.g. map[string]int{} instead
vlib/v/checker/tests/assign_to_typeless_variable_err.vv:2:10: error: invalid empty map initialisation syntax, use e.g. map[string]int{} instead
1 | fn main() {
2 | val := {}
| ~~

View File

@ -1,6 +1,12 @@
vlib/v/checker/tests/map_init_invalid_syntax.vv:2:7: error: invalid empty map initilization syntax, use e.g. map[string]int{} instead
vlib/v/checker/tests/map_init_invalid_syntax.vv:2:7: error: invalid empty map initialisation syntax, use e.g. map[string]int{} instead
1 | fn main() {
2 | a := {}
| ~~
3 | println(a)
4 | }
4 | }
vlib/v/checker/tests/map_init_invalid_syntax.vv:3:2: error: `println` can not print void expressions
1 | fn main() {
2 | a := {}
3 | println(a)
| ~~~~~~~~~~
4 | }

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/method_call_with_empty_struct_init.vv:10:11: error: `{}` can not be used for initialising empty structs any more. Use `Abc{}` instead.
8 | fn main() {
9 | s := Abc{}
10 | s.abc(0, {})
| ~~
11 | }

View File

@ -0,0 +1,11 @@
struct Abc {
x int
}
fn (s Abc) abc(x int, param Abc) {
}
fn main() {
s := Abc{}
s.abc(0, {})
}