From 788520eb52709ded364597962b186b421336ba8d Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Fri, 21 May 2021 19:33:04 +0200 Subject: [PATCH] fmt, ast: fix usage of import aliases (#10151) --- vlib/v/ast/types.v | 15 ++++++++++++--- vlib/v/fmt/fmt.v | 13 +++++++------ .../{import_keep.vv => import_comments_keep.vv} | 0 ...e_expected.vv => import_duplicate_expected.vv} | 0 ...ultiple_input.vv => import_duplicate_input.vv} | 0 vlib/v/fmt/tests/import_selective_input.vv | 1 - vlib/v/fmt/tests/import_selective_keep.vv | 5 +++++ vlib/v/fmt/tests/import_with_alias_keep.vv | 4 ++++ vlib/v/fmt/tests/module_alias_keep.vv | 10 ++++++++++ 9 files changed, 38 insertions(+), 10 deletions(-) rename vlib/v/fmt/tests/{import_keep.vv => import_comments_keep.vv} (100%) rename vlib/v/fmt/tests/{import_multiple_expected.vv => import_duplicate_expected.vv} (100%) rename vlib/v/fmt/tests/{import_multiple_input.vv => import_duplicate_input.vv} (100%) create mode 100644 vlib/v/fmt/tests/import_selective_keep.vv diff --git a/vlib/v/ast/types.v b/vlib/v/ast/types.v index 40ff552c35..d028e43422 100644 --- a/vlib/v/ast/types.v +++ b/vlib/v/ast/types.v @@ -909,7 +909,7 @@ pub fn (t &Table) type_to_str_using_aliases(typ Type, import_aliases map[string] } else { if res.starts_with('fn (') { // fn foo () - res = t.fn_signature(info.func, type_only: true) + res = t.fn_signature_using_aliases(info.func, import_aliases, type_only: true) } else { // FnFoo res = t.shorten_user_defined_typenames(res, import_aliases) @@ -1001,9 +1001,14 @@ fn (t Table) shorten_user_defined_typenames(originalname string, import_aliases mut parts := res.split('.') if parts.len > 1 { ind := parts.len - 2 + if t.is_fmt { + // Rejoin the module parts for correct usage of aliases + parts[ind] = parts[..ind + 1].join('.') + } if parts[ind] in import_aliases { parts[ind] = import_aliases[parts[ind]] } + res = parts[ind..].join('.') } else { res = parts[0] @@ -1018,6 +1023,10 @@ pub struct FnSignatureOpts { } pub fn (t &Table) fn_signature(func &Fn, opts FnSignatureOpts) string { + return t.fn_signature_using_aliases(func, map[string]string{}, opts) +} + +pub fn (t &Table) fn_signature_using_aliases(func &Fn, import_aliases map[string]string, opts FnSignatureOpts) string { mut sb := strings.new_builder(20) if !opts.skip_receiver { sb.write_string('fn ') @@ -1041,7 +1050,7 @@ pub fn (t &Table) fn_signature(func &Fn, opts FnSignatureOpts) string { if !opts.type_only { sb.write_string('$param.name ') } - styp := t.type_to_str(typ) + styp := t.type_to_str_using_aliases(typ, import_aliases) if i == func.params.len - 1 && func.is_variadic { sb.write_string('...$styp') } else { @@ -1050,7 +1059,7 @@ pub fn (t &Table) fn_signature(func &Fn, opts FnSignatureOpts) string { } sb.write_string(')') if func.return_type != ast.void_type { - sb.write_string(' ${t.type_to_str(func.return_type)}') + sb.write_string(' ${t.type_to_str_using_aliases(func.return_type, import_aliases)}') } return sb.str() } diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index ed84aef41f..02538085cc 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -75,11 +75,10 @@ pub fn fmt(file ast.File, table &ast.Table, pref &pref.Preferences, is_debug boo pub fn (mut f Fmt) process_file_imports(file &ast.File) { for imp in file.imports { - mod := imp.mod.all_after_last('.') - f.mod2alias[mod] = imp.alias + f.mod2alias[imp.mod] = imp.alias for sym in imp.syms { f.mod2alias['${imp.mod}.$sym.name'] = sym.name - f.mod2alias['${mod}.$sym.name'] = sym.name + f.mod2alias['${imp.mod.all_after_last('.')}.$sym.name'] = sym.name f.mod2alias[sym.name] = sym.name f.import_syms_used[sym.name] = false } @@ -211,7 +210,8 @@ pub fn (mut f Fmt) short_module(name string) string { if vals.len < 2 { return name } - mname, tprefix := f.get_modname_prefix(vals[vals.len - 2]) + idx := vals.len - 1 + mname, tprefix := f.get_modname_prefix(vals[..idx].join('.')) symname := vals[vals.len - 1] aname := f.mod2alias[mname] if aname == '' { @@ -2194,7 +2194,8 @@ pub fn (mut f Fmt) match_expr(node ast.MatchExpr) { } pub fn (mut f Fmt) offset_of(node ast.OffsetOf) { - f.write('__offsetof(${f.table.type_to_str(node.struct_type)}, $node.field)') + f.write('__offsetof(${f.table.type_to_str_using_aliases(node.struct_type, f.mod2alias)}, $node.field)') + f.mark_types_import_as_used(node.struct_type) } pub fn (mut f Fmt) or_expr(node ast.OrExpr) { @@ -2427,7 +2428,7 @@ pub fn (mut f Fmt) string_inter_literal(node ast.StringInterLiteral) { } pub fn (mut f Fmt) type_expr(node ast.TypeNode) { - f.write(f.table.type_to_str(node.typ)) + f.write(f.table.type_to_str_using_aliases(node.typ, f.mod2alias)) } pub fn (mut f Fmt) type_of(node ast.TypeOf) { diff --git a/vlib/v/fmt/tests/import_keep.vv b/vlib/v/fmt/tests/import_comments_keep.vv similarity index 100% rename from vlib/v/fmt/tests/import_keep.vv rename to vlib/v/fmt/tests/import_comments_keep.vv diff --git a/vlib/v/fmt/tests/import_multiple_expected.vv b/vlib/v/fmt/tests/import_duplicate_expected.vv similarity index 100% rename from vlib/v/fmt/tests/import_multiple_expected.vv rename to vlib/v/fmt/tests/import_duplicate_expected.vv diff --git a/vlib/v/fmt/tests/import_multiple_input.vv b/vlib/v/fmt/tests/import_duplicate_input.vv similarity index 100% rename from vlib/v/fmt/tests/import_multiple_input.vv rename to vlib/v/fmt/tests/import_duplicate_input.vv diff --git a/vlib/v/fmt/tests/import_selective_input.vv b/vlib/v/fmt/tests/import_selective_input.vv index 5af68c42ca..cb92d42dc9 100644 --- a/vlib/v/fmt/tests/import_selective_input.vv +++ b/vlib/v/fmt/tests/import_selective_input.vv @@ -59,4 +59,3 @@ fn main() { println(file_ext('main.v')) println(imaginary(1)) } - diff --git a/vlib/v/fmt/tests/import_selective_keep.vv b/vlib/v/fmt/tests/import_selective_keep.vv new file mode 100644 index 0000000000..032e4bca5f --- /dev/null +++ b/vlib/v/fmt/tests/import_selective_keep.vv @@ -0,0 +1,5 @@ +import math.complex { Complex } + +fn main() { + assert *(&f64(&byte(&num) + __offsetof(Complex, re))) == 1.0 +} diff --git a/vlib/v/fmt/tests/import_with_alias_keep.vv b/vlib/v/fmt/tests/import_with_alias_keep.vv index c93fc0822a..f883f0e84b 100644 --- a/vlib/v/fmt/tests/import_with_alias_keep.vv +++ b/vlib/v/fmt/tests/import_with_alias_keep.vv @@ -1,6 +1,8 @@ import os import time as t import some.library as slib +import crypto.sha256 +import mymod.sha256 as mysha256 type my_alias = fn (t slib.MyType) @@ -18,4 +20,6 @@ fn main() { t.sleep_ms(500) println('end') os.system('date') + v_hash := sha256.sum('hi'.bytes()).hex() + my_hash := mysha256.sum('hi'.bytes()).hex() } diff --git a/vlib/v/fmt/tests/module_alias_keep.vv b/vlib/v/fmt/tests/module_alias_keep.vv index f35e6d0fa5..f6f77c0530 100644 --- a/vlib/v/fmt/tests/module_alias_keep.vv +++ b/vlib/v/fmt/tests/module_alias_keep.vv @@ -1,6 +1,7 @@ import time import semver as sv import term.ui as tui +import v.ast interface Inter { code tui.KeyCode @@ -24,3 +25,12 @@ fn bar_multi_return(b sv.Version) (sv.Version, int) { b2 := sv.Version{} return b, 0 } + +struct SomeStruct { + a fn (ast.Stmt, voidptr) bool +} + +fn main() { + if x is ast.FnDecl { + } +}