1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vfmt: do not replace module aliases in fn param and return types (#7472)

This commit is contained in:
Lukas Neubert 2020-12-22 21:38:13 +01:00 committed by GitHub
parent 6aadc828b1
commit c4aae2b55f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 44 additions and 16 deletions

View File

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

View File

@ -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()
}

View File

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

View File

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

View File

@ -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(' {')

View File

@ -1,7 +0,0 @@
fn (v JS.String) toString() JS.String
fn JS.Math.abs(f64) f64
fn main() {
JS.Math.abs(0)
}

View File

@ -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('')
}

View File

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