checker: fix missing incompatible pushval type for chan <- operator (#21040)

This commit is contained in:
Felipe Pena 2024-03-17 23:02:06 -03:00 committed by GitHub
parent 8e9ddb13ea
commit 2798a063fc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 1 deletions

View File

@ -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 {

View File

@ -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 | }

View File

@ -0,0 +1,7 @@
struct Foo {}
fn main() {
ch := chan Foo{}
foo := Foo{}
ch <- &foo
}