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: fix assigning static method to anon fn (fix #19484) #19499

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 22 additions & 0 deletions vlib/v/parser/parser.v
Original file line number Diff line number Diff line change
Expand Up @@ -2825,6 +2825,28 @@ fn (mut p Parser) name_expr() ast.Expr {
p.tok.pos())
}
}
// `anon_fn := Foo.bar` assign static method
if !known_var && lit0_is_capital && p.peek_tok.kind == .dot && language == .v
&& p.peek_token(2).kind == .name {
if func := p.table.find_fn(p.prepend_mod(p.tok.lit) + '__static__' + p.peek_token(2).lit) {
fn_type := ast.new_type(p.table.find_or_register_fn_type(func, false,
true))
pos := p.tok.pos()
typ_name := p.check_name()
p.check(.dot)
field_name := p.check_name()
pos.extend(p.tok.pos())
return ast.Ident{
name: p.prepend_mod(typ_name) + '__static__' + field_name
mod: p.mod
kind: .function
info: ast.IdentFn{
typ: fn_type
}
scope: p.scope
}
}
}
// `Color.green`
mut enum_name := p.check_name()
enum_name_pos := p.prev_tok.pos()
Expand Down
15 changes: 15 additions & 0 deletions vlib/v/tests/assign_static_method_to_anon_fn_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
struct Foo {
}

fn Foo.bar() string {
println('bar')
return 'bar'
}

fn test_assign_static_method_to_anon_fn() {
// vfmt off
anon_fn := Foo.bar
// vfmt on
ret := anon_fn()
assert ret == 'bar'
}
Loading