Skip to content

[2.x] Check the transforms of types that stop a recursion - #1286

Merged
lorisleiva merged 2 commits into
mainfrom
11-19-check_transforms_of_finite_values
Nov 19, 2026
Merged

lorisleiva merged 2 commits into
mainfrom
11-19-check_transforms_of_finite_values

Conversation

@lorisleiva

Copy link
Copy Markdown
Member

This PR makes getHasFiniteValueVisitor check every part of a type that may stop a recursion, not just its item, so a constant of the type itself hidden in a link's or an option's transforms, a sentinel count or a zero value is no longer mistaken for a finite value. Found while porting the visitor to codama-rs (codama-idl/codama-rs#190).

self = struct { x: link(other) + hiddenPrefix(constant(link(self), …)) }
// Before: hasDefinedTypeFiniteValue → true
// After:  hasDefinedTypeFiniteValue → false

@lorisleiva

Copy link
Copy Markdown
Member Author

@trevor-cortex

@changeset-bot

changeset-bot Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a5a39cb

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 17 packages
Name Type
@codama/visitors-core Patch
@codama/cli Patch
@codama/dynamic-codecs Patch
@codama/dynamic-parsers Patch
@codama/renderers-core Patch
@codama/upgrade Patch
@codama/validators Patch
@codama/visitors Patch
codama Patch
@codama/dynamic-address-resolution Patch
@codama/dynamic-client Patch
@codama/dynamic-instructions Patch
@codama/nodes-from-anchor Patch
@codama/errors Patch
@codama/fragments Patch
@codama/node-types Patch
@codama/nodes Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@trevor-cortex trevor-cortex left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

getHasFiniteValueVisitor previously short-circuited to true on the nodes that can stop a recursion (options, non-fixed collections, unresolvable links, enums) without looking at anything but their item/variants. This PR keeps that logic for the item but additionally visits the parts of those nodes that are present in every value: count, size, prefix, zeroValue and transforms. A constant whose type links back to the defined type being visited (e.g. hiddenPrefixTransformNode([constantValueNode(link(self), …)])) now correctly yields false. Unresolvable links go through next(node) instead of returning true so their transforms are checked too. Nine new negative test cases, one per newly inspected slot, plus a patch changeset for @codama/visitors-core.

Verification notes

I traced the mechanics rather than trusting the description:

  • extendVisitor passes self: this, so visit(node.count, self) etc. go through the outer recordNodeStackVisitor and links inside counts/transforms resolve with the right path. Same mechanism the existing struct-field tests already rely on.
  • The generated mergeVisitor.visitDefinedTypeLink visits program, transforms and plugins, so next(node) on a link does what the comment says. For the resolved case, stack.visitPath(linkedPath, self) && next(node) runs next with the stack back at the link node, which is correct.
  • sentinelCountNode → sentinel → constantValueNode → type reaches the self link; prefixedCountNode → prefix and optionTypeNode.prefix / enumTypeNode.size are number types whose own transforms are traversed by the merge visitor, so those slots are covered without special-casing.
  • Short-circuit order is item-first in every override, so the fixed-array/map paths bail before touching counts/transforms when the item already fails. No double visits: the overrides replace the merge visitor's traversal entirely for those kinds.
  • Changeset: patch on a lockstep core package for a bug fix, entry starts with a verb — matches CONTRIBUTING.md.

Nits (non-blocking)

  • Doc comment, ~L20–21 (unchanged lines): "Links that cannot be resolved count as finite" is now only true of the link itself — an unresolvable link with a self-constant in its transforms returns false (and there's a test for exactly that). Something like "count as finite on their own" would keep the sentence accurate.
  • See inline: two cheap extra test cases would cover the prefix/size/prefixedCount slots the changeset explicitly lists, which currently only get exercised indirectly.

Logic is correct and the tests pin every new branch. LGTM.

Comment on lines +73 to +81
['transforms of links', field(definedTypeLinkNode('missing', { transforms: [prefixOfSelf] }))],
['transforms of options', field(optionTypeNode(self, { prefix: u8, transforms: [prefixOfSelf] }))],
['zero values of zeroable options', field(zeroableOptionTypeNode(u8, { zeroValue: constantOfSelf }))],
['transforms of remainder options', field(remainderOptionTypeNode(u8, { transforms: [prefixOfSelf] }))],
['sentinel counts', arrayTypeNode(u8, sentinelCountNode(constantOfSelf))],
['transforms of prefixed arrays', arrayTypeNode(u8, prefixedCountNode(u8), { transforms: [prefixOfSelf] })],
['transforms of sets', setTypeNode(u8, remainderCountNode(), { transforms: [prefixOfSelf] })],
['transforms of maps', mapTypeNode(u8, u8, prefixedCountNode(u8), { transforms: [prefixOfSelf] })],
['transforms of enums', enumTypeNode([enumVariantTypeNode('nil')], { transforms: [prefixOfSelf] })],

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These cover every transforms, the zeroValue and the sentinel count, but not the three remaining slots the changeset calls out — the option prefix, the enum size, and a prefixedCountNode's prefix. They all reduce to "a number type whose transforms carry a self constant", so one shared helper would do, e.g.:

const u8OfSelf = integerTypeNode('u8', { transforms: [prefixOfSelf] });
// …
['prefixes of options', field(optionTypeNode(u8, { prefix: u8OfSelf }))],
['sizes of enums', enumTypeNode([enumVariantTypeNode('nil')], { size: u8OfSelf })],
['prefixed counts', arrayTypeNode(u8, prefixedCountNode(u8OfSelf))],

Not blocking — I traced the merge visitor and these already pass through visitIntegerType → transforms — but it would make the test list match the contract in the doc comment one-to-one and guard the visit(node.prefix|size|count, self) calls against a future refactor dropping one.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added all three, with the shared u8OfSelf helper. Dropping the prefix or size visit now fails its case. Also narrowed the doc sentence to "count as finite on their own".

@lorisleiva
lorisleiva merged commit 9210c1f into main Nov 19, 2026
5 checks passed
@lorisleiva
lorisleiva deleted the 11-19-check_transforms_of_finite_values branch November 19, 2026 19:10
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