all: unify the displaying of compiler errors, using a common util.show_compiler_message/2 function.

This commit is contained in:
Delyan Angelov 2022-08-24 12:25:25 +03:00
parent 0d8b6ee7f2
commit 4718b8b45a
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
12 changed files with 82 additions and 99 deletions

View File

@ -366,8 +366,7 @@ fn (t Tree) errors(errors []errors.Error) &Node {
obj.add_terse('message', t.string_node(e.message)) obj.add_terse('message', t.string_node(e.message))
obj.add_terse('file_path', t.string_node(e.file_path)) obj.add_terse('file_path', t.string_node(e.file_path))
obj.add('pos', t.pos(e.pos)) obj.add('pos', t.pos(e.pos))
obj.add_terse('backtrace', t.string_node(e.backtrace)) obj.add('reporter', t.enum_node(e.reporter))
obj.add_terse('reporter', t.enum_node(e.reporter))
errs.add_item(obj) errs.add_item(obj)
} }
return errs return errs
@ -377,8 +376,8 @@ fn (t Tree) warnings(warnings []errors.Warning) &Node {
mut warns := new_array() mut warns := new_array()
for w in warnings { for w in warnings {
mut obj := new_object() mut obj := new_object()
obj.add('message', t.string_node(w.message)) obj.add_terse('message', t.string_node(w.message))
obj.add('file_path', t.string_node(w.file_path)) obj.add_terse('file_path', t.string_node(w.file_path))
obj.add('pos', t.pos(w.pos)) obj.add('pos', t.pos(w.pos))
obj.add('reporter', t.enum_node(w.reporter)) obj.add('reporter', t.enum_node(w.reporter))
warns.add_item(obj) warns.add_item(obj)
@ -390,8 +389,8 @@ fn (t Tree) notices(notices []errors.Notice) &Node {
mut notice_array := new_array() mut notice_array := new_array()
for n in notices { for n in notices {
mut obj := new_object() mut obj := new_object()
obj.add('message', t.string_node(n.message)) obj.add_terse('message', t.string_node(n.message))
obj.add('file_path', t.string_node(n.file_path)) obj.add_terse('file_path', t.string_node(n.file_path))
obj.add('pos', t.pos(n.pos)) obj.add('pos', t.pos(n.pos))
obj.add('reporter', t.enum_node(n.reporter)) obj.add('reporter', t.enum_node(n.reporter))
notice_array.add_item(obj) notice_array.add_item(obj)

View File

@ -461,11 +461,7 @@ pub fn (mut b Builder) print_warnings_and_errors() {
} else { } else {
'notice:' 'notice:'
} }
ferror := util.formatted_error(kind, err.message, err.file_path, err.pos) util.show_compiler_message(kind, err.CompilerMessage)
eprintln(ferror)
if err.details.len > 0 {
eprintln('Details: $err.details')
}
} }
} }
} }
@ -477,11 +473,7 @@ pub fn (mut b Builder) print_warnings_and_errors() {
} else { } else {
'error:' 'error:'
} }
ferror := util.formatted_error(kind, err.message, err.file_path, err.pos) util.show_compiler_message(kind, err.CompilerMessage)
eprintln(ferror)
if err.details.len > 0 {
eprintln('Details: $err.details')
}
} }
} }
@ -493,11 +485,7 @@ pub fn (mut b Builder) print_warnings_and_errors() {
} else { } else {
'warning:' 'warning:'
} }
ferror := util.formatted_error(kind, err.message, err.file_path, err.pos) util.show_compiler_message(kind, err.CompilerMessage)
eprintln(ferror)
if err.details.len > 0 {
eprintln('Details: $err.details')
}
} }
} }
} }
@ -522,11 +510,7 @@ pub fn (mut b Builder) print_warnings_and_errors() {
} else { } else {
'notice:' 'notice:'
} }
ferror := util.formatted_error(kind, err.message, err.file_path, err.pos) util.show_compiler_message(kind, err.CompilerMessage)
eprintln(ferror)
if err.details.len > 0 {
eprintln('Details: $err.details')
}
} }
} }
if b.checker.nr_warnings > 0 && !b.pref.skip_warnings { if b.checker.nr_warnings > 0 && !b.pref.skip_warnings {
@ -536,11 +520,7 @@ pub fn (mut b Builder) print_warnings_and_errors() {
} else { } else {
'warning:' 'warning:'
} }
ferror := util.formatted_error(kind, err.message, err.file_path, err.pos) util.show_compiler_message(kind, err.CompilerMessage)
eprintln(ferror)
if err.details.len > 0 {
eprintln('Details: $err.details')
}
} }
} }
// //
@ -554,11 +534,7 @@ pub fn (mut b Builder) print_warnings_and_errors() {
} else { } else {
'error:' 'error:'
} }
ferror := util.formatted_error(kind, err.message, err.file_path, err.pos) util.show_compiler_message(kind, err.CompilerMessage)
eprintln(ferror)
if err.details.len > 0 {
eprintln('Details: $err.details')
}
} }
b.show_total_warns_and_errors_stats() b.show_total_warns_and_errors_stats()
exit(1) exit(1)
@ -586,12 +562,15 @@ pub fn (mut b Builder) print_warnings_and_errors() {
} }
} }
if redefines.len > 0 { if redefines.len > 0 {
ferror := util.formatted_error('builder error:', 'redefinition of function `$fn_name`', util.show_compiler_message('builder error:',
'', token.Pos{}) message: 'redefinition of function `$fn_name`'
eprintln(ferror) )
for redefine in redefines { for redefine in redefines {
eprintln(util.formatted_error('conflicting declaration:', redefine.fheader, util.show_compiler_message('conflicting declaration:',
redefine.fpath, redefine.f.pos)) message: redefine.fheader
file_path: redefine.fpath
pos: redefine.f.pos
)
} }
total_conflicts++ total_conflicts++
} }
@ -612,8 +591,7 @@ struct FunctionRedefinition {
pub fn (b &Builder) error_with_pos(s string, fpath string, pos token.Pos) errors.Error { pub fn (b &Builder) error_with_pos(s string, fpath string, pos token.Pos) errors.Error {
if !b.pref.check_only { if !b.pref.check_only {
ferror := util.formatted_error('builder error:', s, fpath, pos) util.show_compiler_message('builder error:', pos: pos, file_path: fpath, message: s)
eprintln(ferror)
exit(1) exit(1)
} }

View File

@ -3742,11 +3742,12 @@ fn (mut c Checker) warn_or_error(message string, pos token.Pos, warn bool) {
} }
if !warn { if !warn {
if c.pref.fatal_errors { if c.pref.fatal_errors {
ferror := util.formatted_error('error:', message, c.file.path, pos) util.show_compiler_message('error:', errors.CompilerMessage{
eprintln(ferror) pos: pos
if details.len > 0 { file_path: c.file.path
eprintln('Details: $details') message: message
} details: details
})
exit(1) exit(1)
} }
c.nr_errors++ c.nr_errors++

View File

@ -10,31 +10,23 @@ pub enum Reporter {
gen gen
} }
[minify] pub struct CompilerMessage {
pub struct Error {
pub: pub:
message string message string
details string details string
file_path string file_path string
pos token.Pos pos token.Pos
backtrace string
reporter Reporter reporter Reporter
} }
pub struct Error {
CompilerMessage
}
pub struct Warning { pub struct Warning {
pub: CompilerMessage
message string
details string
file_path string
pos token.Pos
reporter Reporter
} }
pub struct Notice { pub struct Notice {
pub: CompilerMessage
message string
details string
file_path string
pos token.Pos
reporter Reporter
} }

View File

@ -393,8 +393,11 @@ pub fn (mut e Eval) expr(expr ast.Expr, expecting ast.Type) Object {
} }
} else if e.table.sym(expr.typ).kind in [.interface_, .sum_type] { } else if e.table.sym(expr.typ).kind in [.interface_, .sum_type] {
if e.pref.is_verbose { if e.pref.is_verbose {
eprintln(util.formatted_error('warning:', 'sumtype or interface casts return void currently', util.show_compiler_message('warning:',
e.cur_file, expr.pos)) pos: expr.pos
file_path: e.cur_file
message: 'sumtype or interface casts return void currently'
)
} }
} else { } else {
e.error('unknown cast: ${e.table.sym(expr.expr_type).str()} to ${e.table.sym(expr.typ).str()}') e.error('unknown cast: ${e.table.sym(expr.expr_type).str()} to ${e.table.sym(expr.typ).str()}')

View File

@ -4860,8 +4860,7 @@ fn verror(s string) {
[noreturn] [noreturn]
fn (g &Gen) error(s string, pos token.Pos) { fn (g &Gen) error(s string, pos token.Pos) {
ferror := util.formatted_error('cgen error:', s, g.file.path, pos) util.show_compiler_message('cgen error:', pos: pos, file_path: g.file.path, message: s)
eprintln(ferror)
exit(1) exit(1)
} }

View File

@ -1080,8 +1080,7 @@ pub fn (mut g Gen) n_error(s string) {
pub fn (mut g Gen) warning(s string, pos token.Pos) { pub fn (mut g Gen) warning(s string, pos token.Pos) {
if g.pref.output_mode == .stdout { if g.pref.output_mode == .stdout {
werror := util.formatted_error('warning', s, g.pref.path, pos) util.show_compiler_message('warning:', pos: pos, file_path: g.pref.path, message: s)
eprintln(werror)
} else { } else {
g.warnings << errors.Warning{ g.warnings << errors.Warning{
file_path: g.pref.path file_path: g.pref.path
@ -1098,8 +1097,7 @@ pub fn (mut g Gen) v_error(s string, pos token.Pos) {
// of guessed from the pref.path ... // of guessed from the pref.path ...
mut kind := 'error:' mut kind := 'error:'
if g.pref.output_mode == .stdout { if g.pref.output_mode == .stdout {
ferror := util.formatted_error(kind, s, g.pref.path, pos) util.show_compiler_message(kind, pos: pos, file_path: g.pref.path, message: s)
eprintln(ferror)
exit(1) exit(1)
} else { } else {
g.errors << errors.Error{ g.errors << errors.Error{

View File

@ -1870,8 +1870,7 @@ pub fn (mut p Parser) note(s string) {
pub fn (mut p Parser) error_with_pos(s string, pos token.Pos) ast.NodeError { pub fn (mut p Parser) error_with_pos(s string, pos token.Pos) ast.NodeError {
mut kind := 'error:' mut kind := 'error:'
if p.pref.fatal_errors { if p.pref.fatal_errors {
ferror := util.formatted_error(kind, s, p.file_name, pos) util.show_compiler_message(kind, pos: pos, file_path: p.file_name, message: s)
eprintln(ferror)
exit(1) exit(1)
} }
if p.pref.output_mode == .stdout && !p.pref.check_only { if p.pref.output_mode == .stdout && !p.pref.check_only {
@ -1879,8 +1878,7 @@ pub fn (mut p Parser) error_with_pos(s string, pos token.Pos) ast.NodeError {
print_backtrace() print_backtrace()
kind = 'parser error:' kind = 'parser error:'
} }
ferror := util.formatted_error(kind, s, p.file_name, pos) util.show_compiler_message(kind, pos: pos, file_path: p.file_name, message: s)
eprintln(ferror)
exit(1) exit(1)
} else { } else {
p.errors << errors.Error{ p.errors << errors.Error{
@ -1912,8 +1910,7 @@ pub fn (mut p Parser) error_with_pos(s string, pos token.Pos) ast.NodeError {
pub fn (mut p Parser) error_with_error(error errors.Error) { pub fn (mut p Parser) error_with_error(error errors.Error) {
mut kind := 'error:' mut kind := 'error:'
if p.pref.fatal_errors { if p.pref.fatal_errors {
ferror := util.formatted_error(kind, error.message, error.file_path, error.pos) util.show_compiler_message(kind, error.CompilerMessage)
eprintln(ferror)
exit(1) exit(1)
} }
if p.pref.output_mode == .stdout && !p.pref.check_only { if p.pref.output_mode == .stdout && !p.pref.check_only {
@ -1921,8 +1918,7 @@ pub fn (mut p Parser) error_with_error(error errors.Error) {
print_backtrace() print_backtrace()
kind = 'parser error:' kind = 'parser error:'
} }
ferror := util.formatted_error(kind, error.message, error.file_path, error.pos) util.show_compiler_message(kind, error.CompilerMessage)
eprintln(ferror)
exit(1) exit(1)
} else { } else {
if p.pref.message_limit >= 0 && p.errors.len >= p.pref.message_limit { if p.pref.message_limit >= 0 && p.errors.len >= p.pref.message_limit {
@ -1949,8 +1945,7 @@ pub fn (mut p Parser) warn_with_pos(s string, pos token.Pos) {
return return
} }
if p.pref.output_mode == .stdout && !p.pref.check_only { if p.pref.output_mode == .stdout && !p.pref.check_only {
ferror := util.formatted_error('warning:', s, p.file_name, pos) util.show_compiler_message('warning:', pos: pos, file_path: p.file_name, message: s)
eprintln(ferror)
} else { } else {
if p.pref.message_limit >= 0 && p.warnings.len >= p.pref.message_limit { if p.pref.message_limit >= 0 && p.warnings.len >= p.pref.message_limit {
p.should_abort = true p.should_abort = true
@ -1973,8 +1968,7 @@ pub fn (mut p Parser) note_with_pos(s string, pos token.Pos) {
return return
} }
if p.pref.output_mode == .stdout && !p.pref.check_only { if p.pref.output_mode == .stdout && !p.pref.check_only {
ferror := util.formatted_error('notice:', s, p.file_name, pos) util.show_compiler_message('notice:', pos: pos, file_path: p.file_name, message: s)
eprintln(ferror)
} else { } else {
p.notices << errors.Notice{ p.notices << errors.Notice{
file_path: p.file_name file_path: p.file_name

View File

@ -1480,7 +1480,7 @@ pub fn (mut s Scanner) note(msg string) {
pos: s.pos pos: s.pos
} }
if s.pref.output_mode == .stdout && !s.pref.check_only { if s.pref.output_mode == .stdout && !s.pref.check_only {
eprintln(util.formatted_error('notice:', msg, s.file_path, pos)) util.show_compiler_message('notice:', pos: pos, file_path: s.file_path, message: msg)
} else { } else {
s.notices << errors.Notice{ s.notices << errors.Notice{
file_path: s.file_path file_path: s.file_path
@ -1497,14 +1497,13 @@ pub fn (mut s Scanner) add_error_detail(msg string) {
} }
pub fn (mut s Scanner) add_error_detail_with_pos(msg string, pos token.Pos) { pub fn (mut s Scanner) add_error_detail_with_pos(msg string, pos token.Pos) {
details := util.formatted_error('details:', msg, s.file_path, pos) s.add_error_detail(util.formatted_error('details:', msg, s.file_path, pos))
s.add_error_detail(details)
} }
fn (mut s Scanner) eat_details() string { fn (mut s Scanner) eat_details() string {
mut details := '' mut details := ''
if s.error_details.len > 0 { if s.error_details.len > 0 {
details = s.error_details.join('\n') details = '\n' + s.error_details.join('\n')
s.error_details = [] s.error_details = []
} }
return details return details
@ -1522,10 +1521,12 @@ pub fn (mut s Scanner) warn(msg string) {
} }
details := s.eat_details() details := s.eat_details()
if s.pref.output_mode == .stdout && !s.pref.check_only { if s.pref.output_mode == .stdout && !s.pref.check_only {
eprintln(util.formatted_error('warning:', msg, s.file_path, pos)) util.show_compiler_message('warning:',
if details.len > 0 { pos: pos
eprintln(details) file_path: s.file_path
} message: msg
details: details
)
} else { } else {
if s.pref.message_limit >= 0 && s.warnings.len >= s.pref.message_limit { if s.pref.message_limit >= 0 && s.warnings.len >= s.pref.message_limit {
s.should_abort = true s.should_abort = true
@ -1549,17 +1550,21 @@ pub fn (mut s Scanner) error(msg string) {
} }
details := s.eat_details() details := s.eat_details()
if s.pref.output_mode == .stdout && !s.pref.check_only { if s.pref.output_mode == .stdout && !s.pref.check_only {
eprintln(util.formatted_error('error:', msg, s.file_path, pos)) util.show_compiler_message('error:',
if details.len > 0 { pos: pos
eprintln(details) file_path: s.file_path
} message: msg
details: details
)
exit(1) exit(1)
} else { } else {
if s.pref.fatal_errors { if s.pref.fatal_errors {
eprintln(util.formatted_error('error:', msg, s.file_path, pos)) util.show_compiler_message('error:',
if details.len > 0 { pos: pos
eprintln(details) file_path: s.file_path
} message: msg
details: details
)
exit(1) exit(1)
} }
if s.pref.message_limit >= 0 && s.errors.len >= s.pref.message_limit { if s.pref.message_limit >= 0 && s.errors.len >= s.pref.message_limit {

View File

@ -4,6 +4,7 @@ vlib/v/scanner/tests/empty_character_literal_err.vv:2:8: error: invalid empty ch
| ^ | ^
3 | println(a) 3 | println(a)
4 | } 4 | }
Details:
vlib/v/scanner/tests/empty_character_literal_err.vv:2:7: details: use quotes for strings, backticks for characters vlib/v/scanner/tests/empty_character_literal_err.vv:2:7: details: use quotes for strings, backticks for characters
1 | fn main() { 1 | fn main() {
2 | a := `` 2 | a := ``

View File

@ -1,6 +1,7 @@
vlib/v/scanner/tests/unfinished_string_literal_err.vv:7:1: error: unfinished string literal vlib/v/scanner/tests/unfinished_string_literal_err.vv:7:1: error: unfinished string literal
5 | 5 |
6 | zzzz 6 | zzzz
Details:
vlib/v/scanner/tests/unfinished_string_literal_err.vv:4:6: details: literal started here vlib/v/scanner/tests/unfinished_string_literal_err.vv:4:6: details: literal started here
2 | a := 'abc' 2 | a := 'abc'
3 | println(a) 3 | println(a)

View File

@ -7,6 +7,7 @@ module util
import os import os
import strings import strings
import term import term
import v.errors
import v.token import v.token
import v.mathutil as mu import v.mathutil as mu
@ -66,6 +67,9 @@ fn color(kind string, msg string) string {
if kind.contains('notice') { if kind.contains('notice') {
return term.yellow(msg) return term.yellow(msg)
} }
if kind.contains('details') {
return term.bright_blue(msg)
}
return term.magenta(msg) return term.magenta(msg)
} }
@ -189,3 +193,11 @@ pub fn vlines_escape_path(path string, ccompiler string) string {
} }
return cescaped_path(os.real_path(path)) return cescaped_path(os.real_path(path))
} }
pub fn show_compiler_message(kind string, err errors.CompilerMessage) {
ferror := formatted_error(kind, err.message, err.file_path, err.pos)
eprintln(ferror)
if err.details.len > 0 {
eprintln(bold('Details: ') + color('details', err.details))
}
}