From 8dc49638c83440b6f979246a0171d978fd81fbc3 Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Thu, 17 Mar 2022 22:48:22 +0100 Subject: [PATCH] checker: semplify error message Signed-off-by: Vincenzo Palazzo --- vlib/v/ast/types.v | 6 ++++++ vlib/v/checker/check_types.v | 20 ++++--------------- .../tests/generic_parameter_on_method.out | 2 +- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/vlib/v/ast/types.v b/vlib/v/ast/types.v index c6cccf0470..1f7784d9d2 100644 --- a/vlib/v/ast/types.v +++ b/vlib/v/ast/types.v @@ -980,6 +980,12 @@ pub fn (mytable &Table) type_to_code(t Type) string { } } +// clean type name from generics form. From Type -> 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' pub fn (t &Table) type_to_str_using_aliases(typ Type, import_aliases map[string]string) string { sym := t.sym(typ) diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index e9b53117e5..9daa792f82 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -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() { // Check if we are making a comparison between two different types of // the same type like `Type and &Type<>` - if (got.is_ptr() != expected.is_ptr()) || (got_typ_str != expected_typ_str) { - mut expected_msg := 'expected ' - if expected_.is_ptr() { - expected_msg += 'a reference ($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 `&`?') + clean_got_typ := c.table.clean_generics_type_str(got.clear_flag(.variadic)) + clean_expected_typ := c.table.clean_generics_type_str(expected.clear_flag(.variadic)) + if (got.is_ptr() != expected.is_ptr()) || (clean_got_typ != clean_expected_typ) { + return error('cannot use `$got_typ_str` as `$expected_typ_str`') } return } diff --git a/vlib/v/checker/tests/generic_parameter_on_method.out b/vlib/v/checker/tests/generic_parameter_on_method.out index 61197d5a27..c9fa6581b2 100644 --- a/vlib/v/checker/tests/generic_parameter_on_method.out +++ b/vlib/v/checker/tests/generic_parameter_on_method.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/generic_parameter_on_method.vv:15:15: error: expected Type<> but received a reference (&Type), maybe you missed a `&`? in argument 1 to `ConatinerType.contains` +vlib/v/checker/tests/generic_parameter_on_method.vv:15:15: error: cannot use `&Type` as `Type<>` in argument 1 to `ConatinerType.contains` 13 | fn main() { 14 | con := ConatinerType{typ: &Type{0}} 15 | con.contains(con.typ)