Skip to content

Commit c1904ec

Browse files
authored
parser, checker: check invalid lambda expr (#20461)
1 parent 28e810d commit c1904ec

File tree

7 files changed

+36
-14
lines changed

7 files changed

+36
-14
lines changed

vlib/v/checker/errors.v

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ fn (mut c Checker) error(message string, pos token.Pos) {
4141
c.warn_or_error(msg, pos, false)
4242
}
4343

44+
fn (mut c Checker) fatal(message string, pos token.Pos) {
45+
if (c.pref.translated || c.file.is_translated) && message.starts_with('mismatched types') {
46+
// TODO move this
47+
return
48+
}
49+
msg := message.replace('`Array_', '`[]')
50+
c.pref.fatal_errors = true
51+
c.warn_or_error(msg, pos, false)
52+
}
53+
4454
fn (mut c Checker) note(message string, pos token.Pos) {
4555
if c.pref.message_limit >= 0 && c.nr_notices >= c.pref.message_limit {
4656
c.should_abort = true

vlib/v/checker/lambda_expr.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ pub fn (mut c Checker) lambda_expr(mut node ast.LambdaExpr, exp_typ ast.Type) as
77
if node.is_checked {
88
return node.typ
99
}
10-
if exp_typ == 0 {
11-
c.error('lambda expressions are allowed only in places expecting function callbacks',
10+
if exp_typ in [0, ast.void_type] {
11+
c.fatal('lambda expressions are allowed only in places expecting function callbacks',
1212
node.pos)
1313
return ast.void_type
1414
}

vlib/v/checker/tests/lambda_expression_in_map.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ vlib/v/checker/tests/lambda_expression_in_map.vv:3:12: error: lambda expressions
22
1 | a := [4, 5]
33
2 | dump(a.map(it))
44
3 | dump(a.map(|| 5))
5-
| ~~
5+
| ~~~~
66
4 | dump(a.map(|x| 5 * x))
77
5 | dump(a.map(|x| x))
88
vlib/v/checker/tests/lambda_expression_in_map.vv:3:8: error: dump expression can not be void
@@ -16,7 +16,7 @@ vlib/v/checker/tests/lambda_expression_in_map.vv:6:12: error: lambda expressions
1616
4 | dump(a.map(|x| 5 * x))
1717
5 | dump(a.map(|x| x))
1818
6 | dump(a.map(|x, y| x))
19-
| ^
19+
| ~~~~~~~~
2020
vlib/v/checker/tests/lambda_expression_in_map.vv:6:8: error: dump expression can not be void
2121
4 | dump(a.map(|x| 5 * x))
2222
5 | dump(a.map(|x| x))
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
vlib/v/checker/tests/lambda_expression_invalid.vv:3:19: error: lambda expressions are allowed only in places expecting function callbacks
2+
1 | fn main() {
3+
2 | x := 5
4+
3 | f := if x == 5 { |x| x - 5 } else { |x| x }
5+
| ~~~~~~~~~
6+
4 | println(f(x))
7+
5 | }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
fn main() {
2+
x := 5
3+
f := if x == 5 { |x| x - 5 } else { |x| x }
4+
println(f(x))
5+
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
vlib/v/checker/tests/lambda_undefined_variables_err.vv:7:2: warning: unused variable: `s2`
22
5 | f(|x| s1)
3-
6 |
3+
6 |
44
7 | s2 := 'abc'
55
| ~~
66
8 | f(|x| s2)
77
9 | }
88
vlib/v/checker/tests/lambda_undefined_variables_err.vv:5:8: error: undefined ident: `s1`
9-
3 |
9+
3 |
1010
4 | fn main() {
1111
5 | f(|x| s1)
1212
| ~~
13-
6 |
13+
6 |
1414
7 | s2 := 'abc'
1515
vlib/v/checker/tests/lambda_undefined_variables_err.vv:5:4: error: `s1` used as value
16-
3 |
16+
3 |
1717
4 | fn main() {
1818
5 | f(|x| s1)
19-
| ^
20-
6 |
19+
| ~~~~~~
20+
6 |
2121
7 | s2 := 'abc'
2222
vlib/v/checker/tests/lambda_undefined_variables_err.vv:8:8: error: undefined variable `s2`
23-
6 |
23+
6 |
2424
7 | s2 := 'abc'
2525
8 | f(|x| s2)
2626
| ~~
2727
9 | }
2828
vlib/v/checker/tests/lambda_undefined_variables_err.vv:8:4: error: `s2` used as value
29-
6 |
29+
6 |
3030
7 | s2 := 'abc'
3131
8 | f(|x| s2)
32-
| ^
32+
| ~~~~~~
3333
9 | }

vlib/v/parser/expr.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ fn (mut p Parser) lambda_expr() ?ast.LambdaExpr {
900900
e := p.expr(0)
901901
pos_end := p.tok.pos()
902902
return ast.LambdaExpr{
903-
pos: pos
903+
pos: pos.extend(e.pos())
904904
pos_expr: pos_expr
905905
pos_end: pos_end
906906
params: params

0 commit comments

Comments
 (0)