Skip to content

Commit 41221c5

Browse files
ekpyroncameel
authored andcommitted
Merge pull request #14654 from ethereum/new-analysis-class-on-class-variables
Use type class as a sort for its type variable
2 parents 826c168 + 924a0e6 commit 41221c5

File tree

4 files changed

+9
-19
lines changed

4 files changed

+9
-19
lines changed

Diff for: libsolidity/experimental/analysis/TypeClassRegistration.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ bool TypeClassRegistration::analyze(SourceUnit const& _sourceUnit)
4242

4343
bool TypeClassRegistration::visit(TypeClassDefinition const& _typeClassDefinition)
4444
{
45-
Type typeVar = m_typeSystem.freshTypeVariable({});
46-
4745
std::variant<TypeClass, std::string> typeClassOrError = m_typeSystem.declareTypeClass(
48-
typeVar,
4946
_typeClassDefinition.name(),
5047
&_typeClassDefinition
5148
);

Diff for: libsolidity/experimental/analysis/TypeInference.cpp

+1-6
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,7 @@ TypeInference::TypeInference(Analysis& _analysis):
5454
TypeSystemHelpers helper{m_typeSystem};
5555

5656
auto declareBuiltinClass = [&](std::string _name, BuiltinClass _class) -> TypeClass {
57-
Type type = m_typeSystem.freshTypeVariable({});
58-
auto result = m_typeSystem.declareTypeClass(
59-
type,
60-
_name,
61-
nullptr
62-
);
57+
auto result = m_typeSystem.declareTypeClass(_name, nullptr);
6358
if (auto error = std::get_if<std::string>(&result))
6459
solAssert(!error, *error);
6560
TypeClass declaredClass = std::get<TypeClass>(result);

Diff for: libsolidity/experimental/ast/TypeSystem.cpp

+7-9
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ TypeSystem::TypeSystem()
127127
solAssert(false, _error);
128128
},
129129
[](TypeClass _class) -> TypeClass { return _class; }
130-
}, declareTypeClass(freshVariable({}), _name, nullptr));
130+
}, declareTypeClass(_name, nullptr, true /* _primitive */));
131131
};
132132

133133
m_primitiveTypeClasses.emplace(PrimitiveClass::Type, declarePrimitiveClass("type"));
@@ -274,20 +274,18 @@ TypeConstructor TypeSystem::declareTypeConstructor(std::string _name, std::strin
274274
return constructor;
275275
}
276276

277-
std::variant<TypeClass, std::string> TypeSystem::declareTypeClass(Type _typeVariable, std::string _name, Declaration const* _declaration)
277+
std::variant<TypeClass, std::string> TypeSystem::declareTypeClass(std::string _name, Declaration const* _declaration, bool _primitive)
278278
{
279-
TypeVariable const* typeVariable = std::get_if<TypeVariable>(&_typeVariable);
280-
if (!typeVariable)
281-
return "Invalid type variable.";
279+
TypeClass typeClass{m_typeClasses.size()};
280+
281+
Type typeVariable = (_primitive ? freshVariable({{typeClass}}) : freshTypeVariable({{typeClass}}));
282+
solAssert(std::holds_alternative<TypeVariable>(typeVariable));
282283

283-
size_t index = m_typeClasses.size();
284284
m_typeClasses.emplace_back(TypeClassInfo{
285-
_typeVariable,
285+
typeVariable,
286286
_name,
287287
_declaration
288288
});
289-
TypeClass typeClass{index};
290-
291289
return typeClass;
292290
}
293291

Diff for: libsolidity/experimental/ast/TypeSystem.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class TypeSystem
149149
return constructorInfo(constructor(_typeConstructor));
150150
}
151151

152-
std::variant<TypeClass, std::string> declareTypeClass(Type _typeVariable, std::string _name, Declaration const* _declaration);
152+
std::variant<TypeClass, std::string> declareTypeClass(std::string _name, Declaration const* _declaration, bool _primitive = false);
153153
[[nodiscard]] std::optional<std::string> instantiateClass(Type _instanceVariable, Arity _arity);
154154

155155
Type freshTypeVariable(Sort _sort);

0 commit comments

Comments
 (0)