Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parser: allow for variable name using keyword 'type' #22512

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions vlib/v/parser/expr.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use .v?

ident := p.ident(.v)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is currently only used in v.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

he meant using p.ident(.v) instead of p.ident(ast.Language.v)

indeed that's simpler and I thought vfmt simplified it already

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see.
Yes, it can. Thank you!

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: ')
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/parser/parser.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 22 additions & 0 deletions vlib/v/tests/var_name_using_type_test.v
Original file line number Diff line number Diff line change
@@ -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
}
Loading