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

pull/7748/head^2
Uwe Krüger 2021-01-09 02:43:48 +01:00 committed by GitHub
parent bbac95a438
commit eff757d0a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 14 deletions

View File

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

View File

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

View File

@ -5045,7 +5045,10 @@ fn (mut g Gen) or_block(var_name string, or_block ast.OrExpr, return_type table.
g.write_defer_stmts() g.write_defer_stmts()
// Now that option types are distinct we need a cast here // Now that option types are distinct we need a cast here
styp := g.typ(g.fn_decl.return_type) styp := g.typ(g.fn_decl.return_type)
g.writeln('\treturn *($styp *)&$cvar_name;') err_obj := g.new_tmp_var()
g.writeln('\t$styp $err_obj;')
g.writeln('\tmemcpy(&$err_obj, &$cvar_name, sizeof(Option));')
g.writeln('\treturn $err_obj;')
} }
} }
g.write('}') g.write('}')