Disallow overriding operators for enums

This commit is contained in:
wechman 2022-07-12 12:03:09 +02:00
parent 275a43068c
commit beae1e6263
2 changed files with 21 additions and 1 deletions

View File

@ -1812,7 +1812,6 @@ void TypeChecker::endVisit(BinaryOperation const& _operation)
Type const* commonType = leftType; Type const* commonType = leftType;
// Either the operator is user-defined or built-in. // Either the operator is user-defined or built-in.
// TODO For enums, we have compare operators. Should we disallow overriding them?
solAssert(!userDefinedOperator || !builtinResult); solAssert(!userDefinedOperator || !builtinResult);
if (!builtinResult && !userDefinedOperator) if (!builtinResult && !userDefinedOperator)
@ -3848,6 +3847,14 @@ void TypeChecker::endVisit(UsingForDirective const& _usingFor)
} }
solAssert(_usingFor.typeName()->annotation().type); solAssert(_usingFor.typeName()->annotation().type);
if (_usingFor.typeName()->annotation().type->category() == Type::Category::Enum)
m_errorReporter.typeError(
9921_error,
_usingFor.location(),
"The \"using\" directive cannot be used to attach functions to the enum type."
);
Type const* normalizedType = TypeProvider::withLocationIfReference( Type const* normalizedType = TypeProvider::withLocationIfReference(
DataLocation::Storage, DataLocation::Storage,
_usingFor.typeName()->annotation().type _usingFor.typeName()->annotation().type

View File

@ -0,0 +1,13 @@
using {add as +} for E;
enum E {
E1,
E2
}
function add(E, E) pure returns (E) {
return E.E1;
}
// ----
// TypeError 9921: (0-23): The "using" directive cannot be used to attach functions to the enum type.