fmt: fix removal of selective imported types used in type decl (#12868)

pull/12873/head
zakuro 2021-12-17 12:20:16 +09:00 committed by GitHub
parent 2f7ac7e407
commit e5e3979e45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 2 deletions

View File

@ -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 := ' | '

View File

@ -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

View File

@ -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