checker,cgen: check sort_with_compare_context parameters too
parent
509367b293
commit
eb772cfcf9
|
@ -1297,26 +1297,12 @@ pub fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
|
|||
c.fail_if_unreadable(arg.expr, got_arg_typ, 'argument')
|
||||
}
|
||||
}
|
||||
if left_sym.kind == .array && method_name == 'sort_with_compare' {
|
||||
array_info := left_sym.info as ast.Array
|
||||
elem_typ := array_info.elem_type
|
||||
arg_sym := c.table.sym(arg.typ)
|
||||
if arg_sym.kind == .function {
|
||||
func_info := arg_sym.info as ast.FnType
|
||||
if func_info.func.params.len == 2 {
|
||||
if func_info.func.params[0].typ.nr_muls() != elem_typ.nr_muls() + 1 {
|
||||
arg_typ_str := c.table.type_to_str(func_info.func.params[0].typ)
|
||||
expected_typ_str := c.table.type_to_str(elem_typ.ref())
|
||||
c.error('sort_with_compare callback function parameter `${func_info.func.params[0].name}` with type `$arg_typ_str` should be `$expected_typ_str`',
|
||||
func_info.func.params[0].type_pos)
|
||||
}
|
||||
if func_info.func.params[1].typ.nr_muls() != elem_typ.nr_muls() + 1 {
|
||||
arg_typ_str := c.table.type_to_str(func_info.func.params[1].typ)
|
||||
expected_typ_str := c.table.type_to_str(elem_typ.ref())
|
||||
c.error('sort_with_compare callback function parameter `${func_info.func.params[1].name}` with type `$arg_typ_str` should be `$expected_typ_str`',
|
||||
func_info.func.params[1].type_pos)
|
||||
}
|
||||
if left_sym.kind == .array {
|
||||
if method_name == 'sort_with_compare' {
|
||||
c.check_sort_cb_args(2, 'sort_with_compare', left_sym, arg)
|
||||
}
|
||||
if method_name == 'sort_with_compare_context' {
|
||||
c.check_sort_cb_args(3, 'sort_with_compare_context', left_sym, arg)
|
||||
}
|
||||
}
|
||||
// Handle expected interface
|
||||
|
@ -1844,3 +1830,26 @@ fn scope_register_a_b(mut s ast.Scope, pos token.Pos, typ ast.Type) {
|
|||
is_used: true
|
||||
})
|
||||
}
|
||||
|
||||
fn (mut c Checker) check_sort_cb_args(howmany int, fname string, left_sym &ast.TypeSymbol, arg &ast.CallArg) {
|
||||
array_info := left_sym.info as ast.Array
|
||||
elem_typ := array_info.elem_type
|
||||
arg_sym := c.table.sym(arg.typ)
|
||||
if arg_sym.kind != .function {
|
||||
return
|
||||
}
|
||||
func_info := arg_sym.info as ast.FnType
|
||||
if func_info.func.params.len != howmany {
|
||||
c.error('$fname callback should have exactly $howmany parameters', arg.expr.pos())
|
||||
return
|
||||
}
|
||||
desired_et_muls := elem_typ.nr_muls() + 1
|
||||
expected_typ_str := c.table.type_to_str(elem_typ.ref())
|
||||
for k in 0 .. 2 {
|
||||
if func_info.func.params[k].typ.nr_muls() != desired_et_muls {
|
||||
arg_typ_str := c.table.type_to_str(func_info.func.params[k].typ)
|
||||
c.error('$fname callback function parameter `${func_info.func.params[k].name}` with type `$arg_typ_str` should be `$expected_typ_str`',
|
||||
func_info.func.params[k].type_pos)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -952,7 +952,7 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
|
|||
receiver_type_name = 'map'
|
||||
}
|
||||
if final_left_sym.kind == .array
|
||||
&& node.name in ['repeat', 'sort_with_compare', 'free', 'push_many', 'trim', 'first', 'last', 'pop', 'clone', 'reverse', 'slice', 'pointers'] {
|
||||
&& node.name in ['repeat', 'sort_with_compare', 'sort_with_compare_context', 'free', 'push_many', 'trim', 'first', 'last', 'pop', 'clone', 'reverse', 'slice', 'pointers'] {
|
||||
if !(left_sym.info is ast.Alias && typ_sym.has_method(node.name)) {
|
||||
// `array_Xyz_clone` => `array_clone`
|
||||
receiver_type_name = 'array'
|
||||
|
|
|
@ -318,7 +318,7 @@ fn (mut g JsGen) method_call(node ast.CallExpr) {
|
|||
}
|
||||
}
|
||||
if final_left_sym.kind == .array
|
||||
&& node.name in ['repeat', 'sort_with_compare', 'free', 'push_many', 'trim', 'first', 'last', 'pop', 'clone', 'reverse', 'slice', 'pointers'] {
|
||||
&& node.name in ['repeat', 'sort_with_compare', 'sort_with_compare_context', 'free', 'push_many', 'trim', 'first', 'last', 'pop', 'clone', 'reverse', 'slice', 'pointers'] {
|
||||
if !(left_sym.info is ast.Alias && typ_sym.has_method(node.name)) {
|
||||
// `array_Xyz_clone` => `array_clone`
|
||||
receiver_type_name = 'array'
|
||||
|
|
Loading…
Reference in New Issue