Skip to content

Commit

Permalink
Merge pull request ethereum#13660 from ethereum/operator-releated-err…
Browse files Browse the repository at this point in the history
…or-message-tweaks

Operator-releated error message tweaks
  • Loading branch information
cameel authored Nov 7, 2022
2 parents 2cc6610 + a866aae commit b66cea1
Show file tree
Hide file tree
Showing 78 changed files with 217 additions and 197 deletions.
2 changes: 1 addition & 1 deletion libsolidity/analysis/DeclarationTypeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ void DeclarationTypeChecker::endVisit(UserDefinedTypeName const& _typeName)
m_errorReporter.fatalTypeError(
5172_error,
_typeName.location(),
"Name has to refer to a struct, enum or contract."
"Name has to refer to a user-defined type."
);
}
}
Expand Down
37 changes: 23 additions & 14 deletions libsolidity/analysis/TypeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/predicate.hpp>

#include <fmt/format.h>

#include <range/v3/algorithm/count_if.hpp>
#include <range/v3/view/drop_exactly.hpp>
#include <range/v3/view/enumerate.hpp>
Expand Down Expand Up @@ -1731,7 +1733,13 @@ bool TypeChecker::visit(UnaryOperation const& _operation)
TypeResult result = type(_operation.subExpression())->unaryOperatorResult(op);
if (!result)
{
string description = "Unary operator " + string(TokenTraits::toString(op)) + " cannot be applied to type " + subExprType->humanReadableName() + "." + (!result.message().empty() ? " " + result.message() : "");
string description = fmt::format(
"Built-in unary operator {} cannot be applied to type {}.{}",
TokenTraits::toString(op),
subExprType->humanReadableName(),
!result.message().empty() ? " " + result.message() : ""
);

if (modifying)
// Cannot just report the error, ignore the unary operator, and continue,
// because the sub-expression was already processed with requireLValue()
Expand Down Expand Up @@ -1760,9 +1768,9 @@ void TypeChecker::endVisit(BinaryOperation const& _operation)
m_errorReporter.typeError(
2271_error,
_operation.location(),
"Operator " +
"Built-in binary operator " +
string(TokenTraits::toString(_operation.getOperator())) +
" not compatible with types " +
" cannot be applied to types " +
leftType->humanReadableName() +
" and " +
rightType->humanReadableName() + "." +
Expand Down Expand Up @@ -3796,9 +3804,11 @@ void TypeChecker::endVisit(UsingForDirective const& _usingFor)
m_errorReporter.fatalTypeError(
4731_error,
path->location(),
"The function \"" + joinHumanReadable(path->path(), ".") + "\" " +
"does not have any parameters, and therefore cannot be bound to the type \"" +
(normalizedType ? normalizedType->humanReadableName() : "*") + "\"."
fmt::format(
"The function \"{}\" does not have any parameters, and therefore cannot be bound to the type \"{}\".",
joinHumanReadable(path->path(), "."),
normalizedType ? normalizedType->toString(true /* withoutDataLocation */) : "*"
)
);

FunctionType const* functionType = dynamic_cast<FunctionType const&>(*functionDefinition.type()).asBoundFunction();
Expand All @@ -3810,14 +3820,13 @@ void TypeChecker::endVisit(UsingForDirective const& _usingFor)
m_errorReporter.typeError(
3100_error,
path->location(),
"The function \"" + joinHumanReadable(path->path(), ".") + "\" "+
"cannot be bound to the type \"" + _usingFor.typeName()->annotation().type->humanReadableName() +
"\" because the type cannot be implicitly converted to the first argument" +
" of the function (\"" + functionType->selfType()->humanReadableName() + "\")" +
(
result.message().empty() ?
"." :
": " + result.message()
fmt::format(
"The function \"{}\" cannot be bound to the type \"{}\" because the type cannot "
"be implicitly converted to the first argument of the function (\"{}\"){}",
joinHumanReadable(path->path(), "."),
_usingFor.typeName()->annotation().type->toString(true /* withoutDataLocation */),
functionType->selfType()->humanReadableName(),
result.message().empty() ? "." : ": " + result.message()
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ contract C {
}
}
// ----
// TypeError 2271: (284-299): Operator + not compatible with types tuple(int_const 1,int_const 1) and tuple(int_const 2,int_const 2).
// TypeError 2271: (284-299): Built-in binary operator + cannot be applied to types tuple(int_const 1,int_const 1) and tuple(int_const 2,int_const 2).
// TypeError 9062: (284-299): Expected an inline tuple, not an expression of a tuple type.
// TypeError 2271: (334-345): Operator / not compatible with types tuple() and tuple().
// TypeError 2271: (334-345): Built-in binary operator / cannot be applied to types tuple() and tuple().
// TypeError 9062: (334-345): Expected an inline tuple, not an expression of a tuple type.
// TypeError 4907: (380-383): Unary operator ! cannot be applied to type tuple().
// TypeError 4907: (380-383): Built-in unary operator ! cannot be applied to type tuple().
// TypeError 9062: (380-383): Expected an inline tuple, not an expression of a tuple type.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ contract C {
}
}
// ----
// TypeError 2271: (144-157): Operator != not compatible with types type(contract super C) and contract C.
// TypeError 2271: (167-180): Operator != not compatible with types contract C and type(contract super C).
// TypeError 2271: (254-264): Operator != not compatible with types type(contract super C) and contract C.
// TypeError 2271: (274-284): Operator != not compatible with types contract C and type(contract super C).
// TypeError 2271: (349-359): Operator != not compatible with types type(contract super C) and contract D.
// TypeError 2271: (369-379): Operator != not compatible with types contract D and type(contract super C).
// TypeError 2271: (144-157): Built-in binary operator != cannot be applied to types type(contract super C) and contract C.
// TypeError 2271: (167-180): Built-in binary operator != cannot be applied to types contract C and type(contract super C).
// TypeError 2271: (254-264): Built-in binary operator != cannot be applied to types type(contract super C) and contract C.
// TypeError 2271: (274-284): Built-in binary operator != cannot be applied to types contract C and type(contract super C).
// TypeError 2271: (349-359): Built-in binary operator != cannot be applied to types type(contract super C) and contract D.
// TypeError 2271: (369-379): Built-in binary operator != cannot be applied to types contract D and type(contract super C).
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,25 @@ contract C {
}
}
// ----
// TypeError 2271: (49-62): Operator << not compatible with types type(contract super C) and contract C.
// TypeError 2271: (72-85): Operator >> not compatible with types type(contract super C) and contract C.
// TypeError 2271: (95-107): Operator ^ not compatible with types type(contract super C) and contract C.
// TypeError 2271: (117-129): Operator | not compatible with types type(contract super C) and contract C.
// TypeError 2271: (139-151): Operator & not compatible with types type(contract super C) and contract C.
// TypeError 2271: (162-174): Operator * not compatible with types type(contract super C) and contract C.
// TypeError 2271: (184-196): Operator / not compatible with types type(contract super C) and contract C.
// TypeError 2271: (206-218): Operator % not compatible with types type(contract super C) and contract C.
// TypeError 2271: (228-240): Operator - not compatible with types type(contract super C) and contract C.
// TypeError 2271: (250-262): Operator + not compatible with types type(contract super C) and contract C.
// TypeError 2271: (272-285): Operator ** not compatible with types type(contract super C) and contract C.
// TypeError 2271: (296-309): Operator == not compatible with types type(contract super C) and contract C.
// TypeError 2271: (319-332): Operator != not compatible with types type(contract super C) and contract C.
// TypeError 2271: (342-355): Operator >= not compatible with types type(contract super C) and contract C.
// TypeError 2271: (365-378): Operator <= not compatible with types type(contract super C) and contract C.
// TypeError 2271: (388-400): Operator < not compatible with types type(contract super C) and contract C.
// TypeError 2271: (410-422): Operator > not compatible with types type(contract super C) and contract C.
// TypeError 2271: (433-446): Operator || not compatible with types type(contract super C) and contract C.
// TypeError 2271: (456-469): Operator && not compatible with types type(contract super C) and contract C.
// TypeError 2271: (49-62): Built-in binary operator << cannot be applied to types type(contract super C) and contract C.
// TypeError 2271: (72-85): Built-in binary operator >> cannot be applied to types type(contract super C) and contract C.
// TypeError 2271: (95-107): Built-in binary operator ^ cannot be applied to types type(contract super C) and contract C.
// TypeError 2271: (117-129): Built-in binary operator | cannot be applied to types type(contract super C) and contract C.
// TypeError 2271: (139-151): Built-in binary operator & cannot be applied to types type(contract super C) and contract C.
// TypeError 2271: (162-174): Built-in binary operator * cannot be applied to types type(contract super C) and contract C.
// TypeError 2271: (184-196): Built-in binary operator / cannot be applied to types type(contract super C) and contract C.
// TypeError 2271: (206-218): Built-in binary operator % cannot be applied to types type(contract super C) and contract C.
// TypeError 2271: (228-240): Built-in binary operator - cannot be applied to types type(contract super C) and contract C.
// TypeError 2271: (250-262): Built-in binary operator + cannot be applied to types type(contract super C) and contract C.
// TypeError 2271: (272-285): Built-in binary operator ** cannot be applied to types type(contract super C) and contract C.
// TypeError 2271: (296-309): Built-in binary operator == cannot be applied to types type(contract super C) and contract C.
// TypeError 2271: (319-332): Built-in binary operator != cannot be applied to types type(contract super C) and contract C.
// TypeError 2271: (342-355): Built-in binary operator >= cannot be applied to types type(contract super C) and contract C.
// TypeError 2271: (365-378): Built-in binary operator <= cannot be applied to types type(contract super C) and contract C.
// TypeError 2271: (388-400): Built-in binary operator < cannot be applied to types type(contract super C) and contract C.
// TypeError 2271: (410-422): Built-in binary operator > cannot be applied to types type(contract super C) and contract C.
// TypeError 2271: (433-446): Built-in binary operator || cannot be applied to types type(contract super C) and contract C.
// TypeError 2271: (456-469): Built-in binary operator && cannot be applied to types type(contract super C) and contract C.
// TypeError 4247: (480-485): Expression has to be an lvalue.
// TypeError 7366: (480-493): Operator -= not compatible with types type(contract super C) and contract C.
// TypeError 4247: (503-508): Expression has to be an lvalue.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error E(uint);
function f(E x) pure returns (uint) {}
// ----
// TypeError 5172: (26-27): Name has to refer to a struct, enum or contract.
// TypeError 5172: (26-27): Name has to refer to a user-defined type.
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ contract C {
}

// ----
// TypeError 2271: (86-116): Operator << not compatible with types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (126-156): Operator >> not compatible with types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (166-195): Operator ^ not compatible with types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (205-234): Operator | not compatible with types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (244-273): Operator & not compatible with types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (284-313): Operator * not compatible with types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (323-352): Operator / not compatible with types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (362-391): Operator % not compatible with types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (401-430): Operator + not compatible with types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (440-469): Operator - not compatible with types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (480-510): Operator == not compatible with types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (520-550): Operator != not compatible with types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (560-590): Operator >= not compatible with types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (600-630): Operator <= not compatible with types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (640-669): Operator < not compatible with types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (679-708): Operator > not compatible with types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (719-749): Operator || not compatible with types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (759-789): Operator && not compatible with types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (86-116): Built-in binary operator << cannot be applied to types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (126-156): Built-in binary operator >> cannot be applied to types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (166-195): Built-in binary operator ^ cannot be applied to types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (205-234): Built-in binary operator | cannot be applied to types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (244-273): Built-in binary operator & cannot be applied to types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (284-313): Built-in binary operator * cannot be applied to types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (323-352): Built-in binary operator / cannot be applied to types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (362-391): Built-in binary operator % cannot be applied to types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (401-430): Built-in binary operator + cannot be applied to types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (440-469): Built-in binary operator - cannot be applied to types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (480-510): Built-in binary operator == cannot be applied to types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (520-550): Built-in binary operator != cannot be applied to types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (560-590): Built-in binary operator >= cannot be applied to types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (600-630): Built-in binary operator <= cannot be applied to types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (640-669): Built-in binary operator < cannot be applied to types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (679-708): Built-in binary operator > cannot be applied to types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (719-749): Built-in binary operator || cannot be applied to types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
// TypeError 2271: (759-789): Built-in binary operator && cannot be applied to types error MyCustomError(uint256,bool) and error MyCustomError(uint256,bool).
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ contract C {

// ----
// TypeError 4247: (86-99): Expression has to be an lvalue.
// TypeError 9767: (86-101): Unary operator ++ cannot be applied to type error MyCustomError(uint256,bool).
// TypeError 9767: (86-101): Built-in unary operator ++ cannot be applied to type error MyCustomError(uint256,bool).
2 changes: 1 addition & 1 deletion test/libsolidity/syntaxTests/errors/using_2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ contract C {
}
}
// ----
// TypeError 5172: (91-92): Name has to refer to a struct, enum or contract.
// TypeError 5172: (91-92): Name has to refer to a user-defined type.
2 changes: 1 addition & 1 deletion test/libsolidity/syntaxTests/errors/weird3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ contract C {
E x;
}
// ----
// TypeError 5172: (29-30): Name has to refer to a struct, enum or contract.
// TypeError 5172: (29-30): Name has to refer to a user-defined type.
2 changes: 1 addition & 1 deletion test/libsolidity/syntaxTests/errors/weird4.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ contract C {
}
}
// ----
// TypeError 5172: (64-65): Name has to refer to a struct, enum or contract.
// TypeError 5172: (64-65): Name has to refer to a user-defined type.
Loading

0 comments on commit b66cea1

Please sign in to comment.