From d68c526eaacc1072e8c0ddb4b3513897cce352b7 Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Fri, 3 Apr 2020 11:56:51 +0200 Subject: [PATCH] Disallow external function pointers as immutables. --- libsolidity/analysis/TypeChecker.cpp | 8 ++++++++ .../semanticTests/immutable/complexGetter.sol | 12 ----------- .../immutable/external_function_pointer.sol | 20 ------------------- .../immutable/external_function_pointer.sol | 5 +++++ 4 files changed, 13 insertions(+), 32 deletions(-) delete mode 100644 test/libsolidity/semanticTests/immutable/complexGetter.sol delete mode 100644 test/libsolidity/semanticTests/immutable/external_function_pointer.sol create mode 100644 test/libsolidity/syntaxTests/immutable/external_function_pointer.sol diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index 8e7bef338..2bdc706a2 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -481,8 +481,16 @@ bool TypeChecker::visit(VariableDeclaration const& _variable) ); } else if (_variable.immutable()) + { if (!_variable.type()->isValueType()) m_errorReporter.typeError(_variable.location(), "Immutable variables cannot have a non-value type."); + if ( + auto const* functionType = dynamic_cast(_variable.type()); + functionType && functionType->kind() == FunctionType::Kind::External + ) + m_errorReporter.typeError(_variable.location(), "Immutable variables of external function type are not yet supported."); + solAssert(_variable.type()->sizeOnStack() == 1 || m_errorReporter.hasErrors(), ""); + } if (!_variable.isStateVariable()) { diff --git a/test/libsolidity/semanticTests/immutable/complexGetter.sol b/test/libsolidity/semanticTests/immutable/complexGetter.sol deleted file mode 100644 index 851120847..000000000 --- a/test/libsolidity/semanticTests/immutable/complexGetter.sol +++ /dev/null @@ -1,12 +0,0 @@ -contract C { - function() external returns (uint, uint) immutable public x = this.f; - function f() external pure returns (uint, uint) { - return (1, 2); - } - - function test() external returns (uint, uint) { - return this.x()(); - } -} -// ---- -// test() -> 1, 2 diff --git a/test/libsolidity/semanticTests/immutable/external_function_pointer.sol b/test/libsolidity/semanticTests/immutable/external_function_pointer.sol deleted file mode 100644 index c815a18c3..000000000 --- a/test/libsolidity/semanticTests/immutable/external_function_pointer.sol +++ /dev/null @@ -1,20 +0,0 @@ -contract D { - function f() external view returns (uint256) { - return 42; - } -} -contract C { - D d; - function() external view returns(uint256) immutable z; - constructor() public { - d = new D(); - z = d.f; - } - function f() public view returns (uint256) { - assert(z.address == address(d)); - assert(z.selector == D.f.selector); - return z(); - } -} -// ---- -// f() -> 42 diff --git a/test/libsolidity/syntaxTests/immutable/external_function_pointer.sol b/test/libsolidity/syntaxTests/immutable/external_function_pointer.sol new file mode 100644 index 000000000..7ab8393cf --- /dev/null +++ b/test/libsolidity/syntaxTests/immutable/external_function_pointer.sol @@ -0,0 +1,5 @@ +contract C { + function() external immutable f; +} +// ---- +// TypeError: (17-48): Immutable variables of external function type are not yet supported.