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

parser: add check for string casting

This commit is contained in:
Swastik Baranwal 2020-01-11 00:29:15 +05:30 committed by Alexander Medvednikov
parent 66a6fa163e
commit 5537b09492

View File

@ -277,8 +277,8 @@ fn (table mut Table) fn_gen_name(f &Fn) string {
`%` { `%` {
name = name.replace('%', 'op_mod') name = name.replace('%', 'op_mod')
} }
else { else {}
}} }
} }
} }
if f.is_interface { if f.is_interface {
@ -600,6 +600,9 @@ fn (p mut Parser) cast(typ string) {
} }
// Strings can't be cast // Strings can't be cast
if expr_typ == 'string' { if expr_typ == 'string' {
if is_number_type(typ) || is_float_type(typ) {
p.error('cannot cast `string` to `$typ`, use `${expr_typ}.${typ}()` instead')
}
p.error('cannot cast `$expr_typ` to `$typ`') p.error('cannot cast `$expr_typ` to `$typ`')
} }
// Nothing can be cast to bool // Nothing can be cast to bool
@ -671,8 +674,8 @@ fn type_default(typ string) string {
'voidptr' { 'voidptr' {
return '0' return '0'
} }
else { else {}
}} }
return '{0}' return '{0}'
// TODO this results in // TODO this results in
// error: expected a field designator, such as '.field = 4' // error: expected a field designator, such as '.field = 4'