Skip to content

Commit 5ac1e14

Browse files
authored
checker: add a deprecation note for any arg, prevent any from being used as map key,value or array type (#24277)
1 parent 8442e02 commit 5ac1e14

4 files changed

Lines changed: 57 additions & 1 deletion

File tree

‎vlib/v/checker/containers.v‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
1818
}
1919
if node.elem_type != 0 {
2020
elem_sym := c.table.sym(node.elem_type)
21-
21+
c.check_any_type(node.elem_type, elem_sym, node.pos)
2222
if node.typ.has_flag(.option) && (node.has_cap || node.has_len) {
2323
c.error('Option array `${elem_sym.name}` cannot have initializers', node.pos)
2424
}
@@ -531,6 +531,12 @@ fn (mut c Checker) map_init(mut node ast.MapInit) ast.Type {
531531
c.ensure_type_exists(info.value_type, node.pos)
532532
node.key_type = info.key_type
533533
node.value_type = info.value_type
534+
if (c.table.sym(info.key_type).language == .v && info.key_type == ast.any_type)
535+
|| (c.table.sym(info.value_type).language == .v && info.value_type == ast.any_type) {
536+
c.note('the `any` type is deprecated and will be removed soon - either use an empty interface, or a sum type',
537+
node.pos)
538+
c.error('cannot use type `any` here', node.pos)
539+
}
534540
return node.typ
535541
}
536542

‎vlib/v/checker/fn.v‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,11 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
280280
c.error('result type arguments are not supported', param.type_pos)
281281
}
282282
arg_typ_sym := c.table.sym(param.typ)
283+
if arg_typ_sym.language == .v && param.typ == ast.any_type
284+
&& c.file.mod.name != 'builtin' {
285+
c.note('the `any` type is deprecated and will be removed soon - either use an empty interface, or a sum type',
286+
param.pos)
287+
}
283288
// resolve unresolved fixed array size e.g. [mod.const]array_type
284289
if arg_typ_sym.info is ast.ArrayFixed
285290
&& c.array_fixed_has_unresolved_size(arg_typ_sym.info) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
vlib/v/checker/tests/deprecate_any.vv:1:8: notice: the `any` type is deprecated and will be removed soon - either use an empty interface, or a sum type
2+
1 | fn foo(a any) {}
3+
| ^
4+
2 |
5+
3 | fn main() {
6+
vlib/v/checker/tests/deprecate_any.vv:5:7: notice: the `any` type is deprecated and will be removed soon - either use an empty interface, or a sum type
7+
3 | fn main() {
8+
4 | _ := []any{}
9+
5 | _ := map[int]any{}
10+
| ~~~~~~~~~~~~~
11+
6 | _ := map[any]int{}
12+
7 | }
13+
vlib/v/checker/tests/deprecate_any.vv:6:7: notice: the `any` type is deprecated and will be removed soon - either use an empty interface, or a sum type
14+
4 | _ := []any{}
15+
5 | _ := map[int]any{}
16+
6 | _ := map[any]int{}
17+
| ~~~~~~~~~~~~~
18+
7 | }
19+
vlib/v/checker/tests/deprecate_any.vv:4:7: error: cannot use type `any` here
20+
2 |
21+
3 | fn main() {
22+
4 | _ := []any{}
23+
| ~~~~~~
24+
5 | _ := map[int]any{}
25+
6 | _ := map[any]int{}
26+
vlib/v/checker/tests/deprecate_any.vv:5:7: error: cannot use type `any` here
27+
3 | fn main() {
28+
4 | _ := []any{}
29+
5 | _ := map[int]any{}
30+
| ~~~~~~~~~~~~~
31+
6 | _ := map[any]int{}
32+
7 | }
33+
vlib/v/checker/tests/deprecate_any.vv:6:7: error: cannot use type `any` here
34+
4 | _ := []any{}
35+
5 | _ := map[int]any{}
36+
6 | _ := map[any]int{}
37+
| ~~~~~~~~~~~~~
38+
7 | }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn foo(a any) {}
2+
3+
fn main() {
4+
_ := []any{}
5+
_ := map[int]any{}
6+
_ := map[any]int{}
7+
}

0 commit comments

Comments
 (0)