From 5ac1e14e7f6d103dbef9cd480492aa19bea49ab0 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Mon, 21 Apr 2025 12:51:15 -0300 Subject: [PATCH] checker: add a deprecation note for `any` arg, prevent `any` from being used as map key,value or array type (#24277) --- vlib/v/checker/containers.v | 8 +++++- vlib/v/checker/fn.v | 5 ++++ vlib/v/checker/tests/deprecate_any.out | 38 ++++++++++++++++++++++++++ vlib/v/checker/tests/deprecate_any.vv | 7 +++++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 vlib/v/checker/tests/deprecate_any.out create mode 100644 vlib/v/checker/tests/deprecate_any.vv diff --git a/vlib/v/checker/containers.v b/vlib/v/checker/containers.v index c751f26a21..45c8fe1e3c 100644 --- a/vlib/v/checker/containers.v +++ b/vlib/v/checker/containers.v @@ -18,7 +18,7 @@ fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type { } if node.elem_type != 0 { elem_sym := c.table.sym(node.elem_type) - + c.check_any_type(node.elem_type, elem_sym, node.pos) if node.typ.has_flag(.option) && (node.has_cap || node.has_len) { c.error('Option array `${elem_sym.name}` cannot have initializers', node.pos) } @@ -531,6 +531,12 @@ fn (mut c Checker) map_init(mut node ast.MapInit) ast.Type { c.ensure_type_exists(info.value_type, node.pos) node.key_type = info.key_type node.value_type = info.value_type + if (c.table.sym(info.key_type).language == .v && info.key_type == ast.any_type) + || (c.table.sym(info.value_type).language == .v && info.value_type == ast.any_type) { + c.note('the `any` type is deprecated and will be removed soon - either use an empty interface, or a sum type', + node.pos) + c.error('cannot use type `any` here', node.pos) + } return node.typ } diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index d1c887351a..c79256cb27 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -280,6 +280,11 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) { c.error('result type arguments are not supported', param.type_pos) } arg_typ_sym := c.table.sym(param.typ) + if arg_typ_sym.language == .v && param.typ == ast.any_type + && c.file.mod.name != 'builtin' { + c.note('the `any` type is deprecated and will be removed soon - either use an empty interface, or a sum type', + param.pos) + } // resolve unresolved fixed array size e.g. [mod.const]array_type if arg_typ_sym.info is ast.ArrayFixed && c.array_fixed_has_unresolved_size(arg_typ_sym.info) { diff --git a/vlib/v/checker/tests/deprecate_any.out b/vlib/v/checker/tests/deprecate_any.out new file mode 100644 index 0000000000..cac80b2ee9 --- /dev/null +++ b/vlib/v/checker/tests/deprecate_any.out @@ -0,0 +1,38 @@ +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 + 1 | fn foo(a any) {} + | ^ + 2 | + 3 | fn main() { +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 + 3 | fn main() { + 4 | _ := []any{} + 5 | _ := map[int]any{} + | ~~~~~~~~~~~~~ + 6 | _ := map[any]int{} + 7 | } +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 + 4 | _ := []any{} + 5 | _ := map[int]any{} + 6 | _ := map[any]int{} + | ~~~~~~~~~~~~~ + 7 | } +vlib/v/checker/tests/deprecate_any.vv:4:7: error: cannot use type `any` here + 2 | + 3 | fn main() { + 4 | _ := []any{} + | ~~~~~~ + 5 | _ := map[int]any{} + 6 | _ := map[any]int{} +vlib/v/checker/tests/deprecate_any.vv:5:7: error: cannot use type `any` here + 3 | fn main() { + 4 | _ := []any{} + 5 | _ := map[int]any{} + | ~~~~~~~~~~~~~ + 6 | _ := map[any]int{} + 7 | } +vlib/v/checker/tests/deprecate_any.vv:6:7: error: cannot use type `any` here + 4 | _ := []any{} + 5 | _ := map[int]any{} + 6 | _ := map[any]int{} + | ~~~~~~~~~~~~~ + 7 | } diff --git a/vlib/v/checker/tests/deprecate_any.vv b/vlib/v/checker/tests/deprecate_any.vv new file mode 100644 index 0000000000..cbe6d39a88 --- /dev/null +++ b/vlib/v/checker/tests/deprecate_any.vv @@ -0,0 +1,7 @@ +fn foo(a any) {} + +fn main() { + _ := []any{} + _ := map[int]any{} + _ := map[any]int{} +}