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 ptr. (fix #14921) (#15721)

This commit is contained in:
shove
2022-09-11 18:54:56 +08:00
committed by GitHub
parent 3e599a1436
commit be0dc0e537
3 changed files with 12 additions and 0 deletions

View File

@@ -2492,6 +2492,10 @@ pub fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
tt := c.table.type_to_str(to_type)
c.error('cannot cast string to `$tt`, use `${snexpr}.${final_to_sym.name}()` instead.',
node.pos)
} else if final_from_sym.kind == .string && to_type.is_ptr() && to_sym.kind != .string {
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)
}
if to_sym.kind == .rune && from_sym.is_string() {

View File

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

View File

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