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

checker: fix check omission in cast string to char. (fix #15760) (#15764)

This commit is contained in:
shove 2022-09-15 14:33:38 +08:00 committed by GitHub
parent 8b962f8446
commit 566a61b136
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 0 deletions

View File

@ -2506,6 +2506,10 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
snexpr := node.expr.str()
tt := c.table.type_to_str(to_type)
c.error('cannot cast string to `$tt`, use `${snexpr}.str` instead.', node.pos)
} else if final_from_sym.kind == .string && to_sym.kind == .char {
snexpr := node.expr.str()
tt := c.table.type_to_str(to_type)
c.error('cannot cast string to `$tt`, use `$snexpr[index]` instead.', node.pos)
} else if final_from_sym.kind == .array && !from_type.is_ptr() && to_type != ast.string_type {
ft := c.table.type_to_str(from_type)
tt := c.table.type_to_str(to_type)

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/cast_string_to_char_err.vv:2:7: error: cannot cast string to `char`, use `'a'[index]` instead.
1 | fn main() {
2 | _ := char('a')
| ~~~~~~~~~
3 | }

View File

@ -0,0 +1,3 @@
fn main() {
_ := char('a')
}