From ce0867f40c7ec2537035e15adef09304f7a7858e Mon Sep 17 00:00:00 2001 From: zakuro Date: Fri, 1 Oct 2021 21:14:21 +0900 Subject: [PATCH] vfmt: fix removal of selective imported type used in map (#12030) --- vlib/v/fmt/fmt.v | 6 ++++++ vlib/v/fmt/tests/import_selective_expected.vv | 3 +++ vlib/v/fmt/tests/import_selective_input.vv | 2 ++ 3 files changed, 11 insertions(+) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 37a5b4bb64..44246970c0 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -245,6 +245,12 @@ pub fn (mut f Fmt) short_module(name string) string { pub fn (mut f Fmt) mark_types_import_as_used(typ ast.Type) { sym := f.table.get_type_symbol(typ) + if sym.info is ast.Map { + map_info := sym.map_info() + f.mark_types_import_as_used(map_info.key_type) + f.mark_types_import_as_used(map_info.value_type) + return + } name := sym.name.split('<')[0] // take `Type` from `Type` f.mark_import_as_used(name) } diff --git a/vlib/v/fmt/tests/import_selective_expected.vv b/vlib/v/fmt/tests/import_selective_expected.vv index 66e4621ff6..8e21735f66 100644 --- a/vlib/v/fmt/tests/import_selective_expected.vv +++ b/vlib/v/fmt/tests/import_selective_expected.vv @@ -18,6 +18,8 @@ import mod { RightOfIs, StructEmbed, StructField, + StructMapFieldKey, + StructMapFieldValue, StructMethodArg, StructMethodArgGeneric, StructMethodRet, @@ -29,6 +31,7 @@ struct Struct { StructEmbed v StructField ref &StructRefField + map map[StructMapFieldKey]StructMapFieldValue } fn (s Struct) method(v StructMethodArg) StructMethodRet { diff --git a/vlib/v/fmt/tests/import_selective_input.vv b/vlib/v/fmt/tests/import_selective_input.vv index c5e2dd4d5f..1433b0e6c3 100644 --- a/vlib/v/fmt/tests/import_selective_input.vv +++ b/vlib/v/fmt/tests/import_selective_input.vv @@ -10,6 +10,7 @@ import os { import mod { Unused, StructEmbed, StructField, StructRefField + StructMapFieldKey, StructMapFieldValue, StructMethodArg, StructMethodArgGeneric, StructMethodRet, @@ -34,6 +35,7 @@ struct Struct { StructEmbed v StructField ref &StructRefField + map map[StructMapFieldKey]StructMapFieldValue } fn (s Struct) method(v StructMethodArg) StructMethodRet {