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 multi return using nil and voidptr #21144

Merged
merged 1 commit into from
Mar 30, 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
7 changes: 6 additions & 1 deletion vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -3855,7 +3855,12 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
fn (mut c Checker) concat_expr(mut node ast.ConcatExpr) ast.Type {
mut mr_types := []ast.Type{}
for mut expr in node.vals {
mr_types << c.expr(mut expr)
mut typ := c.expr(mut expr)
if typ == ast.nil_type {
// nil and voidptr produces the same struct type name
typ = ast.voidptr_type
}
mr_types << typ
}
if node.vals.len == 1 {
typ := mr_types[0]
Expand Down
17 changes: 17 additions & 0 deletions vlib/v/tests/multi_return_nil_voidptr_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
fn ret_int_ptr(arg int) !(int, voidptr) {
if arg < 0 {
return error('argument is smaller then zero')
}
return 1, [1, 2, 3].data
}

fn test_main() {
mut val1 := 0
mut val2 := unsafe { nil }
val1, val2 = ret_int_ptr(-1) or {
println(err)
val1, val2
}
assert val1 == 0
assert val2 == unsafe { nil }
}