From 0891b9451b1fab88383c4865ffe861a4a7ffea85 Mon Sep 17 00:00:00 2001 From: Mathias Baumann Date: Thu, 14 Nov 2019 12:48:15 +0100 Subject: [PATCH] Disallow variables of library types --- Changelog.md | 1 + libsolidity/analysis/TypeChecker.cpp | 3 +++ test/libsolidity/ABIJson/library_function.sol | 13 +++++++++---- .../301_library_instances_cannot_be_used.sol | 1 + .../typeChecking/library_instances.sol | 14 ++++++++++++++ 5 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 test/libsolidity/syntaxTests/nameAndTypeResolution/typeChecking/library_instances.sol diff --git a/Changelog.md b/Changelog.md index d3294901f..e61ffff0f 100644 --- a/Changelog.md +++ b/Changelog.md @@ -12,6 +12,7 @@ Compiler Features: * SMTChecker: Support inheritance and function overriding. * Standard JSON Interface: Output the storage layout of a contract when artifact ``storageLayout`` is requested. * TypeChecker: List possible candidates when overload resolution fails. + * TypeChecker: Disallow variables of library types. Bugfixes: diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index 73bf1b18e..262db9d1d 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -450,6 +450,9 @@ bool TypeChecker::visit(VariableDeclaration const& _variable) TypePointer varType = _variable.annotation().type; solAssert(!!varType, "Variable type not provided."); + if (auto contractType = dynamic_cast(varType)) + if (contractType->contractDefinition().isLibrary()) + m_errorReporter.typeError(_variable.location(), "The type of a variable cannot be a library."); if (_variable.value()) expectType(*_variable.value(), *varType); if (_variable.isConstant()) diff --git a/test/libsolidity/ABIJson/library_function.sol b/test/libsolidity/ABIJson/library_function.sol index 2aad8e164..f61a1f8a9 100644 --- a/test/libsolidity/ABIJson/library_function.sol +++ b/test/libsolidity/ABIJson/library_function.sol @@ -1,9 +1,14 @@ +contract X {} library test { struct StructType { uint a; } - function f(StructType storage b, uint[] storage c, test d) public returns (uint[] memory e, StructType storage f) { f = f; } - function f1(uint[] memory c, test d) public pure returns (uint[] memory e) { } + function f(StructType storage b, uint[] storage c, X d) public returns (uint[] memory e, StructType storage f) { f = f; } + function f1(uint[] memory c, X d) public pure returns (uint[] memory e) { } } // ---- +// :X +// [] +// +// // :test // [ // { @@ -16,9 +21,9 @@ library test { // "type": "uint256[]" // }, // { -// "internalType": "library test", +// "internalType": "contract X", // "name": "d", -// "type": "test" +// "type": "X" // } // ], // "name": "f1", diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/301_library_instances_cannot_be_used.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/301_library_instances_cannot_be_used.sol index dcf11a6e3..3eebc7926 100644 --- a/test/libsolidity/syntaxTests/nameAndTypeResolution/301_library_instances_cannot_be_used.sol +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/301_library_instances_cannot_be_used.sol @@ -6,4 +6,5 @@ contract test { } } // ---- +// TypeError: (87-90): The type of a variable cannot be a library. // TypeError: (100-103): Member "l" not found or not visible after argument-dependent lookup in library L. diff --git a/test/libsolidity/syntaxTests/nameAndTypeResolution/typeChecking/library_instances.sol b/test/libsolidity/syntaxTests/nameAndTypeResolution/typeChecking/library_instances.sol new file mode 100644 index 000000000..c28bec419 --- /dev/null +++ b/test/libsolidity/syntaxTests/nameAndTypeResolution/typeChecking/library_instances.sol @@ -0,0 +1,14 @@ +library X { } + +contract Y { + X abc; + function foo(X param) private view + { + X ofg; + ofg = abc; + } +} +// ---- +// TypeError: (29-34): The type of a variable cannot be a library. +// TypeError: (50-57): The type of a variable cannot be a library. +// TypeError: (77-82): The type of a variable cannot be a library.