Skip to content

Commit

Permalink
Reject duplicate built-in type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
cameel committed Oct 30, 2023
1 parent a28ece9 commit 6cbe55b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
20 changes: 19 additions & 1 deletion libsolidity/experimental/analysis/TypeRegistration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,25 @@ void TypeRegistration::endVisit(TypeDefinition const& _typeDefinition)
return;

if (auto const* builtin = dynamic_cast<Builtin const*>(_typeDefinition.typeExpression()))
annotation(_typeDefinition).typeConstructor = annotation(*builtin).typeConstructor;
{
auto [previousDefinitionIt, inserted] = annotation().builtinTypeDefinitions.try_emplace(
builtin->nameParameter(),
&_typeDefinition
);

if (inserted)
annotation(_typeDefinition).typeConstructor = annotation(*builtin).typeConstructor;
else
{
auto const& [builtinName, previousDefinition] = *previousDefinitionIt;
m_errorReporter.typeError(
9609_error,
_typeDefinition.location(),
SecondarySourceLocation{}.append("Previous definition:", previousDefinition->location()),
"Duplicate builtin type definition."
);
}
}
else
annotation(_typeDefinition).typeConstructor = m_typeSystem.declareTypeConstructor(
_typeDefinition.name(),
Expand Down
1 change: 1 addition & 0 deletions libsolidity/experimental/analysis/TypeRegistration.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class TypeRegistration: public ASTConstVisitor
{
std::map<PrimitiveClass, TypeClassInstantiations> primitiveClassInstantiations;
std::map<BuiltinClass, TypeClassInstantiations> builtinClassInstantiations;
std::map<std::string, TypeDefinition const*> builtinTypeDefinitions;
};
TypeRegistration(Analysis& _analysis);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pragma experimental solidity;

type void1 = __builtin("void");
type void2 = __builtin("void");

type word1 = __builtin("word");
type word2 = __builtin("word");

type fun1(T, U) = __builtin("fun");
type fun2(T, U) = __builtin("fun");
// ====
// EVMVersion: >=constantinople
// ----
// Warning 2264: (0-29): Experimental features are turned on. Do not use experimental features on live deployments.
// TypeError 9609: (63-94): Duplicate builtin type definition.
// TypeError 9609: (128-159): Duplicate builtin type definition.
// TypeError 9609: (197-232): Duplicate builtin type definition.

0 comments on commit 6cbe55b

Please sign in to comment.