"Operator has not been user-defined." is displayed only is operator can be user-defined

This commit is contained in:
wechman 2022-09-01 14:57:33 +02:00
parent 5942912bf1
commit dcce3fa89b
6 changed files with 58 additions and 8 deletions

View File

@ -63,6 +63,7 @@ set(sources
ast/CallGraph.cpp
ast/CallGraph.h
ast/ExperimentalFeatures.h
ast/OverridableOperators.h
ast/Types.cpp
ast/Types.h
ast/TypeProvider.cpp

View File

@ -0,0 +1,32 @@
#pragma once
#include <liblangutil/Token.h>
#include <vector>
namespace solidity::frontend
{
std::vector<langutil::Token> const overridableOperators = {
langutil::Token::BitOr,
langutil::Token::BitAnd,
langutil::Token::BitXor,
langutil::Token::Add,
langutil::Token::Sub,
langutil::Token::Mul,
langutil::Token::Div,
langutil::Token::Mod,
langutil::Token::Equal,
langutil::Token::NotEqual,
langutil::Token::LessThan,
langutil::Token::GreaterThan,
langutil::Token::LessThanOrEqual,
langutil::Token::GreaterThanOrEqual,
langutil::Token::BitNot,
langutil::Token::SHL,
langutil::Token::SAR,
langutil::Token::Exp,
langutil::Token::Not
};
}

View File

@ -25,6 +25,7 @@
#include <libsolidity/ast/AST.h>
#include <libsolidity/ast/TypeProvider.h>
#include <libsolidity/ast/OverridableOperators.h>
#include <libsolidity/analysis/ConstantEvaluator.h>
@ -387,7 +388,7 @@ vector<UsingForDirective const*> usingForDirectivesForType(Type const& _type, AS
Result<FunctionDefinition const*> Type::userDefinedOperator(Token _token, ASTNode const& _scope, bool _unaryOperation) const
{
if (!typeDefinition())
if (!typeDefinition() || !util::contains(overridableOperators, _token))
return nullptr;
set<FunctionDefinition const*> matchingDefinitions;

View File

@ -23,6 +23,7 @@
#include <libsolidity/parsing/Parser.h>
#include <libsolidity/ast/OverridableOperators.h>
#include <libsolidity/interface/Version.h>
#include <libyul/AST.h>
#include <libyul/AsmParser.h>
@ -980,13 +981,6 @@ ASTPointer<UsingForDirective> Parser::parseUsingDirective()
{
advance();
Token operator_ = m_scanner->currentToken();
vector<Token> static const overridableOperators = {
Token::BitOr, Token::BitAnd, Token::BitXor,
Token::Add, Token::Sub, Token::Mul, Token::Div, Token::Mod,
Token::Equal, Token::NotEqual,
Token::LessThan, Token::GreaterThan, Token::LessThanOrEqual, Token::GreaterThanOrEqual,
Token::BitNot, Token::SHL, Token::SAR, Token::Exp, Token::Not
};
if (!util::contains(overridableOperators, operator_))
{
parserError(

View File

@ -0,0 +1,11 @@
type Int is int256;
function f() pure {
Int a = Int.wrap(0);
a + a;
a >>> a;
}
// ----
// TypeError 2271: (70-75): Binary operator + not compatible with types Int and Int. No matching user-defined operator found.
// TypeError 2271: (81-88): Binary operator >>> not compatible with types Int and Int.

View File

@ -0,0 +1,11 @@
type Int is int256;
function f() pure {
Int a = Int.wrap(0);
-a;
a++;
}
// ----
// TypeError 4907: (70-72): Unary operator - cannot be applied to type Int. No matching user-defined operator found.
// TypeError 9767: (78-81): Unary operator ++ cannot be applied to type Int.