Skip to content

Store SubTree literals as strings again; numeric strings work in script arithmetic (#1246) - #1254

Merged
facontidavide merged 2 commits into
masterfrom
fix/1246-subtree-literal-strings
Oct 24, 2026
Merged

facontidavide merged 2 commits into
masterfrom
fix/1246-subtree-literal-strings

Conversation

@facontidavide

Copy link
Copy Markdown
Collaborator

Fixes #1246.

Problem

Since #1121 (first released in 4.9.1), a numeric literal passed to a SubTree, e.g. <SubTree ID="Sub" mode="1"/>, is stored in the child blackboard as int/int64_t/double instead of std::string. That locks the entry type, so:

  • the same value can no longer be read by a std::string port: tree creation fails with "port [mode] was initially created with type [int] and, later type [std::string] was used";
  • a double port reading "1" fails the same way, and custom convertFromString<T> parsing is bypassed;
  • the spelling is lost ("007" becomes 7, "1e3" becomes 1000).

Fix

  • xml_parsing.cpp: literals are stored as strings again, as before 4.9.1. Each port converts the text to its own type.
  • scripting/operators.hpp: the goal of Preserve numeric types for literal subtree port values #1121 (arithmetic on numeric literals in Script) moves into the evaluator. In + - * /, a string operand mixed with a number is converted if the whole string is a number (integer first, then parseDouble with full consumption, the same grammar Preserve numeric types for literal subtree port values #1121 used). "42abc", "true" and " 42" are rejected, so "text" + 1 still throws. String+string, .., comparisons, bitwise and logic operators are unchanged.

The #1121 test SubTree.LiteralNumericPortsPreserveType is kept unchanged and passes.

Behavior change

Compared to 4.9.1 – 4.10.0, these go back to pre-4.9.1 behavior:

  • two literal operands are strings: a + b with a="1" b="2" gives "12", and a < b with a="10" b="2" compares text;
  • int_val += 1, -int_val, int_val & 1, int_val && 1 and a bare <Precondition if="int_val"> on such a literal need an explicit number again.

No API/ABI change: only the inline evaluator in a public header and a .cpp change.

Tests

  • SubTree.LiteralPortsAreReadByAnyPortType: the issue's case. mode="1" is read by string, int and double ports, and code="007" keeps its spelling.
  • ParserTest.NumericStringArithmetic: both operand orders, rejected strings, string+string concatenation unchanged.

Both new tests fail on master and pass with the fix. Full suite: 574 passed.

🤖 Generated with Claude Code

facontidavide and others added 2 commits October 24, 2026 16:55
…ript arithmetic (#1246)

Since #1121 (shipped in 4.9.1), a numeric literal passed to a SubTree port
was stored as int/int64_t/double. That locked the entry type, so the same
value could no longer be read by a std::string port (tree creation failed),
custom convertFromString<T> was bypassed, and the spelling was lost
("007" became 7).

Literals are stored as strings again. The goal of #1121, arithmetic on
numeric literals in Script, is kept by the script evaluator instead: in
+ - * /, a string operand mixed with a number is converted when the whole
string is a number (integer first, then double).

Behavior change compared to 4.9.1 - 4.10.0, back to pre-4.9.1 behavior:
two literal operands are strings ("1" + "2" is "12", "10" < "2" compares
text), and compound assignment, unary minus, bitwise and logic operators
on such a literal require an explicit number again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@facontidavide
facontidavide merged commit 0388ee8 into master Oct 24, 2026
@facontidavide
facontidavide deleted the fix/1246-subtree-literal-strings branch October 24, 2026 15:03
@sonarqubecloud

Copy link
Copy Markdown

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.

Question about automatic promotion of string to int in ports

1 participant