diff --git a/cmd/tools/check_os_api_parity.v b/cmd/tools/check_os_api_parity.v index 4d3054cfb0..cb5f32c30c 100644 --- a/cmd/tools/check_os_api_parity.v +++ b/cmd/tools/check_os_api_parity.v @@ -105,7 +105,7 @@ fn (app App) gen_api_for_module_in_os(mod_name string, os_name string) string { for s in f.stmts { if s is ast.FnDecl { if s.is_pub { - fn_signature := s.stringify(b.table, mod_name) + fn_signature := s.stringify(b.table, mod_name, map[string]string) fn_mod := s.modname() if fn_mod == mod_name { fline := '$fn_mod: $fn_signature' diff --git a/vlib/v/ast/str.v b/vlib/v/ast/str.v index 7855aa457b..9f6a022f59 100644 --- a/vlib/v/ast/str.v +++ b/vlib/v/ast/str.v @@ -19,7 +19,7 @@ pub fn (node &FnDecl) modname() string { } // These methods are used only by vfmt, vdoc, and for debugging. -pub fn (node &FnDecl) stringify(t &table.Table, cur_mod string) string { +pub fn (node &FnDecl) stringify(t &table.Table, cur_mod string, m2a map[string]string) string { mut f := strings.new_builder(30) if node.is_pub { f.write('pub ') @@ -84,6 +84,9 @@ pub fn (node &FnDecl) stringify(t &table.Table, cur_mod string) string { } } s = util.no_cur_mod(s, cur_mod) + for mod, alias in m2a { + s = s.replace(mod, alias) + } if should_add_type { if !is_type_only { f.write(' ') @@ -99,9 +102,11 @@ pub fn (node &FnDecl) stringify(t &table.Table, cur_mod string) string { } f.write(')') if node.return_type != table.void_type { - // typ := t.type_to_str(node.typ) - // if typ.starts_with(' - f.write(' ' + util.no_cur_mod(t.type_to_str(node.return_type), cur_mod)) + mut rs := util.no_cur_mod(t.type_to_str(node.return_type), cur_mod) + for mod, alias in m2a { + rs = rs.replace(mod, alias) + } + f.write(' ' + rs) } return f.str() } diff --git a/vlib/v/builder/builder.v b/vlib/v/builder/builder.v index 40ae8ee253..3332eb7e85 100644 --- a/vlib/v/builder/builder.v +++ b/vlib/v/builder/builder.v @@ -331,7 +331,7 @@ fn (b &Builder) print_warnings_and_errors() { for stmt in file.stmts { if stmt is ast.FnDecl { if stmt.name == fn_name { - fheader := stmt.stringify(b.table, 'main') + fheader := stmt.stringify(b.table, 'main', map[string]string{}) redefines << FunctionRedefinition{ fpath: file.path fline: stmt.pos.line_nr diff --git a/vlib/v/doc/utils.v b/vlib/v/doc/utils.v index 38d322157a..4c4acc5b97 100644 --- a/vlib/v/doc/utils.v +++ b/vlib/v/doc/utils.v @@ -85,7 +85,7 @@ pub fn (mut d Doc) stmt_signature(stmt ast.Stmt) string { return 'module $stmt.name' } ast.FnDecl { - return stmt.stringify(d.table, d.fmt.cur_mod) + return stmt.stringify(d.table, d.fmt.cur_mod, d.fmt.mod2alias) } else { d.fmt.out = strings.new_builder(1000) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 7e36e4e132..abe779df5a 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -705,7 +705,7 @@ pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) { f.comments_after_last_field(node.pre_comments) for method in node.methods { f.write('\t') - f.write(method.stringify(f.table, f.cur_mod).after('fn ')) + f.write(method.stringify(f.table, f.cur_mod, f.mod2alias).after('fn ')) f.comments(method.comments, inline: true, has_nl: false, level: .indent) f.writeln('') } @@ -1251,7 +1251,7 @@ pub fn (mut f Fmt) fn_decl(node ast.FnDecl) { // println('$it.name find_comment($it.pos.line_nr)') // f.find_comment(it.pos.line_nr) f.attrs(node.attrs) - f.write(node.stringify(f.table, f.cur_mod)) // `Expr` instead of `ast.Expr` in mod ast + f.write(node.stringify(f.table, f.cur_mod, f.mod2alias)) // `Expr` instead of `ast.Expr` in mod ast if node.language == .v { if !node.no_body { f.writeln(' {') diff --git a/vlib/v/fmt/tests/fn_js_object_keep.vv b/vlib/v/fmt/tests/fn_js_object_keep.vv deleted file mode 100644 index 3a2f172499..0000000000 --- a/vlib/v/fmt/tests/fn_js_object_keep.vv +++ /dev/null @@ -1,7 +0,0 @@ -fn (v JS.String) toString() JS.String - -fn JS.Math.abs(f64) f64 - -fn main() { - JS.Math.abs(0) -} diff --git a/vlib/v/fmt/tests/language_prefixes_keep.vv b/vlib/v/fmt/tests/language_prefixes_keep.vv new file mode 100644 index 0000000000..a0a25c5994 --- /dev/null +++ b/vlib/v/fmt/tests/language_prefixes_keep.vv @@ -0,0 +1,13 @@ +fn (v JS.String) toString() JS.String + +fn (v JS.String) toMultiRet() (JS.String, int) + +fn JS.Math.abs(f64) f64 + +fn main() { + JS.Math.abs(0) +} + +fn object_ref_optional() ?&C.File { + return error('') +} diff --git a/vlib/v/fmt/tests/module_alias_keep.vv b/vlib/v/fmt/tests/module_alias_keep.vv new file mode 100644 index 0000000000..6057d39ad5 --- /dev/null +++ b/vlib/v/fmt/tests/module_alias_keep.vv @@ -0,0 +1,17 @@ +import time +import semver as sv + +fn foo(f time.Time) time.Time { + f2 := time.Time{} + return f +} + +fn bar(b sv.Version) sv.Version { + b2 := sv.Version{} + return b +} + +fn bar_multi_return(b sv.Version) (sv.Version, int) { + b2 := sv.Version{} + return b, 0 +}