parser/checker: `if select { ... } {` (#6434)

pull/6439/head
Uwe Krüger 2020-09-21 07:34:04 +02:00 committed by GitHub
parent 27f91faae5
commit 0f1c484ad1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 12 additions and 3 deletions

View File

@ -1891,6 +1891,8 @@ if select {
ch <- a { ch <- a {
... ...
} }
} {
// channel was open
} else { } else {
// channel is closed // channel is closed
} }

View File

@ -91,7 +91,8 @@ fn test_select_blocks() {
ch1.close() ch1.close()
ch2.close() ch2.close()
mut h := 7 mut h := 7
u := select { mut is_open := true
if select {
b := <- ch2 { b := <- ch2 {
h = 0 h = 0
} }
@ -101,11 +102,15 @@ fn test_select_blocks() {
else { else {
h = 2 h = 2
} }
} {
panic('channel is still open')
} else {
is_open = false
} }
// no branch should have run // no branch should have run
assert h == 7 assert h == 7
// since all channels are closed `select` should return `false` // since all channels are closed `select` should return `false`
assert u == false assert is_open == false
} }

View File

@ -3220,6 +3220,7 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) table.Type {
should_skip = c.comp_if_branch(branch.cond, branch.pos) should_skip = c.comp_if_branch(branch.cond, branch.pos)
} else { } else {
// check condition type is boolean // check condition type is boolean
c.expected_type = table.bool_type
cond_typ := c.expr(branch.cond) cond_typ := c.expr(branch.cond)
if cond_typ.idx() !in [table.bool_type_idx, table.void_type_idx] && !c.pref.translated { if cond_typ.idx() !in [table.bool_type_idx, table.void_type_idx] && !c.pref.translated {
// void types are skipped, because they mean the var was initialized incorrectly // void types are skipped, because they mean the var was initialized incorrectly

View File

@ -1076,7 +1076,8 @@ pub fn (mut p Parser) name_expr() ast.Expr {
} else if (p.peek_tok.kind == .lcbr || } else if (p.peek_tok.kind == .lcbr ||
(p.peek_tok.kind == .lt && lit0_is_capital)) && (p.peek_tok.kind == .lt && lit0_is_capital)) &&
(!p.inside_match || (p.inside_select && prev_tok_kind == .arrow && lit0_is_capital)) && !p.inside_match_case && (!p.inside_match || (p.inside_select && prev_tok_kind == .arrow && lit0_is_capital)) && !p.inside_match_case &&
!p.inside_if && !p.inside_for { // && (p.tok.lit[0].is_capital() || p.builtin_mod) { (!p.inside_if || p.inside_select) &&
(!p.inside_for || p.inside_select) { // && (p.tok.lit[0].is_capital() || p.builtin_mod) {
return p.struct_init(false) // short_syntax: false return p.struct_init(false) // short_syntax: false
} else if p.peek_tok.kind == .dot && (lit0_is_capital && !known_var && language == .v) { } else if p.peek_tok.kind == .dot && (lit0_is_capital && !known_var && language == .v) {
// `Color.green` // `Color.green`