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

checker: check non generic struct used like a generic one (#15690)

This commit is contained in:
yuyi 2022-09-07 19:53:43 +08:00 committed by GitHub
parent c5ed179e76
commit 2693935066
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 0 deletions

View File

@ -256,6 +256,10 @@ pub fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
}
}
}
if node.generic_types.len > 0 && struct_sym.info.generic_types.len == 0 {
c.error('a non generic struct `$node.typ_str` used like a generic struct',
node.name_pos)
}
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)

View File

@ -0,0 +1,14 @@
vlib/v/checker/tests/generics_non_generic_struct_used_like_a_generic_one.vv:6:9: error: a non generic struct `Toy` used like a generic struct
4 |
5 | fn get_toy1(toy Toy) Toy{
6 | return Toy<T>{toy.toy_name}
| ~~~
7 | }
8 |
vlib/v/checker/tests/generics_non_generic_struct_used_like_a_generic_one.vv:10:9: error: a non generic struct `Toy` used like a generic struct
8 |
9 | fn get_toy2(toy Toy) Toy{
10 | return Toy<T,U>{toy.toy_name}
| ~~~
11 | }
12 |

View File

@ -0,0 +1,16 @@
struct Toy{
toy_name string
}
fn get_toy1(toy Toy) Toy{
return Toy<T>{toy.toy_name}
}
fn get_toy2(toy Toy) Toy{
return Toy<T,U>{toy.toy_name}
}
fn main() {
println(get_toy1(Toy{'Fire Truck'}))
println(get_toy2(Toy{'Police Car'}))
}