checker: handle error token positions with negative pos.len

pull/5669/head
Delyan Angelov 2020-07-04 21:55:14 +03:00
parent 0637feb382
commit 108913c69e
2 changed files with 5 additions and 4 deletions

View File

@ -1198,11 +1198,12 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type {
candidate_fn_name := if typ_sym.name.starts_with('anon_') { 'anonymous function' } else { 'fn `$typ_sym.name`' } candidate_fn_name := if typ_sym.name.starts_with('anon_') { 'anonymous function' } else { 'fn `$typ_sym.name`' }
c.error('cannot use $candidate_fn_name as function type `$arg_typ_sym.str()` in argument ${i+1} to `$fn_name`', c.error('cannot use $candidate_fn_name as function type `$arg_typ_sym.str()` in argument ${i+1} to `$fn_name`',
call_expr.pos) call_expr.pos)
} } else {
c.error('cannot use type `$typ_sym.str()` as type `$arg_typ_sym.str()` in argument ${i+1} to `$fn_name`', c.error('cannot use type `$typ_sym.str()` as type `$arg_typ_sym.str()` in argument ${i+1} to `$fn_name`',
call_expr.pos) call_expr.pos)
} }
} }
}
if call_expr.generic_type != table.void_type && f.return_type != 0 { // table.t_type { if call_expr.generic_type != table.void_type && f.return_type != 0 { // table.t_type {
// Handle `foo<T>() T` => `foo<int>() int` => return int // Handle `foo<T>() T` => `foo<int>() int` => return int
return_sym := c.table.get_type_symbol(f.return_type) return_sym := c.table.get_type_symbol(f.return_type)

View File

@ -110,8 +110,8 @@ pub fn source_context(kind, source string, column int, pos token.Position) []str
tab_spaces := ' ' tab_spaces := ' '
for iline := bline; iline <= aline; iline++ { for iline := bline; iline <= aline; iline++ {
sline := source_lines[iline] sline := source_lines[iline]
start_column := imin(column, sline.len) start_column := imax(0, imin(column, sline.len))
end_column := imin(column + pos.len, sline.len) end_column := imax(0, imin(column + imax(0, pos.len), sline.len))
cline := if iline == pos.line_nr { cline := if iline == pos.line_nr {
sline[..start_column] + color(kind, sline[start_column..end_column]) + sline[end_column..] sline[..start_column] + color(kind, sline[start_column..end_column]) + sline[end_column..]
} else { } else {