The key point is that node_id is an allocation sequence number
(++parser->node_id), not an identity derived from a node's type or source
location.
Merely reordering node definitions in config.yml does not affect node_id.
Changing the number or order of node allocations does.
The reproduction from note #13 of Feature #21795 changed from 25 to 26 nodes between Prism versions.
This can be bisected to Ensure context terminators terminate expressions:
case 1
in 2
A.print message:
in 3
A.print message:
endBefore the fix, the second in was represented by a MatchPredicateNode
nested inside the first clause. After the fix, it was represented by a second
InNode.
The commit was an ordinary six-line parser fix and did not change
config.yml. Consequently, a node-definition ABI version would not have
detected this change.
Internal support for node_id was added on February 8, 2024. Less than two
weeks later,
ItParametersNode was added.
The unchanged source:
-> { it }gained an extra node. This shifted the LambdaNode and every node allocated
after it.
A month later,
ShareableConstantNode was added:
# shareable_constant_value: literal
A = 1
def target
endThe constant write gained a ShareableConstantNode wrapper before target,
shifting the DefNode ID.
It is also believable that this node could disappear in the future. For example, the shareability mode could be stored as flags directly on the constant-write nodes instead of using a wrapper. The same thing could happen if this behavior became the default.
Shortly afterward,
ConstantPathNode#child was replaced with name and name_loc:
Foo::Bar
def target
endPreviously, Foo::Bar contained a child ConstantReadNode for Bar. After
the change, Bar was stored directly as a name and location on the
ConstantPathNode.
This removed one node allocation, shifting the ID of target down by one.
The commit explicitly described itself as an API-breaking representation
change.
The instability is broader than changes to the final AST. Nodes that are allocated and later discarded still consume IDs.
For example:
A = 1
def target
endcurrently has visible nodes with IDs resembling:
StatementsNode 1
# ID 2: temporary ConstantReadNode, absent from the final tree
IntegerNode 3
ConstantWriteNode 4
DefNode 5
ProgramNode 6
Prism first parses A as a read/target and then converts it into a write. A
reasonable optimization could construct the ConstantWriteNode directly,
eliminating the invisible allocation. That would change every later ID
without changing the final AST or the node definitions at all.
This means an ABI version that changes only when config.yml changes would
not be sufficient.
IDs can also move when the same nodes are allocated in a different order.
For example, DefNode, LambdaNode, and BlockNode are currently allocated
after their bodies have been parsed. A parser refactor could instead allocate
the outer node when its opening token is encountered and fill in its body
later.
Such a refactor would change the ID of exactly the node returned by
Method#ast or Proc#ast, without adding or removing any nodes.
Similarly, a CallNode is commonly allocated before its arguments. Building
the arguments first and the call afterward would change the CallNode ID
relevant to Thread::Backtrace::Location#ast, even if the final AST were
structurally equivalent.
ImplicitNode and ImplicitRestNode are particularly plausible candidates.
Prism describes these as synthetic nodes that do not directly correspond to
source syntax:
{ foo: } # ImplicitNode
foo { |bar,| } # ImplicitRestNode
foo in [bar,] # ImplicitRestNode
foo, = bar # ImplicitRestNodeThese could naturally be replaced with flags or nullable fields on nodes such
as AssocNode, ParametersNode, ArrayPatternNode, or MultiTargetNode.
Other plausible areas include:
-
ShareableConstantNode, which could be folded into constant-write flags. -
ItParametersNodeandNumberedParametersNode, which could be represented as modes on another parameter node. -
Grouping nodes such as
StatementsNode,ArgumentsNode,BlockParametersNode, andKeywordHashNode. -
The many specialized target and write nodes, which could be merged, split, or replaced with flags.
There are historical precedents for these kinds of changes even before
node_id was introduced: Prism split CaseNode and CaseMatchNode, split
CallNode in target position, and replaced
RequiredDestructuredParameterNode with MultiTargetNode.
Stable node_id values require the exact same parser implementation, parser
options, and source—not merely synchronized node definitions.
Normal bug fixes, temporary-node optimizations, representation changes, and allocation-order refactors can all change IDs. Some of these changes do not alter the node ABI or even the shape of the final AST.
Start and end source offsets are materially more robust across Prism versions because they describe actual positions in the source rather than incidental parser allocation order.
I asked Codex to find concrete examples, and it notably happens on the latest Ruby 3.4 release (search for
3.4.10for to find it).This is based on the Ruby versions installed locally, there are probably more cases.
Concrete
Prism.findfailures across Prism versionsPrism.findon CRuby reads anode_idrecorded in an instruction sequence orbacktrace location by CRuby's embedded Prism. It then reparses the source using
the loaded Prism library and looks up that numeric ID.
If those are different Prism versions, an ordinary parser fix can change node
allocation and make the ID identify an unrelated node.
These examples were reproduced with the current checkout reporting
Prism::VERSION == "1.9.0".Results
Prism.findcase/inparser fixmethod(:b)DefNode(:a)it = itparser fixmethod(:a)DefNode(:b)it = itparser fixLocalVariableWriteNodeit = itparser fixLocalVariableWriteNodeit = itparser fixIntegerNodefor0%winterrupted by a heredocmethod(:b)DefNode(:a)The commands below deliberately invoke a specific Ruby rather than relying on
the
rubyselected byPATH. They assume the current Prism extension hasbeen built for Ruby 3.4.
CRuby 3.4.5 / Prism 1.2.0:
case/inSave this as
case_in.rb:Run:
~/.rubies/ruby-3.4.5/bin/ruby -Ilib case_in.rbResult:
The embedded Prism 1.2.0 assigns IDs 28 and 29 to
aandb. Prism 1.9.0assigns IDs 29 and 30 to them. Consequently, the ID 29 recorded for
bselects
aafter reparsing.The allocation changed in
915f6b3ae90aae73bb438e51fb9baa8b6ec0c489,“Ensure context terminators terminate expressions.” The fix makes the second
ina separateInNodeinstead of leaving aMatchPredicateNodenested inthe first clause.
CRuby 3.4.5 / Prism 1.2.0:
it = itSave this as
it_assignment.rb:Run:
~/.rubies/ruby-3.4.5/bin/ruby -Ilib it_assignment.rbResult:
In the minimal method-only form, Prism 1.2.0 assigns IDs 10 and 11 to
aandb; Prism 1.9.0 assigns IDs 9 and 10. Thus, the ID 10 recorded foraselects
b.The same one-node shift makes the lambda's ID select the local-variable write
which contains it. It likewise makes the non-lambda proc's block ID select
its containing local-variable write.
This was caused by
cf3bbf9d2c21fca6c656e83883f9d37f683227b0,“Make
it = itassignnilto match parse.y behavior.” The change removesthe
ItParametersNodethat Prism 1.2.0 allocated for this source, shiftingall subsequent IDs down by one.
Backtrace-location variant
Save this as
backtrace_location.rb:Run:
~/.rubies/ruby-3.4.5/bin/ruby -Ilib backtrace_location.rbResult:
Prism 1.2.0 gives the
caller_locations(0, 1)CallNodeID 11. In Prism1.9.0, ID 11 belongs to the
IntegerNodefor the first argument,0.CRuby 3.4.10 / Prism 1.5.3: heredoc-interrupted
%wSave this as
heredoc_words.rb. The columns are significant: in particular,if,i, the heredoc terminatorA,j], and bothdefkeywords must beginin column zero. Indenting the entire example by two spaces prevents this
specific reproduction.
Run:
~/.rubies/ruby-3.4.10/bin/ruby -Ilib heredoc_words.rbResult:
Prism 1.5.3 assigns IDs 11 and 12 to
aandb. Prism 1.9.0 assigns IDs 12and 13. The ID 12 recorded for
btherefore selectsa.The allocation changed in
211677000ef383faca258515f344700e61a07d83,“Correctly handle line continuations in
%w/i%interrupted by heredocs.”Prism 1.5.3 represents the
%wcontents as twoStringNodes. Prism 1.9.0wraps two string parts in an
InterpolatedStringNode, adding one allocationbefore the method definitions.
What these examples establish
These are not hypothetical node-schema changes. They are failures caused by
released CRuby versions embedding older Prism parsers while
Prism.findreparses with a newer Prism library.
The wrong result can have:
ainstead ofb);LocalVariableWriteNodeinstead ofLambdaNodeorBlockNode); orIntegerNode) rather than the call represented bya backtrace location.
None of the three parser fixes changed the meaning of the later target code.
They only changed allocations earlier in the file, which is enough to make a
numeric
node_idlookup return the wrong node.