From ea3983a91b56ce3bc888cd84931756df6c3061b2 Mon Sep 17 00:00:00 2001 From: shadowninja55 <49539636+shadowninja55@users.noreply.github.com> Date: Sun, 27 Jun 2021 10:17:03 -0400 Subject: [PATCH] checker: require .sort() body for some types (#10550) --- vlib/v/checker/checker.v | 4 ++++ .../tests/array_sort_struct_no_body_err.out | 5 +++++ .../tests/array_sort_struct_no_body_err.vv | 6 +++++ vlib/v/tests/array_sort_lt_overload_test.v | 22 +++++++++++++++++++ 4 files changed, 37 insertions(+) create mode 100644 vlib/v/checker/tests/array_sort_struct_no_body_err.out create mode 100644 vlib/v/checker/tests/array_sort_struct_no_body_err.vv create mode 100644 vlib/v/tests/array_sort_lt_overload_test.v diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index c7dfd7ce7..9b7693853 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2196,6 +2196,10 @@ fn (mut c Checker) array_builtin_method_call(mut call_expr ast.CallExpr, left_ty '`.sort()` requires a `<` or `>` comparison as the first and only argument' + '\ne.g. `users.sort(a.id < b.id)`', call_expr.pos) } + } else if !(c.table.get_type_symbol(elem_typ).has_method('<') + || c.table.unalias_num_type(elem_typ) in [ast.int_type, ast.int_type.to_ptr(), ast.string_type, ast.string_type.to_ptr(), ast.i8_type, ast.i16_type, ast.i64_type, ast.byte_type, ast.rune_type, ast.u16_type, ast.u32_type, ast.u64_type, ast.f32_type, ast.f64_type, ast.char_type, ast.bool_type, ast.float_literal_type, ast.int_literal_type, ast.size_t_type_idx]) { + c.error('custom sorting condition must be supplied for type `${c.table.type_to_str(elem_typ)}`', + call_expr.pos) } } else if method_name == 'wait' { elem_sym := c.table.get_type_symbol(elem_typ) diff --git a/vlib/v/checker/tests/array_sort_struct_no_body_err.out b/vlib/v/checker/tests/array_sort_struct_no_body_err.out new file mode 100644 index 000000000..dd83a0bfb --- /dev/null +++ b/vlib/v/checker/tests/array_sort_struct_no_body_err.out @@ -0,0 +1,5 @@ +vlib/v/checker/tests/array_sort_struct_no_body_err.vv:6:5: error: custom sorting condition must be supplied for type `Foo` + 4 | + 5 | mut arr := []Foo{} + 6 | arr.sort() + | ~~~~~~ diff --git a/vlib/v/checker/tests/array_sort_struct_no_body_err.vv b/vlib/v/checker/tests/array_sort_struct_no_body_err.vv new file mode 100644 index 000000000..6b53ea9e9 --- /dev/null +++ b/vlib/v/checker/tests/array_sort_struct_no_body_err.vv @@ -0,0 +1,6 @@ +struct Foo { + bar int +} + +mut arr := []Foo{} +arr.sort() diff --git a/vlib/v/tests/array_sort_lt_overload_test.v b/vlib/v/tests/array_sort_lt_overload_test.v new file mode 100644 index 000000000..b37f022bf --- /dev/null +++ b/vlib/v/tests/array_sort_lt_overload_test.v @@ -0,0 +1,22 @@ +struct Num { + value int +} + +fn (a Num) < (b Num) bool { + return a.value < b.value +} + +fn test_sort_lt_overloaded_struct_array() { + mut arr := []Num{} + arr << { + value: 10 + } + arr << { + value: 5 + } + arr << { + value: 7 + } + arr.sort() + assert arr[0].value == 5 +} -- 2.39.5