Skip to content

Commit a3ea4e9

Browse files
Warn about invalid strings in arithmetic
Squashed commit of the following: commit 4bc40d9 Author: Andrea Faulds <[email protected]> Date: Sun Mar 20 02:20:28 2016 +0000 Update spec for invalid numeric strings commit 2461e4f Author: Andrea Faulds <[email protected]> Date: Wed Feb 24 16:53:26 2016 +0000 Updated tests for invalid numeric strings
1 parent 0b1a497 commit a3ea4e9

16 files changed

+1929
-771
lines changed

spec/10-expressions.md

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,12 +1528,12 @@ left-hand operand to the power of the right-hand one.
15281528
If either of the operands have an object type supporting `**` operation,
15291529
then the object semantics defines the result. The left operand is checked first.
15301530

1531-
If either or both operands have non-numeric types, their values are converted to type `int`
1532-
or `float`, as appropriate. If both operands have non-negative integer
1533-
values and the result can be represented as an `int`, the result has type
1534-
`int`; otherwise, the result has type `float`.
1535-
1536-
**Examples**
1531+
If either or both operands have non-numeric types, their values are converted
1532+
to type `int` or `float`, as appropriate. If both operands have non-negative
1533+
integer values and the result can be represented as an `int`, the result has
1534+
type `int`; otherwise, the result has type `float`. If either or both operands
1535+
were leading-numeric or non-numeric strings, a non-fatal error must be produced
1536+
for each. **Examples**
15371537

