Skip to content

Commit

Permalink
checker: fix missing incompatible pushval type for chan <- operator (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Mar 18, 2024
1 parent 8e9ddb1 commit 2798a06
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion vlib/v/checker/infix.v
Expand Up @@ -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...
Expand All @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions 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 | }
7 changes: 7 additions & 0 deletions vlib/v/checker/tests/chan_incompatible_type_err.vv
@@ -0,0 +1,7 @@
struct Foo {}

fn main() {
ch := chan Foo{}
foo := Foo{}
ch <- &foo
}

0 comments on commit 2798a06

Please sign in to comment.