From 974737f26bf722c902f9d2a4fe8344c0129f70b2 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 12 Jul 2021 10:40:11 +0300 Subject: [PATCH] vfmt: keep `module mymodule ... x := []thread MyStruct{}` as it is --- vlib/v/ast/types.v | 15 ++++++++++++++- vlib/v/fmt/tests/thread_in_a_module_keep.vv | 17 +++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 vlib/v/fmt/tests/thread_in_a_module_keep.vv diff --git a/vlib/v/ast/types.v b/vlib/v/ast/types.v index 9f5acdb766..d6f7fb7eeb 100644 --- a/vlib/v/ast/types.v +++ b/vlib/v/ast/types.v @@ -865,6 +865,15 @@ pub fn (mytable &Table) type_to_code(t Type) string { pub fn (t &Table) type_to_str_using_aliases(typ Type, import_aliases map[string]string) string { sym := t.get_type_symbol(typ) mut res := sym.name + // Note, that the duplication of code in some of the match branches here + // is VERY deliberate. DO NOT be tempted to use `else {}` instead, because + // that strongly reduces the usefullness of the exhaustive checking that + // match does. + // Using else{} here led to subtle bugs in vfmt discovered *months* + // after the original code was written. + // It is important that each case here is handled *explicitly* and + // *clearly*, and that when a new kind is added, it should also be handled + // explicitly. match sym.kind { .int_literal, .float_literal { res = sym.name @@ -989,7 +998,11 @@ pub fn (t &Table) type_to_str_using_aliases(typ Type, import_aliases map[string] } return 'void' } - else { + .thread { + res = 'thread ' + + t.type_to_str_using_aliases(sym.thread_info().return_type, import_aliases) + } + .alias, .any, .sum_type, .interface_, .size_t, .aggregate, .placeholder, .enum_ { res = t.shorten_user_defined_typenames(res, import_aliases) } } diff --git a/vlib/v/fmt/tests/thread_in_a_module_keep.vv b/vlib/v/fmt/tests/thread_in_a_module_keep.vv new file mode 100644 index 0000000000..41e7ea69c5 --- /dev/null +++ b/vlib/v/fmt/tests/thread_in_a_module_keep.vv @@ -0,0 +1,17 @@ +module my_module + +import another +import other as ooo + +pub fn do_something() { + mut simples := []MyStruct{} + // + mut threads := []thread MyStruct{} + threads.wait() + // + mut another_threads := []thread another.MyStruct{} + another_threads.wait() + // + mut other_threads := []thread ooo.MyStruct{} + other_threads.wait() +}