mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
all: better type error positions (#6345)
This commit is contained in:
parent
62a692b29c
commit
ce62f997f3
@ -127,6 +127,7 @@ pub struct StructField {
|
||||
pub:
|
||||
name string
|
||||
pos token.Position
|
||||
type_pos token.Position
|
||||
comments []Comment
|
||||
default_expr Expr
|
||||
has_default_expr bool
|
||||
|
@ -326,21 +326,21 @@ pub fn (mut c Checker) struct_decl(decl ast.StructDecl) {
|
||||
sym := c.table.get_type_symbol(field.typ)
|
||||
if sym.kind == .placeholder && decl.language != .c && !sym.name.starts_with('C.') {
|
||||
c.error(util.new_suggestion(sym.source_name, c.table.known_type_names()).say('unknown type `$sym.source_name`'),
|
||||
field.pos)
|
||||
field.type_pos)
|
||||
}
|
||||
if sym.kind == .array {
|
||||
array_info := sym.array_info()
|
||||
elem_sym := c.table.get_type_symbol(array_info.elem_type)
|
||||
if elem_sym.kind == .placeholder {
|
||||
c.error(util.new_suggestion(elem_sym.source_name, c.table.known_type_names()).say('unknown type `$elem_sym.source_name`'),
|
||||
field.pos)
|
||||
field.type_pos)
|
||||
}
|
||||
}
|
||||
if sym.kind == .struct_ {
|
||||
info := sym.info as table.Struct
|
||||
if info.is_ref_only && !field.typ.is_ptr() {
|
||||
c.error('`$sym.source_name` type can only be used as a reference: `&$sym.source_name`',
|
||||
field.pos)
|
||||
field.type_pos)
|
||||
}
|
||||
}
|
||||
if sym.kind == .map {
|
||||
@ -348,10 +348,10 @@ pub fn (mut c Checker) struct_decl(decl ast.StructDecl) {
|
||||
key_sym := c.table.get_type_symbol(info.key_type)
|
||||
value_sym := c.table.get_type_symbol(info.value_type)
|
||||
if key_sym.kind == .placeholder {
|
||||
c.error('unknown type `$key_sym.source_name`', field.pos)
|
||||
c.error('unknown type `$key_sym.source_name`', field.type_pos)
|
||||
}
|
||||
if value_sym.kind == .placeholder {
|
||||
c.error('unknown type `$value_sym.source_name`', field.pos)
|
||||
c.error('unknown type `$value_sym.source_name`', field.type_pos)
|
||||
}
|
||||
}
|
||||
if field.has_default_expr {
|
||||
|
@ -1,5 +1,5 @@
|
||||
vlib/v/checker/tests/map_unknown_value.vv:2:5: error: unknown type `DoesNotExist`
|
||||
1 | struct App {
|
||||
2 | my_map map[string]DoesNotExist
|
||||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
3 | }
|
||||
vlib/v/checker/tests/map_unknown_value.vv:2:23: error: unknown type `DoesNotExist`
|
||||
1 | struct App {
|
||||
2 | my_map map[string]DoesNotExist
|
||||
| ~~~~~~~~~~~~
|
||||
3 | }
|
@ -1,6 +1,6 @@
|
||||
vlib/v/checker/tests/unknown_array_element_type_b.vv:2:2: error: unknown type `abc`
|
||||
vlib/v/checker/tests/unknown_array_element_type_b.vv:2:6: error: unknown type `abc`
|
||||
1 | struct Aaa {
|
||||
2 | a []abc
|
||||
| ~~~~~~~
|
||||
| ~~~
|
||||
3 | }
|
||||
4 |
|
||||
4 |
|
@ -1,9 +1,9 @@
|
||||
vlib/v/checker/tests/unknown_method_suggest_name.vv:13:2: error: unknown type `hash.crc32.Crc33`.
|
||||
vlib/v/checker/tests/unknown_method_suggest_name.vv:13:12: error: unknown type `hash.crc32.Crc33`.
|
||||
Did you mean `crc32.Crc32`?
|
||||
11 | y int
|
||||
12 | z int
|
||||
13 | ccc crc32.Crc33
|
||||
| ~~~~~~~~~~~~~~~
|
||||
| ~~~~~
|
||||
14 | }
|
||||
15 |
|
||||
vlib/v/checker/tests/unknown_method_suggest_name.vv:27:9: error: unknown method: `Point.tranzlate`.
|
||||
@ -13,4 +13,4 @@ Did you mean `translate`?
|
||||
27 | z := p.tranzlate(v)
|
||||
| ~~~~~~~~~~~~
|
||||
28 | println('p: $p')
|
||||
29 | println('v: $v')
|
||||
29 | println('v: $v')
|
@ -152,12 +152,8 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
|
||||
}
|
||||
// println(p.tok.position())
|
||||
typ := p.parse_type()
|
||||
// field_pos := field_start_pos.extend(p.tok.position())
|
||||
field_pos := token.Position{
|
||||
line_nr: field_start_pos.line_nr
|
||||
pos: field_start_pos.pos
|
||||
len: p.tok.position().pos - field_start_pos.pos
|
||||
}
|
||||
type_pos := p.prev_tok.position()
|
||||
field_pos := field_start_pos.extend(type_pos)
|
||||
// if name == '_net_module_s' {
|
||||
// if name.contains('App') {
|
||||
// s := p.table.get_type_symbol(typ)
|
||||
@ -197,6 +193,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
|
||||
ast_fields << ast.StructField{
|
||||
name: field_name
|
||||
pos: field_pos
|
||||
type_pos: type_pos
|
||||
typ: typ
|
||||
comments: comments
|
||||
default_expr: default_expr
|
||||
|
Loading…
Reference in New Issue
Block a user