checker: add support for static methods in @FN and @METHOD (#21990)

This commit is contained in:
Swastik Baranwal 2024-08-03 20:06:35 +05:30 committed by GitHub
parent 3ca5bc3bcd
commit 06cf796b21
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 1 deletions

View file

@ -3546,8 +3546,12 @@ fn (mut c Checker) at_expr(mut node ast.AtExpr) ast.Type {
if c.table.cur_fn == unsafe { nil } {
return ast.void_type
}
if _ := c.table.cur_fn.name.index('__static__') {
node.val = c.table.cur_fn.name.all_after_last('__static__')
} else {
node.val = c.table.cur_fn.name.all_after_last('.')
}
}
.method_name {
if c.table.cur_fn == unsafe { nil } {
return ast.void_type
@ -3556,6 +3560,8 @@ fn (mut c Checker) at_expr(mut node ast.AtExpr) ast.Type {
if c.table.cur_fn.is_method {
node.val = c.table.type_to_str(c.table.cur_fn.receiver.typ).all_after_last('.') +
'.' + fname
} else if _ := fname.index('__static__') {
node.val = fname.all_before('__static__') + '.' + fname.all_after('__static__')
} else {
node.val = fname
}

View file

@ -60,6 +60,12 @@ fn (mut t TestFn) tst_2(cb fn (int)) {
cb(1)
}
fn TestFn.static_fn() {
assert @FN == 'static_fn'
assert @METHOD == 'TestFn.static_fn'
assert @STRUCT == 'TestFn'
}
fn fn_name_mod_level() {
assert @FN == 'fn_name_mod_level'
assert @METHOD == 'fn_name_mod_level'
@ -96,6 +102,7 @@ fn test_at_fn() {
t := i + 1
assert t == 2
})
TestFn.static_fn()
}
fn test_at_mod() {