fmt: keep generics for selective import as fn param (#10138)

pull/10143/head
Lukas Neubert 2021-05-19 20:25:02 +02:00 committed by GitHub
parent de080ba149
commit be189e0059
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 7 deletions

View File

@ -848,12 +848,6 @@ pub fn (mytable &Table) type_to_code(t Type) string {
// import_aliases is a map of imported symbol aliases 'module.Type' => 'Type'
pub fn (t &Table) type_to_str_using_aliases(typ Type, import_aliases map[string]string) string {
/*
if t.pref.is_verbose {
print_backtrace()
exit(0)
}
*/
sym := t.get_type_symbol(typ)
mut res := sym.name
match sym.kind {
@ -957,6 +951,19 @@ pub fn (t &Table) type_to_str_using_aliases(typ Type, import_aliases map[string]
res = t.shorten_user_defined_typenames(res, import_aliases)
}
}
.generic_struct_inst {
info := sym.info as GenericStructInst
res = sym.name.all_before('<')
res += '<'
for i, ctyp in info.concrete_types {
res += t.get_type_symbol(ctyp).name
if i != info.concrete_types.len - 1 {
res += ', '
}
}
res += '>'
res = t.shorten_user_defined_typenames(res, import_aliases)
}
.void {
if typ.has_flag(.optional) {
return '?'

View File

@ -1,3 +1,9 @@
import mymod { ImpNode }
fn foobar_mymod<U>(inode ImpNode<U>) ImpNode<U> {
return ImpNode{}
}
fn simple<T>() T {
return T{}
}
@ -8,6 +14,18 @@ fn (_ Foo) simple<T>() T {
return T{}
}
struct NonGenericStruct {}
fn use_as_generic(ngs NonGenericStruct<V>) NonGenericStruct<V> {
return NonGenericStruct{}
}
struct GenericStruct<A, B> {}
fn proper_generics(gs GenericStruct<A, B>) GenericStruct<A, B> {
return gs
}
fn main() {
simple<int>()
Foo{}.simple<int>()

View File

@ -536,7 +536,8 @@ pub fn (mut p Parser) parse_generic_struct_inst_type(name string) ast.Type {
p.check(.gt)
p.in_generic_params = false
bs_name += '>'
if is_instance && concrete_types.len > 0 {
// fmt operates on a per-file basis, so is_instance might be not set correctly. Thus it's ignored.
if (is_instance || p.pref.is_fmt) && concrete_types.len > 0 {
mut gt_idx := p.table.find_type_idx(bs_name)
if gt_idx > 0 {
return ast.new_type(gt_idx)