diff --git a/vlib/v/parser/expr.v b/vlib/v/parser/expr.v index a554bc8d813b84..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) @@ -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(.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: ') @@ -938,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 abc1775fb9a3ab..349af16e22ac76 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -2326,7 +2326,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 { @@ -2621,7 +2621,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() 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 +}