checker: fix generic fn assign (#10479)

pull/10484/head
yuyi 2021-06-17 00:17:07 +08:00 committed by GitHub
parent 1dca06495d
commit e31be9f5c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 1 deletions

View File

@ -3625,7 +3625,8 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
}
}
if !is_blank_ident && !left.is_auto_deref_var() && !right.is_auto_deref_var()
&& right_sym.kind != .placeholder && left_sym.kind != .interface_ {
&& right_sym.kind != .placeholder && left_sym.kind != .interface_
&& !right_type_unwrapped.has_flag(.generic) && !left_type_unwrapped.has_flag(.generic) {
// Dual sides check (compatibility check)
c.check_expected(right_type_unwrapped, left_type_unwrapped) or {
// allow for ptr += 2

View File

@ -13,3 +13,26 @@ fn test_generics_assign_generics_struct() {
println('$x.v')
assert x.v == 1
}
// test generics assign generics struct_init
struct Node<T> {
pub mut:
val T
next &Node<T> = 0
}
fn new<T>() &Node<T> {
return &Node<T>{}
}
fn (mut n Node<T>) add(val T) {
node := &Node<T>{val, 0}
n.next = node
}
fn test_generic_fn_assign_generic_struct_init() {
mut list := new<int>()
list.add(100)
println(list.next)
assert list.next.val == 100
}