From d5102c1db7cd2334e127ff684a6ecdd6aff156c6 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 17 Mar 2017 16:37:02 +0000 Subject: [PATCH] Disallow constructor in interfaces --- docs/contracts.rst | 1 + libsolidity/analysis/TypeChecker.cpp | 3 +++ test/libsolidity/SolidityNameAndTypeResolution.cpp | 2 +- 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/contracts.rst b/docs/contracts.rst index 921d28706..28c003bda 100644 --- a/docs/contracts.rst +++ b/docs/contracts.rst @@ -931,6 +931,7 @@ Interfaces Interfaces are similar to abstract contracts, but they cannot have any functions implemented. There are further restrictions: #. Cannot inherit other contracts or interfaces. +#. Cannot define constructor. #. Cannot define variables. #. Cannot define structs. #. Cannot define enums. diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index 3dffecdba..87951003a 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -463,6 +463,9 @@ bool TypeChecker::visit(FunctionDefinition const& _function) typeError(_function.location(), "Functions in interfaces cannot have an implementation."); _function.body().accept(*this); } + if (_function.isConstructor()) + if (m_scope->contractKind() == ContractDefinition::ContractKind::Interface) + typeError(_function.location(), "Constructor cannot be defined in interfaces."); return false; } diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index f6c875f18..39306f848 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -5343,7 +5343,7 @@ BOOST_AUTO_TEST_CASE(interface_constructor) function I(); } )"; - success(text); + CHECK_ERROR(text, TypeError, "Constructor cannot be defined in interfaces"); } BOOST_AUTO_TEST_CASE(interface_functions)