checker: strengthen the judgment of generic type parameters replacement, merge generic type init tests (#13460)

pull/13465/head
yuyi 2022-02-14 08:38:47 +08:00 committed by GitHub
parent 2e0f8ee9e2
commit b2f984280b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 28 deletions

View File

@ -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) c.table.replace_generic_type(node.typ, node.generic_types)
} }
} else if struct_sym.info is ast.Alias { } else if struct_sym.info is ast.Alias {

View File

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

View File

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

View File

@ -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 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 | 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>{ 6 | msg := Response<U>{
| ~~~~~~~~~~~~ | ~~~~~~~~~~~~
7 | result: res 7 | result: res
8 | } 8 | }
vlib/v/checker/tests/generics_struct_init_type_parameter_err.vv:7:3: error: cannot assign to field `result`: expected `U`, not `int` 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>{ 6 | msg := Response<U>{
7 | result: res 7 | result: res
| ~~~~~~~~~~~ | ~~~~~~~~~~~
8 | } 8 | }
9 | println(b) 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)

View File

@ -2,7 +2,7 @@ struct Response<U> {
result 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>{ msg := Response<U>{
result: res result: res
} }
@ -10,7 +10,18 @@ pub fn send<A, B>(res A, b B) string {
return '$msg' return '$msg'
} }
fn send_2<A, B>(res A, b B) string {
msg := Response<A, B>{
result: res
}
println(b)
return '$msg'
}
fn main() { 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) println(ret)
} }