Skip to content

Fix mixed-type addition chain incorrectly treated as pure string concatenation#2337

Merged
lahma merged 2 commits into
mainfrom
copilot/fix-wrong-evaluation-expression
Mar 19, 2026
Merged

Fix mixed-type addition chain incorrectly treated as pure string concatenation#2337
lahma merged 2 commits into
mainfrom
copilot/fix-wrong-evaluation-expression

Conversation

Copilot AI commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

The StringConcatenationExpression optimization introduced in 4.6.0 incorrectly flattened any left-recursive + chain containing a string literal — regardless of position — into a single string concat pass. This broke left-to-right numeric evaluation when numeric operands preceded the string literal.

engine.Evaluate("2.0 + 3.0 + 'm'"); // returned "23m", expected "5m"
engine.Evaluate("1 + 2 + 3 + '4'"); // returned "1234", expected "64"
engine.Evaluate("'m' + 2 + 3");     // returned "m23", still correct
engine.Evaluate("2 + 'm' + 3");     // returned "2m3", still correct

Changes

  • JintBinaryExpression.csTryBuildStringConcatenation now only creates a StringConcatenationExpression when the string literal appears at operand index 0 or 1. A string at index ≥ 2 means the first operation (operands[0] + operands[1]) may be numeric addition, so flattening is unsafe. Adds a defensive operands.Count < 2 guard with an explanatory comment.
  • StringTests.cs — Adds MixedTypeAdditionShouldEvaluateLeftToRight regression test covering the reported case and related variants.
Original prompt

This section details on the original issue you should resolve

<issue_title>Wrong evaluation of expressions with mixed types</issue_title>
<issue_description>This applies to Jint versions 4.6.0 to 4.6.3 running on .NET 8.0 to .NET 10.

Operating system used for testing Win 11.

The following expression is evaluated wrong:

2.0 + 3.0 + 'm' -> '23m' (expected: '5m')

Jint version 4.5.0 worked as expected (same result as in Firefox and Chrome).

Sample code:

			using Engine engine = new Engine((opt) =>
			{
				opt.Culture(new CultureInfo("en-US"));
			});

			JsValue val = engine.Evaluate("2.0 + 3.0 + 'm'");

			Console.WriteLine(val.ToString());
```</issue_description>

## Comments on the Issue (you are @copilot in this section)

<comments>
</comments>

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

….0 + 'm' should be '5m' not '23m')

Co-authored-by: lahma <171892+lahma@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix wrong evaluation of mixed type expressions in Jint Fix mixed-type addition chain incorrectly treated as pure string concatenation Mar 19, 2026
Copilot AI requested a review from lahma March 19, 2026 16:42
@lahma
lahma marked this pull request as ready for review March 19, 2026 16:44
@lahma
lahma enabled auto-merge (squash) March 19, 2026 17:02
@lahma
lahma merged commit 0d203ba into main Mar 19, 2026
7 of 8 checks passed
@lahma
lahma deleted the copilot/fix-wrong-evaluation-expression branch March 19, 2026 17:04
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.

Wrong evaluation of expressions with mixed types

2 participants