From 52c49aebe80ada117f3244e73eb5e643bbbfafe1 Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 1 Dec 2020 10:24:50 +0100 Subject: [PATCH] Disallow meta type on super. --- Changelog.md | 1 + libsolidity/analysis/TypeChecker.cpp | 26 +++++-------------- .../metaTypes/name_other_contract.sol | 4 --- .../metaTypes/codeAccess_super.sol | 3 +-- .../metaTypes/interfaceid_super.sol | 2 +- .../metaTypes/super_name.sol | 3 +-- .../syntaxTests/metaTypes/typeRecursive.sol | 1 - .../syntaxTests/tryCatch/no_special.sol | 1 - 8 files changed, 11 insertions(+), 30 deletions(-) rename test/libsolidity/{semanticTests => syntaxTests}/metaTypes/super_name.sol (87%) diff --git a/Changelog.md b/Changelog.md index 0f22b00e0..34f4f434f 100644 --- a/Changelog.md +++ b/Changelog.md @@ -17,6 +17,7 @@ Breaking Changes: * Type System: Explicit conversions from literals to integer type is as strict as implicit conversions. * Type System: Explicit conversions from literals to enums are only allowed if the value fits in the enum. * Type System: Declarations with the name ``this``, ``super`` and ``_`` are disallowed, with the exception of public functions and events. + * Type System: Disallow ``type(super)``. * Command Line Interface: JSON fields `abi`, `devdoc`, `userdoc` and `storage-layout` are now sub-objects rather than strings. Language Features: diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index 46b0b04f2..8eb390183 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -226,16 +226,13 @@ TypePointers TypeChecker::typeCheckMetaTypeFunctionAndRetrieveReturnType(Functio { vector> arguments = _functionCall.arguments(); if (arguments.size() != 1) - { - m_errorReporter.typeError( + m_errorReporter.fatalTypeError( 8885_error, _functionCall.location(), "This function takes one argument, but " + toString(arguments.size()) + " were provided." ); - return {}; - } TypePointer firstArgType = type(*arguments.front()); bool wrongType = false; @@ -243,26 +240,22 @@ TypePointers TypeChecker::typeCheckMetaTypeFunctionAndRetrieveReturnType(Functio { TypeType const* typeTypePtr = dynamic_cast(firstArgType); Type::Category typeCategory = typeTypePtr->actualType()->category(); - if ( - typeCategory != Type::Category::Contract && - typeCategory != Type::Category::Integer - ) + if (auto const* contractType = dynamic_cast(typeTypePtr->actualType())) + wrongType = contractType->isSuper(); + else if (typeCategory != Type::Category::Integer) wrongType = true; } else wrongType = true; if (wrongType) - { - m_errorReporter.typeError( + m_errorReporter.fatalTypeError( 4259_error, arguments.front()->location(), "Invalid type for argument in the function call. " "A contract type or an integer type is required, but " + type(*arguments.front())->toString(true) + " provided." ); - return {}; - } return {TypeProvider::meta(dynamic_cast(*firstArgType).actualType())}; } @@ -2886,6 +2879,7 @@ bool TypeChecker::visit(MemberAccess const& _memberAccess) { annotation.isPure = true; ContractType const& accessedContractType = dynamic_cast(*magicType->typeArgument()); + solAssert(!accessedContractType.isSuper(), ""); if ( memberName == "runtimeCode" && !accessedContractType.immutableVariables().empty() @@ -2895,13 +2889,7 @@ bool TypeChecker::visit(MemberAccess const& _memberAccess) _memberAccess.location(), "\"runtimeCode\" is not available for contracts containing immutable variables." ); - if (accessedContractType.isSuper()) - m_errorReporter.typeError( - 3625_error, - _memberAccess.location(), - "\"creationCode\" and \"runtimeCode\" are not available for the \"super\" contract." - ); - else if (m_currentContract) + if (m_currentContract) { // TODO in the same way as with ``new``, // this is not properly detecting creation-cycles if they go through diff --git a/test/libsolidity/semanticTests/metaTypes/name_other_contract.sol b/test/libsolidity/semanticTests/metaTypes/name_other_contract.sol index 9eb7ee38e..f4b05fa13 100644 --- a/test/libsolidity/semanticTests/metaTypes/name_other_contract.sol +++ b/test/libsolidity/semanticTests/metaTypes/name_other_contract.sol @@ -21,13 +21,9 @@ contract Test is C { function i() public pure returns (string memory) { return type(I).name; } - function j() public pure returns (string memory) { - return type(super).name; - } } // ---- // c() -> 0x20, 1, "C" // a() -> 0x20, 1, "A" // i() -> 0x20, 1, "I" -// j() -> 0x20, 1, "C" diff --git a/test/libsolidity/syntaxTests/metaTypes/codeAccess_super.sol b/test/libsolidity/syntaxTests/metaTypes/codeAccess_super.sol index 942d37824..e7b3a6845 100644 --- a/test/libsolidity/syntaxTests/metaTypes/codeAccess_super.sol +++ b/test/libsolidity/syntaxTests/metaTypes/codeAccess_super.sol @@ -10,5 +10,4 @@ contract SuperTest is Other { } } // ---- -// TypeError 3625: (172-196): "creationCode" and "runtimeCode" are not available for the "super" contract. -// TypeError 3625: (272-295): "creationCode" and "runtimeCode" are not available for the "super" contract. +// TypeError 4259: (177-182): Invalid type for argument in the function call. A contract type or an integer type is required, but type(contract super SuperTest) provided. diff --git a/test/libsolidity/syntaxTests/metaTypes/interfaceid_super.sol b/test/libsolidity/syntaxTests/metaTypes/interfaceid_super.sol index d96a43003..328870447 100644 --- a/test/libsolidity/syntaxTests/metaTypes/interfaceid_super.sol +++ b/test/libsolidity/syntaxTests/metaTypes/interfaceid_super.sol @@ -14,4 +14,4 @@ abstract contract Test is ERC165 { } } // ---- -// TypeError 9582: (587-610): Member "interfaceID" not found or not visible after argument-dependent lookup in type(contract super Test). +// TypeError 4259: (592-597): Invalid type for argument in the function call. A contract type or an integer type is required, but type(contract super Test) provided. diff --git a/test/libsolidity/semanticTests/metaTypes/super_name.sol b/test/libsolidity/syntaxTests/metaTypes/super_name.sol similarity index 87% rename from test/libsolidity/semanticTests/metaTypes/super_name.sol rename to test/libsolidity/syntaxTests/metaTypes/super_name.sol index ce5490bad..120c60dd9 100644 --- a/test/libsolidity/semanticTests/metaTypes/super_name.sol +++ b/test/libsolidity/syntaxTests/metaTypes/super_name.sol @@ -49,6 +49,5 @@ contract D is B, C { return true; } } - // ---- -// f() -> true +// TypeError 4259: (440-445): Invalid type for argument in the function call. A contract type or an integer type is required, but type(contract super B) provided. diff --git a/test/libsolidity/syntaxTests/metaTypes/typeRecursive.sol b/test/libsolidity/syntaxTests/metaTypes/typeRecursive.sol index 4c21e8054..c28d2fe05 100644 --- a/test/libsolidity/syntaxTests/metaTypes/typeRecursive.sol +++ b/test/libsolidity/syntaxTests/metaTypes/typeRecursive.sol @@ -5,4 +5,3 @@ contract Test { } // ---- // TypeError 4259: (65-75): Invalid type for argument in the function call. A contract type or an integer type is required, but type(contract Test) provided. -// TypeError 4259: (60-76): Invalid type for argument in the function call. A contract type or an integer type is required, but tuple() provided. diff --git a/test/libsolidity/syntaxTests/tryCatch/no_special.sol b/test/libsolidity/syntaxTests/tryCatch/no_special.sol index 858823afa..6cfed6eda 100644 --- a/test/libsolidity/syntaxTests/tryCatch/no_special.sol +++ b/test/libsolidity/syntaxTests/tryCatch/no_special.sol @@ -15,4 +15,3 @@ contract C { // TypeError 5347: (72-76): Try can only be used with external function calls and contract creation calls. // TypeError 2536: (119-128): Try can only be used with external function calls and contract creation calls. // TypeError 4259: (176-183): Invalid type for argument in the function call. A contract type or an integer type is required, but type(address) provided. -// TypeError 2536: (171-184): Try can only be used with external function calls and contract creation calls.