Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checker: fix missing check for or expr on string interpolation #17566

Merged
merged 8 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading