diff --git a/vlib/v/ast/types.v b/vlib/v/ast/types.v index 9df8e27580..40ff552c35 100644 --- a/vlib/v/ast/types.v +++ b/vlib/v/ast/types.v @@ -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 '?' diff --git a/vlib/v/fmt/tests/generics_keep.vv b/vlib/v/fmt/tests/generics_keep.vv index 58716f5f8b..4fe55e76b3 100644 --- a/vlib/v/fmt/tests/generics_keep.vv +++ b/vlib/v/fmt/tests/generics_keep.vv @@ -1,3 +1,9 @@ +import mymod { ImpNode } + +fn foobar_mymod(inode ImpNode) ImpNode { + return ImpNode{} +} + fn simple() T { return T{} } @@ -8,6 +14,18 @@ fn (_ Foo) simple() T { return T{} } +struct NonGenericStruct {} + +fn use_as_generic(ngs NonGenericStruct) NonGenericStruct { + return NonGenericStruct{} +} + +struct GenericStruct {} + +fn proper_generics(gs GenericStruct) GenericStruct { + return gs +} + fn main() { simple() Foo{}.simple() diff --git a/vlib/v/parser/parse_type.v b/vlib/v/parser/parse_type.v index ef35f9b65b..357f453104 100644 --- a/vlib/v/parser/parse_type.v +++ b/vlib/v/parser/parse_type.v @@ -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)