Skip to content

Commit 28d61f4

Browse files
authored
checker:fix compiler panic on wrong arg count with or block (#26665)
1 parent 57e617a commit 28d61f4

4 files changed

Lines changed: 43 additions & 10 deletions

File tree

‎vlib/v/checker/fn.v‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,7 +1517,10 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
15171517
}
15181518
}
15191519
}
1520-
c.check_expected_arg_count(mut node, func) or { return func.return_type }
1520+
c.check_expected_arg_count(mut node, func) or {
1521+
node.return_type = func.return_type
1522+
return func.return_type
1523+
}
15211524
}
15221525
// println / eprintln / panic can print anything
15231526
if args_len > 0 && fn_name in print_everything_fns {
@@ -2358,7 +2361,10 @@ fn (mut c Checker) method_call(mut node ast.CallExpr, mut continue_check &bool)
23582361
node.is_field = true
23592362
info := field_sym.info as ast.FnType
23602363

2361-
c.check_expected_arg_count(mut node, info.func) or { return info.func.return_type }
2364+
c.check_expected_arg_count(mut node, info.func) or {
2365+
node.return_type = info.func.return_type
2366+
return info.func.return_type
2367+
}
23622368
node.return_type = info.func.return_type
23632369
mut earg_types := []ast.Type{}
23642370

@@ -2546,7 +2552,10 @@ fn (mut c Checker) method_call(mut node ast.CallExpr, mut continue_check &bool)
25462552
&& method.ctdefine_idx != ast.invalid_type_idx {
25472553
node.should_be_skipped = c.evaluate_once_comptime_if_attribute(mut method.attrs[method.ctdefine_idx])
25482554
}
2549-
c.check_expected_arg_count(mut node, method) or { return method.return_type }
2555+
c.check_expected_arg_count(mut node, method) or {
2556+
node.return_type = method.return_type
2557+
return method.return_type
2558+
}
25502559
mut exp_arg_typ := ast.no_type // type of 1st arg for special builtin methods
25512560
mut param_is_mut := false
25522561
mut no_type_promotion := false

‎vlib/v/checker/tests/method_call_on_undefined_err.out‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,3 @@ vlib/v/checker/tests/method_call_on_undefined_err.vv:14:13: error: expected 0 ar
5656
16 | exit(1)
5757
Details: have (string)
5858
want ()
59-
vlib/v/checker/tests/method_call_on_undefined_err.vv:14:31: error: unexpected `or` block, the function `foo` does not return an Option or a Result
60-
12 |
61-
13 | fn main() {
62-
14 | res := foo('fe80::1234%ne0') or {
63-
| ~~~~
64-
15 | eprintln(err)
65-
16 | exit(1)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
vlib/v/checker/tests/wrong_arg_count_with_or_block_no_panic.vv:9:6: notice: condition is always true
2+
7 | // Test case: function with no params, called with extra args and or block with if-else
3+
8 | foo(1, 2, 3) or {
4+
9 | if true {
5+
| ~~~~
6+
10 | println('error')
7+
11 | } else {
8+
vlib/v/checker/tests/wrong_arg_count_with_or_block_no_panic.vv:8:6: error: expected 0 arguments, but got 3
9+
6 | fn main() {
10+
7 | // Test case: function with no params, called with extra args and or block with if-else
11+
8 | foo(1, 2, 3) or {
12+
| ~~~~~~~
13+
9 | if true {
14+
10 | println('error')
15+
Details: have (int literal, int literal, int literal)
16+
want ()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Test that wrong argument count with or block containing if-else
2+
// does not cause compiler panic (issue #26647)
3+
fn foo() ! {
4+
}
5+
6+
fn main() {
7+
// Test case: function with no params, called with extra args and or block with if-else
8+
foo(1, 2, 3) or {
9+
if true {
10+
println('error')
11+
} else {
12+
println('other')
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)