Skip to content

Commit 37d4533

Browse files
authored
Merge pull request #15002 from pcw109550/pcw109550/docs-fix-string-literal-conversion-to-bytesXX
Fix docs for string/hex string literal conversion to bytesNN
2 parents 30d7878 + 04b220d commit 37d4533

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

docs/types/conversion.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,14 @@ converted to any fixed-size bytes type:
174174
bytes4 g = 0x0; // fine
175175
176176
String literals and hex string literals can be implicitly converted to fixed-size byte arrays,
177-
if their number of characters matches the size of the bytes type:
177+
if their number of characters is less than or equal to the size of the bytes type:
178178

179179
.. code-block:: solidity
180180
181181
bytes2 a = hex"1234"; // fine
182182
bytes2 b = "xy"; // fine
183-
bytes2 c = hex"12"; // not allowed
184-
bytes2 d = hex"123"; // not allowed
185-
bytes2 e = "x"; // not allowed
183+
bytes2 c = hex"12"; // fine
184+
bytes2 e = "x"; // fine
186185
bytes2 f = "xyz"; // not allowed
187186
188187
.. index:: literal;address
@@ -199,4 +198,4 @@ An ``address a`` can be converted explicitly to ``address payable`` via ``payabl
199198

200199
.. note::
201200
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``.
202-
Starting with in 0.8.0 only conversion from ``uint160`` is allowed.
201+
Starting with 0.8.0 only conversion from ``uint160`` is allowed.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
contract C {
2+
function f() public pure {
3+
bytes1 b1 = hex"";
4+
bytes1 b2 = hex"1234";
5+
bytes2 b3 = hex"12";
6+
bytes2 b4 = hex"1234";
7+
bytes2 b5 = hex"123456";
8+
bytes3 b6 = hex"1234";
9+
bytes3 b7 = hex"123456";
10+
bytes3 b8 = hex"12345678";
11+
bytes4 b9 = hex"123456";
12+
bytes4 b10 = hex"12345678";
13+
bytes4 b11 = hex"1234567890";
14+
}
15+
}
16+
// ----
17+
// TypeError 9574: (72-93): Type literal_string hex"1234" is not implicitly convertible to expected type bytes1. Literal is larger than the type.
18+
// TypeError 9574: (154-177): Type literal_string hex"123456" is not implicitly convertible to expected type bytes2. Literal is larger than the type.
19+
// TypeError 9574: (242-267): Type literal_string hex"12345678" is not implicitly convertible to expected type bytes3. Literal is larger than the type.
20+
// TypeError 9574: (337-365): Type literal_string hex"1234567890" is not implicitly convertible to expected type bytes4. Literal is larger than the type.

0 commit comments

Comments
 (0)