Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Nov 12, 2024
1 parent 0467c17 commit ebc24de
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 60 deletions.
4 changes: 0 additions & 4 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -1381,10 +1381,6 @@ fn (mut c Checker) check_or_last_stmt(mut stmt ast.Stmt, ret_type ast.Type, expr
ast.ExprStmt {
c.expected_type = ret_type
c.expected_or_type = ret_type.clear_option_and_result()
if stmt.expr is ast.None && ret_type.has_flag(.option) {
// or { none } where fn returns Option
return
}
last_stmt_typ := c.expr(mut stmt.expr)
if last_stmt_typ.has_flag(.option) || last_stmt_typ == ast.none_type {
if stmt.expr in [ast.Ident, ast.SelectorExpr, ast.CallExpr, ast.None, ast.CastExpr] {
Expand Down
110 changes: 62 additions & 48 deletions vlib/v/checker/tests/call_empty_or_block_err.out
Original file line number Diff line number Diff line change
@@ -1,56 +1,70 @@
vlib/v/checker/tests/call_empty_or_block_err.vv:9:2: warning: unused variable: `a`
7 |
8 | fn main() {
9 | a := foo() or { foo() or {} }
vlib/v/checker/tests/call_empty_or_block_err.vv:21:2: warning: unused variable: `a`
19 |
20 | fn main() {
21 | a := foo() or { foo() or {} }
| ^
10 |
11 | // must be error
vlib/v/checker/tests/call_empty_or_block_err.vv:12:2: warning: unused variable: `y`
10 |
11 | // must be error
12 | y := if c := foo() {
22 |
23 | // must be error
vlib/v/checker/tests/call_empty_or_block_err.vv:24:2: warning: unused variable: `y`
22 |
23 | // must be error
24 | y := if c := foo() {
| ^
13 | dump(c)
14 | bar() or {}
vlib/v/checker/tests/call_empty_or_block_err.vv:20:2: warning: unused variable: `z`
18 |
19 | // ok
20 | z := if d := foo() {
25 | dump(c)
26 | bar() or {}
vlib/v/checker/tests/call_empty_or_block_err.vv:32:2: warning: unused variable: `z`
30 |
31 | // ok
32 | z := if d := foo() {
| ^
21 | dump(d)
22 | bar() or {}
vlib/v/checker/tests/call_empty_or_block_err.vv:29:2: warning: unused variable: `w`
27 |
28 | // ok
29 | w := foo() or {
33 | dump(d)
34 | bar() or {}
vlib/v/checker/tests/call_empty_or_block_err.vv:41:2: warning: unused variable: `w`
39 |
40 | // ok
41 | w := foo() or {
| ^
30 | bar() or {}
31 | false
vlib/v/checker/tests/call_empty_or_block_err.vv:35:2: warning: unused variable: `b`
33 |
34 | // ok
35 | b := foo() or {
42 | bar() or {}
43 | false
vlib/v/checker/tests/call_empty_or_block_err.vv:47:2: warning: unused variable: `b`
45 |
46 | // ok
47 | b := foo() or {
| ^
36 | foo() or {}
37 | false
vlib/v/checker/tests/call_empty_or_block_err.vv:9:24: error: expression requires a non empty `or {}` block
7 |
8 | fn main() {
9 | a := foo() or { foo() or {} }
48 | foo() or {}
49 | false
vlib/v/checker/tests/call_empty_or_block_err.vv:10:22: error: expression requires a non empty `or {}` block
8 |
9 | fn unwrap_function1() ?int {
10 | return unwrap_int() or { }
| ~~~~~~
11 | }
12 |
vlib/v/checker/tests/call_empty_or_block_err.vv:14:27: error: `or` block must provide a value of type `int`, not `none`
12 |
13 | fn unwrap_function2() ?int {
14 | return unwrap_int() or { none }
| ~~~~
15 | }
16 |
vlib/v/checker/tests/call_empty_or_block_err.vv:21:24: error: expression requires a non empty `or {}` block
19 |
20 | fn main() {
21 | a := foo() or { foo() or {} }
| ~~~~~
10 |
11 | // must be error
vlib/v/checker/tests/call_empty_or_block_err.vv:14:9: error: expression requires a non empty `or {}` block
12 | y := if c := foo() {
13 | dump(c)
14 | bar() or {}
22 |
23 | // must be error
vlib/v/checker/tests/call_empty_or_block_err.vv:26:9: error: expression requires a non empty `or {}` block
24 | y := if c := foo() {
25 | dump(c)
26 | bar() or {}
| ~~~~~
15 | } else {
16 | false
vlib/v/checker/tests/call_empty_or_block_err.vv:14:3: error: the final expression in `if` or `match`, must have a value of a non-void type
12 | y := if c := foo() {
13 | dump(c)
14 | bar() or {}
27 | } else {
28 | false
vlib/v/checker/tests/call_empty_or_block_err.vv:26:3: error: the final expression in `if` or `match`, must have a value of a non-void type
24 | y := if c := foo() {
25 | dump(c)
26 | bar() or {}
| ~~~~~
15 | } else {
16 | false
27 | } else {
28 | false
12 changes: 12 additions & 0 deletions vlib/v/checker/tests/call_empty_or_block_err.vv
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@ fn foo() !bool {
return true
}

fn unwrap_int() ?int {
return 1
}

fn unwrap_function1() ?int {
return unwrap_int() or {}
}

fn unwrap_function2() ?int {
return unwrap_int() or { none }
}

fn bar() ! {
}

Expand Down
8 changes: 0 additions & 8 deletions vlib/v/tests/return_result_in_or_block_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,8 @@ fn unwrap_function1() !int {
return unwrap_int() or { error('we are issing the return?') }
}

fn unwrap_function2() ?int {
return unwrap_int() or { none }
}

fn test_return_result_in_or_block() {
x1 := unwrap_function1() or { panic(err) }
println(x1)
assert x1 == 1

x2 := unwrap_function2() or { panic(err) }
println(x2)
assert x2 == 1
}

0 comments on commit ebc24de

Please sign in to comment.