diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index aa05372aa8adb5..6b3528177bdad9 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -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 { @@ -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 @@ -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 } diff --git a/vlib/v/tests/selectorexpt_field_name_test.v b/vlib/v/tests/selectorexpt_field_name_test.v new file mode 100644 index 00000000000000..68fa25be23ce17 --- /dev/null +++ b/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 + }() +}