Merge pull request #9892 from ethereum/constantsAtFileLevel

Constants at file-level.
This commit is contained in:
chriseth
2020-10-08 19:53:33 +02:00
committed by GitHub
52 changed files with 405 additions and 57 deletions
@@ -1,4 +1,4 @@
Error: Expected pragma, import directive or contract/interface/library/struct/enum/function definition.
Error: Expected pragma, import directive or contract/interface/library/struct/enum/constant/function definition.
--> recovery_ast_empty_contract/input.sol:3:1:
|
3 | c
@@ -1,4 +1,6 @@
pragma solidity ^99.99.0;
this is surely invalid
// ----
// ParserError 7858: (26-30): Expected pragma, import directive or contract/interface/library/struct/enum/function definition.
// ParserError 6635: (31-33): Expected identifier but got 'is'
// ParserError 6635: (34-40): Expected ';' but got identifier
// ParserError 6635: (49-49): Expected ';' but got end of source
@@ -0,0 +1,9 @@
address constant e = 0x1212121212121212121212121000002134593163;
contract C {
function f() public returns (address z) {
assembly { z := e }
}
}
// ----
// f() -> 0x1212121212121212121212121000002134593163
@@ -0,0 +1,33 @@
bytes constant a = "\x03\x01\x02";
bytes constant b = hex"030102";
string constant c = "hello";
uint256 constant x = 56;
enum ActionChoices {GoLeft, GoRight, GoStraight, Sit}
ActionChoices constant choices = ActionChoices.GoRight;
bytes32 constant st = "abc\x00\xff__";
contract C {
function f() public returns (bytes memory) {
return a;
}
function g() public returns (bytes memory) {
return b;
}
function h() public returns (bytes memory) {
return bytes(c);
}
function i() public returns (uint, ActionChoices, bytes32) {
return (x, choices, st);
}
}
// ====
// compileViaYul: also
// ----
// f() -> 0x20, 3, "\x03\x01\x02"
// g() -> 0x20, 3, "\x03\x01\x02"
// h() -> 0x20, 5, "hello"
// i() -> 0x38, 1, 0x61626300ff5f5f00000000000000000000000000000000000000000000000000
@@ -0,0 +1,41 @@
==== Source: s1.sol ====
bytes constant a = b;
bytes constant b = hex"030102";
function fre() pure returns (bytes memory) {
return a;
}
==== Source: s2.sol ====
import "s1.sol";
uint256 constant c = uint8(a[0]) + 2;
contract C {
function f() public returns (bytes memory) {
return a;
}
function g() public returns (bytes memory) {
return b;
}
function h() public returns (uint) {
return c;
}
function i() public returns (bytes memory) {
return fre();
}
}
// ====
// compileViaYul: also
// ----
// f() -> 0x20, 3, "\x03\x01\x02"
// g() -> 0x20, 3, "\x03\x01\x02"
// h() -> 5
// i() -> 0x20, 3, "\x03\x01\x02"
@@ -0,0 +1,26 @@
==== Source: s1.sol ====
uint constant a = 89;
function fre() pure returns (uint) {
return a;
}
==== Source: s2.sol ====
import {a as b, fre} from "s1.sol";
import "s1.sol" as M;
uint256 constant a = 13;
contract C {
function f() public returns (uint, uint, uint, uint) {
return (a, fre(), M.a, b);
}
}
// ====
// compileViaYul: also
// ----
// f() -> 0x0d, 0x59, 0x59, 0x59
@@ -0,0 +1,15 @@
==== Source: a.sol ====
function f(uint) pure returns (uint) { return 7; }
function f(bytes memory x) pure returns (uint) { return x.length; }
==== Source: b.sol ====
import "a.sol" as M;
contract C {
function f() public pure returns (uint, uint) {
return (M.f(2), M.f("abc"));
}
}
// ====
// compileViaYul: also
// ----
// f() -> 7, 3
@@ -4,6 +4,6 @@ contract C {
}
}
// ----
// DeclarationError 1788: (28-45): The "constant" keyword can only be used for state variables.
// DeclarationError 1788: (28-45): The "constant" keyword can only be used for state variables or variables at file level.
// TypeError 5462: (69-72): Invalid array length, expected integer literal or constant expression.
// TypeError 6651: (64-75): Data location must be "storage", "memory" or "calldata" for variable, but none was given.
@@ -0,0 +1,3 @@
/// @dev Documentation
uint constant x = 8;
// ----
@@ -0,0 +1,4 @@
/// Documentation
uint constant x = 8;
// ----
// DocstringParsingError 6546: (0-18): Documentation tag @notice not valid for file-level variables.
@@ -0,0 +1,3 @@
uint constant override x = 2;
// ----
// ParserError 2314: (14-22): Expected identifier but got 'override'
@@ -0,0 +1,3 @@
uint constant x;
// ----
// TypeError 4266: (0-15): Uninitialized "constant" variable.
@@ -0,0 +1,3 @@
uint constant virtual x;
// ----
// ParserError 2314: (14-21): Expected identifier but got 'virtual'
@@ -0,0 +1,3 @@
uint public constant x = 7;
// ----
// ParserError 2314: (5-11): Expected identifier but got 'public'
@@ -0,0 +1,3 @@
uint constant public y = 7;
// ----
// ParserError 2314: (14-20): Expected identifier but got 'public'
@@ -0,0 +1,15 @@
==== Source: a ====
import "b";
uint constant c = d;
==== Source: b ====
import "a";
uint constant b = c;
uint constant d = b;
contract C {
uint constant a = b;
}
// ----
// TypeError 6161: (b:12-31): The value of the constant b has a cyclic dependency via c.
// TypeError 6161: (b:33-52): The value of the constant d has a cyclic dependency via b.
// TypeError 6161: (b:71-90): The value of the constant a has a cyclic dependency via b.
// TypeError 6161: (a:12-31): The value of the constant c has a cyclic dependency via d.
@@ -0,0 +1,15 @@
==== Source: a ====
import "b";
uint constant c = d;
==== Source: b ====
import "a" as M;
uint constant b = M.c;
uint constant d = b;
contract C {
uint constant a = b;
}
// ----
// TypeError 6161: (b:17-38): The value of the constant b has a cyclic dependency via c.
// TypeError 6161: (b:40-59): The value of the constant d has a cyclic dependency via b.
// TypeError 6161: (b:78-97): The value of the constant a has a cyclic dependency via b.
// TypeError 6161: (a:12-31): The value of the constant c has a cyclic dependency via d.
@@ -0,0 +1,3 @@
uint[] memory constant x = 2;
// ----
// ParserError 2314: (7-13): Expected identifier but got 'memory'
@@ -0,0 +1,3 @@
uint[] constant memory x = 2;
// ----
// ParserError 2314: (16-22): Expected identifier but got 'memory'
@@ -0,0 +1,4 @@
uint immutable x = 7;
// ----
// DeclarationError 8342: (0-20): Only constant variables are allowed at file level.
// DeclarationError 8297: (0-20): The "immutable" keyword can only be used for state variables.
@@ -0,0 +1,3 @@
mapping(uint => uint) constant b = b;
// ----
// TypeError 9259: (0-36): Constants of non-value type not yet implemented.
@@ -0,0 +1,7 @@
==== Source: a ====
uint constant c = 7;
==== Source: b ====
import {c as d} from "a";
uint constant d = 7;
// ----
// DeclarationError 2333: (b:26-45): Identifier already declared.
@@ -0,0 +1,3 @@
uint x = 7;
// ----
// DeclarationError 8342: (0-10): Only constant variables are allowed at file level.
@@ -0,0 +1,8 @@
==== Source: a ====
import "b";
uint constant c = 7;
==== Source: b ====
import "a";
uint constant c = 7;
// ----
// DeclarationError 2333: (b:12-31): Identifier already declared.
@@ -0,0 +1,4 @@
uint constant c = 7;
uint constant c = 8;
// ----
// DeclarationError 2333: (21-40): Identifier already declared.
@@ -0,0 +1,5 @@
struct S { uint x; }
S constant s;
// ----
// TypeError 9259: (21-33): Constants of non-value type not yet implemented.
// TypeError 4266: (21-33): Uninitialized "constant" variable.
@@ -1,3 +1,3 @@
constructor() {}
// ----
// ParserError 7858: (0-11): Expected pragma, import directive or contract/interface/library/struct/enum/function definition.
// ParserError 7858: (0-11): Expected pragma, import directive or contract/interface/library/struct/enum/constant/function definition.
@@ -1,3 +1,3 @@
fallback(){}
// ----
// ParserError 7858: (0-8): Expected pragma, import directive or contract/interface/library/struct/enum/function definition.
// ParserError 7858: (0-8): Expected pragma, import directive or contract/interface/library/struct/enum/constant/function definition.
@@ -1,3 +1,3 @@
receive() {}
// ----
// ParserError 7858: (0-7): Expected pragma, import directive or contract/interface/library/struct/enum/function definition.
// ParserError 7858: (0-7): Expected pragma, import directive or contract/interface/library/struct/enum/constant/function definition.
@@ -2,4 +2,4 @@ contract test {
function f(uint[] memory constant a) public { }
}
// ----
// DeclarationError 1788: (31-55): The "constant" keyword can only be used for state variables.
// DeclarationError 1788: (31-55): The "constant" keyword can only be used for state variables or variables at file level.
@@ -0,0 +1,4 @@
uint constant c = 7;
function c() returns (uint) {}
// ----
// DeclarationError 2333: (21-51): Identifier already declared.
@@ -11,4 +11,4 @@ contract C {
}
}
// ----
// ParserError 2837: (290-295): Only state variables can have a docstring.
// ParserError 2837: (290-295): Only state variables or file-level variables can have a docstring.
@@ -2,4 +2,4 @@ contract Foo {
function f(uint[] storage constant x, uint[] memory y) internal { }
}
// ----
// DeclarationError 1788: (30-55): The "constant" keyword can only be used for state variables.
// DeclarationError 1788: (30-55): The "constant" keyword can only be used for state variables or variables at file level.
+1 -1
View File
@@ -1,3 +1,3 @@
unexpected
// ----
// ParserError 7858: (0-10): Expected pragma, import directive or contract/interface/library/struct/enum/function definition.
// ParserError 7858: (0-10): Expected pragma, import directive or contract/interface/library/struct/enum/constant/function definition.