Skip to content

std.builtin.TypeInfo.Declaration.data.Fn.arg_names is empty #8259

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

Closed
ghost opened this issue Mar 15, 2021 · 12 comments
Closed

std.builtin.TypeInfo.Declaration.data.Fn.arg_names is empty #8259

ghost opened this issue Mar 15, 2021 · 12 comments
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@ghost
Copy link

ghost commented Mar 15, 2021

I'm trying to integrate zig with dart. Dart supports ffi with C. For the purposes of my project, zig would be a better choice than C. I need only 1-way calling : dart -> zig.
To make this possible, I have to generate a bit of glue code for dart. It would be enough for me to be able to get @typeInfo for my zig functions that includes the argument names and argument types. Currently, @typeInfo does not seem to provide this information.
Is there any workaround?

@LemonBoy
Copy link
Contributor

The argument types are available and so are the argument names.

@ghost
Copy link
Author

ghost commented Mar 15, 2021

Sorry for the stupid question, but I'm new to zig (2 days of experience with the language), and from tutorials, I got only the reference to @typeInfo, but argument names are in Declaration, and I have no clue how I can get a Declaration based on FnArg I have. I can't see Declaration among the fields of FnArg

@data-man
Copy link
Contributor

Related: #6530

@ghost
Copy link
Author

ghost commented Mar 16, 2021

Probably it's indeed related somehow, but maybe you can answer my question directly? I browsed through the "related" thing, don't have a clue how it can help me. I'm doing a very important experiment in the history of dart language, no one has ever tried to integrate it with zig and 99.9% of dart users never heard of it, to begin with, and even if they heard, they heard it from me!. In case of success, my plan was to start spamming dart mailing list extolling the virtues of a new language and alleging that it fits perfectly into ffi framework etc.. But I can't get ahold of a damn Declaration, without which not only can't I accomplish my plan - I can't even start putting it in motion! 😢

@SpexGuy
Copy link
Contributor

SpexGuy commented Mar 16, 2021

maybe you can answer my question directly?

You can't obtain the argument names directly from function type info. Argument names are not part of function types. Instead, you need to pull arguments from a specific function declaration site. To do this, you must find the Declaration entry in the declaring type. There is no other way. So for example:

const Foo = struct {
  pub fn myFunc(a: u32) void {}
};
comptime {
  // get info for declaring type
  const info = @typeInfo(Foo);
  // get the myFunc decl.  There's only one.
  const decl = info.Struct.decls[0];
  // get the argument names
  const args = decl.data.Fn.arg_names;
  // args is [_][]const u8{ "a" }
}

@ghost
Copy link
Author

ghost commented Mar 16, 2021

So in order to get the names of function arguments, I have to declare this function inside some Struct? And it won't work for a standalone function? E.g. I have

pub fn myFunction(m: usize, N: i32) u32 {
   //...
}

Do I have to put it into a struct?
Actually, that's not that bad, Foo may as well serve as a namespace, but that's a bit weird, isn't it?
And what about the types of arguments? Will I get them from @typeInfo? Like

const t=@TypeOf(Foo.myFunc);
const typeInfo = @typeInfo(t);
const myArgs = typeInfo.Fn.args;
// and from there, I get all types

Right?

@SpexGuy
Copy link
Contributor

SpexGuy commented Mar 16, 2021

The top-level of any file is itself a struct. You can use @This() to access that struct. It's also the type returned from @import when you import the file.

The argument types are stored in the type info for the function type.

@data-man
Copy link
Contributor

There is no other way.

std.meta.declarations(@This())
?

@ghost
Copy link
Author

ghost commented Mar 16, 2021

Yes, @This() works! And std.meta.declarations(@This()) works, too. Thanks a lot!

@ghost ghost closed this as completed Mar 16, 2021
@ghost
Copy link
Author

ghost commented Mar 19, 2021

Everything works, except arg_names. The list of arg_names is always empty

const Foo = struct {
  pub fn myFunc(a: u32, b:u32, c:u32) void {}
};

| *"fn_type=", fn(u32, u32, u32) void
| *"type info=", (struct std.builtin.Fn constant)
| *"return_type=", void
| *"is_export=", false
| *"is_extern=", false
| *"is_var_args=", false
| *"arg_names.len=", 0

@SpexGuy SpexGuy reopened this Mar 19, 2021
@SpexGuy SpexGuy changed the title @typeInfo on function types std.builtin.TypeInfo.Declaration.data.Fn.arg_names is empty Mar 19, 2021
@SpexGuy SpexGuy added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Mar 19, 2021
@Vexu Vexu added this to the 0.9.0 milestone Mar 19, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@Jarred-Sumner
Copy link
Contributor

Jarred-Sumner commented Jul 17, 2021

I'm able to reproduce this as well.

This code prints args count: 0:

const Foo = struct {
    pub fn myFunc(a: u32) void {}
};
comptime {
    // get info for declaring type
    const info = @typeInfo(Foo);
    // get the myFunc decl.  There's only one.
    const decl = info.Struct.decls[0];
    // get the argument names
    const args = decl.data.Fn.arg_names;
    // args is [_][]const u8{ "a" }
    @compileError(std.fmt.comptimePrint("args count: {d}", .{args.len}));
    
}

@Vexu
Copy link
Member

Vexu commented Mar 19, 2022

Declaration.data was removed in #10753.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

6 participants