Skip to content

Commit 834cf40

Browse files
authored
cgen, fmt, scanner: fix and use nested lambda in scanner (#11967)
1 parent 400ab78 commit 834cf40

File tree

3 files changed

+13
-19
lines changed

3 files changed

+13
-19
lines changed

vlib/v/fmt/fmt.v

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub mut:
4343
use_short_fn_args bool
4444
single_line_fields bool // should struct fields be on a single line
4545
it_name string // the name to replace `it` with
46-
inside_lambda bool
46+
in_lambda_depth int
4747
inside_const bool
4848
is_mbranch_expr bool // match a { x...y { } }
4949
fn_scope &ast.Scope = voidptr(0)
@@ -1539,9 +1539,9 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) {
15391539
}
15401540
if node.is_method {
15411541
if node.name in ['map', 'filter', 'all', 'any'] {
1542-
f.inside_lambda = true
1542+
f.in_lambda_depth++
15431543
defer {
1544-
f.inside_lambda = false
1544+
f.in_lambda_depth--
15451545
}
15461546
}
15471547
if node.left is ast.Ident {
@@ -1724,7 +1724,7 @@ pub fn (mut f Fmt) ident(node ast.Ident) {
17241724
}
17251725
}
17261726
f.write_language_prefix(node.language)
1727-
if node.name == 'it' && f.it_name != '' && !f.inside_lambda { // allow `it` in lambdas
1727+
if node.name == 'it' && f.it_name != '' && f.in_lambda_depth == 0 { // allow `it` in lambdas
17281728
f.write(f.it_name)
17291729
} else if node.kind == .blank_ident {
17301730
f.write('_')

vlib/v/gen/c/array.v

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ fn (mut g Gen) gen_array_map(node ast.CallExpr) {
196196
}
197197
}
198198
ast.CallExpr {
199-
if expr.name in ['map', 'filter'] {
199+
if expr.name in ['map', 'filter', 'all', 'any'] {
200200
is_embed_map_filter = true
201201
g.stmt_path_pos << g.out.len
202202
}
@@ -387,7 +387,7 @@ fn (mut g Gen) gen_array_filter(node ast.CallExpr) {
387387
}
388388
}
389389
ast.CallExpr {
390-
if expr.name in ['map', 'filter'] {
390+
if expr.name in ['map', 'filter', 'all', 'any'] {
391391
is_embed_map_filter = true
392392
g.stmt_path_pos << g.out.len
393393
}
@@ -678,7 +678,7 @@ fn (mut g Gen) gen_array_any(node ast.CallExpr) {
678678
}
679679
}
680680
ast.CallExpr {
681-
if expr.name in ['map', 'filter'] {
681+
if expr.name in ['map', 'filter', 'all', 'any'] {
682682
is_embed_map_filter = true
683683
g.stmt_path_pos << g.out.len
684684
}
@@ -761,7 +761,7 @@ fn (mut g Gen) gen_array_all(node ast.CallExpr) {
761761
}
762762
}
763763
ast.CallExpr {
764-
if expr.name in ['map', 'filter'] {
764+
if expr.name in ['map', 'filter', 'all', 'any'] {
765765
is_embed_map_filter = true
766766
g.stmt_path_pos << g.out.len
767767
}

vlib/v/scanner/scanner.v

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -926,20 +926,14 @@ fn (mut s Scanner) text_scan() token.Token {
926926
// here we set the limit 100 which should be nice for real cases
927927
// e.g. ...Bar<int, []Foo<int>, Baz_, [20]f64, map[string][]bool>> =>
928928
// <int, Baz_, [20]f64, map[string][]bool => int, Baz_, f64, bool
929-
mut is_generic := true
930-
if s.last_lt >= 0 && s.pos - s.last_lt < 100 {
929+
is_generic := if s.last_lt >= 0 && s.pos - s.last_lt < 100 {
931930
typs := s.text[s.last_lt + 1..s.pos].split(',').map(it.trim_space().trim_right('>').after(']'))
932931
// if any typ is neither Type nor builtin, then the case is non-generic
933-
for typ in typs {
934-
if typ.len == 0
935-
|| (!(typ[0].is_capital() && typ[1..].bytes().all(it.is_alnum()
936-
|| it == `_`)) && typ !in ast.builtin_type_names) {
937-
is_generic = false
938-
break
939-
}
940-
}
932+
typs.all(it.len > 0
933+
&& ((it[0].is_capital() && it[1..].bytes().all(it.is_alnum()
934+
|| it == `_`)) || it in ast.builtin_type_names))
941935
} else {
942-
is_generic = false
936+
false
943937
}
944938
if is_generic {
945939
return s.new_token(.gt, '', 1)

0 commit comments

Comments
 (0)