rust-bindgen
rust-bindgen copied to clipboard
clang::Cursor should never be the null cursor
Right now we have various methods on clang::Cursor that return Options just because (I think?) we call underlying clang functions that return null if the cursor is the "null cursor" (see clang_getNullCursor and clang_isNull).
It would be better if these methods did not return Option and always returned a value, if clang::Cursor was never the null cursor, and the place(s) where we first get a cursor return an Option<Cursor> to handle the null cursor case. If we adopt these invariants, we should also assert them throught clang::Cursor methods.
What do you think @emilio?
This sounds good, ideally we could try to never have a null/invalid cursor around, and we'd constrain their existence to the clang module. It's not a super-easy task though.
Is this change still wanted? It sounds like something that wouldn't be too difficult to do.
I think so, yeah.
I did some tests by adding debug_assert!(ret.is_valid()) at the end of methods that are returning a Cursor and running the test suite. The Cursor methods canonical() and declaration() currently can return an invalid cursor and should return an Option<Cursor> instead.
Since this would need a fair number of changes I wanted to confirm with you that this is the right way to go.
I'd consider this as something important so we don't end up with garbage down the road by accident. However, I wonder if a transition to clang-rs would fix this.