From 0f1a63c3fa5db2c55e0a94b72653a1f1172e1ca9 Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Fri, 11 Mar 2022 17:01:30 +0100 Subject: [PATCH] Fix import directive visits in type checker and view pure checker. --- Changelog.md | 1 + libsolidity/analysis/TypeChecker.cpp | 5 +++++ libsolidity/analysis/TypeChecker.h | 2 ++ libsolidity/analysis/ViewPureChecker.cpp | 5 +++++ libsolidity/analysis/ViewPureChecker.h | 2 ++ libsolidity/formal/SMTEncoder.cpp | 6 ++++++ libsolidity/formal/SMTEncoder.h | 1 + .../multiSource/import_overloaded_function.sol | 15 +++++++++++++++ 8 files changed, 37 insertions(+) create mode 100644 test/libsolidity/semanticTests/multiSource/import_overloaded_function.sol diff --git a/Changelog.md b/Changelog.md index c7bf10c8a..a23843c45 100644 --- a/Changelog.md +++ b/Changelog.md @@ -14,6 +14,7 @@ Compiler Features: Bugfixes: * General: Fix internal error for locales with unusual capitalization rules. Locale set in the environment is now completely ignored. + * Type Checker: Fix incorrect type checker errors when importing overloaded functions. * Yul IR Code Generation: Optimize embedded creation code with correct settings. This fixes potential mismatches between the constructor code of a contract compiled in isolation and the bytecode in ``type(C).creationCode``, resp. the bytecode used for ``new C(...)``. diff --git a/libsolidity/analysis/TypeChecker.cpp b/libsolidity/analysis/TypeChecker.cpp index 379033117..4bf30d4eb 100644 --- a/libsolidity/analysis/TypeChecker.cpp +++ b/libsolidity/analysis/TypeChecker.cpp @@ -268,6 +268,11 @@ TypePointers TypeChecker::typeCheckMetaTypeFunctionAndRetrieveReturnType(Functio return {TypeProvider::meta(dynamic_cast(*firstArgType).actualType())}; } +bool TypeChecker::visit(ImportDirective const&) +{ + return false; +} + void TypeChecker::endVisit(InheritanceSpecifier const& _inheritance) { auto base = dynamic_cast(&dereference(_inheritance.name())); diff --git a/libsolidity/analysis/TypeChecker.h b/libsolidity/analysis/TypeChecker.h index 969ee4394..2ef6b818b 100644 --- a/libsolidity/analysis/TypeChecker.h +++ b/libsolidity/analysis/TypeChecker.h @@ -125,6 +125,8 @@ private: FunctionType const* _functionType ); + bool visit(ImportDirective const&) override; + void endVisit(InheritanceSpecifier const& _inheritance) override; void endVisit(ModifierDefinition const& _modifier) override; bool visit(FunctionDefinition const& _function) override; diff --git a/libsolidity/analysis/ViewPureChecker.cpp b/libsolidity/analysis/ViewPureChecker.cpp index 3179a8eb7..7483af892 100644 --- a/libsolidity/analysis/ViewPureChecker.cpp +++ b/libsolidity/analysis/ViewPureChecker.cpp @@ -134,6 +134,11 @@ bool ViewPureChecker::check() return !m_errors; } +bool ViewPureChecker::visit(ImportDirective const&) +{ + return false; +} + bool ViewPureChecker::visit(FunctionDefinition const& _funDef) { solAssert(!m_currentFunction, ""); diff --git a/libsolidity/analysis/ViewPureChecker.h b/libsolidity/analysis/ViewPureChecker.h index dfbb2f747..b0abe8fd8 100644 --- a/libsolidity/analysis/ViewPureChecker.h +++ b/libsolidity/analysis/ViewPureChecker.h @@ -50,6 +50,8 @@ private: langutil::SourceLocation location; }; + bool visit(ImportDirective const&) override; + bool visit(FunctionDefinition const& _funDef) override; void endVisit(FunctionDefinition const& _funDef) override; bool visit(ModifierDefinition const& _modifierDef) override; diff --git a/libsolidity/formal/SMTEncoder.cpp b/libsolidity/formal/SMTEncoder.cpp index e699e28b8..7cc978d28 100644 --- a/libsolidity/formal/SMTEncoder.cpp +++ b/libsolidity/formal/SMTEncoder.cpp @@ -115,6 +115,12 @@ void SMTEncoder::endVisit(ContractDefinition const& _contract) m_context.popSolver(); } +bool SMTEncoder::visit(ImportDirective const&) +{ + // do not visit because the identifier therein will confuse us. + return false; +} + void SMTEncoder::endVisit(VariableDeclaration const& _varDecl) { // State variables are handled by the constructor. diff --git a/libsolidity/formal/SMTEncoder.h b/libsolidity/formal/SMTEncoder.h index fe84189be..84a80461d 100644 --- a/libsolidity/formal/SMTEncoder.h +++ b/libsolidity/formal/SMTEncoder.h @@ -136,6 +136,7 @@ protected: // because the order of expression evaluation is undefined // TODO: or just force a certain order, but people might have a different idea about that. + bool visit(ImportDirective const& _node) override; bool visit(ContractDefinition const& _node) override; void endVisit(ContractDefinition const& _node) override; void endVisit(VariableDeclaration const& _node) override; diff --git a/test/libsolidity/semanticTests/multiSource/import_overloaded_function.sol b/test/libsolidity/semanticTests/multiSource/import_overloaded_function.sol new file mode 100644 index 000000000..503f65680 --- /dev/null +++ b/test/libsolidity/semanticTests/multiSource/import_overloaded_function.sol @@ -0,0 +1,15 @@ +==== Source: A ==== +function sub(uint256 x, uint256 y) pure returns (uint) { return 1; } +function sub(uint256 x) pure returns (uint) { return 2; } +==== Source: B ==== +import {sub} from "A"; +contract C +{ + function f() public pure returns (uint, uint) { + return (sub(1, 2), sub(2)); + } +} +// ==== +// compileViaYul: also +// ---- +// f() -> 1, 2