checker: strengthen the judgment of generic type parameters replacement, merge generic type init tests (#13460)
parent
2e0f8ee9e2
commit
b2f984280b
|
@ -154,7 +154,8 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
|
|||
}
|
||||
}
|
||||
}
|
||||
if node.generic_types.len > 0 && struct_sym.info.generic_types != node.generic_types {
|
||||
if node.generic_types.len > 0 && struct_sym.info.generic_types.len == node.generic_types.len
|
||||
&& struct_sym.info.generic_types != node.generic_types {
|
||||
c.table.replace_generic_type(node.typ, node.generic_types)
|
||||
}
|
||||
} else if struct_sym.info is ast.Alias {
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
vlib/v/checker/tests/generics_struct_init_type_parameter_count_err.vv:6:9: error: generic struct init expects 1 generic parameter, but got 2
|
||||
4 |
|
||||
5 | pub fn send<A, B>(res A, b B) string {
|
||||
6 | msg := Response<A, B>{
|
||||
| ~~~~~~~~~~~~~~~
|
||||
7 | result: res
|
||||
8 | }
|
|
@ -1,16 +0,0 @@
|
|||
struct Response<U> {
|
||||
result U
|
||||
}
|
||||
|
||||
pub fn send<A, B>(res A, b B) string {
|
||||
msg := Response<A, B>{
|
||||
result: res
|
||||
}
|
||||
println(b)
|
||||
return '$msg'
|
||||
}
|
||||
|
||||
fn main() {
|
||||
mut ret := send(123, 22.2)
|
||||
println(ret)
|
||||
}
|
|
@ -1,14 +1,28 @@
|
|||
vlib/v/checker/tests/generics_struct_init_type_parameter_err.vv:6:9: error: generic struct init type parameter `U` must be within the parameters `(A,B)` of the current generic function
|
||||
4 |
|
||||
5 | pub fn send<A, B>(res A, b B) string {
|
||||
5 | fn send_1<A, B>(res A, b B) string {
|
||||
6 | msg := Response<U>{
|
||||
| ~~~~~~~~~~~~
|
||||
7 | result: res
|
||||
8 | }
|
||||
vlib/v/checker/tests/generics_struct_init_type_parameter_err.vv:7:3: error: cannot assign to field `result`: expected `U`, not `int`
|
||||
5 | pub fn send<A, B>(res A, b B) string {
|
||||
5 | fn send_1<A, B>(res A, b B) string {
|
||||
6 | msg := Response<U>{
|
||||
7 | result: res
|
||||
| ~~~~~~~~~~~
|
||||
8 | }
|
||||
9 | println(b)
|
||||
vlib/v/checker/tests/generics_struct_init_type_parameter_err.vv:14:9: error: generic struct init expects 1 generic parameter, but got 2
|
||||
12 |
|
||||
13 | fn send_2<A, B>(res A, b B) string {
|
||||
14 | msg := Response<A, B>{
|
||||
| ~~~~~~~~~~~~~~~
|
||||
15 | result: res
|
||||
16 | }
|
||||
vlib/v/checker/tests/generics_struct_init_type_parameter_err.vv:15:3: error: cannot assign to field `result`: expected `U`, not `int`
|
||||
13 | fn send_2<A, B>(res A, b B) string {
|
||||
14 | msg := Response<A, B>{
|
||||
15 | result: res
|
||||
| ~~~~~~~~~~~
|
||||
16 | }
|
||||
17 | println(b)
|
||||
|
|
|
@ -2,7 +2,7 @@ struct Response<U> {
|
|||
result U
|
||||
}
|
||||
|
||||
pub fn send<A, B>(res A, b B) string {
|
||||
fn send_1<A, B>(res A, b B) string {
|
||||
msg := Response<U>{
|
||||
result: res
|
||||
}
|
||||
|
@ -10,7 +10,18 @@ pub fn send<A, B>(res A, b B) string {
|
|||
return '$msg'
|
||||
}
|
||||
|
||||
fn send_2<A, B>(res A, b B) string {
|
||||
msg := Response<A, B>{
|
||||
result: res
|
||||
}
|
||||
println(b)
|
||||
return '$msg'
|
||||
}
|
||||
|
||||
fn main() {
|
||||
mut ret := send(123, 22.2)
|
||||
mut ret := send_1(123, 22.2)
|
||||
println(ret)
|
||||
|
||||
ret = send_2(123, 22.2)
|
||||
println(ret)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue