1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix sorting of shared array fields (fix #15244)

This commit is contained in:
Delyan Angelov 2022-08-02 16:08:21 +03:00
parent 857e047d01
commit 45c601b79c
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 43 additions and 2 deletions

View File

@ -519,8 +519,15 @@ fn (mut g Gen) gen_array_sort(node ast.CallExpr) {
} }
fn (mut g Gen) gen_array_sort_call(node ast.CallExpr, compare_fn string) { fn (mut g Gen) gen_array_sort_call(node ast.CallExpr, compare_fn string) {
deref_field := if node.left_type.is_ptr() || node.left_type.is_pointer() { '->' } else { '.' } mut deref_field := if node.left_type.is_ptr() || node.left_type.is_pointer() {
// eprintln('> qsort: pointer $node.left_type | deref: `$deref`') '->'
} else {
'.'
}
if node.left_type.has_flag(.shared_f) {
deref_field += 'val.'
}
// eprintln('> qsort: pointer $node.left_type | deref_field: `$deref_field`')
g.empty_line = true g.empty_line = true
g.write('qsort(') g.write('qsort(')
g.expr(node.left) g.expr(node.left)

View File

@ -0,0 +1,34 @@
module main
import time
struct Alarms {
mut:
times shared []time.Time
}
fn (alarms Alarms) add(alarm time.Time) {
lock alarms.times {
alarms.times << alarm
alarms.times.sort(a < b)
}
}
fn test_sorting_shared_arrays() ? {
alarms := Alarms{}
utc := time.utc()
alarms.add(utc)
alarms.add(time.parse_iso8601('2022-03-01')?)
alarms.add(time.parse_iso8601('3001-03-01')?)
alarms.add(time.parse_iso8601('2002-03-01')?)
alarms.add(time.parse_iso8601('3002-03-01')?)
alarms.add(time.parse_iso8601('2021-03-01')?)
println(alarms)
lock alarms.times {
assert alarms.times.len == 6
assert alarms.times[0].year == 2002
assert alarms.times.pop().year == 3002
assert alarms.times.pop().year == 3001
assert alarms.times.len == 4
}
}