From 7bc3e67e2453e23b6896000d0af95ae645ff9996 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 5 Oct 2021 16:44:48 +0800 Subject: [PATCH] fmt: fix import with symbols (fix #12065) (#12069) --- cmd/tools/vtest-cleancode.v | 1 - vlib/readline/readline_test.v | 2 +- vlib/v/fmt/fmt.v | 1 + vlib/v/fmt/tests/import_with_symbols_keep.vv | 14 ++++++++++++++ 4 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 vlib/v/fmt/tests/import_with_symbols_keep.vv diff --git a/cmd/tools/vtest-cleancode.v b/cmd/tools/vtest-cleancode.v index 2cf4c32163..0f0587c225 100644 --- a/cmd/tools/vtest-cleancode.v +++ b/cmd/tools/vtest-cleancode.v @@ -42,7 +42,6 @@ const ( ] vfmt_known_failing_exceptions = arrays.merge(verify_known_failing_exceptions, [ 'vlib/regex/regex_test.v' /* contains meaningfull formatting of the test case data */, - 'vlib/readline/readline_test.v' /* vfmt eats `{ Readline }` from `import readline { Readline }` */, 'vlib/glm/glm.v' /* `mut res &f32` => `mut res f32`, which then fails to compile */, 'vlib/crypto/sha512/sha512block_generic.v' /* formatting of large constant arrays wraps to too many lines */, 'vlib/crypto/aes/const.v' /* formatting of large constant arrays wraps to too many lines */, diff --git a/vlib/readline/readline_test.v b/vlib/readline/readline_test.v index fc43a83a47..a3613e85af 100644 --- a/vlib/readline/readline_test.v +++ b/vlib/readline/readline_test.v @@ -17,4 +17,4 @@ fn test_struct_readline() { // eprintln('methods: $methods') assert 'read_line_utf8' in methods assert 'read_line' in methods -} \ No newline at end of file +} diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 44246970c0..7b049e2d86 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -760,6 +760,7 @@ pub fn (mut f Fmt) branch_stmt(node ast.BranchStmt) { pub fn (mut f Fmt) comp_for(node ast.CompFor) { typ := f.no_cur_mod(f.table.type_to_str_using_aliases(node.typ, f.mod2alias)) f.write('\$for $node.val_var in ${typ}.$node.kind.str() {') + f.mark_types_import_as_used(node.typ) if node.stmts.len > 0 || node.pos.line_nr < node.pos.last_line { f.writeln('') f.stmts(node.stmts) diff --git a/vlib/v/fmt/tests/import_with_symbols_keep.vv b/vlib/v/fmt/tests/import_with_symbols_keep.vv new file mode 100644 index 0000000000..b5aae389e3 --- /dev/null +++ b/vlib/v/fmt/tests/import_with_symbols_keep.vv @@ -0,0 +1,14 @@ +import readline { Readline } + +fn no_lines(s string) string { + return s.replace('\n', ' ') +} + +fn main() { + mut methods := []string{} + $for method in Readline.methods { + methods << method.name + } + assert 'read_line_utf8' in methods + assert 'read_line' in methods +}