From 566a61b1364d23d84813b7c826bec4606350bc2e Mon Sep 17 00:00:00 2001 From: shove Date: Thu, 15 Sep 2022 14:33:38 +0800 Subject: [PATCH] checker: fix check omission in cast string to char. (fix #15760) (#15764) --- vlib/v/checker/checker.v | 4 ++++ vlib/v/checker/tests/cast_string_to_char_err.out | 5 +++++ vlib/v/checker/tests/cast_string_to_char_err.vv | 3 +++ 3 files changed, 12 insertions(+) create mode 100644 vlib/v/checker/tests/cast_string_to_char_err.out create mode 100644 vlib/v/checker/tests/cast_string_to_char_err.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 431f0a66b..029b9bbd3 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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) diff --git a/vlib/v/checker/tests/cast_string_to_char_err.out b/vlib/v/checker/tests/cast_string_to_char_err.out new file mode 100644 index 000000000..40c830e07 --- /dev/null +++ b/vlib/v/checker/tests/cast_string_to_char_err.out @@ -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 | } diff --git a/vlib/v/checker/tests/cast_string_to_char_err.vv b/vlib/v/checker/tests/cast_string_to_char_err.vv new file mode 100644 index 000000000..32ea5a335 --- /dev/null +++ b/vlib/v/checker/tests/cast_string_to_char_err.vv @@ -0,0 +1,3 @@ +fn main() { + _ := char('a') +} -- 2.39.5