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

cgen: fix address violations for error propagation (#7972)

This commit is contained in:
Uwe Krüger
2021-01-09 02:43:48 +01:00
committed by GitHub
parent bbac95a438
commit eff757d0a1
3 changed files with 17 additions and 14 deletions

View File

@ -4,13 +4,13 @@ const (
num_iterations = 10000
)
fn get_val_from_chan(ch chan int) ?int {
fn get_val_from_chan(ch chan i64) ?i64 {
r := <-ch ?
return r
}
// this function gets an array of channels for `int`
fn do_rec_calc_send(chs []chan int, sem sync.Semaphore) {
// this function gets an array of channels for `i64`
fn do_rec_calc_send(chs []chan i64, sem sync.Semaphore) {
mut msg := ''
for {
mut s := get_val_from_chan(chs[0]) or {
@ -25,10 +25,10 @@ fn do_rec_calc_send(chs []chan int, sem sync.Semaphore) {
}
fn test_channel_array_mut() {
mut chs := [chan int{}, chan int{cap: 10}]
mut chs := [chan i64{}, chan i64{cap: 10}]
sem := sync.new_semaphore()
go do_rec_calc_send(chs, sem)
mut t := int(100)
mut t := i64(100)
for _ in 0 .. num_iterations {
chs[0] <- t
t = <-chs[1]

View File

@ -1,27 +1,27 @@
const n = 1000
fn f(ch chan int) {
mut s := 0
fn f(ch chan f64) {
mut s := 0.
for _ in 0 .. n {
s += <-ch
}
assert s == n * (n + 1) / 2
assert s == f64(n * (n + 1) / 2)
ch.close()
}
fn do_send(ch chan int, val int) ?int {
fn do_send(ch chan f64, val f64) ?f64 {
ch <- val ?
return val + 1
return val + 1.0
}
fn test_push_propargate() {
ch := chan int{}
ch := chan f64{}
go f(ch)
mut s := 1
mut s := 1.0
for {
s = do_send(ch, s) or {
break
}
}
assert s == n + 1
assert s == f64(n + 1)
}