Skip to content

Commit

Permalink
Fix string/hex string literal implicit conversion to bytesNN
Browse files Browse the repository at this point in the history
  • Loading branch information
pcw109550 committed Apr 17, 2024
1 parent 3d7b3d9 commit 41f8bb3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
8 changes: 4 additions & 4 deletions docs/types/conversion.rst
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,15 @@ converted to any fixed-size bytes type:
bytes4 g = 0x0; // fine
String literals and hex string literals can be implicitly converted to fixed-size byte arrays,
if their number of characters matches the size of the bytes type:
if their number of characters is less than or equal to the size of the bytes type:

.. code-block:: solidity
bytes2 a = hex"1234"; // fine
bytes2 b = "xy"; // fine
bytes2 c = hex"12"; // not allowed
bytes2 c = hex"12"; // fine
bytes2 d = hex"123"; // not allowed
bytes2 e = "x"; // not allowed
bytes2 e = "x"; // fine
bytes2 f = "xyz"; // not allowed
.. index:: literal;address
Expand All @@ -199,4 +199,4 @@ An ``address a`` can be converted explicitly to ``address payable`` via ``payabl

.. note::
Prior to version 0.8.0, it was possible to explicitly convert from any integer type (of any size, signed or unsigned) to ``address`` or ``address payable``.
Starting with in 0.8.0 only conversion from ``uint160`` is allowed.
Starting with 0.8.0 only conversion from ``uint160`` is allowed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
contract C {
function f() public pure {
bytes1 b1 = hex"";
bytes1 b2 = hex"1234";
bytes2 b3 = hex"12";
bytes2 b4 = hex"1234";
bytes2 b5 = hex"123456";
bytes3 b6 = hex"1234";
bytes3 b7 = hex"123456";
bytes3 b8 = hex"12345678";
bytes4 b9 = hex"123456";
bytes4 b10 = hex"12345678";
bytes4 b11 = hex"1234567890";
}
}
// ----
// TypeError 9574: (72-93): Type literal_string hex"1234" is not implicitly convertible to expected type bytes1. Literal is larger than the type.
// TypeError 9574: (154-177): Type literal_string hex"123456" is not implicitly convertible to expected type bytes2. Literal is larger than the type.
// TypeError 9574: (242-267): Type literal_string hex"12345678" is not implicitly convertible to expected type bytes3. Literal is larger than the type.
// TypeError 9574: (337-365): Type literal_string hex"1234567890" is not implicitly convertible to expected type bytes4. Literal is larger than the type.

0 comments on commit 41f8bb3

Please sign in to comment.