From 2d867a27662b28387f0281b01bf9623f275c7b0f Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Fri, 8 Apr 2022 16:38:34 +0200 Subject: [PATCH] ast: fix reported errors, when arrays of types defined in modules are involved (#13952) --- vlib/v/ast/types.v | 7 +++++ .../tests/check_err_msg_with_generics.out | 27 +++++++++++++++++++ .../tests/check_err_msg_with_generics.vv | 16 +++++++++++ 3 files changed, 50 insertions(+) create mode 100644 vlib/v/checker/tests/check_err_msg_with_generics.out create mode 100644 vlib/v/checker/tests/check_err_msg_with_generics.vv diff --git a/vlib/v/ast/types.v b/vlib/v/ast/types.v index 842c304629..7bc360d1c8 100644 --- a/vlib/v/ast/types.v +++ b/vlib/v/ast/types.v @@ -1155,6 +1155,13 @@ fn (t Table) shorten_user_defined_typenames(originalname string, import_aliases } else if res in import_aliases { res = import_aliases[res] } else { + // FIXME: clean this case and remove the following if + // because it is an hack to format well the type when + // there is a []modul.name + if res.contains('[]') { + idx := res.index('.') or { -1 } + return res[idx + 1..] + } // types defined by the user // mod.submod.submod2.Type => submod2.Type mut parts := res.split('.') diff --git a/vlib/v/checker/tests/check_err_msg_with_generics.out b/vlib/v/checker/tests/check_err_msg_with_generics.out new file mode 100644 index 0000000000..f4587a294a --- /dev/null +++ b/vlib/v/checker/tests/check_err_msg_with_generics.out @@ -0,0 +1,27 @@ +vlib/v/checker/tests/check_err_msg_with_generics.vv:15:10: error: cannot cast struct `BSTree>>` to `int` + 13 | fn test_err_msg() { + 14 | typ := datatypes.BSTree>>{} + 15 | println(int(typ)) + | ~~~~~~~~ + 16 | } +vlib/datatypes/bstree.v:190:17: error: cannot append `T` to `[]T` + 188 | } + 189 | bst.in_order_traversal_helper(node.left, mut result) + 190 | result << node.value + | ~~~~~ + 191 | bst.in_order_traversal_helper(node.right, mut result) + 192 | } +vlib/datatypes/bstree.v:210:17: error: cannot append `T` to `[]T` + 208 | bst.post_order_traversal_helper(node.left, mut result) + 209 | bst.post_order_traversal_helper(node.right, mut result) + 210 | result << node.value + | ~~~~~ + 211 | } + 212 | +vlib/datatypes/bstree.v:226:17: error: cannot append `T` to `[]T` + 224 | return + 225 | } + 226 | result << node.value + | ~~~~~ + 227 | bst.pre_order_traversal_helper(node.left, mut result) + 228 | bst.pre_order_traversal_helper(node.right, mut result) diff --git a/vlib/v/checker/tests/check_err_msg_with_generics.vv b/vlib/v/checker/tests/check_err_msg_with_generics.vv new file mode 100644 index 0000000000..57da5432de --- /dev/null +++ b/vlib/v/checker/tests/check_err_msg_with_generics.vv @@ -0,0 +1,16 @@ +import datatypes + +type Result = Err | Ok + +struct Ok { + value T +} + +struct Err { + value U +} + +fn test_err_msg() { + typ := datatypes.BSTree>>{} + println(int(typ)) +}