From 175ede54b6f576e468011b7c198f1ce57324487e Mon Sep 17 00:00:00 2001 From: Vinicius Silva Date: Sun, 14 Jan 2024 06:37:49 -0300 Subject: [PATCH] repl: fix an issue with `print` and println after the execution of `for` or `if` (fix #20524) (#20525) --- cmd/tools/vrepl.v | 16 +++------------- vlib/v/slow_tests/repl/error.repl | 5 ----- .../repl/error_and_continue_print.repl | 10 ---------- vlib/v/slow_tests/repl/error_exitasdfasdf.repl | 5 ----- vlib/v/slow_tests/repl/function.repl.skip | 4 ---- .../if_and_for_with_print_inside_them.repl | 18 ++++++++++++++++++ 6 files changed, 21 insertions(+), 37 deletions(-) delete mode 100644 vlib/v/slow_tests/repl/function.repl.skip create mode 100644 vlib/v/slow_tests/repl/if_and_for_with_print_inside_them.repl diff --git a/cmd/tools/vrepl.v b/cmd/tools/vrepl.v index 41e794fed..a1f6145d9 100644 --- a/cmd/tools/vrepl.v +++ b/cmd/tools/vrepl.v @@ -477,23 +477,13 @@ fn run_repl(workdir string, vrepl_prefix string) int { } else if temp_line.starts_with('#include ') { temp_source_code = '${temp_line}\n' + r.current_source_code(false, false) } else { - for i, l in r.lines { - if (l.starts_with('for ') || l.starts_with('if ')) && l.contains('println') { - r.lines.delete(i) - break - } - } temp_source_code = r.current_source_code(true, false) + '\n${temp_line}\n' } os.write_file(temp_file, temp_source_code) or { panic(err) } s := repl_run_vfile(temp_file) or { return 1 } if s.exit_code == 0 { - for r.temp_lines.len > 0 { - if !r.temp_lines[0].starts_with('print') { - r.lines << r.temp_lines[0] - } - r.temp_lines.delete(0) - } + r.lines << r.temp_lines + r.temp_lines.clear() if r.line.starts_with('import ') { r.parse_import(r.line) } else if r.line.starts_with('#include ') { @@ -600,7 +590,7 @@ fn repl_run_vfile(file string) !os.Result { $if trace_repl_temp_files ? { eprintln('>> repl_run_vfile file: ${file}') } - s := os.execute('${os.quoted_path(vexe)} -repl run ${os.quoted_path(file)}') + s := os.execute('${os.quoted_path(vexe)} -message-limit 1 -repl run ${os.quoted_path(file)}') if s.exit_code < 0 { rerror(s.output) return error(s.output) diff --git a/vlib/v/slow_tests/repl/error.repl b/vlib/v/slow_tests/repl/error.repl index 68136316c..e85d252f9 100644 --- a/vlib/v/slow_tests/repl/error.repl +++ b/vlib/v/slow_tests/repl/error.repl @@ -5,8 +5,3 @@ error: undefined ident: `a` 6 | 7 | println(a) | ^ -error: `println` can not print void expressions - 5 | import math - 6 | - 7 | println(a) - | ~~~~~~~~~~ diff --git a/vlib/v/slow_tests/repl/error_and_continue_print.repl b/vlib/v/slow_tests/repl/error_and_continue_print.repl index 253f7f18f..3cfc0bfbf 100644 --- a/vlib/v/slow_tests/repl/error_and_continue_print.repl +++ b/vlib/v/slow_tests/repl/error_and_continue_print.repl @@ -7,19 +7,9 @@ error: undefined ident: `a` (use `:=` to declare a variable) 6 | 7 | a = 3 | ^ -error: cannot assign to `a`: expected `void`, not `int literal` - 5 | import math - 6 | - 7 | a = 3 - | ^ error: undefined ident: `b` 5 | import math 6 | 7 | println(b) | ^ -error: `println` can not print void expressions - 5 | import math - 6 | - 7 | println(b) - | ~~~~~~~~~~ [4] diff --git a/vlib/v/slow_tests/repl/error_exitasdfasdf.repl b/vlib/v/slow_tests/repl/error_exitasdfasdf.repl index 1ab0f356c..a026a8ee1 100644 --- a/vlib/v/slow_tests/repl/error_exitasdfasdf.repl +++ b/vlib/v/slow_tests/repl/error_exitasdfasdf.repl @@ -5,8 +5,3 @@ error: undefined ident: `exitasdfasdf` 6 | 7 | println(exitasdfasdf) | ~~~~~~~~~~~~ -error: `println` can not print void expressions - 5 | import math - 6 | - 7 | println(exitasdfasdf) - | ~~~~~~~~~~~~~~~~~~~~~ diff --git a/vlib/v/slow_tests/repl/function.repl.skip b/vlib/v/slow_tests/repl/function.repl.skip deleted file mode 100644 index 25c0b9d88..000000000 --- a/vlib/v/slow_tests/repl/function.repl.skip +++ /dev/null @@ -1,4 +0,0 @@ -fn test() { println('foo') } test() fn test2(a int) { println(a) } test2(42) -===output=== -foo -42 diff --git a/vlib/v/slow_tests/repl/if_and_for_with_print_inside_them.repl b/vlib/v/slow_tests/repl/if_and_for_with_print_inside_them.repl new file mode 100644 index 000000000..454571c64 --- /dev/null +++ b/vlib/v/slow_tests/repl/if_and_for_with_print_inside_them.repl @@ -0,0 +1,18 @@ +mut numbers := [1,2,3,4,5] +if 1 in numbers { +println('yes') +} +println('hi') +for number in numbers { +println(number) +} +println(numbers) +===output=== +yes +hi +1 +2 +3 +4 +5 +[1, 2, 3, 4, 5] -- 2.39.5