nros_core/codegen_version.rs
1// RFC-0090 / phase-429 — the codegen version, the one token that says whether
2// generated code and this runtime can work together.
3//
4// NO `//!` INNER DOC COMMENTS IN THIS FILE, ON PURPOSE. It is `include!`d
5// verbatim by `nros-build-helpers`, and an inner doc comment inside an
6// `include!` expansion is a hard error (E0753). The module docs live on the
7// `pub mod` in `lib.rs`. Keep this file a dependency-free set of `const`s and
8// `const fn`s: anything else breaks the include.
9//
10// THREE READERS, THREE REASONS — do not "unify" them:
11//
12// * `nros-core` itself, directly.
13// * `nros-build-helpers` (defines the runtime anchors) and `rosidl-codegen`
14// (stamps the emitted artifacts), both by `include!`. Neither can afford a
15// `nros-core` dependency edge for a pair of `const`s: the first is host-only
16// and appears in every tracked leaf lockfile, and the second lives in the
17// separate `packages/cli` workspace, so the edge would add rows to its
18// lockfile too. `include!` costs nothing in the graph, and rustc records the
19// path in the depfile, so editing the constants rebuilds both.
20// * the CLI's guard, by parsing this file as TEXT. It inspects a CONSUMER's
21// tree at run time, where compiling is not available. This is the only
22// parser, and it exists because the other two options do not apply.
23
24/// The codegen version this runtime emits and accepts.
25///
26/// **Bump this deliberately** when the interface between generated code and the
27/// runtime changes: a trait signature generated code implements, a symbol it
28/// defines, a layout rule it obeys. Do NOT bump it for a cosmetic template
29/// edit — that moves the fingerprint, which is a different question (see the
30/// module docs).
31///
32/// Bumping invalidates every generated tree. That is affordable here because
33/// `generated/` is never committed (CLAUDE.md), so regeneration is always
34/// available — and it is exactly why the value must not move on cosmetics.
35///
36/// Gated by `check-codegen-version-surface`, which fails when the surface
37/// generated code names changes and this constant does not.
38pub const NROS_CODEGEN_VERSION: u32 = 21;
39
40/// The oldest codegen version this runtime still accepts.
41///
42/// Equal to [`NROS_CODEGEN_VERSION`] at introduction: no window, because
43/// `generated/` is never committed and regeneration is therefore always
44/// available. Raise the floor only when a real migration needs one, and lower
45/// it never.
46///
47/// Held at 1 while [`NROS_CODEGEN_VERSION`] moved to 2. That surface move is
48/// ADDITIVE plus one relocation, so a tree generated against version 1 still
49/// runs: the C additions are new entry points, the two C++ changes add
50/// `size()`/`empty()` and `std::string` interop to `FixedString`/`HeapString`,
51/// and the four `rcl_ret_t` declarations that left `action.h` / `client.h` /
52/// `parameter.h` / `service.h` were CONSOLIDATED into `nros_generated.h`, which
53/// all four still reach through `rclc/types.h` — the headers say so in a
54/// comment, and keeping that include is what makes `#include <rclc/action.h>`
55/// continue to compile. Nothing a version-1 tree names was withdrawn, so this
56/// is the window the doc above describes rather than a migration.
57///
58/// Still 2 while [`NROS_CODEGEN_VERSION`] moved to 3. That
59/// move is one ADDED runtime entry point, `rclc_cdr_align`, which the
60/// header-only `rclc_cdr_borrow_le_slice_*` now call so an 8-byte borrowed
61/// sequence starts on its stream boundary. A version-2 tree names nothing that
62/// was withdrawn: it compiles against the corrected `rclc/detail/view.h` and picks the
63/// fix up for free, because the view helpers are `static inline` in the header
64/// rather than symbols the generated code defines.
65///
66/// Still 2 while [`NROS_CODEGEN_VERSION`] moved to 4. That move
67/// is a SPELLING change and nothing else: `nros::bind_subscription_sized` — the
68/// one demanded C++ signature that names the node type — now writes its
69/// parameter `::rclcpp::Node&` where it wrote `Node&`, because the class moved
70/// to `rclcpp::` and `nros::Node` became a deprecated alias for it. The two
71/// spellings are ONE TYPE (`std::is_same<rclcpp::Node, nros::Node>` is asserted
72/// by `tests/compile/one_node_type.cpp`), so a version-3 tree — which declares
73/// its node `static ::nros::Node __nros_node_0;` — still compiles and still
74/// links, with a deprecation warning naming its replacement. Nothing it names
75/// was withdrawn, which is the window the doc above describes.
76///
77/// The version moves anyway because the gate is fail-closed on the TEXT of a
78/// demanded declaration and should stay that way: it cannot know that two
79/// spellings are one type, and a gate that tried to would be one that misses a
80/// real rename.
81///
82/// Still 2 while [`NROS_CODEGEN_VERSION`] moved to 5. That
83/// move is ADDITIVE and, in most of its entries, is not a change to the runtime
84/// at all: the rclc preset constructors landed as `static inline` forwarders in
85/// `<nros/{publisher,subscription,service,client,action}.h>`, and naming a type
86/// inside one of those headers is what puts a `c|type|<header>|<name>` row in
87/// the extracted surface — `rclc_message_type_t`, `rcl_node_t`,
88/// `rclc_service_type_t` and `rclc_action_type_t` were already declared in
89/// `nros_generated.h` and still are, reached the same way. The one genuinely
90/// new declaration a demanded name resolves to is
91/// `rclc_service_init_with_qos`, which is an entry point that already existed
92/// and is now also visible from `<rclc/service.h>`. Nothing a version-4 tree
93/// names was withdrawn, so this is the window the doc above describes rather
94/// than a migration.
95///
96/// The version moves anyway for the reason the version-4 paragraph gives: the
97/// gate is fail-closed on the TEXT of a demanded declaration and should stay
98/// that way. A gate that tried to decide which additions are "really" runtime
99/// changes is one that misses a real withdrawal.
100///
101/// Still 2 while [`NROS_CODEGEN_VERSION`] moved to 6. That
102/// move is a WITHDRAWAL, and it is the first one — `nros_core::action` stopped
103/// re-exporting `ActionServer` and `ActionClient`, two "type-level marker"
104/// structs. A version-5 tree still runs, because no generated tree has ever
105/// named them: measured before the deletion, they had ZERO references anywhere
106/// in this repository, and they were UNREACHABLE through the `nros` facade at
107/// all, which re-exports `nros_node`'s live `ActionServer` / `ActionClient`
108/// under exactly those spellings. So the floor stays where it is. The gate is
109/// fail-closed on the extracted surface and cannot know which names nothing
110/// reached, which is the property that makes it worth keeping.
111///
112/// Still 2 while [`NROS_CODEGEN_VERSION`] moved to 7.
113/// ADDITIVE on the surface generated code names, plus one APPEND to a struct
114/// nothing generated declares. The additions are six new C entry points
115/// (`nros_{publisher,subscription}_get_actual_qos` and the four service /
116/// client halves) and two enumerators on each of the four ~~`nros_qos_*_t`~~
117/// enums (`UNKNOWN`, and `SYSTEM_DEFAULT` on three of them), appended so every
118/// existing discriminant keeps its value. The append is `_executor` on
119/// `rcl_subscription_t`, placed after `_opaque` so every existing field
120/// offset is unchanged — it grows the struct, which is why this is a surface
121/// move at all, and a version-6 tree that only names the struct by pointer is
122/// unaffected. Nothing was withdrawn, so the floor stays where it is.
123///
124/// The gate reports the move as two CHANGED types rather than as the four
125/// added prototypes, because a `const struct rcl_service_t *` parameter is a
126/// tagged reference and keys on the same `c|type|<header>|<name>` row as the
127/// struct itself. That is the gate being fail-closed on text, which is the
128/// property the version-4 paragraph above argues for keeping.
129///
130/// Still 2 while [`NROS_CODEGEN_VERSION`] moved to 8. ADDITIVE,
131/// and the first move that adds a TRAIT generated code implements — which is
132/// the trigger the doc above names first. `nros_core::SecNanosecMsg` is the
133/// Rust stand-in for the C++ `template <typename TimeMsgT> Time::to_msg`: Rust
134/// has no structural bound for "has `sec` and `nanosec`", so
135/// `rosidl-codegen` emits `impl nros_core::SecNanosecMsg for <Msg>` for every
136/// generated message with that exact field set, and
137/// `nros_core::Time::to_ros_msg()` returns through it. The surface moved by two
138/// rows, both additions: the trait, and the `pub use time::` re-export that
139/// publishes it.
140///
141/// A version-7 tree still runs: it names nothing that was withdrawn, and the
142/// impl is something a NEWER tree emits rather than something an older one is
143/// missing. What an older tree loses is only the conversion itself —
144/// `t.to_ros_msg()` into one of ITS types will not resolve until it is
145/// regenerated, which is a compile error at the call site on the day someone
146/// writes the call, not a silent mismatch. So the floor stays where it is.
147///
148/// Still 2 while [`NROS_CODEGEN_VERSION`] moved to 9. ADDITIVE:
149/// `nros_serdes::schema::EMPTY_STRUCT_MEMBER`, the name of rosidl's
150/// `uint8 structure_needs_at_least_one_member`, which a version-9 tree names as
151/// the schema of every empty message and whose byte it writes. A version-8 tree
152/// still compiles and runs against this runtime: it names nothing that was
153/// withdrawn. What it keeps is the empty-message defect version 9 fixes for a
154/// regenerated tree, and only for ITS OWN empty messages: they still go out
155/// with no byte (which every stock RMW refuses) and still have no Cyclone
156/// descriptor (`BuildError::EmptySchema`, which says to regenerate). That is
157/// the behaviour the tree already had, not a new mismatch the runtime
158/// introduces, so the floor stays where it is.
159///
160/// RAISED to 10 when [`NROS_CODEGEN_VERSION`] moved to 10, and
161/// this is the first move that needs it. The C++ user API left `nros::` for
162/// `rclcpp::`, and `nros::` was deleted rather than kept as an alias, so every
163/// name a version-9 C++ tree spells — `nros::FixedString<N>`,
164/// `nros::FixedSequence<T, N>`, `nros::HeapString`, `nros::HeapSequence<T>`,
165/// `nros::Span<T>` / `LeSpan<T>` / `StringView`, `nros::bind_subscription_sized`
166/// and `nros::detail::size_bound_dependent_false` — is WITHDRAWN. A version-9
167/// C++ tree does not compile against this runtime, so accepting it would
168/// replace the version mismatch this constant reports with a wall of
169/// unresolved-name errors. Rust and C trees are unaffected by the move, but the
170/// version is one number for all three, so they regenerate too.
171///
172/// Still 10 while [`NROS_CODEGEN_VERSION`] moved to 11. The
173/// surface the gate saw move is `subscription.h`'s `static inline`
174/// `rclc_subscription_init_best_effort`, whose body forwards to
175/// `rclc_subscription_init_with_qos` and lost the two `NULL`s that function no
176/// longer takes. Generated C does not call either: a message header's typed
177/// registration was ~~`rclc_executor_add_subscription_typed_sized`~~, whose
178/// signature is unchanged, and it passes `struct rcl_subscription_t *`, whose
179/// layout shrank but which generated code never dereferences. A version-10
180/// tree therefore still compiles and runs.
181///
182/// RAISED to 12 when [`NROS_CODEGEN_VERSION`] moved to 12. A
183/// generated C service header's `<Srv>_service_init` called
184/// `rclc_service_init_with_qos` with the trampoline and handler in two
185/// positions that function no longer has, because the executor arena owns a
186/// dispatched server's callback and the struct keeps none. The binding moved
187/// to a new generated `<Srv>_executor_add_service` over
188/// `rclc_executor_add_service_raw`. A version-10 or -11 C tree with a service
189/// therefore does not compile against this runtime, so it is refused by
190/// version rather than by a wall of argument-count errors.
191///
192/// RAISED to 13 with [`NROS_CODEGEN_VERSION`]. The C API dropped `nros_`: every
193/// function, type and constant generated C names took its upstream spelling or
194/// `rclc_` (`docs/reference/c-api-rename-map.tsv`), with no forwarders. A
195/// version-12 C tree calls names this runtime no longer defines, so it is
196/// refused by version rather than by a wall of undeclared identifiers. Rust and
197/// C++ trees are unaffected, but the version is one number for all three.
198///
199/// RAISED to 14 with [`NROS_CODEGEN_VERSION`]: messages take rosidl's C names
200/// AND its member shape, and `ROSIDL_GET_*_TYPE_SUPPORT` is adopted as a
201/// pass-through. Generated C
202/// messages take rosidl's names (`std_msgs__msg__String` in
203/// `<std_msgs/msg/string.h>`) and member shape (`rosidl_runtime_c__String`,
204/// `__Sequence`, wired by a generated `__Storage` + `__init_in`); the type
205/// descriptors `rclc_message_type_t` / `rclc_service_type_t` /
206/// `rclc_action_type_t` gained codec slots and the generated
207/// `<pkg>__<sub>__<Name>__nros_type` fills them; the typed helpers
208/// (`<Msg>_publish`, `<Msg>_executor_add_subscription*`, `_subscribe*`) and
209/// ~~`rclc_executor_add_subscription_typed{,_sized}`~~ retired for `rcl_publish`
210/// and rclc's six-argument `rclc_executor_add_subscription_with_context`. A
211/// version-13 C tree names a descriptor layout and entry points this runtime
212/// no longer has, so it is refused by version. Rust and C++ trees are
213/// unaffected, but the version is one number for all three.
214///
215/// Still 14 while [`NROS_CODEGEN_VERSION`] moved to 15. Generated `RosMessage`
216/// impls now state `MAX_SERIALIZED_BOUND`, the type's size bound, so a typed
217/// subscription is sized from its type on every backend. The trait const is
218/// DEFAULTED (`None`), so a version-14 tree still compiles against this runtime
219/// and only keeps the image-wide receive size it had. Nothing it names is gone,
220/// so it is accepted.
221///
222/// Was 14 while [`NROS_CODEGEN_VERSION`] moved to 16. Generated
223/// actions now carry the hash each of their five endpoints is keyed on: Rust
224/// impls state `RosAction::{CANCEL_GOAL_SERVICE,STATUS_MESSAGE}_HASH`, the C
225/// descriptor `rclc_action_type_t` gained a trailing `endpoint_hashes`, and C++
226/// action types gained five `<Endpoint>_HASH` members. All three are ADDITIVE
227/// on the runtime side: the Rust consts default to `ACTION_HASH`, an older C
228/// designated initializer leaves the trailing member NULL (read as "use
229/// `type_hash`"), and `rclcpp::detail::action_endpoint_hashes<A>()` detects the
230/// C++ members. So an older tree still compiles and runs with the single hash
231/// it always had, and is accepted; a NEW tree names a member an older runtime
232/// lacks, which is what the bump refuses.
233///
234/// 17 with [`NROS_CODEGEN_VERSION`]: the C API's headers moved
235/// from `nros/` into `rclc/` (`<rclc/types.h>`, `<rclc/detail/cdr.h>`,
236/// `<rclc/detail/view.h>`, ...), with no forwarding headers left behind. A
237/// version-16 C tree includes `<nros/types.h>`, which this runtime no longer
238/// ships, so it cannot compile and is refused by version instead. Rust and C++
239/// trees are unaffected, but the version is one number for all three.
240///
241/// 18 with [`NROS_CODEGEN_VERSION`]: the same move for C++. The
242/// nros-cpp headers left `nros/` for `rclcpp/`, `rclcpp/detail/`,
243/// `rclcpp_action/` and `rclcpp_lifecycle/`, again with no forwarding headers.
244/// A version-17 C++ tree includes `<nros/heap_string.hpp>`, which this runtime
245/// no longer ships, so it is refused by version rather than failing to compile.
246///
247/// 19, MIN unchanged: a generated C++ message has rosidl's
248/// `operator==`, through the new `<rclcpp/detail/message_equality.hpp>`, and
249/// `rclcpp::FixedSequence` gained `resize`. A version-18 tree still compiles
250/// against this runtime, since nothing it names moved; a version-19 tree
251/// includes a header an 18 runtime does not ship, which is what the bump says.
252///
253/// 20, MIN unchanged: `GoalId`, `GoalStatus` and
254/// `CancelReturnCode` took rclrs's names (`GoalUuid`, `GoalStatusCode`,
255/// `CancelResponseCode`), so the `Serialize`/`Deserialize` impls generated code
256/// relies on are declared on the new names. The old names stay as
257/// `#[deprecated]` type aliases of the same types, so a version-19 tree still
258/// compiles and runs; a version-20 tree names types a 19 runtime does not have.
259///
260/// 21, MIN unchanged: a generated C action header emits rclc's
261/// three user envelopes (`<Action>_SendGoal_Request`, `_GetResult_Response`,
262/// `_FeedbackMessage`) and includes the new `<rclc/detail/uuid.h>` for the
263/// goal id they carry, and every generated C struct is wrapped in a
264/// `NROS_DEFINED_<name>` guard so that header and the generated
265/// `unique_identifier_msgs` one can both define the UUID. A version-20 tree
266/// still compiles: it includes neither the new header nor anything that does.
267/// A version-21 tree includes a header a 20 runtime does not ship.
268///
269/// The range `[NROS_CODEGEN_VERSION_MIN, NROS_CODEGEN_VERSION]` is expressed to
270/// C and C++ as a SET OF DEFINED SYMBOLS rather than as a comparison — see
271/// `nros-build-helpers`' codegen-version anchor — so there is no range check on
272/// that side that could itself be wrong.
273pub const NROS_CODEGEN_VERSION_MIN: u32 = 18;
274
275/// Does `emitted` fall in the range this runtime accepts?
276///
277/// The ONE comparison. The CLI reaches it through `abi_guard`, which does not
278/// re-spell the bounds; the runtime's Rust call site,
279/// ~~`nros_node::codegen_version_check`~~, is gone.
280#[must_use]
281pub const fn accepts(emitted: u32) -> bool {
282 emitted >= NROS_CODEGEN_VERSION_MIN && emitted <= NROS_CODEGEN_VERSION
283}
284
285// The accepted range must be non-empty, checked at COMPILE time because it is a
286// property of two constants. A runtime test would be the wrong tool — and
287// clippy says so (`assertions_on_constants`) in every crate that `include!`s
288// this file, which is how it was found.
289const _: () = assert!(
290 NROS_CODEGEN_VERSION_MIN <= NROS_CODEGEN_VERSION,
291 "the accepted codegen range is empty: nothing could ever be compatible"
292);