diff --git a/doc/docs.md b/doc/docs.md index a12d4a6479..f361aa8167 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -1891,6 +1891,8 @@ if select { ch <- a { ... } +} { + // channel was open } else { // channel is closed } diff --git a/vlib/sync/channel_select_3_test.v b/vlib/sync/channel_select_3_test.v index 70bc0b2857..7ce6c67c87 100644 --- a/vlib/sync/channel_select_3_test.v +++ b/vlib/sync/channel_select_3_test.v @@ -91,7 +91,8 @@ fn test_select_blocks() { ch1.close() ch2.close() mut h := 7 - u := select { + mut is_open := true + if select { b := <- ch2 { h = 0 } @@ -101,11 +102,15 @@ fn test_select_blocks() { else { h = 2 } + } { + panic('channel is still open') + } else { + is_open = false } // no branch should have run assert h == 7 // since all channels are closed `select` should return `false` - assert u == false + assert is_open == false } diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 8ccf1a4f43..853033e49b 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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) } else { // check condition type is boolean + c.expected_type = table.bool_type cond_typ := c.expr(branch.cond) 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 diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index f964b805fa..d440629841 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1076,7 +1076,8 @@ pub fn (mut p Parser) name_expr() ast.Expr { } else if (p.peek_tok.kind == .lcbr || (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_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 } else if p.peek_tok.kind == .dot && (lit0_is_capital && !known_var && language == .v) { // `Color.green`