string cast: better error message
parent
c3c6648c57
commit
1b4656a4dc
|
@ -2668,26 +2668,31 @@ 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 p.tok == .comma {
|
if is_byteptr || is_bytearr {
|
||||||
p.check(.comma)
|
if p.tok == .comma {
|
||||||
p.cgen.set_placeholder(pos, 'tos(')
|
p.check(.comma)
|
||||||
if is_bytearr {
|
p.cgen.set_placeholder(pos, 'tos(')
|
||||||
p.gen('.data')
|
if is_bytearr {
|
||||||
|
p.gen('.data')
|
||||||
|
}
|
||||||
|
p.gen(', ')
|
||||||
|
p.check_types(p.expression(), 'int')
|
||||||
|
} else {
|
||||||
|
if is_bytearr {
|
||||||
|
p.gen('.data')
|
||||||
|
}
|
||||||
|
p.cgen.set_placeholder(pos, 'tos2(')
|
||||||
}
|
}
|
||||||
p.gen(', ')
|
}
|
||||||
p.check_types(p.expression(), 'int')
|
// `string(234)` => error
|
||||||
} else {
|
else if expr_typ == 'int' {
|
||||||
if is_bytearr {
|
p.error('cannot cast `$expr_typ` to `$typ`, use `str()` method instead')
|
||||||
p.gen('.data')
|
}
|
||||||
}
|
else {
|
||||||
p.cgen.set_placeholder(pos, 'tos2(')
|
p.error('cannot cast `$expr_typ` to `$typ`')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// `string(234)` => error
|
|
||||||
else if typ == 'string' && expr_typ == 'int' {
|
|
||||||
p.error('cannot cast `$expr_typ` to `$typ`, use `str()` method instead')
|
|
||||||
}
|
|
||||||
else {
|
else {
|
||||||
p.cgen.set_placeholder(pos, '($typ)(')
|
p.cgen.set_placeholder(pos, '($typ)(')
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue