refactor(ast): use NonNull for ref casting#24389
Conversation
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. This stack of pull requests is managed by Graphite. Learn more about stacking. |
There was a problem hiding this comment.
Pull request overview
Refactors the AstKind::ty discriminant read to use std::ptr::NonNull when casting a &AstKind to &AstType, encoding the non-null guarantee in the pointer type rather than relying on as_ref_unchecked on a raw pointer. This aligns with the post-#24381 direction of making unsafe ref-casts more structurally sound.
Changes:
- Update the
AstKindgenerator to emitNonNull::from_ref(self).cast::<AstType>().as_ref()for thety()fast-path. - Regenerate
crates/oxc_ast/src/generated/ast_kind.rsto apply the same change in the produced code.
Reviewed changes
Copilot reviewed 1 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| tasks/ast_tools/src/generators/ast_kind.rs | Switch generator output from ptr::from_ref(...).as_ref_unchecked() to NonNull::from_ref(...).as_ref() for the AstKind::ty discriminant read. |
| crates/oxc_ast/src/generated/ast_kind.rs | Apply the regenerated NonNull-based cast in AstKind::ty and update imports accordingly. |
Merge activity
|
Merging this PR will not alter performance
Comparing Footnotes
|
Follow-on after #24381. When casting a `&` reference, it's preferable to use `NonNull` - that safely gains the "pointer is not null" fact, rather than unsafely promising it with `as_ref_unchecked` on a raw pointer.
4aaaccc to
347c61b
Compare

Follow-on after #24381. When casting a
&reference, it's preferable to useNonNull- that safely gains the "pointer is not null" fact, rather than unsafely promising it withas_ref_uncheckedon a raw pointer.