-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
checker: don't cast match branch structs to IError, when the return t…
…ype of a function is !SumType (#22444)
- Loading branch information
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
vlib/v/tests/return_match_expr_of_sumtype_do_not_cast_to_ierror_test.v
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
type Foo = int | string | Bar | ||
|
||
struct Bar {} | ||
|
||
fn foo_result_struct_second() !Foo { | ||
return match true { | ||
true { 1 } | ||
else { Bar{} } | ||
} | ||
} | ||
|
||
fn foo_option_struct_second() ?Foo { | ||
return match true { | ||
true { 1 } | ||
else { Bar{} } | ||
} | ||
} | ||
|
||
fn foo_result_string_second() !Foo { | ||
return match true { | ||
true { 1 } | ||
else { '' } | ||
} | ||
} | ||
|
||
fn foo_option_string_second() ?Foo { | ||
return match true { | ||
true { 1 } | ||
else { '' } | ||
} | ||
} | ||
|
||
fn foo_result_struct_first() !Foo { | ||
return match true { | ||
true { Bar{} } | ||
else { 7 } | ||
} | ||
} | ||
|
||
fn foo_option_struct_first() ?Foo { | ||
return match true { | ||
true { Bar{} } | ||
else { 7 } | ||
} | ||
} | ||
|
||
fn test_return_match_expr_of_sumtype_opt_res() { | ||
mut ret := Foo{} | ||
|
||
ret = foo_result_struct_second() or { return } | ||
println(ret) | ||
assert '${ret}' == 'Foo(1)' | ||
|
||
ret = foo_option_struct_second() or { return } | ||
println(ret) | ||
assert '${ret}' == 'Foo(1)' | ||
|
||
ret = foo_result_string_second() or { return } | ||
println(ret) | ||
assert '${ret}' == 'Foo(1)' | ||
|
||
ret = foo_option_string_second() or { return } | ||
println(ret) | ||
assert '${ret}' == 'Foo(1)' | ||
|
||
ret = foo_result_struct_first() or { return } | ||
println(ret) | ||
assert '${ret}' == 'Foo(Bar{})' | ||
|
||
ret = foo_option_struct_first() or { return } | ||
println(ret) | ||
assert '${ret}' == 'Foo(Bar{})' | ||
} |