1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: fix error for generic method on nested struct (#14322)

This commit is contained in:
yuyi 2022-05-07 05:51:04 +08:00 committed by GitHub
parent 0699f324b5
commit 621574c12a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 1 deletions

View File

@ -1223,7 +1223,8 @@ pub fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
rec_sym := c.table.sym(node.left_type)
rec_is_generic := left_type.has_flag(.generic)
if rec_sym.info is ast.Struct {
if rec_is_generic && node.concrete_types.len == 0 {
if rec_is_generic && node.concrete_types.len == 0
&& method.generic_names.len == rec_sym.info.generic_types.len {
node.concrete_types = rec_sym.info.generic_types
} else if !rec_is_generic && rec_sym.info.concrete_types.len > 0
&& node.concrete_types.len > 0

View File

@ -0,0 +1,38 @@
struct Outer<T> {
mut:
inner Inner<T>
}
struct Inner<T> {
val T
}
fn (mut i Inner<T>) next<S>(input S) f64 {
$if S is f32 {
return 32
} $else {
panic('"$S.name" is not supported')
return 0
}
}
fn (mut o Outer<T>) next<S>(input S) f64 {
$if S is f32 {
return o.inner.next(input)
} $else {
panic('"$S.name" is not supported')
return 0
}
}
fn test_generics_method_on_nested_struct() {
mut outer := Outer<f64>{
inner: Inner<f64>{
val: 1.1
}
}
res := outer.next(f32(99.0))
println(res)
assert res == 32.0
}