mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Validate immutable variables
This commit is contained in:
committed by
chriseth
parent
9a8ca6ca33
commit
ac7b31e559
@@ -0,0 +1,11 @@
|
||||
contract C {
|
||||
uint immutable x;
|
||||
constructor() public {
|
||||
if (false)
|
||||
return;
|
||||
|
||||
x = 1;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (93-100): Construction control flow ends without initializing all immutable state variables.
|
||||
@@ -0,0 +1,9 @@
|
||||
contract C {
|
||||
uint immutable x;
|
||||
constructor() public {
|
||||
if (false)
|
||||
x = 1;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (93-94): Immutable variables must be initialized unconditionally, not in an if statement.
|
||||
@@ -0,0 +1,12 @@
|
||||
contract C {
|
||||
uint immutable x;
|
||||
constructor() public {
|
||||
initX();
|
||||
}
|
||||
|
||||
function initX() internal {
|
||||
x = 3;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (126-127): Immutable variables can only be initialized inline or assigned directly in the constructor.
|
||||
@@ -0,0 +1,10 @@
|
||||
contract C {
|
||||
uint immutable x;
|
||||
constructor() public {
|
||||
x = f();
|
||||
}
|
||||
|
||||
function f() public pure returns (uint) { return 3 + x; }
|
||||
}
|
||||
// ----
|
||||
// TypeError: (143-144): Immutable variables cannot be read during contract creation time, which means they cannot be read in the constructor or any function or modifier called from it.
|
||||
@@ -0,0 +1,8 @@
|
||||
contract C {
|
||||
uint immutable x;
|
||||
constructor() public {
|
||||
x = 3 + x;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (78-79): Immutable variables cannot be read during contract creation time, which means they cannot be read in the constructor or any function or modifier called from it.
|
||||
@@ -0,0 +1,13 @@
|
||||
contract C {
|
||||
uint immutable x;
|
||||
uint immutable y;
|
||||
constructor() public {
|
||||
(x, y) = f();
|
||||
}
|
||||
|
||||
function f() internal pure returns(uint _x, uint _y) {
|
||||
_x = 3;
|
||||
_y = 4;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
@@ -0,0 +1,12 @@
|
||||
contract C {
|
||||
uint immutable x;
|
||||
constructor() readX(x = 3) public { }
|
||||
|
||||
modifier readX(uint _x) {
|
||||
_; f(_x);
|
||||
}
|
||||
|
||||
function f(uint a) internal pure {}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (59-60): Immutable variables can only be initialized inline or assigned directly in the constructor.
|
||||
@@ -0,0 +1,11 @@
|
||||
contract C {
|
||||
uint immutable x;
|
||||
constructor() initX public {
|
||||
}
|
||||
|
||||
modifier initX() {
|
||||
_; x = 23;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (109-110): Immutable variables can only be initialized inline or assigned directly in the constructor.
|
||||
@@ -0,0 +1,14 @@
|
||||
contract C {
|
||||
uint immutable x;
|
||||
constructor() readX public {
|
||||
x = 3;
|
||||
}
|
||||
|
||||
modifier readX() {
|
||||
_; f(x);
|
||||
}
|
||||
|
||||
function f(uint a) internal pure {}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (126-127): Immutable variables cannot be read during contract creation time, which means they cannot be read in the constructor or any function or modifier called from it.
|
||||
@@ -0,0 +1,8 @@
|
||||
contract C {
|
||||
uint immutable x = 3;
|
||||
constructor() public {
|
||||
x--;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (74-75): Immutable variables cannot be read during contract creation time, which means they cannot be read in the constructor or any function or modifier called from it.
|
||||
@@ -0,0 +1,8 @@
|
||||
contract C {
|
||||
uint immutable x = 3;
|
||||
constructor() public {
|
||||
delete x;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (81-82): Immutable variables cannot be read during contract creation time, which means they cannot be read in the constructor or any function or modifier called from it.
|
||||
@@ -0,0 +1,5 @@
|
||||
contract C {
|
||||
uint immutable x = f();
|
||||
|
||||
function f() public pure returns (uint) { return 3; }
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
contract C {
|
||||
uint immutable x = f();
|
||||
|
||||
function f() public pure returns (uint) { return 3 + x; }
|
||||
}
|
||||
// ----
|
||||
// TypeError: (99-100): Immutable variables cannot be read during contract creation time, which means they cannot be read in the constructor or any function or modifier called from it.
|
||||
@@ -0,0 +1,14 @@
|
||||
contract B {
|
||||
uint immutable x;
|
||||
|
||||
constructor(function() internal returns(uint) fp) internal {
|
||||
x = fp();
|
||||
}
|
||||
}
|
||||
|
||||
contract C is B(C.f) {
|
||||
function f() internal returns(uint) { return x = 2; }
|
||||
}
|
||||
// ----
|
||||
// TypeError: (200-201): Immutable variables can only be initialized inline or assigned directly in the constructor.
|
||||
// TypeError: (200-201): Immutable state variable already initialized.
|
||||
@@ -0,0 +1,13 @@
|
||||
contract B {
|
||||
uint immutable x;
|
||||
|
||||
constructor(function() internal returns(uint) fp) internal {
|
||||
x = fp();
|
||||
}
|
||||
}
|
||||
|
||||
contract C is B(C.f) {
|
||||
function f() internal returns(uint) { return x + 2; }
|
||||
}
|
||||
// ----
|
||||
// TypeError: (200-201): Immutable variables cannot be read during contract creation time, which means they cannot be read in the constructor or any function or modifier called from it.
|
||||
@@ -1,5 +0,0 @@
|
||||
contract C {
|
||||
uint immutable public x;
|
||||
}
|
||||
// ----
|
||||
// UnimplementedFeatureError: NONE
|
||||
@@ -1,3 +1,3 @@
|
||||
contract C {
|
||||
uint immutable x;
|
||||
}
|
||||
uint immutable x = 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
contract C {
|
||||
uint immutable x = 3;
|
||||
constructor() public {
|
||||
x++;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (74-75): Immutable variables cannot be read during contract creation time, which means they cannot be read in the constructor or any function or modifier called from it.
|
||||
@@ -0,0 +1,8 @@
|
||||
contract C {
|
||||
uint immutable x = 0;
|
||||
uint y = f();
|
||||
|
||||
function f() internal returns(uint) { return x; }
|
||||
}
|
||||
// ----
|
||||
// TypeError: (107-108): Immutable variables cannot be read during contract creation time, which means they cannot be read in the constructor or any function or modifier called from it.
|
||||
@@ -0,0 +1,15 @@
|
||||
contract B {
|
||||
uint immutable x;
|
||||
|
||||
constructor() public {
|
||||
x = 3;
|
||||
}
|
||||
}
|
||||
|
||||
contract C is B {
|
||||
uint immutable y;
|
||||
constructor() public {
|
||||
y = 3;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
@@ -0,0 +1,14 @@
|
||||
contract B {
|
||||
uint immutable x;
|
||||
|
||||
constructor(uint _x) public {
|
||||
x = _x;
|
||||
}
|
||||
}
|
||||
|
||||
contract C is B {
|
||||
uint immutable y;
|
||||
constructor() B(y = 3) public { }
|
||||
}
|
||||
// ----
|
||||
// TypeError: (155-156): Immutable variables can only be initialized inline or assigned directly in the constructor.
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
contract B {
|
||||
uint immutable x;
|
||||
|
||||
constructor(uint _x) public {
|
||||
x = _x;
|
||||
}
|
||||
}
|
||||
|
||||
contract C is B(C.y = 3) {
|
||||
uint immutable y;
|
||||
}
|
||||
// ----
|
||||
// TypeError: (111-114): Immutable variables can only be initialized inline or assigned directly in the constructor.
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
contract B {
|
||||
uint immutable x;
|
||||
|
||||
constructor(uint _x) public {
|
||||
x = _x;
|
||||
}
|
||||
}
|
||||
|
||||
contract C is B(C.y) {
|
||||
uint immutable y;
|
||||
constructor() public {
|
||||
y = 3;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (111-114): Immutable variables cannot be read during contract creation time, which means they cannot be read in the constructor or any function or modifier called from it.
|
||||
@@ -0,0 +1,19 @@
|
||||
contract B {
|
||||
uint immutable x;
|
||||
|
||||
constructor() public {
|
||||
x = xInit();
|
||||
}
|
||||
|
||||
function xInit() internal virtual returns(uint) {
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
|
||||
contract C is B {
|
||||
function xInit() internal override returns(uint) {
|
||||
return x;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (260-261): Immutable variables cannot be read during contract creation time, which means they cannot be read in the constructor or any function or modifier called from it.
|
||||
@@ -0,0 +1,19 @@
|
||||
contract B {
|
||||
uint immutable x = 3;
|
||||
|
||||
function readX() internal virtual returns(uint) {
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
contract C is B {
|
||||
constructor() public {
|
||||
B.readX;
|
||||
}
|
||||
|
||||
function readX() internal override returns(uint) {
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (109-110): Immutable variables cannot be read during contract creation time, which means they cannot be read in the constructor or any function or modifier called from it.
|
||||
@@ -0,0 +1,19 @@
|
||||
contract B {
|
||||
uint immutable x = 3;
|
||||
|
||||
function readX() internal view virtual returns(uint) {
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
contract C is B {
|
||||
constructor() public {
|
||||
super.readX();
|
||||
}
|
||||
|
||||
function readX() internal view override returns(uint) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (114-115): Immutable variables cannot be read during contract creation time, which means they cannot be read in the constructor or any function or modifier called from it.
|
||||
@@ -0,0 +1,21 @@
|
||||
contract B {
|
||||
uint immutable x;
|
||||
|
||||
constructor() readX public {
|
||||
x = 3;
|
||||
}
|
||||
|
||||
modifier readX() virtual {
|
||||
_; f(3);
|
||||
}
|
||||
|
||||
function f(uint a) internal pure {}
|
||||
}
|
||||
|
||||
contract C is B {
|
||||
modifier readX() override {
|
||||
_; f(x);
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (252-253): Immutable variables cannot be read during contract creation time, which means they cannot be read in the constructor or any function or modifier called from it.
|
||||
@@ -0,0 +1,12 @@
|
||||
contract B {
|
||||
uint immutable x = 4;
|
||||
}
|
||||
|
||||
contract C is B {
|
||||
constructor() public {
|
||||
x = 3;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (95-96): Immutable variables must be initialized in the constructor of the contract they are defined in.
|
||||
// TypeError: (95-96): Immutable state variable already initialized.
|
||||
@@ -0,0 +1,8 @@
|
||||
contract C {
|
||||
constructor() public {
|
||||
return;
|
||||
}
|
||||
|
||||
uint immutable x = 3;
|
||||
}
|
||||
// ----
|
||||
@@ -0,0 +1,9 @@
|
||||
contract C {
|
||||
uint immutable x;
|
||||
constructor() public {
|
||||
while (true)
|
||||
x = 1;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (95-96): Immutable variables can only be initialized once, not in a while statement.
|
||||
@@ -0,0 +1,29 @@
|
||||
contract A {
|
||||
function f() internal virtual returns(uint) { return 3; }
|
||||
}
|
||||
|
||||
contract B {
|
||||
uint immutable x;
|
||||
|
||||
constructor() public {
|
||||
x = xInit();
|
||||
}
|
||||
|
||||
function xInit() internal virtual returns(uint) {
|
||||
return f();
|
||||
}
|
||||
|
||||
function f() internal virtual returns(uint) { return 3; }
|
||||
}
|
||||
|
||||
contract C is A, B {
|
||||
function xInit() internal override returns(uint) {
|
||||
return B.xInit();
|
||||
}
|
||||
|
||||
function f() internal override(A, B) returns(uint) {
|
||||
return x;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (496-497): Immutable variables cannot be read during contract creation time, which means they cannot be read in the constructor or any function or modifier called from it.
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
contract A {
|
||||
function f() internal virtual returns(uint) { return 3; }
|
||||
}
|
||||
|
||||
contract B {
|
||||
uint immutable x;
|
||||
|
||||
constructor() public {
|
||||
x = xInit();
|
||||
}
|
||||
|
||||
function xInit() internal virtual returns(uint) {
|
||||
return f();
|
||||
}
|
||||
|
||||
function f() internal virtual returns(uint) { return 3; }
|
||||
}
|
||||
|
||||
contract C is A, B {
|
||||
function xInit() internal override returns(uint) {
|
||||
return super.xInit();
|
||||
}
|
||||
|
||||
function f() internal override(A, B) returns(uint) {
|
||||
return x;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (500-501): Immutable variables cannot be read during contract creation time, which means they cannot be read in the constructor or any function or modifier called from it.
|
||||
@@ -0,0 +1,9 @@
|
||||
contract C {
|
||||
uint immutable x;
|
||||
constructor() public {
|
||||
x = 1;
|
||||
x = 4;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (85-86): Immutable state variable already initialized.
|
||||
@@ -0,0 +1,20 @@
|
||||
contract B {
|
||||
uint immutable private x = f();
|
||||
|
||||
constructor() public {
|
||||
}
|
||||
|
||||
function f() internal view virtual returns(uint) { return 1; }
|
||||
function readX() internal view returns(uint) { return x; }
|
||||
}
|
||||
|
||||
contract C is B {
|
||||
uint immutable y;
|
||||
constructor() public {
|
||||
y = 3;
|
||||
}
|
||||
function f() internal view override returns(uint) { return readX(); }
|
||||
|
||||
}
|
||||
// ----
|
||||
// TypeError: (209-210): Immutable variables cannot be read during contract creation time, which means they cannot be read in the constructor or any function or modifier called from it.
|
||||
@@ -0,0 +1,8 @@
|
||||
contract C {
|
||||
uint immutable x = 0;
|
||||
uint y = 0;
|
||||
|
||||
function f() internal {
|
||||
y = x + 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
contract C {
|
||||
uint immutable x = 0;
|
||||
uint y = 0;
|
||||
|
||||
function f() readX internal {
|
||||
}
|
||||
|
||||
modifier readX() {
|
||||
_;
|
||||
y = x + 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
contract C {
|
||||
uint immutable x = 0;
|
||||
uint y = x;
|
||||
}
|
||||
// ----
|
||||
// TypeError: (52-53): Immutable variables cannot be read during contract creation time, which means they cannot be read in the constructor or any function or modifier called from it.
|
||||
@@ -0,0 +1,10 @@
|
||||
contract C {
|
||||
uint immutable x;
|
||||
constructor() public {
|
||||
return;
|
||||
|
||||
x = 1;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (70-77): Construction control flow ends without initializing all immutable state variables.
|
||||
@@ -0,0 +1,12 @@
|
||||
contract C {
|
||||
uint immutable x;
|
||||
constructor() public {
|
||||
x = 3;
|
||||
this.readX.selector;
|
||||
}
|
||||
|
||||
function readX() external view returns(uint) { return x; }
|
||||
}
|
||||
// ----
|
||||
// Warning: (85-104): Statement has no effect.
|
||||
// Warning: (85-89): "this" used in constructor. Note that external functions of a contract cannot be called while it is being constructed.
|
||||
@@ -0,0 +1,13 @@
|
||||
contract C {
|
||||
uint immutable x;
|
||||
constructor() public {
|
||||
x = 3;
|
||||
C.selector.selector;
|
||||
C.selector;
|
||||
}
|
||||
|
||||
function selector() external view returns(uint) { return x; }
|
||||
}
|
||||
// ----
|
||||
// Warning: (85-104): Statement has no effect.
|
||||
// Warning: (114-124): Statement has no effect.
|
||||
@@ -0,0 +1,16 @@
|
||||
contract C {
|
||||
uint immutable x;
|
||||
constructor() public {
|
||||
x = 3;
|
||||
readX().selector;
|
||||
}
|
||||
|
||||
function f() external view returns(uint) {
|
||||
return x;
|
||||
}
|
||||
|
||||
function readX() public view returns(function() external view returns(uint) _f) {
|
||||
_f = this.f;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
@@ -0,0 +1,5 @@
|
||||
contract C {
|
||||
uint immutable x;
|
||||
}
|
||||
// ----
|
||||
// TypeError: (0-36): Construction control flow ends without initializing all immutable state variables.
|
||||
@@ -0,0 +1,21 @@
|
||||
contract B {
|
||||
uint immutable private x;
|
||||
|
||||
constructor() public {
|
||||
}
|
||||
|
||||
function f() internal view virtual returns(uint) { return 1; }
|
||||
function readX() internal view returns(uint) { return x; }
|
||||
}
|
||||
|
||||
contract C is B {
|
||||
uint immutable y;
|
||||
constructor() public {
|
||||
y = 3;
|
||||
}
|
||||
function f() internal view override returns(uint) { return readX(); }
|
||||
|
||||
}
|
||||
// ----
|
||||
// TypeError: (0-209): Construction control flow ends without initializing all immutable state variables.
|
||||
// TypeError: (211-375): Construction control flow ends without initializing all immutable state variables.
|
||||
@@ -0,0 +1,8 @@
|
||||
contract C {
|
||||
uint immutable x = 1;
|
||||
|
||||
function readX() internal view returns(uint) {
|
||||
return x + 3;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
@@ -0,0 +1,10 @@
|
||||
contract C {
|
||||
uint immutable x = 0;
|
||||
|
||||
function f() internal {
|
||||
x = 1;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (76-77): Immutable variables can only be initialized inline or assigned directly in the constructor.
|
||||
// TypeError: (76-77): Immutable state variable already initialized.
|
||||
@@ -0,0 +1,12 @@
|
||||
contract C {
|
||||
uint immutable x = 0;
|
||||
|
||||
function f() readX internal { }
|
||||
|
||||
modifier readX() {
|
||||
_; x = 1;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (111-112): Immutable variables can only be initialized inline or assigned directly in the constructor.
|
||||
// TypeError: (111-112): Immutable state variable already initialized.
|
||||
@@ -1,8 +1,8 @@
|
||||
contract B {
|
||||
uint immutable x;
|
||||
uint immutable x = 1;
|
||||
function f() public pure returns (uint) {
|
||||
return x;
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// TypeError: (96-97): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view".
|
||||
// TypeError: (100-101): Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view".
|
||||
|
||||
Reference in New Issue
Block a user