From cf810ebc8b969cd03a9fc7d8eb457f1414c1a115 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Fri, 29 Sep 2023 16:22:36 +0200 Subject: [PATCH 1/6] TypeSystem: Fix constructor creating two Function types instead of Function and Itself --- libsolidity/experimental/ast/TypeSystem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libsolidity/experimental/ast/TypeSystem.cpp b/libsolidity/experimental/ast/TypeSystem.cpp index 717ecd4fe087..5a8efadbb9d1 100644 --- a/libsolidity/experimental/ast/TypeSystem.cpp +++ b/libsolidity/experimental/ast/TypeSystem.cpp @@ -154,7 +154,7 @@ TypeSystem::TypeSystem() Sort typeSort{{classType}}; m_typeConstructors.at(m_primitiveTypeConstructors.at(PrimitiveType::TypeFunction).m_index).arities = {Arity{std::vector{{typeSort},{typeSort}}, classType}}; m_typeConstructors.at(m_primitiveTypeConstructors.at(PrimitiveType::Function).m_index).arities = {Arity{std::vector{{typeSort, typeSort}}, classType}}; - m_typeConstructors.at(m_primitiveTypeConstructors.at(PrimitiveType::Function).m_index).arities = {Arity{std::vector{{typeSort, typeSort}}, classType}}; + m_typeConstructors.at(m_primitiveTypeConstructors.at(PrimitiveType::Itself).m_index).arities = {Arity{std::vector{{typeSort, typeSort}}, classType}}; } experimental::Type TypeSystem::freshVariable(Sort _sort) From a77bb5a366f4d93b2c216db599137f7643341a5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Thu, 12 Oct 2023 21:06:19 +0200 Subject: [PATCH 2/6] TypeEnvironment::instantiate(): Fix index() being used on the Type variant rather than on the type variable --- libsolidity/experimental/ast/TypeSystem.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/libsolidity/experimental/ast/TypeSystem.cpp b/libsolidity/experimental/ast/TypeSystem.cpp index 5a8efadbb9d1..a7f21874be25 100644 --- a/libsolidity/experimental/ast/TypeSystem.cpp +++ b/libsolidity/experimental/ast/TypeSystem.cpp @@ -171,9 +171,10 @@ experimental::Type TypeSystem::freshTypeVariable(Sort _sort) std::vector TypeEnvironment::instantiate(TypeVariable _variable, Type _type) { - for (auto typeVar: TypeEnvironmentHelpers{*this}.typeVars(_type)) - if (typeVar.index() == _variable.index()) - return {UnificationFailure{RecursiveUnification{_variable, _type}}}; + for (auto const& maybeTypeVar: TypeEnvironmentHelpers{*this}.typeVars(_type)) + if (auto const* typeVar = std::get_if(&maybeTypeVar)) + if (typeVar->index() == _variable.index()) + return {UnificationFailure{RecursiveUnification{_variable, _type}}}; Sort typeSort = sort(_type); if (!(_variable.sort() <= typeSort)) { From 1a7a88ab6d6359f0d6d463ea96014274ad048729 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Wed, 11 Oct 2023 13:55:46 +0200 Subject: [PATCH 3/6] TypeEnvironment::fresh(): Fix uint64_t used for IDs --- libsolidity/experimental/ast/TypeSystem.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libsolidity/experimental/ast/TypeSystem.cpp b/libsolidity/experimental/ast/TypeSystem.cpp index a7f21874be25..5413e97d61d4 100644 --- a/libsolidity/experimental/ast/TypeSystem.cpp +++ b/libsolidity/experimental/ast/TypeSystem.cpp @@ -307,7 +307,7 @@ experimental::Type TypeSystem::type(TypeConstructor _constructor, std::vector mapping; + std::unordered_map mapping; auto freshImpl = [&](Type _type, auto _recurse) -> Type { return std::visit(util::GenericVisitor{ [&](TypeConstant const& _type) -> Type { From c2b701aaad0705ac76e3091859f27bec4fb64f7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Wed, 18 Oct 2023 14:40:45 +0200 Subject: [PATCH 4/6] TypeSystem::unify(): There should be no variables with same index but different sorts --- libsolidity/experimental/ast/TypeSystem.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/libsolidity/experimental/ast/TypeSystem.cpp b/libsolidity/experimental/ast/TypeSystem.cpp index 5413e97d61d4..2d252ad0ddfd 100644 --- a/libsolidity/experimental/ast/TypeSystem.cpp +++ b/libsolidity/experimental/ast/TypeSystem.cpp @@ -49,10 +49,7 @@ std::vector TypeEnvironment::unify(Type _a, std::visit(util::GenericVisitor{ [&](TypeVariable _left, TypeVariable _right) { if (_left.index() == _right.index()) - { - if (_left.sort() != _right.sort()) - unificationFailure(); - } + solAssert(_left.sort() == _right.sort()); else { if (_left.sort() <= _right.sort()) From f8fd4f2ec46ffe8783d9c7a61ac4c66817111b73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Fri, 13 Oct 2023 22:35:56 +0200 Subject: [PATCH 5/6] DebugWarner::visitNode(): Don't print the sort - typeToString() already prints it --- libsolidity/experimental/analysis/DebugWarner.cpp | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/libsolidity/experimental/analysis/DebugWarner.cpp b/libsolidity/experimental/analysis/DebugWarner.cpp index f99bd486768c..2b5c23b9bed0 100644 --- a/libsolidity/experimental/analysis/DebugWarner.cpp +++ b/libsolidity/experimental/analysis/DebugWarner.cpp @@ -39,19 +39,12 @@ bool DebugWarner::analyze(ASTNode const& _astRoot) bool DebugWarner::visitNode(ASTNode const& _node) { - auto const& typeInferenceAnnotation = m_analysis.annotation(_node); - if (typeInferenceAnnotation.type) - { - Type type = *typeInferenceAnnotation.type; - Sort sort = m_analysis.typeSystem().env().sort(type); - std::string sortString; - if (sort.classes.size() != 1 || *sort.classes.begin() != m_analysis.typeSystem().primitiveClass(PrimitiveClass::Type)) - sortString = ":" + TypeSystemHelpers{m_analysis.typeSystem()}.sortToString(m_analysis.typeSystem().env().sort(type)); + std::optional const& inferredType = m_analysis.annotation(_node).type; + if (inferredType.has_value()) m_errorReporter.info( 4164_error, _node.location(), - "Inferred type: " + TypeEnvironmentHelpers{m_analysis.typeSystem().env()}.typeToString(type) + sortString + "Inferred type: " + TypeEnvironmentHelpers{m_analysis.typeSystem().env()}.typeToString(*inferredType) ); - } return true; } From 9ac8ba4e9aaed4737b8c441556f0ba685a2151ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20=C5=9Aliwak?= Date: Fri, 13 Oct 2023 23:43:48 +0200 Subject: [PATCH 6/6] TypeSystem::resolveRecursive(): Fix unresolved type variables being returned --- libsolidity/experimental/ast/TypeSystem.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/libsolidity/experimental/ast/TypeSystem.cpp b/libsolidity/experimental/ast/TypeSystem.cpp index 2d252ad0ddfd..c3806dbc5c71 100644 --- a/libsolidity/experimental/ast/TypeSystem.cpp +++ b/libsolidity/experimental/ast/TypeSystem.cpp @@ -195,19 +195,19 @@ experimental::Type TypeEnvironment::resolve(Type _type) const experimental::Type TypeEnvironment::resolveRecursive(Type _type) const { return std::visit(util::GenericVisitor{ - [&](TypeConstant const& _type) -> Type { + [&](TypeConstant const& _typeConstant) -> Type { return TypeConstant{ - _type.constructor, - _type.arguments | ranges::views::transform([&](Type _argType) { + _typeConstant.constructor, + _typeConstant.arguments | ranges::views::transform([&](Type const& _argType) { return resolveRecursive(_argType); }) | ranges::to> }; }, - [&](TypeVariable const&) -> Type { - return _type; + [](TypeVariable const& _typeVar) -> Type { + return _typeVar; }, - [&](std::monostate) -> Type { - return _type; + [](std::monostate _nothing) -> Type { + return _nothing; } }, resolve(_type)); }