checker: fix c error on improper string to rune cast (#13197) (#13257)

pull/13263/head
skurgs 2022-01-24 05:13:22 -05:00 committed by GitHub
parent 216a505c2a
commit 7fd08eca96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 0 deletions

View File

@ -2732,6 +2732,11 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
node.pos)
}
if to_sym.kind == .rune && from_sym.is_string() {
snexpr := node.expr.str()
c.error('cannot cast `$from_sym.name` to rune, use `${snexpr}.runes()` instead.', node.pos)
}
if to_type == ast.string_type {
if from_type in [ast.byte_type, ast.bool_type] {
snexpr := node.expr.str()

View File

@ -0,0 +1,26 @@
vlib/v/checker/tests/cast_string_to_rune_err.vv:2:7: error: cannot cast `string` to rune, use `'0'.runes()` instead.
1 | fn main() {
2 | _ := rune('0')
| ~~~~~~~~~
3 | _ := rune('012345')
4 | _ := rune('v')
vlib/v/checker/tests/cast_string_to_rune_err.vv:3:7: error: cannot cast `string` to rune, use `'012345'.runes()` instead.
1 | fn main() {
2 | _ := rune('0')
3 | _ := rune('012345')
| ~~~~~~~~~~~~~~
4 | _ := rune('v')
5 | _ := rune('abcd')
vlib/v/checker/tests/cast_string_to_rune_err.vv:4:7: error: cannot cast `string` to rune, use `'v'.runes()` instead.
2 | _ := rune('0')
3 | _ := rune('012345')
4 | _ := rune('v')
| ~~~~~~~~~
5 | _ := rune('abcd')
6 | }
vlib/v/checker/tests/cast_string_to_rune_err.vv:5:7: error: cannot cast `string` to rune, use `'abcd'.runes()` instead.
3 | _ := rune('012345')
4 | _ := rune('v')
5 | _ := rune('abcd')
| ~~~~~~~~~~~~
6 | }

View File

@ -0,0 +1,6 @@
fn main() {
_ := rune('0')
_ := rune('012345')
_ := rune('v')
_ := rune('abcd')
}