cgen: fix array sort with fn call parameter (fix #19220) (#19221)

This commit is contained in:
yuyi 2023-08-26 23:50:06 +08:00 committed by GitHub
parent c85232ef1f
commit adf10f88cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -611,7 +611,8 @@ fn (mut g Gen) gen_array_sort(node ast.CallExpr) {
comparison_type = g.unwrap(infix_expr.left_type.set_nr_muls(0))
left_name := infix_expr.left.str()
if left_name.len > 1 {
compare_fn += '_by' + left_name[1..].replace_each(['.', '_', '[', '_', ']', '_'])
compare_fn += '_by' +
left_name[1..].replace_each(['.', '_', '[', '_', ']', '_', "'", '_', '"', '_', '(', '', ')', '', ',', ''])
}
// is_reverse is `true` for `.sort(a > b)` and `.sort(b < a)`
is_reverse := (left_name.starts_with('a') && infix_expr.op == .gt)

View File

@ -0,0 +1,13 @@
struct Info {
mut:
fields []string
}
fn test_sort_with_fn_call() {
mut info := Info{
fields: ['aaa(', 'b(']
}
info.fields.sort(a.before('(').len < b.before('(').len)
println(info.fields)
assert info.fields == ['b(', 'aaa(']
}