This section highlights the main breaking changes introduced in Solidity version 0.5.0, along with the reasoning behind the changes and how to update affected code. For the full list check the release changelog.
Note
Contracts compiled with Solidity v0.5.0 can still interface with contracts and even libraries compiled with older versions without recompiling or redeploying them. Changing the interfaces to include data locations and visibility and mutability specifiers suffices.
This section lists the changes that are semantic-only, thus potentially hiding new and different behavior in existing code.
- Signed right shift now uses proper arithmetic shift, i.e. rounding towards negative infinity, instead of rounding towards zero. Signed and unsigned shift will have dedicated opcodes in Constantinople, and are emulated by Solidity for the moment.
- The
continuestatement in ado...whileloop now jumps to the condition, which is the common behavior in such cases. It used to jump to the loop body. Thus, if the condition is false, the loop terminates. - The functions
.call(),.delegatecall()and.staticcall()do not pad anymore when given a singlebytesparameter. - Pure and view functions are now called using the opcode
STATICCALLinstead ofCALLif the EVM version is Byzantium or later. This disallows state changes on the EVM level. - The ABI encoder now properly pads byte arrays and strings from calldata
(
msg.dataand external function parameters) when used in external function calls and inabi.encode. For unpadded encoding, useabi.encodePacked. - The ABI decoder reverts in the beginning of functions and in
abi.decode()if passed calldata is too short or points out of bounds. Note that dirty higher order bits are still simply ignored. - Forward all available gas with external function calls starting from Tangerine Whistle.
This section highlights changes that affect syntax and semantics.
- The functions
.call(),.delegatecall()`, ``staticcall(),keccak256(),sha256()andripemd160()now accept only a singlebytesargument. Moreover, the argument is not padded. This was changed to make more explicit and clear how the arguments are concatenated. Change every.call()(and family) to a.call("")and every.call(signature, a, b, c)to use.call(abi.encodeWithSignature(signature, a, b, c))(the last one only works for value types). Change everykeccak256(a, b, c)tokeccak256(abi.encodePacked(a, b, c)). Even though it is not a breaking change, it is suggested that developers changex.call(bytes4(keccak256("f(uint256)"), a, b)tox.call(abi.encodeWithSignature("f(uint256)", a, b)). - Functions
.call(),.delegatecall()and.staticcall()now return(bool, bytes memory)to provide access to the return data. Changebool success = otherContract.call("f")to(bool success, bytes memory data) = otherContract.call("f"). - Solidity now implements C99-style scoping rules for function local
variables, that is, variables can only be used after they have been
declared and only in the same or nested scopes. Variables declared in the
initialization block of a
forloop are valid at any point inside the loop.
This section lists changes where the code now needs to be more explicit. For most of the topics the compiler will provide suggestions.
- Explicit function visibility is now mandatory. Add
publicto every function and constructor, andexternalto every fallback or interface function that does not specify its visibility already. - Explicit data location for all variables of struct, array or mapping types is
now mandatory. This is also applied to function parameters and return
variables. For example, change
uint[] x = m_xtouint[] storage x = m_x, andfunction f(uint[][] x)tofunction f(uint[][] memory x)wherememoryis the data location and might be replaced bystorageorcalldataaccordingly. Note thatexternalfunctions require parameters with a data location ofcalldata. - Contract types do not include
addressmembers anymore in order to separate the namespaces. Therefore, it is now necessary to explicitly convert values of contract type to addresses before using anaddressmember. Example: ifcis a contract, changec.transfer(...)toaddress(c).transfer(...), andc.balancetoaddress(c).balance. - The
addresstype was split intoaddressandaddress payable, where onlyaddress payableprovides thetransferfunction. Anaddress payablecan be directly converted to anaddress, but the other way around is not allowed. Convertingaddresstoaddress payableis possible via conversion throughuint160. Ifcis a contract,address(c)results inaddress payableonly ifchas a payable fallback function. If you use the :ref:`withdraw pattern<withdrawal_pattern>`, you most likely do not have to change your code becausetransferis only used onmsg.senderinstead of stored addresses andmsg.senderis anaddress payable. - Conversions between
bytesXanduintYof different size are now disallowed due tobytesXpadding on the right anduintYpadding on the left which may cause unexpected conversion results. The size must now be adjusted within the type before the conversion. For example, you can convert abytes4(4 bytes) to auint64(8 bytes) by first converting thebytes4variable tobytes8and then touint64. You get the opposite padding when converting throughuint32. - Using
msg.valuein non-payable functions (or introducing it via a modifier) is disallowed as a security feature. Turn the function intopayableor create a new internal function for the program logic that usesmsg.value. - For clarity reasons, the command line interface now requires
-if the standard input is used as source.
This section lists changes that deprecate prior features or syntax. Note that
many of these changes were already enabled in the experimental mode
v0.5.0.
- The command line option
--formal(used to generate Why3 output for further formal verification) was deprecated and is now removed. A new formal verification module, the SMTChecker, is enabled viapragma experimental SMTChecker;. - The command line option
--juliawas renamed to--yuldue to the renaming of the intermediate languageJuliatoYul. - The
--clone-binand--combined-json clone-bincommand line options were removed. - Remappings with empty prefix are disallowed.
- The JSON AST fields
constantandpayablewere removed. The information is now present in thestateMutabilityfield.
- Constructors must now be defined using the
constructorkeyword. - Calling base constructors without parentheses is now disallowed.
- Specifying base constructor arguments multiple times in the same inheritance hierarchy is now disallowed.
- Calling a constructor with arguments but with wrong argument count is now disallowed. If you only want to specify an inheritance relation without giving arguments, do not provide parentheses at all.
- Function
callcodeis now disallowed (in favor ofdelegatecall). It is still possible to use it via inline assembly. suicideis now disallowed (in favor ofselfdestruct).sha3is now disallowed (in favor ofkeccak256).throwis now disallowed (in favor ofrevert,requireandassert).
- Explicit and implicit conversions from decimal literals to
bytesXXtypes is now disallowed. - Explicit and implicit conversions from hex literals to
bytesXXtypes of different size is now disallowed.
- The unit denomination
yearsis now disallowed due to complications and confusions about leap years. - Trailing dots that are not followed by a number are now disallowed.
- Combining hex numbers with unit denominations (e.g.
0x1e wei) is now disallowed. - The prefix
0Xfor hex numbers is disallowed, only0xis possible.
- Declaring empty structs is now disallowed for clarity.
- The
varkeyword is now disallowed to favor explicitness. - Assignments between tuples with different number of components is now disallowed.
- Values for constants that are not compile-time constants are disallowed.
- Multi-variable declarations with mismatching number of values are now disallowed.
- Uninitialized storage variables are now disallowed.
- Empty tuple components are now disallowed.
- Detecting cyclic dependencies in variables and structs is limited in recursion to 256.
- Fixed-size arrays with a length of zero are now disallowed.
- Using
constantas function state mutability modifier is now disallowed. - Boolean expressions cannot use arithmetic operations.
- The unary
+operator is now disallowed. - Literals cannot anymore be used with
abi.encodePackedwithout prior conversion to an explicit type. - Empty return statements for functions with one or more return values are now disallowed.
- The "loose assembly" syntax is now disallowed entirely, that is, jump labels,
jumps and non-functional instructions cannot be used anymore. Use the new
while,switchandifconstructs instead. - Functions without implementation cannot use modifiers anymore.
- Function types with named return values are now disallowed.
- Single statement variable declarations inside if/while/for bodies that are not blocks are now disallowed.
- New keywords:
calldataandconstructor. - New reserved keywords:
alias,apply,auto,copyof,define,immutable,implements,macro,mutable,override,partial,promise,reference,sealed,sizeof,supports,typedefandunchecked.
The following example shows a contract and its updated version for Solidity v0.5.0 with some of the changes listed in this section.
Old version:
// This will not compile
pragma solidity ^0.4.25;
contract OtherContract {
uint x;
function f(uint y) external {
x = y;
}
function() payable external {}
}
contract Old {
OtherContract other;
uint myNumber;
// Function mutability not provided, not an error.
function someInteger() internal returns (uint) { return 2; }
// Function visibility not provided, not an error.
// Function mutability not provided, not an error.
function f(uint x) returns (bytes) {
// Var is fine in this version.
var z = someInteger();
x += z;
// Throw is fine in this version.
if (x > 100)
throw;
bytes b = new bytes(x);
y = -3 >> 1;
// y == -1 (wrong, should be -2)
do {
x += 1;
if (x > 10) continue;
// 'Continue' causes an infinite loop.
} while (x < 11);
// Call returns only a Bool.
bool success = address(other).call("f");
if (!success)
revert();
else {
// Local variables could be declared after their use.
int y;
}
return b;
}
// No need for an explicit data location for 'arr'
function g(uint[] arr, bytes8 x, OtherContract otherContract) public {
otherContract.transfer(1 ether);
// Since uint32 (4 bytes) is smaller than bytes8 (8 bytes),
// the first 4 bytes of x will be lost. This might lead to
// unexpected behavior since bytesX are right padded.
uint32 y = uint32(x);
myNumber += y + msg.value;
}
}
New version:
pragma solidity >0.4.99 <0.6.0;
contract OtherContract {
uint x;
function f(uint y) external {
x = y;
}
function() payable external {}
}
contract New {
OtherContract other;
uint myNumber;
// Function mutability must be specified.
function someInteger() internal pure returns (uint) { return 2; }
// Function visibility must be specified.
// Function mutability must be specified.
function f(uint x) public returns (bytes memory) {
// The type must now be explicitly given.
uint z = someInteger();
x += z;
// Throw is now disallowed.
require(x > 100);
int y = -3 >> 1;
// y == -2 (correct)
do {
x += 1;
if (x > 10) continue;
// 'Continue' jumps to the condition below.
} while (x < 11);
// Call returns (bool, bytes).
// Data location must be specified.
(bool success, bytes memory data) = address(other).call("f");
if (!success)
revert();
return data;
}
using address_make_payable for address;
// Data location for 'arr' must be specified
function g(uint[] memory arr, bytes8 x, OtherContract otherContract, address unknownContract) public payable {
// 'otherContract.transfer' is not provided.
// Since the code of 'OtherContract' is known and has the fallback
// function, address(otherContract) has type 'address payable'.
address(otherContract).transfer(1 ether);
// 'unknownContract.transfer' is not provided.
// 'address(unknownContract).transfer' is not provided
// since 'address(unknownContract)' is not 'address payable'.
// If the function takes an 'address' which you want to send
// funds to, you can convert it to 'address payable' via 'uint160'.
// Note: This is not recommended and the explicit type
// 'address payable' should be used whenever possible.
// To increase clarity, we suggest the use of a library for
// the conversion (provided after the contract in this example).
address payable addr = unknownContract.make_payable();
require(addr.send(1 ether));
// Since uint32 (4 bytes) is smaller than bytes8 (8 bytes),
// the conversion is not allowed.
// We need to convert to a common size first:
bytes4 x4 = bytes4(x); // Padding happens on the right
uint32 y = uint32(x4); // Conversion is consistent
// 'msg.value' cannot be used in a 'non-payable' function.
// We need to make the function payable
myNumber += y + msg.value;
}
}
// We can define a library for explicitly converting ``address``
// to ``address payable`` as a workaround.
library address_make_payable {
function make_payable(address x) internal pure returns (address payable) {
return address(uint160(x));
}
}