Skip to content

Commit f7c611c

Browse files
authored
parser: allow for |mut x, y|expr (fix #25734) (#25735)
1 parent f4a8e84 commit f7c611c

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

vlib/v/checker/if.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
156156
c.error('non-bool type `${c.table.type_to_str(cond_typ)}` used as if condition',
157157
branch.cond.pos())
158158
}
159-
if !c.pref.translated && !c.file.is_translated {
159+
if !c.pref.translated && !c.file.is_translated && !c.inside_unsafe {
160160
mut check_expr := branch.cond
161161
t_expr := c.checker_transformer.expr(mut check_expr)
162162
if t_expr is ast.BoolLiteral {

vlib/v/parser/expr.v

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,7 @@ fn (mut p Parser) lambda_expr() ?ast.LambdaExpr {
10171017
// a) `f(||expr)` for a callback lambda expression with 0 arguments
10181018
// b) `f(|a_1,...,a_n| expr_with_a_1_etc_till_a_n)` for a callback with several arguments
10191019
if !(p.tok.kind == .logical_or
1020+
|| (p.peek_token(1).kind == .key_mut && p.peek_token(2).kind == .name)
10201021
|| (p.peek_token(1).kind == .name && p.peek_token(2).kind == .pipe)
10211022
|| (p.peek_token(1).kind == .name && p.peek_token(2).kind == .comma)) {
10221023
return none
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
struct Window {
2+
mut:
3+
calls int
4+
on_init fn (mut w Window) = unsafe { nil }
5+
cb fn (mut w Window, x int) = unsafe { nil }
6+
}
7+
8+
fn (mut w Window) run() {
9+
w.on_init(mut w)
10+
w.cb(mut w, w.calls)
11+
}
12+
13+
fn (mut w Window) method_with_mut_receiver() {
14+
w.calls++
15+
}
16+
17+
fn test_mut_lambda_expr() {
18+
mut window := Window{
19+
on_init: |mut w| w.method_with_mut_receiver()
20+
cb: |mut w, mut y| unsafe {
21+
if true {
22+
assert y == 1
23+
w.calls++
24+
}
25+
}
26+
}
27+
window.run()
28+
assert window.calls == 2
29+
}

0 commit comments

Comments
 (0)