Solidity v0.7.0 重大更新¶
This section highlights the main breaking changes introduced in Solidity version 0.7.0, along with the reasoning behind the changes and how to update affected code. For the full list check the release changelog.
Silent Changes of the Semantics¶
Exponentiation and shifts of literals by non-literals (e.g.
1 << xor2 ** x) will always use either the typeuint256(for non-negative literals) orint256(for negative literals) to perform the operation. Previously, the operation was performed in the type of the shift amount / the exponent which can be misleading.
Changes to the Syntax¶
In external function and contract creation calls, Ether and gas is now specified using a new syntax:
x.f{gas: 10000, value: 2 ether}(arg1, arg2). The old syntax –x.f.gas(10000).value(2 ether)(arg1, arg2)– will cause an error.The global variable
nowis deprecated,block.timestampshould be used instead. The single identifiernowis too generic for a global variable and could give the impression that it changes during transaction processing, whereasblock.timestampcorrectly reflects the fact that it is just a property of the block.NatSpec comments on variables are only allowed for public state variables and not for local or internal variables.
The token
gweiis a keyword now (used to specify, e.g.2 gweias a number) and cannot be used as an identifier.String literals now can only contain printable ASCII characters and this also includes a variety of escape sequences, such as hexadecimal (
\xff) and unicode escapes (\u20ac).Unicode string literals are supported now to accommodate valid UTF-8 sequences. They are identified with the
unicodeprefix:unicode"Hello 😃".State Mutability: The state mutability of functions can now be restricted during inheritance: Functions with default state mutability can be overridden by
pureandviewfunctions whileviewfunctions can be overridden bypurefunctions. At the same time, public state variables are consideredviewand evenpureif they are constants.
Inline Assembly¶
Disallow
.in user-defined function and variable names in inline assembly. It is still valid if you use Solidity in Yul-only mode.Slot and offset of storage pointer variable
xare accessed viax.slotandx.offsetinstead ofx_slotandx_offset.
Removal of Unused or Unsafe Features¶
Mappings outside Storage¶
If a struct or array contains a mapping, it can only be used in storage. Previously, mapping members were silently skipped in memory, which is confusing and error-prone.
Assignments to structs or arrays in storage does not work if they contain mappings. Previously, mappings were silently skipped during the copy operation, which is misleading and error-prone.
Functions and Events¶
Visibility (
public/internal) is not needed for constructors anymore: To prevent a contract from being created, it can be markedabstract. This makes the visibility concept for constructors obsolete.Type Checker: Disallow
virtualfor library functions: Since libraries cannot be inherited from, library functions should not be virtual.Multiple events with the same name and parameter types in the same inheritance hierarchy are disallowed.
using A for Bonly affects the contract it is mentioned in. Previously, the effect was inherited. Now, you have to repeat theusingstatement in all derived contracts that make use of the feature.
Expressions¶
Shifts by signed types are disallowed. Previously, shifts by negative amounts were allowed, but reverted at runtime.
The
finneyandszabodenominations are removed. They are rarely used and do not make the actual amount readily visible. Instead, explicit values like1e20or the very commongweican be used.
Declarations¶
The keyword
varcannot be used anymore. Previously, this keyword would parse but result in a type error and a suggestion about which type to use. Now, it results in a parser error.
Interface Changes¶
JSON AST: Mark hex string literals with
kind: "hexString".JSON AST: Members with value
nullare removed from JSON output.NatSpec: Constructors and functions have consistent userdoc output.
How to update your code¶
This section gives detailed instructions on how to update prior code for every breaking change.
Change
x.f.value(...)()tox.f{value: ...}(). Similarly(new C).value(...)()tonew C{value: ...}()andx.f.gas(...).value(...)()tox.f{gas: ..., value: ...}().Change
nowtoblock.timestamp.Change types of right operand in shift operators to unsigned types. For example change
x >> (256 - y)tox >> uint(256 - y).Repeat the
using A for Bstatements in all derived contracts if needed.Remove the
publickeyword from every constructor.Remove the
internalkeyword from every constructor and addabstractto the contract (if not already present).Change
_slotand_offsetsuffixes in inline assembly to.slotand.offset, respectively.