From 2798a063fcdbeaf6ac7f3d0a72ccc7877e0c4d37 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 17 Mar 2024 23:02:06 -0300 Subject: [PATCH] checker: fix missing incompatible pushval type for chan <- operator (#21040) --- vlib/v/checker/infix.v | 6 +++++- vlib/v/checker/tests/chan_incompatible_type_err.out | 6 ++++++ vlib/v/checker/tests/chan_incompatible_type_err.vv | 7 +++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 vlib/v/checker/tests/chan_incompatible_type_err.out create mode 100644 vlib/v/checker/tests/chan_incompatible_type_err.vv diff --git a/vlib/v/checker/infix.v b/vlib/v/checker/infix.v index cc86522e58f9ce..306c3860032fa7 100644 --- a/vlib/v/checker/infix.v +++ b/vlib/v/checker/infix.v @@ -705,7 +705,8 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type { chan_info := left_sym.chan_info() elem_type := chan_info.elem_type if !c.check_types(right_type, elem_type) { - c.error('cannot push `${right_sym.name}` on `${left_sym.name}`', right_pos) + c.error('cannot push `${c.table.type_to_str(right_type)}` on `${left_sym.name}`', + right_pos) } if chan_info.is_mut { // TODO: The error message of the following could be more specific... @@ -714,6 +715,9 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type { if elem_type.is_ptr() && !right_type.is_ptr() { c.error('cannot push non-reference `${right_sym.name}` on `${left_sym.name}`', right_pos) + } else if right_type.is_ptr() != elem_type.is_ptr() { + c.error('cannot push `${c.table.type_to_str(right_type)}` on `${left_sym.name}`', + right_pos) } c.stmts_ending_with_expression(mut node.or_block.stmts) } else { diff --git a/vlib/v/checker/tests/chan_incompatible_type_err.out b/vlib/v/checker/tests/chan_incompatible_type_err.out new file mode 100644 index 00000000000000..ba36d8039d0e71 --- /dev/null +++ b/vlib/v/checker/tests/chan_incompatible_type_err.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/chan_incompatible_type_err.vv:6:11: error: cannot push `&Foo` on `chan Foo` + 4 | ch := chan Foo{} + 5 | foo := Foo{} + 6 | ch <- &foo + | ^ + 7 | } diff --git a/vlib/v/checker/tests/chan_incompatible_type_err.vv b/vlib/v/checker/tests/chan_incompatible_type_err.vv new file mode 100644 index 00000000000000..d8669a08e9cc4d --- /dev/null +++ b/vlib/v/checker/tests/chan_incompatible_type_err.vv @@ -0,0 +1,7 @@ +struct Foo {} + +fn main() { + ch := chan Foo{} + foo := Foo{} + ch <- &foo +} \ No newline at end of file