From d7b1e571863fe2629407e19675792921c9921488 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 17 May 2022 17:12:20 +0800 Subject: [PATCH] v.builder: improve builder error messages (fix #14386) (#14421) --- vlib/v/builder/builder.v | 4 +++- vlib/v/checker/tests/fn_duplicate.out | 17 ++++++++--------- vlib/v/checker/tests/fn_duplicate.vv | 2 -- vlib/v/util/errors.v | 10 +++++++--- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/vlib/v/builder/builder.v b/vlib/v/builder/builder.v index a05fa77a0b..c635425043 100644 --- a/vlib/v/builder/builder.v +++ b/vlib/v/builder/builder.v @@ -556,7 +556,9 @@ pub fn (mut b Builder) print_warnings_and_errors() { } } if redefines.len > 0 { - eprintln('redefinition of function `$fn_name`') + ferror := util.formatted_error('builder error:', 'redefinition of function `$fn_name`', + '', token.Pos{}) + eprintln(ferror) for redefine in redefines { eprintln(util.formatted_error('conflicting declaration:', redefine.fheader, redefine.fpath, redefine.f.pos)) diff --git a/vlib/v/checker/tests/fn_duplicate.out b/vlib/v/checker/tests/fn_duplicate.out index 3cf949c733..8f4451bac6 100644 --- a/vlib/v/checker/tests/fn_duplicate.out +++ b/vlib/v/checker/tests/fn_duplicate.out @@ -1,13 +1,12 @@ -redefinition of function `main.f` +builder error: redefinition of function `f` vlib/v/checker/tests/fn_duplicate.vv:1:1: conflicting declaration: fn f() 1 | fn f() { | ~~~~~~ - 2 | - 3 | } -vlib/v/checker/tests/fn_duplicate.vv:5:1: conflicting declaration: fn f(i int) - 3 | } - 4 | - 5 | fn f(i int) { + 2 | } + 3 | +vlib/v/checker/tests/fn_duplicate.vv:4:1: conflicting declaration: fn f(i int) + 2 | } + 3 | + 4 | fn f(i int) { | ~~~~~~~~~~~ - 6 | - 7 | } + 5 | } diff --git a/vlib/v/checker/tests/fn_duplicate.vv b/vlib/v/checker/tests/fn_duplicate.vv index 08c1dfbe2a..f3929df535 100644 --- a/vlib/v/checker/tests/fn_duplicate.vv +++ b/vlib/v/checker/tests/fn_duplicate.vv @@ -1,7 +1,5 @@ fn f() { - } fn f(i int) { - } diff --git a/vlib/v/util/errors.v b/vlib/v/util/errors.v index 32b9401f99..d18c8f4f00 100644 --- a/vlib/v/util/errors.v +++ b/vlib/v/util/errors.v @@ -86,14 +86,18 @@ pub fn formatted_error(kind string, omsg string, filepath string, pos token.Pos) path = path.replace_once(util.normalised_workdir, '') } } - // - position := '$path:${pos.line_nr + 1}:${mu.max(1, pos.col + 1)}:' + + position := if filepath.len > 0 { + '$path:${pos.line_nr + 1}:${mu.max(1, pos.col + 1)}:' + } else { + '' + } scontext := source_file_context(kind, filepath, pos).join('\n') final_position := bold(position) final_kind := bold(color(kind, kind)) final_msg := emsg final_context := if scontext.len > 0 { '\n$scontext' } else { '' } - // + return '$final_position $final_kind $final_msg$final_context'.trim_space() }