checker: semplify error message

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
pull/13718/head
Vincenzo Palazzo 2022-03-17 22:48:22 +01:00
parent c30f02abc0
commit 8dc49638c8
No known key found for this signature in database
GPG Key ID: 8B6DC2B870B80D5F
3 changed files with 11 additions and 17 deletions

View File

@ -980,6 +980,12 @@ pub fn (mytable &Table) type_to_code(t Type) string {
} }
} }
// clean type name from generics form. From Type<int> -> Type
pub fn (t &Table) clean_generics_type_str(typ Type) string {
result := t.type_to_str(typ)
return result.split('<')[0]
}
// import_aliases is a map of imported symbol aliases 'module.Type' => 'Type' // import_aliases is a map of imported symbol aliases 'module.Type' => 'Type'
pub fn (t &Table) type_to_str_using_aliases(typ Type, import_aliases map[string]string) string { pub fn (t &Table) type_to_str_using_aliases(typ Type, import_aliases map[string]string) string {
sym := t.sym(typ) sym := t.sym(typ)

View File

@ -200,22 +200,10 @@ pub fn (mut c Checker) check_expected_call_arg(got ast.Type, expected_ ast.Type,
if got_typ_sym.module_name() == expected_typ_sym.module_name() { if got_typ_sym.module_name() == expected_typ_sym.module_name() {
// Check if we are making a comparison between two different types of // Check if we are making a comparison between two different types of
// the same type like `Type<int> and &Type<>` // the same type like `Type<int> and &Type<>`
if (got.is_ptr() != expected.is_ptr()) || (got_typ_str != expected_typ_str) { clean_got_typ := c.table.clean_generics_type_str(got.clear_flag(.variadic))
mut expected_msg := 'expected ' clean_expected_typ := c.table.clean_generics_type_str(expected.clear_flag(.variadic))
if expected_.is_ptr() { if (got.is_ptr() != expected.is_ptr()) || (clean_got_typ != clean_expected_typ) {
expected_msg += 'a reference ($expected_typ_str)' return error('cannot use `$got_typ_str` as `$expected_typ_str`')
} else {
expected_msg += '$expected_typ_str'
}
mut got_msg := 'received '
if arg.typ.is_ptr() {
got_msg += 'a reference ($got_typ_str)'
} else {
got_msg += '$got_typ_str'
}
return error('$expected_msg but $got_msg, maybe you missed a `&`?')
} }
return return
} }

View File

@ -1,4 +1,4 @@
vlib/v/checker/tests/generic_parameter_on_method.vv:15:15: error: expected Type<> but received a reference (&Type<int>), maybe you missed a `&`? in argument 1 to `ConatinerType<int>.contains` vlib/v/checker/tests/generic_parameter_on_method.vv:15:15: error: cannot use `&Type<int>` as `Type<>` in argument 1 to `ConatinerType<int>.contains`
13 | fn main() { 13 | fn main() {
14 | con := ConatinerType<int>{typ: &Type<int>{0}} 14 | con := ConatinerType<int>{typ: &Type<int>{0}}
15 | con.contains(con.typ) 15 | con.contains(con.typ)