Skip to content

Commit

Permalink
checker: fix missing check for or expr on string interpolation (#17566)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Apr 23, 2024
1 parent 354b210 commit 0059ca8
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions vlib/v/checker/str.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions vlib/v/checker/tests/wrong_or_expr_err.out
Original file line number Diff line number Diff line change
@@ -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 | }
8 changes: 8 additions & 0 deletions vlib/v/checker/tests/wrong_or_expr_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module main

fn main() {
get_number := fn () ?i16 {
return 1
}
println('${get_number() or { '1' }}')
}
2 changes: 1 addition & 1 deletion vlib/v/tests/results_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -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) ! {
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/tests/str_gen_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 0059ca8

Please sign in to comment.