Repository navigation
Tidy the validators and fail loudly on non-instruction paths - #206
Conversation
trevor-cortex
left a comment
There was a problem hiding this comment.
Summary
Pre-rc.0 tidy of codama-validators and codama-visitors-core:
validation_error_itemcentralises the "error item with itsCodamaErroras cause" construction that was duplicated acrosscontextual_value_items.rsand the visitor.get_node_itemsis split: a genericget_missing_identifier_itemdriven byNodeRef::identifier()(with anames_another_nodeshort-circuit so a link/value with an empty identifier is reported once rather than also as "missing X named """), thenget_node_kind_itemssorted alphabetically by kind with instructions and programs pulled out intoget_instruction_items/get_program_items.NodePath::expect_last::<T>()replaces the ad-hoclet Some(NodeRef::X(..)) = path.last() else …patterns, andget_instruction_data_fields/get_resolved_instruction_inputsnow fail withUnexpectedNodeKindon non-instruction paths instead of returning an empty vec.
I read the whole visitor post-refactor and compared it branch by branch against the old match: no check was dropped, and the ordering of items for the kinds that previously combined identifier + kind-specific checks (DefinedType, Error, Program) is preserved. The one behavioural shift — "Instruction account has no identifier." now reported when the account itself is visited rather than from the parent instruction — is covered by reports_instruction_accounts_without_identifiers_against_the_account and is the better home for it anyway.
The error-propagation path checks out too: expect_last returns CodamaResult, and ? in get_resolved_instruction_inputs goes through the existing From<CodamaError> for ResolveInstructionInputsError, so the validator's Err(cause) => validation_error_item(cause, …) arm handles it without changes.
Things to watch
- Change file. The diff has no
.changeset/entry, and this touches published crates with user-visible changes:get_instruction_data_fieldsnow returnsCodamaResult<Vec<_>>,get_resolved_instruction_inputserrs where it used to returnOk(vec![]), and a number of new/renamed validation messages. PerCONTRIBUTING.mdthe Knope Bot check will want one (or theno changeloglabel if you consider this folded into an existing rc.0 entry). Just flagging so it isn't a surprise at merge time. - The generic fallback arm in
get_missing_identifier_item("Node of kind [{}] has no identifier.") means any future node kind that grows anidentifierfield is reported automatically, just with a less polished message. That's a sensible safety net; worth remembering when new node kinds land so the hand-written message list stays in step. AccountDataValueNodekeeps its account identifier underaccountrather thanidentifier, so it isn't covered by the new helper and an empty one still surfaces throughget_contextual_value_itemsas "missing account named """. Pre-existing and arguably correct since the field isn't calledidentifier, so no change requested — just noting the one asymmetry with the "every node kind with an identifier" framing.
Notes for other reviewers
Nothing needs extra scrutiny beyond the changeset question. The validation_items_visitor.rs diff looks big (+220/-189) but is almost entirely a reorder; reading the file at HEAD rather than the diff is the quicker way to verify it.
| /// recorded node. Ranges are not checked, so `256` is a value of a `u8`. Injected and constant values | ||
| /// are not concrete, so they are never values of a type here. Numbers accept integers and floats also | ||
| /// accept floats, a struct value must provide every field without a default value, and an enum value | ||
| /// are not concrete, so they are never values of a type here. Number types accept integer values and |
There was a problem hiding this comment.
Nit: the rewording is clearer, but line 11 now runs to ~125 chars while the rest of the doc block is wrapped at 100. rustfmt won't rewrap comments, so this one needs a manual re-flow if you want it to match.
trevor-cortex
left a comment
There was a problem hiding this comment.
Re-review
Three changes since my last pass, all good:
get_missing_identifier_itemnow special-casesAccountDataValueNode(its identifier lives underaccount), reporting "Account data value has no account." and short-circuiting vianames_another_nodeso it isn't also reported as "missing account named """. That closes the one asymmetry I noted; the PR's "every node kind with an identifier" framing now holds without exception.- Two new tests cover the generic helper (
reports_every_node_without_an_identifieron a PDA + variable seed) and the single-report behaviour for links/values naming another node, including theAccountDataValuecase. Paths in the assertions match where the items are now emitted. - The
is_value_node_of_typedoc block is re-wrapped at 100 and reads better than the previous version ("Number types accept integer values and float types also accept float values").
Re-read the full visitor at HEAD once more: still a pure reorder plus the above, nothing dropped.
Still no change file in the diff — same note as before, the Knope check will want one or the no changelog label. Not blocking on it.
This PR tidies
codama-validatorsandcodama-visitors-corebefore rc.0. Error items are built in one place,get_node_itemsis sorted by node kind with instructions and programs in their own helpers, every node kind with an identifier now reports an empty one, and the instruction input helpers fail on paths that don't end with an instruction instead of returning nothing.