fmt, ast: fix usage of import aliases (#10151)
parent
9be596ef12
commit
788520eb52
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -59,4 +59,3 @@ fn main() {
|
|||
println(file_ext('main.v'))
|
||||
println(imaginary(1))
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
import math.complex { Complex }
|
||||
|
||||
fn main() {
|
||||
assert *(&f64(&byte(&num) + __offsetof(Complex, re))) == 1.0
|
||||
}
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue