Skip to content

Add nullability annotations to AST pointer types#2922

Merged
soutaro merged 2 commits intomasterfrom
claude/add-nullability-annotations-DLZF6
Apr 7, 2026
Merged

Add nullability annotations to AST pointer types#2922
soutaro merged 2 commits intomasterfrom
claude/add-nullability-annotations-DLZF6

Conversation

@soutaro
Copy link
Copy Markdown
Member

@soutaro soutaro commented Apr 7, 2026

Summary

This PR adds Clang nullability annotations (_Nullable and _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_NULLABLE and RBS_NONNULL) in include/rbs/defines.h that expand to Clang's _Nullable and _Nonnull attributes on Clang, and to nothing on other compilers for compatibility

  • Annotated struct fields across all AST node types with appropriate nullability markers:

    • Required pointer fields marked with RBS_NONNULL
    • Optional pointer fields marked with RBS_NULLABLE
    • Examples include class declarations, method definitions, type parameters, and various annotation types
  • Annotated function signatures for all AST constructor and utility functions:

    • Return types annotated (e.g., rbs_node_list_t *RBS_NONNULL)
    • Parameter types annotated (e.g., rbs_allocator_t *RBS_NONNULL)
    • Functions like rbs_hash_find() and rbs_hash_get() marked as returning RBS_NULLABLE since they can return NULL
  • Updated template files (templates/template.rb and templates/include/rbs/ast.h.erb) to generate nullability annotations automatically for constructor parameters based on whether fields are optional or required

Implementation Details

  • The nullability annotations are conditionally compiled using #ifdef __clang__ to ensure compatibility with non-Clang compilers
  • Constructor parameters for pointer types are annotated based on the field's optional status: optional fields use RBS_NULLABLE, required fields use RBS_NONNULL
  • The template system was enhanced to track nullability information for function parameters and generate appropriate annotations

https://claude.ai/code/session_013XvhPzhCAZGQ9qniMY1YQY

@soutaro soutaro added this to the RBS 4.1 milestone Apr 7, 2026
claude added 2 commits April 7, 2026 05:42
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
@soutaro soutaro force-pushed the claude/add-nullability-annotations-DLZF6 branch from f092bd5 to e496517 Compare April 7, 2026 05:42
@soutaro soutaro marked this pull request as ready for review April 7, 2026 13:02
@soutaro soutaro merged commit cf907f5 into master Apr 7, 2026
28 checks passed
@soutaro soutaro deleted the claude/add-nullability-annotations-DLZF6 branch April 7, 2026 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants