Add nullability annotations to AST pointer types#2922
Merged
Conversation
Add RBS_NULLABLE and RBS_NONNULL macros that expand to _Nullable/_Nonnull on Clang and to nothing on other compilers. Annotate all pointer fields in generated AST structs and constructor functions based on their optionality defined in config.yml, and hand-written list/hash data structures in ast.h/ast.c. Also enable -Wnullable-to-nonnull-conversion in extconf.rb so that Clang warns when a nullable pointer is implicitly passed to a nonnull parameter. https://claude.ai/code/session_013XvhPzhCAZGQ9qniMY1YQY
The while loop condition already guarantees next_token is kUSE, so parse_use_directive always sets use_node. The NULL check was unreachable and triggered -Wnonnull with the new nullability annotations. https://claude.ai/code/session_013XvhPzhCAZGQ9qniMY1YQY
f092bd5 to
e496517
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds Clang nullability annotations (
_Nullableand_Nonnull) to pointer types throughout the RBS AST header and implementation files. These annotations help static analysis tools and IDEs better understand which pointers can be NULL and which cannot.Key Changes
Added nullability macros (
RBS_NULLABLEandRBS_NONNULL) ininclude/rbs/defines.hthat expand to Clang's_Nullableand_Nonnullattributes on Clang, and to nothing on other compilers for compatibilityAnnotated struct fields across all AST node types with appropriate nullability markers:
RBS_NONNULLRBS_NULLABLEAnnotated function signatures for all AST constructor and utility functions:
rbs_node_list_t *RBS_NONNULL)rbs_allocator_t *RBS_NONNULL)rbs_hash_find()andrbs_hash_get()marked as returningRBS_NULLABLEsince they can return NULLUpdated template files (
templates/template.rbandtemplates/include/rbs/ast.h.erb) to generate nullability annotations automatically for constructor parameters based on whether fields are optional or requiredImplementation Details
#ifdef __clang__to ensure compatibility with non-Clang compilersRBS_NULLABLE, required fields useRBS_NONNULLhttps://claude.ai/code/session_013XvhPzhCAZGQ9qniMY1YQY