-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Type checking for contract explicit storage base location #15528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+530
−25
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
test/cmdlineTests/storage_layout_already_defined_in_ancestor/args
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- |
21 changes: 21 additions & 0 deletions
21
test/cmdlineTests/storage_layout_already_defined_in_ancestor/err
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Error: Storage layout can only be specified in the most derived contract. | ||
--> <stdin>:5:1: | ||
| | ||
5 | contract B is A {} | ||
| ^^^^^^^^^^^^^^^^^^ | ||
Note: Storage layout was already specified here. | ||
--> <stdin>:4:12: | ||
| | ||
4 | contract A layout at 0x1234 {} | ||
| ^^^^^^^^^^^^^^^^ | ||
|
||
Error: Storage layout can only be specified in the most derived contract. | ||
--> <stdin>:6:1: | ||
| | ||
6 | contract C layout at 0x1234 is B {} | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
Note: Storage layout was already specified here. | ||
--> <stdin>:4:12: | ||
| | ||
4 | contract A layout at 0x1234 {} | ||
| ^^^^^^^^^^^^^^^^ |
1 change: 1 addition & 0 deletions
1
test/cmdlineTests/storage_layout_already_defined_in_ancestor/exit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1 |
6 changes: 6 additions & 0 deletions
6
test/cmdlineTests/storage_layout_already_defined_in_ancestor/stdin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// SPDX-License-Identifier: GPL-3.0 | ||
pragma solidity *; | ||
|
||
contract A layout at 0x1234 {} | ||
contract B is A {} | ||
contract C layout at 0x1234 is B {} |
23 changes: 23 additions & 0 deletions
23
test/libsolidity/natspecJSON/storage_layout_specifier_no_doc.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
contract C | ||
/// @notice this changes the base slot of the contract storage | ||
layout at | ||
/// @dev the expression must be in range of uint256 | ||
0x1234 | ||
{ } | ||
// ==== | ||
// stopAfter: analysis | ||
// ---- | ||
// ---- | ||
// :C devdoc | ||
// { | ||
// "kind": "dev", | ||
// "methods": {}, | ||
// "version": 1 | ||
// } | ||
// | ||
// :C userdoc | ||
// { | ||
// "kind": "user", | ||
// "methods": {}, | ||
// "version": 1 | ||
// } |
3 changes: 3 additions & 0 deletions
3
test/libsolidity/syntaxTests/storageLayoutSpecifier/abi_decode.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
contract C layout at abi.decode(abi.encode(42), (uint)) {} | ||
// ---- | ||
// TypeError 6396: (21-55): The base slot of the storage layout must evaluate to a rational number. |
3 changes: 1 addition & 2 deletions
3
test/libsolidity/syntaxTests/storageLayoutSpecifier/abstract_contract.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
abstract contract C layout at 42 { } | ||
// ==== | ||
// stopAfter: parsing | ||
// ---- | ||
// TypeError 7587: (20-32): Storage layout cannot be specified for abstract contracts. |
3 changes: 3 additions & 0 deletions
3
test/libsolidity/syntaxTests/storageLayoutSpecifier/address.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
contract C layout at address(0x1234) {} | ||
// ---- | ||
// TypeError 6396: (21-36): The base slot of the storage layout must evaluate to a rational number. |
7 changes: 7 additions & 0 deletions
7
test/libsolidity/syntaxTests/storageLayoutSpecifier/address_non_constant.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
contract A {} | ||
|
||
contract C layout at address(new A()) {} | ||
contract D layout at uint160(address(this)) {} | ||
// ---- | ||
// TypeError 1139: (36-52): The base slot of the storage layout must be a compile-time constant expression. | ||
// TypeError 1139: (77-99): The base slot of the storage layout must be a compile-time constant expression. |
3 changes: 3 additions & 0 deletions
3
test/libsolidity/syntaxTests/storageLayoutSpecifier/array_literal.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
contract C layout at [1, 2, 3] {} | ||
// ---- | ||
// TypeError 6396: (21-30): The base slot of the storage layout must evaluate to a rational number. |
4 changes: 4 additions & 0 deletions
4
test/libsolidity/syntaxTests/storageLayoutSpecifier/assignment.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
contract C layout at 1 = 2 { } | ||
// ---- | ||
// TypeError 4247: (21-22): Expression has to be an lvalue. | ||
// TypeError 7407: (25-26): Type int_const 2 is not implicitly convertible to expected type int_const 1. |
3 changes: 3 additions & 0 deletions
3
test/libsolidity/syntaxTests/storageLayoutSpecifier/bitwise_negation_after_cast.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
contract C layout at ~uint(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF) {} | ||
// ---- | ||
// TypeError 6396: (21-94): The base slot of the storage layout must evaluate to a rational number. |
3 changes: 3 additions & 0 deletions
3
test/libsolidity/syntaxTests/storageLayoutSpecifier/boolean.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
contract C layout at true {} | ||
// ---- | ||
// TypeError 6396: (21-25): The base slot of the storage layout must evaluate to a rational number. |
30 changes: 30 additions & 0 deletions
30
test/libsolidity/syntaxTests/storageLayoutSpecifier/builtin_functions.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
contract A layout at block.basefee { } | ||
contract B layout at block.chainid { } | ||
contract C layout at block.number { } | ||
contract D layout at uint160(address(block.coinbase)) { } | ||
contract E layout at block.prevrandao { } | ||
contract F layout at uint(blockhash(0)) { } | ||
contract G layout at msg.value { } | ||
contract H layout at msg.sender { } | ||
contract I layout at msg.data { } | ||
contract J layout at tx.gasprice { } | ||
contract K layout at uint160(tx.origin) { } | ||
contract L layout at address(this).balance { } | ||
contract M layout at uint(address(this).codehash) { } | ||
|
||
// ==== | ||
// EVMVersion: >=paris | ||
// ---- | ||
// TypeError 1139: (21-34): The base slot of the storage layout must be a compile-time constant expression. | ||
// TypeError 1139: (60-73): The base slot of the storage layout must be a compile-time constant expression. | ||
// TypeError 1139: (99-111): The base slot of the storage layout must be a compile-time constant expression. | ||
// TypeError 1139: (137-169): The base slot of the storage layout must be a compile-time constant expression. | ||
// TypeError 1139: (195-211): The base slot of the storage layout must be a compile-time constant expression. | ||
// TypeError 1139: (237-255): The base slot of the storage layout must be a compile-time constant expression. | ||
// TypeError 1139: (281-290): The base slot of the storage layout must be a compile-time constant expression. | ||
// TypeError 1139: (316-326): The base slot of the storage layout must be a compile-time constant expression. | ||
// TypeError 1139: (352-360): The base slot of the storage layout must be a compile-time constant expression. | ||
// TypeError 1139: (386-397): The base slot of the storage layout must be a compile-time constant expression. | ||
// TypeError 1139: (423-441): The base slot of the storage layout must be a compile-time constant expression. | ||
// TypeError 1139: (467-488): The base slot of the storage layout must be a compile-time constant expression. | ||
// TypeError 1139: (514-542): The base slot of the storage layout must be a compile-time constant expression. |
3 changes: 3 additions & 0 deletions
3
test/libsolidity/syntaxTests/storageLayoutSpecifier/bytes_length_member.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
contract C layout at bytes("ABCD").length {} | ||
// ---- | ||
// TypeError 1139: (21-41): The base slot of the storage layout must be a compile-time constant expression. |
4 changes: 4 additions & 0 deletions
4
test/libsolidity/syntaxTests/storageLayoutSpecifier/constant_divided_by_its_negation.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
uint constant N = 100; | ||
contract C layout at N / ~N {} | ||
// ---- | ||
// TypeError 6396: (44-50): The base slot of the storage layout must evaluate to a rational number. |
4 changes: 4 additions & 0 deletions
4
test/libsolidity/syntaxTests/storageLayoutSpecifier/constant_divided_by_zero.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
uint constant N = 100; | ||
contract C layout at N / 0 {} | ||
// ---- | ||
// TypeError 6396: (44-49): The base slot of the storage layout must evaluate to a rational number. |
7 changes: 7 additions & 0 deletions
7
test/libsolidity/syntaxTests/storageLayoutSpecifier/constant_from_base_contract.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
contract A { | ||
uint constant x = 10; | ||
} | ||
|
||
contract C is A layout at A.x { } | ||
// ---- | ||
// TypeError 6396: (68-71): The base slot of the storage layout must evaluate to a rational number. |
3 changes: 1 addition & 2 deletions
3
test/libsolidity/syntaxTests/storageLayoutSpecifier/contract_named_at.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
contract at layout at 0x1234ABC { } | ||
// ==== | ||
// stopAfter: parsing | ||
// ---- | ||
// UnimplementedFeatureError 1834: (0-35): Code generation is not supported for contracts with specified storage layout base. |
3 changes: 1 addition & 2 deletions
3
test/libsolidity/syntaxTests/storageLayoutSpecifier/contract_named_layout.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
contract layout layout at 0x1234ABC { } | ||
// ==== | ||
// stopAfter: parsing | ||
// ---- | ||
// UnimplementedFeatureError 1834: (0-39): Code generation is not supported for contracts with specified storage layout base. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
test/libsolidity/syntaxTests/storageLayoutSpecifier/delete.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
contract C layout at delete 2 { } | ||
// ---- | ||
// TypeError 4247: (28-29): Expression has to be an lvalue. | ||
// TypeError 9767: (21-29): Built-in unary operator delete cannot be applied to type int_const 2. |
3 changes: 3 additions & 0 deletions
3
test/libsolidity/syntaxTests/storageLayoutSpecifier/division_by_zero.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
contract A layout at 1 / 0 {} | ||
// ---- | ||
// TypeError 2271: (21-26): Built-in binary operator / cannot be applied to types int_const 1 and int_const 0. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
enum Color {Red, Green, Blue} | ||
|
||
contract C layout at Color.Red {} | ||
// ---- | ||
// TypeError 6396: (52-61): The base slot of the storage layout must evaluate to a rational number. |
7 changes: 7 additions & 0 deletions
7
test/libsolidity/syntaxTests/storageLayoutSpecifier/function_address.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
contract A { | ||
function f() public pure {} | ||
} | ||
|
||
contract C is A layout at this.f.address {} | ||
// ---- | ||
// TypeError 1139: (74-88): The base slot of the storage layout must be a compile-time constant expression. |
7 changes: 7 additions & 0 deletions
7
test/libsolidity/syntaxTests/storageLayoutSpecifier/function_defined_in_other_contract.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
contract A { | ||
function f() external pure {} | ||
} | ||
|
||
contract C layout at A.f { } | ||
// ---- | ||
// TypeError 6396: (71-74): The base slot of the storage layout must evaluate to a rational number. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at test output, the errors generated by this may be a bit annoying. It reports an error for every specifier in the hierarchy of every contract, which may be a lot. Consider this example:
You'll get 13 error messages here. I think it would be more useful to only report an error when a contract directly inherits from one with a layout specifier. The point where a fix needs to be applied is unlikely to be far from the specifier. It's either the specifier itself being applied to the wrong contract (secondary messages already point at this) or the first contract that inherits from it (that's what the primary message should point at). This would reduce the number of messages here to 5.
This should be very easy to implement simply by iterating over
baseContracts()
rather than the wholeannotation().linearizedBaseContract
. We could then point at the exact inheritance specifier that triggered the error rather than whole contract definition (baseContracts()
contains specifiers rather than contract definitions, and we can easily report their locations).We should also have the above as a test case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in #15900.