More descriptive error message in TypeChecker::visit(UnaryOperation)

This commit is contained in:
wechman 2022-08-01 11:25:21 +02:00
parent e001f8346b
commit 6fcd717ab4
3 changed files with 14 additions and 11 deletions

View File

@ -1731,24 +1731,24 @@ bool TypeChecker::visit(UnaryOperation const& _operation)
// Check if the operator is built-in or user-defined.
FunctionDefinition const* userDefinedOperator = subExprType->userDefinedOperator(
Result<FunctionDefinition const*> userDefinedOperatorResult = subExprType->userDefinedOperator(
_operation.getOperator(),
*currentDefinitionScope(),
true // _unaryOperation
);
_operation.annotation().userDefinedFunction = userDefinedOperator;
_operation.annotation().userDefinedFunction = userDefinedOperatorResult;
FunctionType const* userDefinedFunctionType = nullptr;
if (userDefinedOperator)
if (userDefinedOperatorResult)
userDefinedFunctionType = &dynamic_cast<FunctionType const&>(
userDefinedOperator->libraryFunction() ?
*userDefinedOperator->typeViaContractName() :
*userDefinedOperator->type()
userDefinedOperatorResult.get()->libraryFunction() ?
*userDefinedOperatorResult.get()->typeViaContractName() :
*userDefinedOperatorResult.get()->type()
);
TypeResult builtinResult = subExprType->unaryOperatorResult(op);
solAssert(!builtinResult || !userDefinedOperator);
if (userDefinedOperator)
solAssert(!builtinResult || !userDefinedOperatorResult);
if (userDefinedOperatorResult)
{
solAssert(userDefinedFunctionType->parameterTypes().size() == 1);
solAssert(userDefinedFunctionType->returnParameterTypes().size() == 1);
@ -1762,7 +1762,10 @@ bool TypeChecker::visit(UnaryOperation const& _operation)
_operation.annotation().type = builtinResult;
else
{
string description = "Unary operator " + string(TokenTraits::toString(op)) + " cannot be applied to type " + subExprType->humanReadableName() + "." + (!builtinResult.message().empty() ? " " + builtinResult.message() : "");
string description =
"Unary operator " + string(TokenTraits::toString(op)) + " cannot be applied to type " + subExprType->humanReadableName() + "." +
(!builtinResult.message().empty() ? " " + builtinResult.message() : "") +
(!userDefinedOperatorResult.message().empty() ? " " + userDefinedOperatorResult.message() : "");
if (modifying)
// Cannot just report the error, ignore the unary operator, and continue,
// because the sub-expression was already processed with requireLValue()

View File

@ -44,6 +44,6 @@ function f() pure {
// TypeError 7995: (90-92): The function "lt" needs to return exactly one value of type bool to be used for the operator <.
// TypeError 2271: (492-517): Operator + not compatible with types Int and Int. No matching user-defined operator found.
// TypeError 2271: (523-548): Operator / not compatible with types Int and Int. No matching user-defined operator found.
// TypeError 4907: (554-566): Unary operator - cannot be applied to type Int
// TypeError 4907: (554-566): Unary operator - cannot be applied to type Int. No matching user-defined operator found.
// TypeError 2271: (572-597): Operator < not compatible with types Int and Int. No matching user-defined operator found.
// TypeError 2271: (603-628): Operator > not compatible with types Int and Int. No matching user-defined operator found.

View File

@ -15,4 +15,4 @@ contract C {
// ----
// TypeError 1147: (32-38): The function "bitnot" needs to have exactly one parameter to be used for the operator ~.
// TypeError 4907: (186-198): Unary operator ~ cannot be applied to type Int
// TypeError 4907: (186-198): Unary operator ~ cannot be applied to type Int. No matching user-defined operator found.