From 385c7a5528f74cf3c1ce1872128538d45782ee1a Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 13 Oct 2024 18:11:06 +0800 Subject: [PATCH 1/2] parser: allow for variable name using keyword 'type' --- vlib/v/parser/expr.v | 10 ++++++++-- vlib/v/parser/parser.v | 2 +- vlib/v/tests/var_name_using_type_test.v | 22 ++++++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 vlib/v/tests/var_name_using_type_test.v diff --git a/vlib/v/parser/expr.v b/vlib/v/parser/expr.v index a554bc8d813b84..371a6ab91fe9c0 100644 --- a/vlib/v/parser/expr.v +++ b/vlib/v/parser/expr.v @@ -519,8 +519,14 @@ fn (mut p Parser) check_expr(precedence int) !ast.Expr { p.next() return p.struct_init('', .anon, false) } - } - if p.tok.kind != .eof && !(p.tok.kind == .rsbr && p.inside_asm) { + } else if p.tok.kind == .key_type { + // variable name: type + ident := p.ident(ast.Language.v) + node = ident + p.mark_var_as_used(ident.name) + p.add_defer_var(ident) + p.is_stmt_ident = is_stmt_ident + } else if p.tok.kind != .eof && !(p.tok.kind == .rsbr && p.inside_asm) { // eof should be handled where it happens return error('none') // return p.unexpected(prepend_msg: 'invalid expression: ') diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 2d1890900db741..7c5ea83b4b2ede 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -2322,7 +2322,7 @@ fn (mut p Parser) ident(language ast.Language) ast.Ident { if is_volatile { p.next() } - if p.tok.kind != .name { + if p.tok.kind !in [.name, .key_type] { if is_mut || is_static || is_volatile { p.error_with_pos('the `${modifier_kind}` keyword is invalid here', mut_pos) } else { diff --git a/vlib/v/tests/var_name_using_type_test.v b/vlib/v/tests/var_name_using_type_test.v new file mode 100644 index 00000000000000..c020f7c17898ed --- /dev/null +++ b/vlib/v/tests/var_name_using_type_test.v @@ -0,0 +1,22 @@ +fn info(type int) int { + return type +} + +fn print_info() { + for type in [1, 2, 3] { + println(type) + } +} + +fn test_var_name_using_type() { + ret := info(22) + println(ret) + assert ret == 22 + + type := 33 + println(type) + assert type == 33 + + print_info() + assert true +} From 87d0d3303398cde22158be65515dcb346122522b Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 14 Oct 2024 09:51:32 +0800 Subject: [PATCH 2/2] change ast.Language.v to .v --- vlib/v/parser/expr.v | 6 +++--- vlib/v/parser/parser.v | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/vlib/v/parser/expr.v b/vlib/v/parser/expr.v index 371a6ab91fe9c0..9a20f0a3efe239 100644 --- a/vlib/v/parser/expr.v +++ b/vlib/v/parser/expr.v @@ -51,7 +51,7 @@ fn (mut p Parser) check_expr(precedence int) !ast.Expr { if p.peek_tok.kind in [.lpar, .lsbr] && p.peek_tok.is_next_to(p.tok) { node = p.call_expr(p.language, p.mod) } else { - ident := p.ident(ast.Language.v) + ident := p.ident(.v) node = ident if p.peek_tok.kind != .assign && (p.inside_if_cond || p.inside_match) { p.mark_var_as_used(ident.name) @@ -521,7 +521,7 @@ fn (mut p Parser) check_expr(precedence int) !ast.Expr { } } else if p.tok.kind == .key_type { // variable name: type - ident := p.ident(ast.Language.v) + ident := p.ident(.v) node = ident p.mark_var_as_used(ident.name) p.add_defer_var(ident) @@ -944,7 +944,7 @@ fn (mut p Parser) lambda_expr() ?ast.LambdaExpr { if p.tok.kind == .eof { break } - ident := p.ident(ast.Language.v) + ident := p.ident(.v) if p.scope.known_var(ident.name) { p.error_with_pos('redefinition of parameter `${ident.name}`', ident.pos) } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 7c5ea83b4b2ede..6498185c661df7 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -2617,7 +2617,7 @@ fn (mut p Parser) name_expr() ast.Expr { is_known_var := p.scope.known_var(p.tok.lit) if is_known_var { p.mark_var_as_used(p.tok.lit) - return p.ident(ast.Language.v) + return p.ident(.v) } else { type_pos := p.tok.pos() typ := p.parse_type()