Skip to content

Commit c766ce4

Browse files
authored
fmt: fix removal of selective imported types, used as array elements (#16963)
1 parent ba091a3 commit c766ce4

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

vlib/v/fmt/fmt.v

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -285,16 +285,23 @@ pub fn (mut f Fmt) short_module(name string) string {
285285

286286
pub fn (mut f Fmt) mark_types_import_as_used(typ ast.Type) {
287287
sym := f.table.sym(typ)
288-
if sym.info is ast.Map {
289-
map_info := sym.map_info()
290-
f.mark_types_import_as_used(map_info.key_type)
291-
f.mark_types_import_as_used(map_info.value_type)
292-
return
293-
}
294-
if sym.info is ast.GenericInst {
295-
for concrete_typ in sym.info.concrete_types {
296-
f.mark_types_import_as_used(concrete_typ)
288+
match sym.info {
289+
ast.Map {
290+
map_info := sym.map_info()
291+
f.mark_types_import_as_used(map_info.key_type)
292+
f.mark_types_import_as_used(map_info.value_type)
293+
return
294+
}
295+
ast.Array, ast.ArrayFixed {
296+
f.mark_types_import_as_used(sym.info.elem_type)
297+
return
298+
}
299+
ast.GenericInst {
300+
for concrete_typ in sym.info.concrete_types {
301+
f.mark_types_import_as_used(concrete_typ)
302+
}
297303
}
304+
else {}
298305
}
299306
name := sym.name.split('[')[0] // take `Type` from `Type[T]`
300307
f.mark_import_as_used(name)

vlib/v/fmt/tests/import_selective_expected.vv

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import mod {
1414
FnArgGeneric,
1515
FnArgTypeParam1,
1616
FnArgTypeParam2,
17+
FnArgVariadic,
1718
FnRet,
1819
FnRetGeneric,
1920
FnRetTypeParam1,
@@ -23,8 +24,10 @@ import mod {
2324
InterfaceMethodRet,
2425
RightOfAs,
2526
RightOfIs,
27+
StructArrayFieldElem,
2628
StructEmbed,
2729
StructField,
30+
StructFixedArrayFieldElem,
2831
StructMapFieldKey,
2932
StructMapFieldValue,
3033
StructMethodArg,
@@ -42,9 +45,11 @@ type Fn = fn (FnAliasParam) FnAliasRet
4245

4346
struct Struct {
4447
StructEmbed
45-
v StructField
46-
ref &StructRefField
47-
map map[StructMapFieldKey]StructMapFieldValue
48+
v StructField
49+
ref &StructRefField
50+
map map[StructMapFieldKey]StructMapFieldValue
51+
arr []StructArrayFieldElem
52+
arr_fixed [2]StructFixedArrayFieldElem
4853
}
4954

5055
fn (s Struct) method(v StructMethodArg) StructMethodRet {
@@ -60,7 +65,7 @@ interface Interface {
6065
f(InterfaceMethodArg) InterfaceMethodRet
6166
}
6267

63-
fn f(v FnArg) FnRet {
68+
fn f(v FnArg, vv ...FnArgVariadic) FnRet {
6469
if v is RightOfIs {
6570
}
6671
_ = v as RightOfAs

vlib/v/fmt/tests/import_selective_input.vv

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import mod {
1111
Unused,
1212
StructEmbed, StructField, StructRefField
1313
StructMapFieldKey, StructMapFieldValue,
14+
StructArrayFieldElem, StructFixedArrayFieldElem
1415
StructMethodArg,
1516
StructMethodArgGeneric,
1617
StructMethodRet,
@@ -21,6 +22,7 @@ import mod {
2122
InterfaceMethodRet,
2223

2324
FnArg,
25+
FnArgVariadic
2426
FnArgGeneric
2527
FnArgTypeParam1
2628
FnArgTypeParam2
@@ -48,6 +50,8 @@ struct Struct {
4850
v StructField
4951
ref &StructRefField
5052
map map[StructMapFieldKey]StructMapFieldValue
53+
arr []StructArrayFieldElem
54+
arr_fixed [2]StructFixedArrayFieldElem
5155
}
5256

5357
fn (s Struct) method(v StructMethodArg) StructMethodRet {
@@ -63,7 +67,7 @@ interface Interface {
6367
f(InterfaceMethodArg) InterfaceMethodRet
6468
}
6569

66-
fn f(v FnArg) FnRet {
70+
fn f(v FnArg, vv ...FnArgVariadic) FnRet {
6771
if v is RightOfIs {}
6872
_ = v as RightOfAs
6973

0 commit comments

Comments
 (0)