checker: check generics undefined operation of infix expression (#13223)
parent
14b33baa3b
commit
09797e493e
|
@ -679,31 +679,25 @@ pub fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if left_sym.kind in [.array, .array_fixed, .map, .struct_] {
|
if left_sym.kind in [.array, .array_fixed, .map, .struct_] {
|
||||||
if left_sym.has_method(node.op.str()) {
|
if left_sym.has_method_with_generic_parent(node.op.str()) {
|
||||||
if method := left_sym.find_method(node.op.str()) {
|
if method := left_sym.find_method_with_generic_parent(node.op.str()) {
|
||||||
return_type = method.return_type
|
return_type = method.return_type
|
||||||
} else {
|
} else {
|
||||||
return_type = left_type
|
return_type = left_type
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if left_sym.kind == .struct_
|
left_name := c.table.type_to_str(left_type)
|
||||||
&& (left_sym.info as ast.Struct).generic_types.len > 0 {
|
right_name := c.table.type_to_str(right_type)
|
||||||
return_type = left_type
|
if left_name == right_name {
|
||||||
|
c.error('undefined operation `$left_name` $node.op.str() `$right_name`',
|
||||||
|
left_right_pos)
|
||||||
} else {
|
} else {
|
||||||
left_name := c.table.type_to_str(left_type)
|
c.error('mismatched types `$left_name` and `$right_name`', left_right_pos)
|
||||||
right_name := c.table.type_to_str(right_type)
|
|
||||||
if left_name == right_name {
|
|
||||||
c.error('undefined operation `$left_name` $node.op.str() `$right_name`',
|
|
||||||
left_right_pos)
|
|
||||||
} else {
|
|
||||||
c.error('mismatched types `$left_name` and `$right_name`',
|
|
||||||
left_right_pos)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if right_sym.kind in [.array, .array_fixed, .map, .struct_] {
|
} else if right_sym.kind in [.array, .array_fixed, .map, .struct_] {
|
||||||
if right_sym.has_method(node.op.str()) {
|
if right_sym.has_method_with_generic_parent(node.op.str()) {
|
||||||
if method := right_sym.find_method(node.op.str()) {
|
if method := right_sym.find_method_with_generic_parent(node.op.str()) {
|
||||||
return_type = method.return_type
|
return_type = method.return_type
|
||||||
} else {
|
} else {
|
||||||
return_type = right_type
|
return_type = right_type
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
vlib/v/checker/tests/generics_undefined_operation.vv:12:5: error: undefined operation `V2d<int>` * `V2d<int>`
|
||||||
|
10 | v3 := V2d<int>{4, 6}
|
||||||
|
11 |
|
||||||
|
12 | if v1 * v2 == v3 {
|
||||||
|
| ~~~~~~~
|
||||||
|
13 | }
|
||||||
|
14 | }
|
|
@ -0,0 +1,14 @@
|
||||||
|
struct V2d<T> {
|
||||||
|
mut:
|
||||||
|
x T
|
||||||
|
y T
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
v1 := V2d<int>{2, 2}
|
||||||
|
v2 := V2d<int>{2, 3}
|
||||||
|
v3 := V2d<int>{4, 6}
|
||||||
|
|
||||||
|
if v1 * v2 == v3 {
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue