Skip to content

fix(core): numeric string arithmetic coercion#432

Draft
Guikingone wants to merge 1 commit into
illegalstudio:mainfrom
Guikingone:fix/362-numeric-string-arithmetic
Draft

fix(core): numeric string arithmetic coercion#432
Guikingone wants to merge 1 commit into
illegalstudio:mainfrom
Guikingone:fix/362-numeric-string-arithmetic

Conversation

@Guikingone

@Guikingone Guikingone commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

Allow Str operands in arithmetic (+, -, *, /, %, **) by treating them as numeric at runtime, matching PHP semantics. Pure integer-form strings coerce to int; float-form strings (containing a dot or exponent) coerce to float. Leading-numeric strings emit a compile-time warning and use the numeric prefix.

Type checker: accept Str in is_numeric_operand_type, produce Mixed result type when either operand is Str, and warn for constant non-numeric strings. Lowering: coerce Str to float for Div via StrToF, route Add/Sub/Mul through MixedNumericBinop with Str operands.

Runtime: new __rt_str_is_float_form helper (ARM64 + x86_64) parses the string with strtoll + strtod and reports whether strtod consumed more bytes (float-form). MixedNumericBinop now checks string operands for float-form on both targets and routes to the float path when needed, fixing "1.5" + 3 producing int(4) instead of float(4.5).

9 regression tests added. Docs and arithmetic example updated.

Fix #362

@Guikingone Guikingone changed the title fix: numeric string arithmetic coercion (#362) fix(core): numeric string arithmetic coercion Jun 29, 2026
@Guikingone
Guikingone force-pushed the fix/362-numeric-string-arithmetic branch from 494c6d7 to b2f4166 Compare July 2, 2026 16:15
Allow Str operands in arithmetic (+, -, *, /, %, **) by treating them as
numeric at runtime, matching PHP semantics. Pure integer-form strings coerce
to int; float-form strings (containing a dot or exponent) coerce to float.
Leading-numeric strings emit a compile-time warning and use the numeric prefix.

Type checker: accept Str in is_numeric_operand_type, produce Mixed result
type when either operand is Str, and warn for constant non-numeric strings.
Lowering: coerce Str to float for Div via StrToF, route Add/Sub/Mul through
MixedNumericBinop with Str operands.

Runtime: new __rt_str_is_float_form helper (ARM64 + x86_64) parses the string
with strtoll + strtod and reports whether strtod consumed more bytes (float-form).
MixedNumericBinop now checks string operands for float-form on both targets and
routes to the float path when needed, fixing "1.5" + 3 producing int(4) instead
of float(4.5).

9 regression tests added. Docs and arithmetic example updated.
@Guikingone
Guikingone force-pushed the fix/362-numeric-string-arithmetic branch from b2f4166 to a2a5489 Compare July 2, 2026 17:05
@github-actions github-actions Bot added area:codegen Touches target-aware assembly or backend lowering. area:runtime Touches runtime helpers, GC, ownership, or bridge runtimes. area:types Touches type checking, inference, or compatibility. scope:multi-area Touches more compiler areas than the automatic area-label cap. size:m Medium-sized pull request. type:fix Corrects broken or incompatible behavior. labels Jul 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:codegen Touches target-aware assembly or backend lowering. area:runtime Touches runtime helpers, GC, ownership, or bridge runtimes. area:types Touches type checking, inference, or compatibility. scope:multi-area Touches more compiler areas than the automatic area-label cap. size:m Medium-sized pull request. type:fix Corrects broken or incompatible behavior.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Numeric string arithmetic is rejected instead of following PHP coercion

1 participant