From f35f7fe9972141e099e82058f884698801a0ca7c Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 24 May 2022 16:21:49 +0800 Subject: [PATCH] fmt: fix using rand.seed() when import rand and rand.seed (#14511) --- vlib/v/fmt/fmt.v | 8 ++++++-- vlib/v/fmt/tests/import_rand_and_rand_seed_keep.vv | 6 ++++++ 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 vlib/v/fmt/tests/import_rand_and_rand_seed_keep.vv diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 470c077ea3..753f7331a4 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -41,6 +41,7 @@ pub mut: used_imports []string // to remove unused imports import_syms_used map[string]bool // to remove unused import symbols. mod2alias map[string]string // for `import time as t`, will contain: 'time'=>'t' + mod2syms map[string]string // import time { now } 'time.now'=>'now' use_short_fn_args bool single_line_fields bool // should struct fields be on a single line it_name string // the name to replace `it` with @@ -82,6 +83,9 @@ pub fn (mut f Fmt) process_file_imports(file &ast.File) { f.mod2alias['${imp.mod}.$sym.name'] = sym.name f.mod2alias['${imp.mod.all_after_last('.')}.$sym.name'] = sym.name f.mod2alias[sym.name] = sym.name + f.mod2syms['${imp.mod}.$sym.name'] = sym.name + f.mod2syms['${imp.mod.all_after_last('.')}.$sym.name'] = sym.name + f.mod2syms[sym.name] = sym.name f.import_syms_used[sym.name] = false } } @@ -208,8 +212,8 @@ pub fn (mut f Fmt) short_module(name string) string { if !name.contains('.') || name.starts_with('JS.') { return name } - if name in f.mod2alias { - return f.mod2alias[name] + if name in f.mod2syms { + return f.mod2syms[name] } if name.ends_with('>') { generic_levels := name.trim_string_right('>').split('<') diff --git a/vlib/v/fmt/tests/import_rand_and_rand_seed_keep.vv b/vlib/v/fmt/tests/import_rand_and_rand_seed_keep.vv new file mode 100644 index 0000000000..ad2ffba5d0 --- /dev/null +++ b/vlib/v/fmt/tests/import_rand_and_rand_seed_keep.vv @@ -0,0 +1,6 @@ +import rand +import rand.seed + +fn main() { + rand.seed(seed.time_seed_array(2)) +}