forked from ethereum/solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ethereum#3745 from ethereum/fixRecursion
Fix invalid recursion errors for structs
- Loading branch information
Showing
19 changed files
with
220 additions
and
155 deletions.
There are no files selected for viewing
This file contains 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 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,76 @@ | ||
/* | ||
This file is part of solidity. | ||
solidity is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
solidity is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with solidity. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
#pragma once | ||
|
||
|
||
#include <functional> | ||
#include <set> | ||
|
||
namespace dev | ||
{ | ||
|
||
/** | ||
* Detector for cycles in directed graphs. It returns the first | ||
* vertex on the path towards a cycle or a nullptr if there is | ||
* no reachable cycle starting from a given vertex. | ||
*/ | ||
template <typename V> | ||
class CycleDetector | ||
{ | ||
public: | ||
/// Initializes the cycle detector | ||
/// @param _visit function that is given the current vertex | ||
/// and is supposed to call @a run on all | ||
/// adjacent vertices. | ||
explicit CycleDetector(std::function<void(V const&, CycleDetector&)> _visit): | ||
m_visit(std::move(_visit)) | ||
{ } | ||
|
||
/// Recursively perform cycle detection starting | ||
/// (or continuing) with @param _vertex | ||
/// @returns the first vertex on the path towards a cycle from @a _vertex | ||
/// or nullptr if no cycle is reachable from @a _vertex. | ||
V const* run(V const& _vertex) | ||
{ | ||
if (m_firstCycleVertex) | ||
return m_firstCycleVertex; | ||
if (m_processed.count(&_vertex)) | ||
return nullptr; | ||
else if (m_processing.count(&_vertex)) | ||
return m_firstCycleVertex = &_vertex; | ||
m_processing.insert(&_vertex); | ||
|
||
m_depth++; | ||
m_visit(_vertex, *this); | ||
m_depth--; | ||
if (m_firstCycleVertex && m_depth == 1) | ||
m_firstCycleVertex = &_vertex; | ||
|
||
m_processing.erase(&_vertex); | ||
m_processed.insert(&_vertex); | ||
return m_firstCycleVertex; | ||
} | ||
|
||
private: | ||
std::function<void(V const&, CycleDetector&)> m_visit; | ||
std::set<V const*> m_processing; | ||
std::set<V const*> m_processed; | ||
size_t m_depth = 0; | ||
V const* m_firstCycleVertex = nullptr; | ||
}; | ||
|
||
} |
This file contains 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 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 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 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
5 changes: 5 additions & 0 deletions
5
test/libsolidity/syntaxTests/constants/cyclic_dependency_1.sol
This file contains 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 @@ | ||
contract C { | ||
uint constant a = a; | ||
} | ||
// ---- | ||
// TypeError: The value of the constant a has a cyclic dependency via a. |
10 changes: 10 additions & 0 deletions
10
test/libsolidity/syntaxTests/constants/cyclic_dependency_2.sol
This file contains 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,10 @@ | ||
contract C { | ||
uint constant a = b * c; | ||
uint constant b = 7; | ||
uint constant c = b + uint(keccak256(d)); | ||
uint constant d = 2 + a; | ||
} | ||
// ---- | ||
// TypeError: The value of the constant a has a cyclic dependency via c. | ||
// TypeError: The value of the constant c has a cyclic dependency via d. | ||
// TypeError: The value of the constant d has a cyclic dependency via a. |
11 changes: 11 additions & 0 deletions
11
test/libsolidity/syntaxTests/constants/cyclic_dependency_3.sol
This file contains 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,11 @@ | ||
contract C { | ||
uint constant x = a; | ||
uint constant a = b * c; | ||
uint constant b = c; | ||
uint constant c = b; | ||
} | ||
// ---- | ||
// TypeError: The value of the constant x has a cyclic dependency via a. | ||
// TypeError: The value of the constant a has a cyclic dependency via b. | ||
// TypeError: The value of the constant b has a cyclic dependency via c. | ||
// TypeError: The value of the constant c has a cyclic dependency via b. |
6 changes: 6 additions & 0 deletions
6
test/libsolidity/syntaxTests/constants/cyclic_dependency_4.sol
This file contains 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 @@ | ||
contract C { | ||
uint constant a = b * c; | ||
uint constant b = 7; | ||
uint constant c = 4 + uint(keccak256(d)); | ||
uint constant d = 2 + b; | ||
} |
15 changes: 15 additions & 0 deletions
15
test/libsolidity/syntaxTests/structs/recursion/multi_struct_composition.sol
This file contains 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,15 @@ | ||
pragma experimental ABIEncoderV2; | ||
|
||
contract C { | ||
struct T { U u; V v; } | ||
|
||
struct U { W w; } | ||
|
||
struct V { W w; } | ||
|
||
struct W { uint x; } | ||
|
||
function f(T) public pure { } | ||
} | ||
// ---- | ||
// Warning: Experimental features are turned on. Do not use experimental features on live deployments. |
Oops, something went wrong.