lucifer1004 opened a new pull request, #824:
URL: https://github.com/apache/tvm-ffi/pull/824
## Why
`#[derive(Object)]` without `#[type_index]` resolves its type key with
`TVMFFITypeKeyToIndex` and panics when nothing has registered the key. A Rust
crate therefore cannot define an object type of its own: someone else, usually
C++, has to register the key first. The tests work around this.
`test_match_any_ast_lookup.rs` declares `TVMFFITypeGetOrAllocIndex` itself and
registers its types by hand before they are used.
C++ does not have this gap. `TVM_FFI_DECLARE_OBJECT_INFO` calls
`TVMFFITypeGetOrAllocIndex` with the parent's index, the type depth and the
child slots. That returns the index the key already has, or allocates one under
the parent.
## What
- **Derive:** `#[derive(Object)]` without `#[type_index]` now calls
`TVMFFITypeGetOrAllocIndex` on first use of `type_index()`. It passes the
parent's `type_index()` (which registers the parent first if needed),
`TYPE_DEPTH` and the child slots. Keys that are already registered keep their
index, so existing types behave as before.
- **New attributes** `#[type_child_slots = N]` and
`#[type_child_slots_can_overflow = bool]` mirror C++ `_type_child_slots` and
`_type_child_slots_can_overflow`, with the same defaults of 0 and true. A
`#[type_final]` type cannot reserve child slots.
- **tvm-ffi-sys** binds `TVMFFITypeGetOrAllocIndex`.
- **Test cleanup:** `test_match_any_ast_lookup.rs` drops its own `extern
"C"` declaration and registers its types through the derive attributes.
## Tests
- New `tests/test_object_registration.rs`:
- A base type and a final leaf, with keys nothing else registers, are
registered on first use. Using the leaf first registers the parent before it.
- Their indices match `TVMFFITypeKeyToIndex`.
- The leaf takes the first of the parent's reserved child slots, and its
type info records the parent as its ancestor.
- An object of the leaf type round-trips through `Any` and casts to its
parent and back.
- `test_match_any_ast_lookup.rs` passes with its types registered through
the derive.
- `test_cast.rs`, whose keys come from `libtvm_ffi_testing`, still passes,
so existing keys keep their indices.
- `cargo test` in `rust/` passes: 26 suites.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]