diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 39b45d747b..164f4f5b3d 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1238,6 +1238,7 @@ pub fn (mut f Fmt) alias_type_decl(node ast.AliasTypeDecl) { f.write('type $node.name = $ptype') f.comments(node.comments, has_nl: false) + f.mark_types_import_as_used(node.parent_type) } pub fn (mut f Fmt) fn_type_decl(node ast.FnTypeDecl) { @@ -1254,6 +1255,7 @@ pub fn (mut f Fmt) fn_type_decl(node ast.FnTypeDecl) { f.write(arg.typ.share().str() + ' ') } f.write(arg.name) + f.mark_types_import_as_used(arg.typ) mut s := f.no_cur_mod(f.table.type_to_str_using_aliases(arg.typ, f.mod2alias)) if arg.is_mut { if s.starts_with('&') { @@ -1278,6 +1280,7 @@ pub fn (mut f Fmt) fn_type_decl(node ast.FnTypeDecl) { } f.write(')') if fn_info.return_type.idx() != ast.void_type_idx { + f.mark_types_import_as_used(fn_info.return_type) ret_str := f.no_cur_mod(f.table.type_to_str_using_aliases(fn_info.return_type, f.mod2alias)) f.write(' $ret_str') @@ -1298,8 +1301,11 @@ pub fn (mut f Fmt) sum_type_decl(node ast.SumTypeDecl) { f.write_generic_types(node.generic_types) f.write(' = ') - mut sum_type_names := node.variants.map(f.table.type_to_str_using_aliases(it.typ, - f.mod2alias)) + mut sum_type_names := []string{cap: node.variants.len} + for variant in node.variants { + sum_type_names << f.table.type_to_str_using_aliases(variant.typ, f.mod2alias) + f.mark_types_import_as_used(variant.typ) + } sum_type_names.sort() mut separator := ' | ' diff --git a/vlib/v/fmt/tests/import_selective_expected.vv b/vlib/v/fmt/tests/import_selective_expected.vv index 8beb6dd9e0..93e5b87613 100644 --- a/vlib/v/fmt/tests/import_selective_expected.vv +++ b/vlib/v/fmt/tests/import_selective_expected.vv @@ -6,7 +6,10 @@ import os { user_os, } import mod { + AliasTarget, Enum, + FnAliasParam, + FnAliasRet, FnArg, FnArgGeneric, FnArgTypeParam1, @@ -29,8 +32,14 @@ import mod { StructMethodRet, StructMethodRetGeneric, StructRefField, + SumVariant1, + SumVariant2, } +type Alias = AliasTarget +type SumType = SumVariant1 | SumVariant2 +type Fn = fn (FnAliasParam) FnAliasRet + struct Struct { StructEmbed v StructField diff --git a/vlib/v/fmt/tests/import_selective_input.vv b/vlib/v/fmt/tests/import_selective_input.vv index a5c8217f53..66153a1bfe 100644 --- a/vlib/v/fmt/tests/import_selective_input.vv +++ b/vlib/v/fmt/tests/import_selective_input.vv @@ -32,9 +32,17 @@ import mod { RightOfIs, RightOfAs, + AliasTarget, + SumVariant1, SumVariant2, + FnAliasParam, FnAliasRet + Enum } +type Alias = AliasTarget +type SumType = SumVariant1 | SumVariant2 +type Fn = fn(FnAliasParam) FnAliasRet + struct Struct { StructEmbed v StructField