Tests/Docs after "stricter explicit conversion from Literals to Integers"

This commit is contained in:
hrkrshnn
2020-11-03 14:31:44 +01:00
parent a555556559
commit 29e23efc93
27 changed files with 90 additions and 76 deletions
+5 -3
View File
@@ -35,8 +35,10 @@ the compiler notifying you about it.
New Restrictions
================
* Explicit conversions from negative literals and literals larger
than ``type(uint160).max`` to ``address`` are disallowed.
* Explicit conversions from negative literals and literals larger than ``type(uint160).max`` to
``address`` are disallowed. Similarly, explicit conversions between literals and an integer type
``T`` are only allowed if the literal lies between ``type(T).min`` and ``type(T).max``. In
particular, replace usages of ``uint(-1)`` with ``type(uint).max``.
The previous behaviour was likely ambiguous.
@@ -44,4 +46,4 @@ New Restrictions
* The global functions ``log0``, ``log1``, ``log2``, ``log3`` and ``log4`` have been removed.
These are low-level functions that were largely unused. Their behaviour can be accessed from inline assembly.
These are low-level functions that were largely unused. Their behaviour can be accessed from inline assembly.
+4 -4
View File
@@ -126,7 +126,7 @@ custom types without the overhead of external function calls:
::
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.6.0 <0.9.0;
pragma solidity >=0.6.8 <0.9.0;
struct bigint {
uint[] limbs;
@@ -145,7 +145,7 @@ custom types without the overhead of external function calls:
uint a = limb(_a, i);
uint b = limb(_b, i);
r.limbs[i] = a + b + carry;
if (a + b < a || (a + b == uint(-1) && carry > 0))
if (a + b < a || (a + b == type(uint).max && carry > 0))
carry = 1;
else
carry = 0;
@@ -175,7 +175,7 @@ custom types without the overhead of external function calls:
function f() public pure {
bigint memory x = BigInt.fromUint(7);
bigint memory y = BigInt.fromUint(uint(-1));
bigint memory y = BigInt.fromUint(type(uint).max);
bigint memory z = x.add(y);
assert(z.limb(1) > 0);
}
@@ -285,4 +285,4 @@ for any non-view and non-pure function.
This means that the actual code stored on chain for a library
is different from the code reported by the compiler as
``deployedBytecode``.
``deployedBytecode``.
+3 -3
View File
@@ -83,7 +83,7 @@ Let us rewrite the set example from the
It is also possible to extend elementary types in that way::
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0;
pragma solidity >=0.6.8 <0.9.0;
library Search {
function indexOf(uint[] storage self, uint value)
@@ -93,7 +93,7 @@ It is also possible to extend elementary types in that way::
{
for (uint i = 0; i < self.length; i++)
if (self[i] == value) return i;
return uint(-1);
return type(uint).max;
}
}
@@ -108,7 +108,7 @@ It is also possible to extend elementary types in that way::
function replace(uint _old, uint _new) public {
// This performs the library function call
uint index = data.indexOf(_old);
if (index == uint(-1))
if (index == type(uint).max)
data.push(_new);
else
data[index] = _new;
+5
View File
@@ -114,6 +114,11 @@ that is large enough to represent it without truncation::
uint32 b = 1234; // fine
uint16 c = 0x123456; // fails, since it would have to truncate to 0x3456
.. note::
Prior to version 0.8.0, any decimal or hexadecimal number literals could be explicitly
converted to an integer type. From 0.8.0, such explicit conversions are as strict as implicit
conversions, i.e., they are only allowed if the literal fits in the resulting range.
Fixed-Size Byte Arrays
----------------------
+2 -2
View File
@@ -124,7 +124,7 @@ the ``sum`` function iterates over to sum all the values.
::
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.6.0 <0.9.0;
pragma solidity >=0.6.8 <0.9.0;
struct IndexValue { uint keyIndex; uint value; }
struct KeyFlag { uint key; bool deleted; }
@@ -165,7 +165,7 @@ the ``sum`` function iterates over to sum all the values.
}
function iterate_start(itmap storage self) internal view returns (uint keyIndex) {
return iterate_next(self, uint(-1));
return iterate_next(self, type(uint).max);
}
function iterate_valid(itmap storage self, uint keyIndex) internal view returns (bool) {