aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorchriseth <chris@ethereum.org>2018-08-07 23:20:05 +0800
committerGitHub <noreply@github.com>2018-08-07 23:20:05 +0800
commitd7756322c07634fa2ca0649a33585fc167079786 (patch)
tree2b28b52fe554d187c30787a34b84ea0dba56faf3
parent901550e473f001d2c6666870d71dbf0b9ff8c1ff (diff)
parent34a711a14daf9874061a83401bf28a57b3abf1b1 (diff)
downloaddexon-solidity-d7756322c07634fa2ca0649a33585fc167079786.tar.gz
dexon-solidity-d7756322c07634fa2ca0649a33585fc167079786.tar.zst
dexon-solidity-d7756322c07634fa2ca0649a33585fc167079786.zip
Merge pull request #4731 from ethereum/interface-enum
Allow enums in interfaces
-rw-r--r--Changelog.md1
-rw-r--r--libsolidity/analysis/TypeChecker.cpp7
-rw-r--r--libsolidity/analysis/TypeChecker.h1
-rw-r--r--test/libsolidity/SolidityEndToEndTest.cpp44
-rw-r--r--test/libsolidity/syntaxTests/nameAndTypeResolution/422_interface_enums.sol1
-rw-r--r--test/libsolidity/syntaxTests/parsing/enum_from_interface.sol9
-rw-r--r--test/libsolidity/syntaxTests/parsing/enum_from_interface_in_library.sol12
-rw-r--r--test/libsolidity/syntaxTests/parsing/enum_from_library.sol9
-rw-r--r--test/libsolidity/syntaxTests/parsing/enum_inheritance_contract.sol9
-rw-r--r--test/libsolidity/syntaxTests/parsing/enum_inheritance_interface.sol9
10 files changed, 93 insertions, 9 deletions
diff --git a/Changelog.md b/Changelog.md
index dbb44983..0790c0ca 100644
--- a/Changelog.md
+++ b/Changelog.md
@@ -69,6 +69,7 @@ Language Features:
* General: Allow appending ``calldata`` keyword to types, to explicitly specify data location for arguments of external functions.
* General: Support ``pop()`` for storage arrays.
* General: Scoping rules now follow the C99-style.
+ * General: Allow ``enum``s in interfaces.
Compiler Features:
* C API (``libsolc``): Export the ``solidity_license``, ``solidity_version`` and ``solidity_compile`` methods.
diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp
index d98d6af1..38331a43 100644
--- a/libsolidity/analysis/TypeChecker.cpp
+++ b/libsolidity/analysis/TypeChecker.cpp
@@ -751,13 +751,6 @@ bool TypeChecker::visit(VariableDeclaration const& _variable)
return false;
}
-bool TypeChecker::visit(EnumDefinition const& _enum)
-{
- if (m_scope->contractKind() == ContractDefinition::ContractKind::Interface)
- m_errorReporter.typeError(_enum.location(), "Enumerable cannot be declared in interfaces.");
- return false;
-}
-
void TypeChecker::visitManually(
ModifierInvocation const& _modifier,
vector<ContractDefinition const*> const& _bases
diff --git a/libsolidity/analysis/TypeChecker.h b/libsolidity/analysis/TypeChecker.h
index 47892a3f..b696de85 100644
--- a/libsolidity/analysis/TypeChecker.h
+++ b/libsolidity/analysis/TypeChecker.h
@@ -96,7 +96,6 @@ private:
virtual bool visit(StructDefinition const& _struct) override;
virtual bool visit(FunctionDefinition const& _function) override;
virtual bool visit(VariableDeclaration const& _variable) override;
- virtual bool visit(EnumDefinition const& _enum) override;
/// We need to do this manually because we want to pass the bases of the current contract in
/// case this is a base constructor call.
void visitManually(ModifierInvocation const& _modifier, std::vector<ContractDefinition const*> const& _bases);
diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp
index bd1d9de7..a6c1372b 100644
--- a/test/libsolidity/SolidityEndToEndTest.cpp
+++ b/test/libsolidity/SolidityEndToEndTest.cpp
@@ -4577,6 +4577,50 @@ BOOST_AUTO_TEST_CASE(constructing_enums_from_ints)
ABI_CHECK(callContractFunction("test()"), encodeArgs(1));
}
+BOOST_AUTO_TEST_CASE(enum_referencing)
+{
+ char const* sourceCode = R"(
+ interface I {
+ enum Direction { A, B, Left, Right }
+ }
+ library L {
+ enum Direction { Left, Right }
+ function f() public pure returns (Direction) {
+ return Direction.Right;
+ }
+ function g() public pure returns (I.Direction) {
+ return I.Direction.Right;
+ }
+ }
+ contract C is I {
+ function f() public pure returns (Direction) {
+ return Direction.Right;
+ }
+ function g() public pure returns (I.Direction) {
+ return I.Direction.Right;
+ }
+ function h() public pure returns (L.Direction) {
+ return L.Direction.Right;
+ }
+ function x() public pure returns (L.Direction) {
+ return L.f();
+ }
+ function y() public pure returns (I.Direction) {
+ return L.g();
+ }
+ }
+ )";
+ compileAndRun(sourceCode, 0, "L");
+ ABI_CHECK(callContractFunction("f()"), encodeArgs(1));
+ ABI_CHECK(callContractFunction("g()"), encodeArgs(3));
+ compileAndRun(sourceCode, 0, "C", bytes(), map<string, Address>{{"L", m_contractAddress}});
+ ABI_CHECK(callContractFunction("f()"), encodeArgs(3));
+ ABI_CHECK(callContractFunction("g()"), encodeArgs(3));
+ ABI_CHECK(callContractFunction("h()"), encodeArgs(1));
+ ABI_CHECK(callContractFunction("x()"), encodeArgs(1));
+ ABI_CHECK(callContractFunction("y()"), encodeArgs(3));
+}
+
BOOST_AUTO_TEST_CASE(inline_member_init)
{
char const* sourceCode = R"(
diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/422_interface_enums.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/422_interface_enums.sol
index 5513817d..1533e7ff 100644
--- a/test/libsolidity/syntaxTests/nameAndTypeResolution/422_interface_enums.sol
+++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/422_interface_enums.sol
@@ -2,4 +2,3 @@ interface I {
enum A { B, C }
}
// ----
-// TypeError: (18-33): Enumerable cannot be declared in interfaces.
diff --git a/test/libsolidity/syntaxTests/parsing/enum_from_interface.sol b/test/libsolidity/syntaxTests/parsing/enum_from_interface.sol
new file mode 100644
index 00000000..0fe0fbae
--- /dev/null
+++ b/test/libsolidity/syntaxTests/parsing/enum_from_interface.sol
@@ -0,0 +1,9 @@
+interface I {
+ enum Direction { Left, Right }
+}
+
+contract D {
+ function f() public pure returns (I.Direction) {
+ return I.Direction.Left;
+ }
+}
diff --git a/test/libsolidity/syntaxTests/parsing/enum_from_interface_in_library.sol b/test/libsolidity/syntaxTests/parsing/enum_from_interface_in_library.sol
new file mode 100644
index 00000000..8d9003eb
--- /dev/null
+++ b/test/libsolidity/syntaxTests/parsing/enum_from_interface_in_library.sol
@@ -0,0 +1,12 @@
+interface I {
+ enum Direction { Left, Right }
+}
+
+library L {
+ function f() public pure returns (I.Direction) {
+ return I.Direction.Left;
+ }
+ function g() internal pure returns (I.Direction) {
+ return I.Direction.Left;
+ }
+}
diff --git a/test/libsolidity/syntaxTests/parsing/enum_from_library.sol b/test/libsolidity/syntaxTests/parsing/enum_from_library.sol
new file mode 100644
index 00000000..ab762a82
--- /dev/null
+++ b/test/libsolidity/syntaxTests/parsing/enum_from_library.sol
@@ -0,0 +1,9 @@
+library L {
+ enum Direction { Left, Right }
+}
+
+contract D {
+ function f() public pure returns (L.Direction) {
+ return L.Direction.Left;
+ }
+}
diff --git a/test/libsolidity/syntaxTests/parsing/enum_inheritance_contract.sol b/test/libsolidity/syntaxTests/parsing/enum_inheritance_contract.sol
new file mode 100644
index 00000000..e5b98352
--- /dev/null
+++ b/test/libsolidity/syntaxTests/parsing/enum_inheritance_contract.sol
@@ -0,0 +1,9 @@
+contract C {
+ enum Direction { Left, Right }
+}
+
+contract D is C {
+ function f() public pure returns (Direction) {
+ return Direction.Left;
+ }
+}
diff --git a/test/libsolidity/syntaxTests/parsing/enum_inheritance_interface.sol b/test/libsolidity/syntaxTests/parsing/enum_inheritance_interface.sol
new file mode 100644
index 00000000..75858744
--- /dev/null
+++ b/test/libsolidity/syntaxTests/parsing/enum_inheritance_interface.sol
@@ -0,0 +1,9 @@
+interface I {
+ enum Direction { Left, Right }
+}
+
+contract D is I {
+ function f() public pure returns (Direction) {
+ return Direction.Left;
+ }
+}