15381538
```PHP
15391539
2**3; // int with value 8
@@ -1752,7 +1752,8 @@ leading-numeric string, the string is first converted to an `int` or
17521752
`float`, as appropriate, after which it is handled as an arithmetic
17531753
operand. The trailing non-numeric characters in leading-numeric strings
17541754
are ignored. With a non-numeric string, the result has type `int` and
1755-
value 0.
1755+
value 0. If the string was leading-numeric or non-numeric, a non-fatal error
1756+
MUST be produced.
17561757

17571758
For a unary `~` operator used with a string, the result is the string with each byte
17581759
being bitwise complement of the corresponding byte of the source string.
@@ -2080,28 +2081,33 @@ The right-hand operand of operator `/` and operator `%` must not be zero.
20802081
If either of the operands is an object supporting the operation, the result is
20812082
defined by that object's semantics, with the left operand checked first.
20822083

2083-
The binary `*` operator produces the product of its operands. If either
2084-
or both operands have non-numeric types, their values are converted to
2085-
type `int` or `float`, as appropriate. Then if either operand has type
2086-
`float`, the other is converted to that type, and the result has type
2087-
`float`. Otherwise, both operands have type `int`, in which case, if the
2088-
resulting value can be represented in type `int` that is the result type.
2089-
Otherwise, the result would have type `float`.
2084+
The binary `*` operator produces the product of its operands. If either or both
2085+
operands have non-numeric types, their values are converted to type `int` or
2086+
`float`, as appropriate. If either or both operands were leading-numeric or
2087+
non-numeric strings, a non-fatal error MUST be produced for each. Then if
2088+
either operand has type `float`, the other is converted to that type, and the
2089+
result has type `float`. Otherwise, both operands have type `int`, in which
2090+
case, if the resulting value can be represented in type `int` that is the
2091+
result type. Otherwise, the result would have type `float`.
20902092

20912093
Division by zero results in a non-fatal error. If the value of the numerator is positive, the result value is `INF`. If the value of the numerator is negative, the result value is `-INF`. If the value of the numerator is zero, the result value is `NAN`.
20922094

20932095
The binary `/` operator produces the quotient from dividing the left-hand
2094-
operand by the right-hand one. If either or both operands have
2095-
non-numeric types, their values are converted to type `int` or `float`, as
2096-
appropriate. Then if either operand has type `float`, the other is
2097-
converted to that type, and the result has type `float`. Otherwise, both
2098-
operands have type `int`, in which case, if the mathematical value of the
2099-
computation can be preserved using type `int`, that is the result type;
2100-
otherwise, the type of the result is `float`.
2096+
operand by the right-hand one. If either or both operands have non-numeric
2097+
types, their values are converted to type `int` or `float`, as appropriate. If
2098+
either or both operands were leading-numeric or non-numeric strings, a
2099+
non-fatal error must be produced for each. Then if either operand has type
2100+
`float`, the other is converted to that type, and the result has type `float`.
2101+
Otherwise, both operands have type `int`, in which case, if the mathematical
2102+
value of the computation can be preserved using type `int`, that is the result
2103+
type; otherwise, the type of the result is `float`.
21012104

21022105
The binary `%` operator produces the remainder from dividing the left-hand
2103-
operand by the right-hand one. If the type of both operands is not `int`,
2104-
their values are converted to that type. The result has type `int`. If the right-hand operand has value zero, an exception of type `DivisionByZeroError` is thrown.
2106+
operand by the right-hand one. If the type of both operands is not `int`, their
2107+
values are converted to that type. If either or both operands were
2108+
leading-numeric or non-numeric strings, a non-fatal error MUST be produced for
2109+
each. The result has type `int`. If the right-hand operand has value zero, an
2110+
exception of type `DivisionByZeroError` is thrown.
21052111

21062112
These operators associate left-to-right.
21072113

@@ -2150,15 +2156,15 @@ If either of the operands is an object supporting the operation, the result is
21502156
defined by that object's semantics, with the left operand checked first.
21512157

21522158
For non-array operands, the binary `+` operator produces the sum of those
2153-
operands, while the binary `-` operator produces the difference of its
2154-
operands when subtracting the right-hand operand from the left-hand one.
2155-
If either or both operands have non-array, non-numeric types, their
2156-
values are converted to type `int` or `float`, as appropriate. Then if
2157-
either operand has type `float`, the other is converted to that type, and
2158-
the result has type `float`. Otherwise, both operands have type `int`, in
2159-
which case, if the resulting value can be represented in type `int` that
2160-
is the result type.
2161-
Otherwise, the result would have type `float`.
2159+
operands, while the binary `-` operator produces the difference of its operands
2160+
when subtracting the right-hand operand from the left-hand one. If either or
2161+
both operands have non-array, non-numeric types, their values are converted to
2162+
type `int` or `float`, as appropriate. If either or both operands were
2163+
leading-numeric or non-numeric strings, a non-fatal error MUST be produced for
2164+
each. Then if either operand has type `float`, the other is converted to that
2165+
type, and the result has type `float`. Otherwise, both operands have type
2166+
`int`, in which case, if the resulting value can be represented in type `int`
2167+
that is the result type. Otherwise, the result would have type `float`.
21622168

21632169
If both operands have array type, the binary `+` operator produces a new
21642170
array that is the union of the two operands. The result is a copy of the
@@ -2226,8 +2232,9 @@ zero bits are shifted on from the right end. Given the expression
22262232
`e2` positions. Bits shifted off the right end are discarded, and the sign
22272233
bit is propagated from the left end.
22282234

2229-
If either operand does not have type `int`, its value is first converted
2230-
to that type.
2235+
If either operand does not have type `int`, its value is first converted to
2236+
that type. If either or both operands were leading-numeric or non-numeric
2237+
strings, a non-fatal error MUST be produced for each.
22312238

22322239
The type of the result is `int`, and the value of the result is that after
22332240
the shifting is complete. The values of `e1` and `e2` are unchanged.
@@ -2465,8 +2472,9 @@ Each of the operands must have scalar-compatible type.
24652472
If either of the operands is an object supporting the operation, the result is
24662473
defined by that object's semantics, with the left operand checked first.
24672474

2468-
If either operand does not have type `int`, its value is first converted
2469-
to that type.
2475+
If either operand does not have type `int`, its value is first converted to
2476+
that type. If either or both operands were leading-numeric or non-numeric
2477+
strings, a non-fatal error MUST be produced for each.
24702478

24712479
The result of this operator is the bitwise-AND of the two operands, and
24722480
the type of that result is `int`.
@@ -2509,8 +2517,9 @@ Each of the operands must have scalar-compatible type.
25092517
If either of the operands is an object supporting the operation, the result is
25102518
defined by that object's semantics, with the left operand checked first.
25112519

2512-
If either operand does not have type `int`, its value is first converted
2513-
to that type.
2520+
If either operand does not have type `int`, its value is first converted to
2521+
that type. If either or both operands were leading-numeric or non-numeric
2522+
strings, a non-fatal error MUST be produced for each.
25142523

25152524
The result of this operator is the bitwise exclusive-OR of the two
25162525
operands, and the type of that result is `int`.
@@ -2555,8 +2564,9 @@ Each of the operands must have scalar-compatible type.
25552564
If either of the operands is an object supporting the operation, the result is
25562565
defined by that object's semantics, with the left operand checked first.
25572566

2558-
If either operand does not have type `int`, its value is first converted
2559-
to that type.
2567+
If either operand does not have type `int`, its value is first converted to
2568+
that type. If either or both operands were leading-numeric or non-numeric
2569+
strings, a non-fatal error MUST be produced for each.
25602570

25612571
The result of this operator is the bitwise inclusive-OR of the two
25622572
operands, and the type of that result is `int`.

0 commit comments

Comments
 (0)