1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

string cast: better error message

This commit is contained in:
Alexander Medvednikov 2019-07-21 00:04:34 +02:00
parent c3c6648c57
commit 1b4656a4dc

View File

@ -2668,7 +2668,8 @@ fn (p mut Parser) cast(typ string) string {
// `string(bytes_array, len)` => `tos(bytes_array.data, len)` // `string(bytes_array, len)` => `tos(bytes_array.data, len)`
is_byteptr := expr_typ == 'byte*' || expr_typ == 'byteptr' is_byteptr := expr_typ == 'byte*' || expr_typ == 'byteptr'
is_bytearr := expr_typ == 'array_byte' is_bytearr := expr_typ == 'array_byte'
if typ == 'string' && (is_byteptr || is_bytearr) { if typ == 'string' {
if is_byteptr || is_bytearr {
if p.tok == .comma { if p.tok == .comma {
p.check(.comma) p.check(.comma)
p.cgen.set_placeholder(pos, 'tos(') p.cgen.set_placeholder(pos, 'tos(')
@ -2685,9 +2686,13 @@ fn (p mut Parser) cast(typ string) string {
} }
} }
// `string(234)` => error // `string(234)` => error
else if typ == 'string' && expr_typ == 'int' { else if expr_typ == 'int' {
p.error('cannot cast `$expr_typ` to `$typ`, use `str()` method instead') p.error('cannot cast `$expr_typ` to `$typ`, use `str()` method instead')
} }
else {
p.error('cannot cast `$expr_typ` to `$typ`')
}
}
else { else {
p.cgen.set_placeholder(pos, '($typ)(') p.cgen.set_placeholder(pos, '($typ)(')
} }