aboutsummaryrefslogtreecommitdiffstats
path: root/test/libsolidity
diff options
context:
space:
mode:
authorchriseth <chris@ethereum.org>2018-02-14 00:08:35 +0800
committerGitHub <noreply@github.com>2018-02-14 00:08:35 +0800
commit23484ba6a4ab17df58dfa1d27b486c10265ce4ae (patch)
tree64944b4cc71593944dc461b288cf25dfdf68cb9e /test/libsolidity
parentf84b2c451425a5913ef3decc18cf56ef95988d83 (diff)
parentaea9e7fe549d93436a1eb355258ea1b11b5dfb22 (diff)
downloaddexon-solidity-23484ba6a4ab17df58dfa1d27b486c10265ce4ae.tar.gz
dexon-solidity-23484ba6a4ab17df58dfa1d27b486c10265ce4ae.tar.zst
dexon-solidity-23484ba6a4ab17df58dfa1d27b486c10265ce4ae.zip
Merge pull request #3498 from ethereum/allowthisfselector
Allow `this.f.selector` to be pure.
Diffstat (limited to 'test/libsolidity')
-rw-r--r--test/libsolidity/SolidityEndToEndTest.cpp11
-rw-r--r--test/libsolidity/SolidityNameAndTypeResolution.cpp10
-rw-r--r--test/libsolidity/ViewPureChecker.cpp47
3 files changed, 56 insertions, 12 deletions
diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp
index 5b98d979..0611e71d 100644
--- a/test/libsolidity/SolidityEndToEndTest.cpp
+++ b/test/libsolidity/SolidityEndToEndTest.cpp
@@ -10219,24 +10219,29 @@ BOOST_AUTO_TEST_CASE(function_types_sig)
{
char const* sourceCode = R"(
contract C {
- function f() returns (bytes4) {
+ uint public x;
+ function f() pure returns (bytes4) {
return this.f.selector;
}
function g() returns (bytes4) {
- function () external returns (bytes4) fun = this.f;
+ function () pure external returns (bytes4) fun = this.f;
return fun.selector;
}
function h() returns (bytes4) {
- function () external returns (bytes4) fun = this.f;
+ function () pure external returns (bytes4) fun = this.f;
var funvar = fun;
return funvar.selector;
}
+ function i() pure returns (bytes4) {
+ return this.x.selector;
+ }
}
)";
compileAndRun(sourceCode, 0, "C");
ABI_CHECK(callContractFunction("f()"), encodeArgs(asString(FixedHash<4>(dev::keccak256("f()")).asBytes())));
ABI_CHECK(callContractFunction("g()"), encodeArgs(asString(FixedHash<4>(dev::keccak256("f()")).asBytes())));
ABI_CHECK(callContractFunction("h()"), encodeArgs(asString(FixedHash<4>(dev::keccak256("f()")).asBytes())));
+ ABI_CHECK(callContractFunction("i()"), encodeArgs(asString(FixedHash<4>(dev::keccak256("x()")).asBytes())));
}
BOOST_AUTO_TEST_CASE(constant_string)
diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp
index c4741657..315c7c5f 100644
--- a/test/libsolidity/SolidityNameAndTypeResolution.cpp
+++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp
@@ -6991,15 +6991,7 @@ BOOST_AUTO_TEST_CASE(function_types_sig)
CHECK_ERROR(text, TypeError, "Member \"selector\" not found");
text = R"(
contract C {
- function f() view external returns (bytes4) {
- return this.f.selector;
- }
- }
- )";
- CHECK_SUCCESS_NO_WARNINGS(text);
- text = R"(
- contract C {
- function f() view external returns (bytes4) {
+ function f() pure external returns (bytes4) {
return this.f.selector;
}
}
diff --git a/test/libsolidity/ViewPureChecker.cpp b/test/libsolidity/ViewPureChecker.cpp
index e91e713c..3a03c877 100644
--- a/test/libsolidity/ViewPureChecker.cpp
+++ b/test/libsolidity/ViewPureChecker.cpp
@@ -326,6 +326,53 @@ BOOST_AUTO_TEST_CASE(function_types)
CHECK_SUCCESS_NO_WARNINGS(text);
}
+BOOST_AUTO_TEST_CASE(selector)
+{
+ string text = R"(
+ contract C {
+ uint public x;
+ function f() payable public {
+ }
+ function g() pure public returns (bytes4) {
+ return this.f.selector ^ this.x.selector;
+ }
+ }
+ )";
+ CHECK_SUCCESS_NO_WARNINGS(text);
+}
+
+BOOST_AUTO_TEST_CASE(selector_complex)
+{
+ string text = R"(
+ contract C {
+ function f(C c) pure public returns (C) {
+ return c;
+ }
+ function g() pure public returns (bytes4) {
+ // By passing `this`, we read from the state, even if f itself is pure.
+ return f(this).f.selector;
+ }
+ }
+ )";
+ CHECK_ERROR(text, TypeError, "reads from the environment or state and thus requires \"view\"");
+}
+
+BOOST_AUTO_TEST_CASE(selector_complex2)
+{
+ string text = R"(
+ contract C {
+ function f() payable public returns (C) {
+ return this;
+ }
+ function g() pure public returns (bytes4) {
+ C x = C(0x123);
+ return x.f.selector;
+ }
+ }
+ )";
+ CHECK_SUCCESS_NO_WARNINGS(text);
+}
+
BOOST_AUTO_TEST_CASE(creation)
{
string text = R"(