rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

clang::Cursor should never be the null cursor

Open fitzgen opened this issue 9 years ago • 5 comments

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?

fitzgen avatar Oct 24 '16 22:10 fitzgen

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.

emilio avatar Nov 01 '16 14:11 emilio

Is this change still wanted? It sounds like something that wouldn't be too difficult to do.

ekse avatar Aug 16 '18 04:08 ekse

I think so, yeah.

emilio avatar Aug 16 '18 12:08 emilio

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.

ekse avatar Aug 18 '18 17:08 ekse

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.

pvdrz avatar Sep 15 '22 22:09 pvdrz