From 0059ca856e83f869a4dc16c1f4d26175d502a0a7 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Tue, 23 Apr 2024 11:36:14 -0300 Subject: [PATCH] checker: fix missing check for or expr on string interpolation (#17566) --- vlib/v/checker/str.v | 1 + vlib/v/checker/tests/wrong_or_expr_err.out | 6 ++++++ vlib/v/checker/tests/wrong_or_expr_err.vv | 8 ++++++++ vlib/v/tests/results_test.v | 2 +- vlib/v/tests/str_gen_test.v | 2 +- 5 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 vlib/v/checker/tests/wrong_or_expr_err.out create mode 100644 vlib/v/checker/tests/wrong_or_expr_err.vv diff --git a/vlib/v/checker/str.v b/vlib/v/checker/str.v index f8f07b6ad9c1bb..ac9ccdb937ec92 100644 --- a/vlib/v/checker/str.v +++ b/vlib/v/checker/str.v @@ -45,6 +45,7 @@ fn (mut c Checker) string_inter_lit(mut node ast.StringInterLiteral) ast.Type { c.inside_interface_deref = true for i, mut expr in node.exprs { mut ftyp := c.expr(mut expr) + ftyp = c.check_expr_option_or_result_call(expr, ftyp) if c.comptime.is_comptime_var(expr) { ctyp := c.comptime.get_comptime_var_type(expr) if ctyp != ast.void_type { diff --git a/vlib/v/checker/tests/wrong_or_expr_err.out b/vlib/v/checker/tests/wrong_or_expr_err.out new file mode 100644 index 00000000000000..dfa5088e2a57cb --- /dev/null +++ b/vlib/v/checker/tests/wrong_or_expr_err.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/wrong_or_expr_err.vv:7:31: error: wrong return type `string` in the `or {}` block, expected `i16` + 5 | return 1 + 6 | } + 7 | println('${get_number() or { '1' }}') + | ~~~ + 8 | } diff --git a/vlib/v/checker/tests/wrong_or_expr_err.vv b/vlib/v/checker/tests/wrong_or_expr_err.vv new file mode 100644 index 00000000000000..aad997b959fc30 --- /dev/null +++ b/vlib/v/checker/tests/wrong_or_expr_err.vv @@ -0,0 +1,8 @@ +module main + +fn main() { + get_number := fn () ?i16 { + return 1 + } + println('${get_number() or { '1' }}') +} diff --git a/vlib/v/tests/results_test.v b/vlib/v/tests/results_test.v index e265060b316a44..103851119e0707 100644 --- a/vlib/v/tests/results_test.v +++ b/vlib/v/tests/results_test.v @@ -21,7 +21,7 @@ fn test_return_err_var() { } fn test_str() { - assert '${foo()}' == 'Result(1)' + assert '${foo() or { 3 }}' == '1' } fn result_void(err bool) ! { diff --git a/vlib/v/tests/str_gen_test.v b/vlib/v/tests/str_gen_test.v index 6b0150e4248e20..26f2ba0aaa4095 100644 --- a/vlib/v/tests/str_gen_test.v +++ b/vlib/v/tests/str_gen_test.v @@ -333,7 +333,7 @@ fn create_option_err() !string { } fn test_result_err() { - assert '${create_option_err()}' == 'Result(error: this is an error)' + assert '${create_option_err() or { 'Result(error: this is an error)' }}' == 'Result(error: this is an error)' } fn create_option_none() ?string {