Support typedefs and function pointers in bindgen - #16847
Conversation
|
One or more of the following people are relevant to this code:
|
| pub fn r#type( | ||
| mut ty: &ir::Type, | ||
| mut override_fn: impl FnMut(&str) -> Option<Primitive>, | ||
| override_fn: &mut dyn FnMut(&str) -> Option<Primitive>, |
There was a problem hiding this comment.
This change is because we're defining the function pointer recursively by calling into r#type -- with the previous version cargo complained about an infinite recursion.
There was a problem hiding this comment.
I don't think we should need dyn here, but I'm not certain:
- we should be able to re-write it to be iterative instead; we're already doing that to handle pointers, which would otherwise be naturally recursive.
- I think we can rewrite it as
&mut impl FnMut(&str) -> Option<Primitive>, and then rely on mutable-reference reborrows to keep the type equal. The initial calls of this function change to&mut |name| { ... }(so you manually reference the immediate-value lambda), but the recursive calls stay as-is.
There was a problem hiding this comment.
I wouldn't really bother with the first bullet point, it's more just a comment that the function already has a natural recursive description, but I wrote it iteratively.
jakelishman
left a comment
There was a problem hiding this comment.
Not an actual review: let's hold off on actually merging this til we've positively decided that we'll actually use it - there's an open question on #16839 about whether we should use (effectively) the same API as #16549, which ends up erasing the function-pointer type from the type system, so doesn't use this handling.
|
FWIW, supporting function pointers in bindgen is something I wanted for #16810 too. I had another branch locally that potentially hit the need for it too. |
|
I have questions about the use of them in that PR too, but yeah, if we use them, we can use this PR to add them no trouble. |
In the recent passmanager in C infrastructure I stumbled upon exposing typedefs and function pointers from Rust to C. This PR adds support to the bindgen crate. There's no explicit tests that I've found but I tested this locally -- with something that is not included in this PR though.
We might end up not needing this for the C passmanager, and I'm fine merging something like this at a later stage. But now that I've had this locally and it's working I wanted to open the PR anyways since it seems very useful to have!
AI/LLM disclosure
Claude added the bit on function pointers, I've tested this locally and went over the changes.