mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix compile time reflection for method types (.typ was always 0) (#16310)
This commit is contained in:
parent
f427a5241a
commit
14f90b1196
@ -512,20 +512,23 @@ fn (mut g Gen) comptime_for(node ast.ComptimeFor) {
|
||||
}
|
||||
g.writeln('}));\n')
|
||||
}
|
||||
mut sig := 'anon_fn_'
|
||||
mut sig := 'fn ('
|
||||
// skip the first (receiver) arg
|
||||
for j, arg in method.params[1..] {
|
||||
// TODO: ignore mut/pts in sig for now
|
||||
typ := arg.typ.set_nr_muls(0)
|
||||
sig += '$typ'
|
||||
sig += g.table.sym(typ).name
|
||||
if j < method.params.len - 2 {
|
||||
sig += '_'
|
||||
sig += ', '
|
||||
}
|
||||
}
|
||||
sig += '_$method.return_type'
|
||||
sig += ')'
|
||||
ret_type := g.table.sym(method.return_type).name
|
||||
if ret_type != 'void' {
|
||||
sig += ' $ret_type'
|
||||
}
|
||||
styp := g.table.find_type_idx(sig)
|
||||
// println(styp)
|
||||
// if styp == 0 { }
|
||||
|
||||
// TODO: type aliases
|
||||
ret_typ := method.return_type.idx()
|
||||
g.writeln('\t${node.val_var}.typ = $styp;')
|
||||
|
@ -48,18 +48,29 @@ fn test_comptime_for() {
|
||||
|
||||
fn test_comptime_for_with_if() {
|
||||
println(@FN)
|
||||
mut methods_found := map[string]int{}
|
||||
$for method in App.methods {
|
||||
println(' method: ' + no_lines('$method'))
|
||||
$if method.typ is fn () {
|
||||
methods_found['fn()'] += 1
|
||||
assert method.name in ['run', 'method2']
|
||||
}
|
||||
$if method.typ is fn () int {
|
||||
methods_found['fn() int'] += 1
|
||||
}
|
||||
$if method.return_type is int {
|
||||
assert method.name in ['int_method1', 'int_method2']
|
||||
}
|
||||
$if method.typ is fn (string) {
|
||||
methods_found['fn(string)'] += 1
|
||||
}
|
||||
$if method.args[0].typ is string {
|
||||
assert method.name == 'string_arg'
|
||||
}
|
||||
}
|
||||
assert methods_found['fn()'] == 2
|
||||
assert methods_found['fn() int'] == 2
|
||||
assert methods_found['fn(string)'] == 1
|
||||
}
|
||||
|
||||
fn test_comptime_for_fields() {
|
||||
|
Loading…
Reference in New Issue
Block a user