fixup! Syntax changes for catching custom errors.

This commit is contained in:
chriseth 2021-02-02 17:03:42 +01:00
parent b6183edd18
commit 6fd0cf547e
10 changed files with 142 additions and 13 deletions

View File

@ -1007,7 +1007,7 @@ void TypeChecker::endVisit(TryStatement const& _tryStatement)
"This try statement already has a low-level catch clause."
);
lowLevelClause = &clause;
if (clause.parameters() && !clause.parameters()->parameters().empty())
if (clause.parameters())
{
if (
clause.parameters()->parameters().size() != 1 ||
@ -1072,7 +1072,6 @@ void TypeChecker::endVisit(TryStatement const& _tryStatement)
else
{
solAssert(clause.kind() == TryCatchClause::Kind::UserDefined, "");
solAssert(*clause.errorName().annotation().requiredLookup == VirtualLookup::Static, "");
ErrorDefinition const* error = dynamic_cast<ErrorDefinition const*>(clause.errorName().annotation().referencedDeclaration);
if (!error)
{
@ -1091,16 +1090,20 @@ void TypeChecker::endVisit(TryStatement const& _tryStatement)
!clause.parameters() ||
clause.parameters()->parameters().size() != error->parameters().size()
)
m_errorReporter.typeError(1271_error, clause.location(), "Expected `catch Panic(uint ...) { ... }`.");
else
for (auto&& [varDecl, parameter]: ranges::views::zip(clause.parameters()->parameters(), error->parameters()))
if (*varDecl->type() != *parameter->type())
m_errorReporter.typeError(
63958_error,
varDecl->location(),
("Expected a parameter of type \"" + parameter->type()->toString(true) + "\" ") +
("but got \"" + varDecl->type()->toString(true) + "\"")
);
m_errorReporter.typeError(
4873_error,
clause.location(),
("Expected " + to_string(error->parameters().size()) + " parameters for error \"") +
(error->name() + "\" but got " + to_string(clause.parameters()->parameters().size()) + ".")
);
for (auto&& [varDecl, parameter]: ranges::views::zip(clause.parameters()->parameters(), error->parameters()))
if (*varDecl->type() != *parameter->type())
m_errorReporter.typeError(
63958_error,
varDecl->location(),
("Expected a parameter of type \"" + parameter->type()->toString(true) + "\" ") +
("but got \"" + varDecl->type()->toString(true) + "\"")
);
}
}
}

View File

@ -0,0 +1,14 @@
contract C {
error E();
function f() public {
try this.f() {
} catch E() {
}
}
}
// ====
// EVMVersion: >=byzantium
// ----
// UnimplementedFeatureError: NONE

View File

@ -0,0 +1,18 @@
==== Source: A ====
error E(uint);
==== Source: B ====
import {E as Error} from "A";
contract C {
function f() public {
try this.f() {
} catch Error(uint x) {
}
}
}
// ====
// EVMVersion: >=byzantium
// ----
// TypeError 2943: (B:104-136): Expected `catch Error(string memory ...) { ... }`.

View File

@ -0,0 +1,18 @@
==== Source: A ====
error E(uint);
==== Source: B ====
import "A" as Error;
contract C {
function f() public {
try this.f() {
} catch Error.E(uint x) {
}
}
}
// ====
// EVMVersion: >=byzantium
// ----
// ParserError 2314: (B:106-107): Expected '(' but got '.'

View File

@ -0,0 +1,14 @@
contract C {
error E();
function f() public returns (uint, uint) {
try this.f() {
} catch E {
}
}
}
// ====
// EVMVersion: >=byzantium
// ----
// ParserError 2314: (117-118): Expected '(' but got '{'

View File

@ -0,0 +1,19 @@
==== Source: A ====
error E(uint);
==== Source: B ====
import "A" as X;
contract C {
function f() public {
try this.f() {
} catch X.E(uint x) {
}
}
}
// ====
// EVMVersion: >=byzantium
// ----
// UnimplementedFeatureError: NONE
// Warning 5667: (B:101-107): Unused try/catch parameter. Remove or comment out the variable name to silence this warning.

View File

@ -0,0 +1,14 @@
contract C {
error E(uint x);
function f() public returns (uint, uint) {
try this.f() {
} catch E() {
}
}
}
// ====
// EVMVersion: >=byzantium
// ----
// TypeError 4873: (115-137): Expected 1 parameters for error "E" but got 0.

View File

@ -0,0 +1,14 @@
contract C {
error E();
function f() public returns (uint, uint) {
try this.f() {
} catch E(uint a) {
}
}
}
// ====
// EVMVersion: >=byzantium
// ----
// TypeError 4873: (109-137): Expected 0 parameters for error "E" but got 1.

View File

@ -0,0 +1,15 @@
contract C {
error E(uint a, uint8 b);
function f() public returns (uint, uint) {
try this.f() {
} catch E(uint8 x, uint y) {
}
}
}
// ====
// EVMVersion: >=byzantium
// ----
// TypeError 63958: (132-139): Expected a parameter of type "uint256" but got "uint8"
// TypeError 63958: (141-147): Expected a parameter of type "uint8" but got "uint256"

View File

@ -8,4 +8,4 @@ contract C {
}
}
// ----
// ParserError 3546: (101-102): Expected type name
// TypeError 6231: (94-115): Expected `catch (bytes memory ...) { ... }` or `catch { ... }`.