cgen: fix blank identifier in IfGard (`if _ := <-ch { ... }`, etc) (#9650)

pull/9653/head
Uwe Krüger 2021-04-09 17:18:36 +02:00 committed by GitHub
parent 6948419595
commit b346dd9464
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 2 deletions

View File

@ -4576,10 +4576,15 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
g.expr(branch.cond.expr)
g.writeln(', ${var_name}.state == 0) {')
}
if branch.cond.var_name != '_' {
if short_opt || branch.cond.var_name != '_' {
base_type := g.base_type(branch.cond.expr_type)
if short_opt {
g.write('\t$base_type $branch.cond.var_name = ')
cond_var_name := if branch.cond.var_name == '_' {
'_dummy_${g.tmp_count + 1}'
} else {
branch.cond.var_name
}
g.write('\t$base_type $cond_var_name = ')
g.expr(branch.cond.expr)
g.writeln(';')
} else {

View File

@ -17,6 +17,14 @@ fn test_fn_return() {
assert res == [31.0, 7.5]
}
fn test_fn_return_empty() {
if _ := f(-3) {
assert false
} else {
assert true
}
}
fn test_map_get() {
mut m := map{
'xy': 5
@ -33,6 +41,18 @@ fn test_map_get() {
assert res == [-17, 7]
}
fn test_map_get_empty() {
mut m := map{
'xy': 5
'zu': 7
}
if _ := m['jk'] {
assert false
} else {
assert true
}
}
fn test_array_get() {
mut a := [12.5, 6.5, -17.25]
mut res := []f64{cap: 2}
@ -46,6 +66,15 @@ fn test_array_get() {
assert res == [6.5, -23.0]
}
fn test_array_get_empty() {
mut a := [12.5, 6.5, -17.25]
if _ := a[7] {
assert false
} else {
assert true
}
}
fn test_chan_pop() {
mut res := []f64{cap: 3}
ch := chan f64{cap: 10}
@ -62,6 +91,20 @@ fn test_chan_pop() {
assert res == [6.75, -3.25, -37.5]
}
fn test_chan_pop_empty() {
ch := chan f64{cap: 10}
ch <- 6.75
ch <- -3.25
ch.close()
for i in 0 .. 3 {
if _ := <-ch {
assert i < 2
} else {
assert i == 2
}
}
}
struct Thing {
name string
}