Skip to content

Commit

Permalink
parser: fix close_scope() missing, when field.name is sort or `sort…
Browse files Browse the repository at this point in the history
…ed` (fix#20436) (#20485)
  • Loading branch information
shove70 committed Jan 11, 2024
1 parent 781d97d commit 690961b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
9 changes: 3 additions & 6 deletions vlib/v/parser/parser.v
Expand Up @@ -3205,6 +3205,9 @@ fn (mut p Parser) dot_expr(left ast.Expr) ast.Expr {
is_filter := field_name in ['filter', 'map', 'any', 'all']
if is_filter || field_name == 'sort' || field_name == 'sorted' {
p.open_scope()
defer {
p.close_scope()
}
}
// ! in mutable methods
if p.tok.kind == .not && p.peek_tok.kind == .lpar {
Expand Down Expand Up @@ -3267,9 +3270,6 @@ fn (mut p Parser) dot_expr(left ast.Expr) ast.Expr {
scope: p.scope
comments: comments
}
if is_filter || field_name == 'sort' || field_name == 'sorted' {
p.close_scope()
}
return mcall_expr
}
mut is_mut := false
Expand Down Expand Up @@ -3315,9 +3315,6 @@ fn (mut p Parser) dot_expr(left ast.Expr) ast.Expr {
next_token: p.tok.kind
}

if is_filter {
p.close_scope()
}
return sel_expr
}

Expand Down
15 changes: 15 additions & 0 deletions vlib/v/tests/selectorexpt_field_name_test.v
@@ -0,0 +1,15 @@
// for issue 20436
// phenomenon:
// close_scope() is lost when selectexpr has two special names: sort and sorted.
// This problem can be tested using closure arguments.
struct Foo {
sort []int
}

fn test_main() {
f := Foo{}

fn [f] () {
assert f.sort.len == 0
}()
}

0 comments on commit 690961b

Please sign in to comment.