From 21805ea2a5ed0f19bf3395c74011666e8108474b Mon Sep 17 00:00:00 2001 From: zakuro Date: Sun, 27 Dec 2020 19:22:16 +0900 Subject: [PATCH] all: make eprint[ln] behave same as print[ln] (#7595) --- vlib/builtin/js/builtin.v | 4 ++-- vlib/v/checker/checker.v | 4 ++-- vlib/v/gen/fn.v | 4 ++-- vlib/v/tests/print_test.v | 1 + vlib/v/tests/vargs_auto_str_method_and_println_test.v | 6 ++++++ 5 files changed, 13 insertions(+), 6 deletions(-) diff --git a/vlib/builtin/js/builtin.v b/vlib/builtin/js/builtin.v index 081ad60638..9491e2bf3a 100644 --- a/vlib/builtin/js/builtin.v +++ b/vlib/builtin/js/builtin.v @@ -22,13 +22,13 @@ pub fn print(s any) { } pub fn eprintln(s any) { - JS.console.error(s) + JS.console.error(s.toString()) } pub fn eprint(s any) { // TODO // $if js.node { - JS.process.stderr.write(s) + JS.process.stderr.write(s.toString()) // } $else { // panic('Cannot `eprint` in a browser, use `eprintln` instead') // } diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index b47b8b5fe9..8929ead1bf 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1589,8 +1589,8 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type { unexpected_arguments_pos) return f.return_type } - // println can print anything - if fn_name in ['println', 'print'] && call_expr.args.len > 0 { + // println / eprintln can print anything + if fn_name in ['println', 'print', 'eprintln', 'eprint'] && call_expr.args.len > 0 { c.expected_type = table.string_type call_expr.args[0].typ = c.expr(call_expr.args[0].expr) /* diff --git a/vlib/v/gen/fn.v b/vlib/v/gen/fn.v index 017585a657..c05d80715a 100644 --- a/vlib/v/gen/fn.v +++ b/vlib/v/gen/fn.v @@ -501,8 +501,8 @@ fn (mut g Gen) fn_call(node ast.CallExpr) { } } mut name := node.name - is_print := name == 'println' || name == 'print' - print_method := if name == 'println' { 'println' } else { 'print' } + is_print := name in ['print', 'println', 'eprint', 'eprintln'] + print_method := name is_json_encode := name == 'json.encode' is_json_decode := name == 'json.decode' g.is_json_fn = is_json_encode || is_json_decode diff --git a/vlib/v/tests/print_test.v b/vlib/v/tests/print_test.v index 18c9360d1c..914269c256 100644 --- a/vlib/v/tests/print_test.v +++ b/vlib/v/tests/print_test.v @@ -1,3 +1,4 @@ fn test_print() { println(2.0) + eprintln(2.0) } diff --git a/vlib/v/tests/vargs_auto_str_method_and_println_test.v b/vlib/v/tests/vargs_auto_str_method_and_println_test.v index 5d6a249ae7..77a9578476 100644 --- a/vlib/v/tests/vargs_auto_str_method_and_println_test.v +++ b/vlib/v/tests/vargs_auto_str_method_and_println_test.v @@ -10,6 +10,8 @@ fn test_autoprint_string_vargs() { fn add_s(column string, other_columns ...string) { println(column) println(other_columns) + eprintln(column) + eprintln(other_columns) } // @@ -23,6 +25,8 @@ fn test_autoprint_int_vargs() { fn add_i(column int, other_columns ...int) { println(column) println(other_columns) + eprintln(column) + eprintln(other_columns) } // @@ -41,4 +45,6 @@ fn test_autoprint_struct_vargs() { fn add_point(column Point, other_columns ...Point) { println(column) println(other_columns) + eprintln(column) + eprintln(other_columns) }