From 4a35ffd01bdc92b62b972f5ba0ebdef786f89d11 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 23 Dec 2024 19:08:11 +0800 Subject: [PATCH] checker: fix pushing enum value to channel (fix #23244) (#23247) --- vlib/v/checker/infix.v | 7 ++++++- vlib/v/tests/concurrency/chan_push_enum_test.v | 11 +++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/concurrency/chan_push_enum_test.v diff --git a/vlib/v/checker/infix.v b/vlib/v/checker/infix.v index 980b6826d..21825362d 100644 --- a/vlib/v/checker/infix.v +++ b/vlib/v/checker/infix.v @@ -9,9 +9,15 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type { c.expected_type = former_expected_type } mut left_type := c.expr(mut node.left) + mut left_sym := c.table.sym(left_type) node.left_type = left_type c.expected_type = left_type + if left_sym.kind == .chan { + chan_info := left_sym.chan_info() + c.expected_type = chan_info.elem_type + } + // `if n is ast.Ident && n.is_mut { ... }` if !c.inside_sql && node.op == .and { mut left_node := node.left @@ -103,7 +109,6 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type { } mut right_sym := c.table.sym(right_type) right_final_sym := c.table.final_sym(right_type) - mut left_sym := c.table.sym(left_type) left_final_sym := c.table.final_sym(left_type) left_pos := node.left.pos() right_pos := node.right.pos() diff --git a/vlib/v/tests/concurrency/chan_push_enum_test.v b/vlib/v/tests/concurrency/chan_push_enum_test.v new file mode 100644 index 000000000..b614a93ad --- /dev/null +++ b/vlib/v/tests/concurrency/chan_push_enum_test.v @@ -0,0 +1,11 @@ +enum FooBar { + foo + bar +} + +fn test_chan_push_enum() { + ch := chan FooBar{cap: 10} + ch <- .foo + println(<-ch) + assert true +} -- 2.39.5