Merge pull request #10027 from ethereum/yul-syntax-test-extractions

Continued Yul syntax test extractions.
This commit is contained in:
chriseth 2020-10-14 12:32:39 +02:00 committed by GitHub
commit 9b1f90512b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
46 changed files with 333 additions and 262 deletions

View File

@ -126,268 +126,6 @@ do \
BOOST_AUTO_TEST_SUITE(YulParser)
BOOST_AUTO_TEST_CASE(recursion_depth)
{
string input;
for (size_t i = 0; i < 20000; i++)
input += "{";
input += "let x:u256 := 0:u256";
for (size_t i = 0; i < 20000; i++)
input += "}";
CHECK_ERROR(input, ParserError, "recursion");
}
BOOST_AUTO_TEST_CASE(multiple_assignment)
{
CHECK_ERROR("{ let x:u256 function f() -> a:u256, b:u256 {} 123:u256, x := f() }", ParserError, "Variable name must precede \",\" in multiple assignment.");
CHECK_ERROR("{ let x:u256 function f() -> a:u256, b:u256 {} x, 123:u256 := f() }", ParserError, "Variable name must precede \":=\" in assignment.");
/// NOTE: Travis hiccups if not having a variable
char const* text = R"(
{
function f(a:u256) -> r1:u256, r2:u256 {
r1 := a
r2 := 7:u256
}
let x:u256 := 9:u256
let y:u256 := 2:u256
x, y := f(x)
}
)";
BOOST_CHECK(successParse(text));
}
BOOST_AUTO_TEST_CASE(if_statement)
{
BOOST_CHECK(successParse("{ if true:bool {} }"));
BOOST_CHECK(successParse("{ if false:bool { let x:u256 := 3:u256 } }"));
BOOST_CHECK(successParse("{ function f() -> x:bool {} if f() { let b:bool := f() } }"));
}
BOOST_AUTO_TEST_CASE(break_outside_of_for_loop)
{
CHECK_ERROR_DIALECT(
"{ let x if x { break } }",
SyntaxError,
"Keyword \"break\" needs to be inside a for-loop body.",
EVMDialect::strictAssemblyForEVMObjects(EVMVersion::constantinople())
);
}
BOOST_AUTO_TEST_CASE(continue_outside_of_for_loop)
{
CHECK_ERROR_DIALECT(
"{ let x if x { continue } }",
SyntaxError,
"Keyword \"continue\" needs to be inside a for-loop body.",
EVMDialect::strictAssemblyForEVMObjects(EVMVersion::constantinople())
);
}
BOOST_AUTO_TEST_CASE(for_statement)
{
auto const& dialect = EVMDialect::strictAssemblyForEVMObjects(EVMVersion::constantinople());
BOOST_CHECK(successParse("{ for {let i := 0} iszero(eq(i, 10)) {i := add(i, 1)} {} }", dialect));
}
BOOST_AUTO_TEST_CASE(for_statement_break)
{
auto const& dialect = EVMDialect::strictAssemblyForEVMObjects(EVMVersion::constantinople());
BOOST_CHECK(successParse("{ for {let i := 0} iszero(eq(i, 10)) {i := add(i, 1)} {break} }", dialect));
}
BOOST_AUTO_TEST_CASE(for_statement_break_init)
{
auto const& dialect = EVMDialect::strictAssemblyForEVMObjects(EVMVersion::constantinople());
CHECK_ERROR_DIALECT(
"{ for {let i := 0 break} iszero(eq(i, 10)) {i := add(i, 1)} {} }",
SyntaxError,
"Keyword \"break\" in for-loop init block is not allowed.",
dialect
);
}
BOOST_AUTO_TEST_CASE(for_statement_break_post)
{
auto const& dialect = EVMDialect::strictAssemblyForEVMObjects(EVMVersion::constantinople());
CHECK_ERROR_DIALECT(
"{ for {let i := 0} iszero(eq(i, 10)) {i := add(i, 1) break} {} }",
SyntaxError,
"Keyword \"break\" in for-loop post block is not allowed.",
dialect
);
}
BOOST_AUTO_TEST_CASE(for_statement_nested_break)
{
auto const& dialect = EVMDialect::strictAssemblyForEVMObjects(EVMVersion::constantinople());
CHECK_ERROR_DIALECT(
"{ for {let i := 0} iszero(eq(i, 10)) {} { function f() { break } } }",
SyntaxError,
"Keyword \"break\" needs to be inside a for-loop body.",
dialect
);
}
BOOST_AUTO_TEST_CASE(for_statement_continue)
{
auto const& dialect = EVMDialect::strictAssemblyForEVMObjects(EVMVersion::constantinople());
BOOST_CHECK(successParse("{ for {let i := 0} iszero(eq(i, 10)) {i := add(i, 1)} {continue} }", dialect));
}
BOOST_AUTO_TEST_CASE(for_statement_continue_fail_init)
{
auto const& dialect = EVMDialect::strictAssemblyForEVMObjects(EVMVersion::constantinople());
CHECK_ERROR_DIALECT(
"{ for {let i := 0 continue} iszero(eq(i, 10)) {i := add(i, 1)} {} }",
SyntaxError,
"Keyword \"continue\" in for-loop init block is not allowed.",
dialect
);
}
BOOST_AUTO_TEST_CASE(for_statement_continue_fail_post)
{
auto const& dialect = EVMDialect::strictAssemblyForEVMObjects(EVMVersion::constantinople());
CHECK_ERROR_DIALECT(
"{ for {let i := 0} iszero(eq(i, 10)) {i := add(i, 1) continue} {} }",
SyntaxError,
"Keyword \"continue\" in for-loop post block is not allowed.",
dialect
);
}
BOOST_AUTO_TEST_CASE(for_statement_nested_continue)
{
auto const& dialect = EVMDialect::strictAssemblyForEVMObjects(EVMVersion::constantinople());
CHECK_ERROR_DIALECT(
"{ for {let i := 0} iszero(eq(i, 10)) {} { function f() { continue } } }",
SyntaxError,
"Keyword \"continue\" needs to be inside a for-loop body.",
dialect
);
}
BOOST_AUTO_TEST_CASE(for_statement_continue_nested_init_in_body)
{
auto const& dialect = EVMDialect::strictAssemblyForEVMObjects(EVMVersion::constantinople());
CHECK_ERROR_DIALECT(
"{ for {} 1 {} {let x for { continue } x {} {}} }",
SyntaxError,
"Keyword \"continue\" in for-loop init block is not allowed.",
dialect
);
}
BOOST_AUTO_TEST_CASE(for_statement_continue_nested_body_in_init)
{
auto const& dialect = EVMDialect::strictAssemblyForEVMObjects(EVMVersion{});
BOOST_CHECK(successParse("{ for {let x for {} x {} { continue }} 1 {} {} }", dialect));
}
BOOST_AUTO_TEST_CASE(for_statement_break_nested_body_in_init)
{
auto const& dialect = EVMDialect::strictAssemblyForEVMObjects(EVMVersion{});
BOOST_CHECK(successParse("{ for {let x for {} x {} { break }} 1 {} {} }", dialect));
}
BOOST_AUTO_TEST_CASE(for_statement_continue_nested_body_in_post)
{
auto const& dialect = EVMDialect::strictAssemblyForEVMObjects(EVMVersion{});
BOOST_CHECK(successParse("{ for {} 1 {let x for {} x {} { continue }} {} }", dialect));
}
BOOST_AUTO_TEST_CASE(for_statement_break_nested_body_in_post)
{
auto const& dialect = EVMDialect::strictAssemblyForEVMObjects(EVMVersion{});
BOOST_CHECK(successParse("{ for {} 1 {let x for {} x {} { break }} {} }", dialect));
}
BOOST_AUTO_TEST_CASE(function_defined_in_init_block)
{
auto const& dialect = EVMDialect::strictAssemblyForEVMObjects(EVMVersion{});
BOOST_CHECK(successParse("{ for { } 1 { function f() {} } {} }", dialect));
BOOST_CHECK(successParse("{ for { } 1 {} { function f() {} } }", dialect));
CHECK_ERROR_DIALECT(
"{ for { function f() {} } 1 {} {} }",
SyntaxError,
"Functions cannot be defined inside a for-loop init block.",
dialect
);
}
BOOST_AUTO_TEST_CASE(function_defined_in_init_nested)
{
auto const& dialect = EVMDialect::strictAssemblyForEVMObjects(EVMVersion{});
BOOST_CHECK(successParse(
"{ for {"
"for { } 1 { function f() {} } {}"
"} 1 {} {} }", dialect));
CHECK_ERROR_DIALECT(
"{ for { for {function foo() {}} 1 {} {} } 1 {} {} }",
SyntaxError,
"Functions cannot be defined inside a for-loop init block.",
dialect
);
CHECK_ERROR_DIALECT(
"{ for {} 1 {for {function foo() {}} 1 {} {} } {} }",
SyntaxError,
"Functions cannot be defined inside a for-loop init block.",
dialect
);
}
BOOST_AUTO_TEST_CASE(if_statement_invalid)
{
CHECK_ERROR("{ if let x:u256 {} }", ParserError, "Literal or identifier expected.");
CHECK_ERROR("{ if true:bool let x:u256 := 3:u256 }", ParserError, "Expected '{' but got reserved keyword 'let'");
CHECK_ERROR("{ if 42:u256 { } }", TypeError, "Expected a value of boolean type");
}
BOOST_AUTO_TEST_CASE(switch_duplicate_case)
{
CHECK_ERROR("{ switch 0:u256 case 0:u256 {} case 0x0:u256 {} }", DeclarationError, "Duplicate case defined.");
BOOST_CHECK(successParse("{ switch 0:u256 case 42:u256 {} case 0x42:u256 {} }"));
}
BOOST_AUTO_TEST_CASE(switch_duplicate_case_different_literal)
{
CHECK_ERROR("{ switch 0:u256 case 0:u256 {} case \"\":u256 {} }", DeclarationError, "Duplicate case defined.");
BOOST_CHECK(successParse("{ switch 1:u256 case \"1\":u256 {} case \"2\":u256 {} }"));
}
BOOST_AUTO_TEST_CASE(switch_case_string_literal_too_long)
{
BOOST_CHECK(successParse("{let x:u256 switch x case \"01234567890123456789012345678901\":u256 {}}"));
CHECK_ERROR("{let x:u256 switch x case \"012345678901234567890123456789012\":u256 {}}", TypeError, "String literal too long (33 > 32)");
}
BOOST_AUTO_TEST_CASE(function_shadowing_outside_vars)
{
CHECK_ERROR("{ let x:u256 function f() -> x:u256 {} }", DeclarationError, "already taken in this scope");
BOOST_CHECK(successParse("{ { let x:u256 } function f() -> x:u256 {} }"));
}
BOOST_AUTO_TEST_CASE(builtins_parser)
{
struct SimpleDialect: public Dialect
{
BuiltinFunction const* builtin(YulString _name) const override
{
return _name == "builtin"_yulstring ? &f : nullptr;
}
BuiltinFunction f;
};
SimpleDialect dialect;
CHECK_ERROR_DIALECT("{ let builtin := 6 }", ParserError, "Cannot use builtin function name \"builtin\" as identifier name.", dialect);
CHECK_ERROR_DIALECT("{ function builtin() {} }", ParserError, "Cannot use builtin function name \"builtin\" as identifier name.", dialect);
CHECK_ERROR_DIALECT("{ function f(x) { f(builtin) } }", ParserError, "Expected '(' but got ')'", dialect);
CHECK_ERROR_DIALECT("{ function f(builtin) {}", ParserError, "Cannot use builtin function name \"builtin\" as identifier name.", dialect);
CHECK_ERROR_DIALECT("{ function f() -> builtin {}", ParserError, "Cannot use builtin function name \"builtin\" as identifier name.", dialect);
}
BOOST_AUTO_TEST_CASE(builtins_analysis)
{
struct SimpleDialect: public Dialect

View File

@ -0,0 +1,8 @@
{
let x:bool
if x { break }
}
// ====
// dialect: evmTyped
// ----
// SyntaxError 2592: (22-27): Keyword "break" needs to be inside a for-loop body.

View File

@ -0,0 +1,5 @@
{
let add := 6
}
// ----
// ParserError 5568: (7-10): Cannot use builtin function name "add" as identifier name.

View File

@ -0,0 +1,5 @@
{
function add() {}
}
// ----
// ParserError 5568: (12-15): Cannot use builtin function name "add" as identifier name.

View File

@ -0,0 +1,5 @@
{
function f(x) { f(add) }
}
// ----
// ParserError 2314: (24-25): Expected '(' but got ')'

View File

@ -0,0 +1,5 @@
{
function f(add) {}
}
// ----
// ParserError 5568: (14-17): Cannot use builtin function name "add" as identifier name.

View File

@ -0,0 +1,5 @@
{
function f() -> add {}
}
// ----
// ParserError 5568: (19-22): Cannot use builtin function name "add" as identifier name.

View File

@ -0,0 +1,8 @@
{
let x:bool
if x { continue }
}
// ====
// dialect: evmTyped
// ----
// SyntaxError 2592: (22-30): Keyword "continue" needs to be inside a for-loop body.

View File

@ -0,0 +1,7 @@
{
let x:bool
for {let i := 0} x {i := add(i, 1)} {}
}
// ====
// dialect: evmTyped
// ----

View File

@ -0,0 +1,10 @@
{
let x:bool
for {let i := 0} x {i := add(i, 1)}
{
break
}
}
// ====
// dialect: evmTyped
// ----

View File

@ -0,0 +1,8 @@
{
let x:bool
for {let i := 0 break} x {i := add(i, 1)} {}
}
// ====
// dialect: evmTyped
// ----
// SyntaxError 9615: (31-36): Keyword "break" in for-loop init block is not allowed.

View File

@ -0,0 +1,7 @@
{
for {let x:bool for {} x {} { break }} 1:bool {}
{}
}
// ====
// dialect: evmTyped
// ----

View File

@ -0,0 +1,7 @@
{
for {} 1:bool {let x:bool for {} x {} { break }}
{}
}
// ====
// dialect: evmTyped
// ----

View File

@ -0,0 +1,8 @@
{
let x:bool
for {let i := 0 } x {i := add(i, 1) break} {}
}
// ====
// dialect: evmTyped
// ----
// SyntaxError 2461: (51-56): Keyword "break" in for-loop post block is not allowed.

View File

@ -0,0 +1,10 @@
{
let x:bool
for {let i := 0} x {i := add(i, 1)}
{
continue
}
}
// ====
// dialect: evmTyped
// ----

View File

@ -0,0 +1,10 @@
{
let x:bool
for {let i := 0 continue} x {i := add(i, 1)}
{
}
}
// ====
// dialect: evmTyped
// ----
// SyntaxError 9615: (31-39): Keyword "continue" in for-loop init block is not allowed.

View File

@ -0,0 +1,8 @@
{
let x:bool
for {let i := 0} x {i := add(i, 1) continue} {}
}
// ====
// dialect: evmTyped
// ----
// SyntaxError 2461: (50-58): Keyword "continue" in for-loop post block is not allowed.

View File

@ -0,0 +1,8 @@
{
for {let x:bool for {} x {} { continue }} 1:bool {}
{
}
}
// ====
// dialect: evmTyped
// ----

View File

@ -0,0 +1,7 @@
{
for {} 1:bool {let x:bool for {} x {} { continue }}
{}
}
// ====
// dialect: evmTyped
// ----

View File

@ -0,0 +1,11 @@
{
for {} 1 {}
{
let x:bool
for { continue } x {} {}
}
}
// ====
// dialect: evmTyped
// ----
// SyntaxError 9615: (39-47): Keyword "continue" in for-loop init block is not allowed.

View File

@ -0,0 +1,11 @@
{
let x:bool
for {let i := 0} x {}
{
function f() { break }
}
}
// ====
// dialect: evmTyped
// ----
// SyntaxError 2592: (57-62): Keyword "break" needs to be inside a for-loop body.

View File

@ -0,0 +1,10 @@
{
for {let i := 0} iszero(eq(i, 10)) {}
{
function f() { continue }
}
}
// ====
// dialect: evmTyped
// ----
// SyntaxError 2592: (61-69): Keyword "continue" needs to be inside a for-loop body.

View File

@ -0,0 +1,3 @@
{
for { } 1:bool { function f() {} } {}
}

View File

@ -0,0 +1,3 @@
{
for { } 1:bool {} { function f() {} }
}

View File

@ -0,0 +1,5 @@
{
for { function f() {} } 1:bool {} {}
}
// ----
// SyntaxError 3441: (9-17): Functions cannot be defined inside a for-loop init block.

View File

@ -0,0 +1,7 @@
{
for {
for {} 1:bool { function f() {} }
{}
} 1:bool {}
{}
}

View File

@ -0,0 +1,5 @@
{
for { for {function foo() {}} 1:bool {} {} } 1:bool {} {}
}
// ----
// SyntaxError 3441: (14-22): Functions cannot be defined inside a for-loop init block.

View File

@ -0,0 +1,8 @@
{
for {}
1:bool
{for {function foo() {}} 1:bool {} {} }
{}
}
// ----
// SyntaxError 3441: (27-35): Functions cannot be defined inside a for-loop init block.

View File

@ -0,0 +1,4 @@
{
{ let x:u256 }
function f() -> x:u256 {}
}

View File

@ -0,0 +1,6 @@
{
let x:u256
function f() -> x:u256 {}
}
// ----
// DeclarationError 1395: (15-40): Variable name x already taken in this scope.

View File

@ -0,0 +1,6 @@
{
if true:bool {}
}
// ====
// dialect: evmTyped
// ----

View File

@ -0,0 +1,9 @@
{
if false:bool
{
let x:u256 := 3:u256
}
}
// ====
// dialect: evmTyped
// ----

View File

@ -0,0 +1,11 @@
{
function f() -> x:bool {}
if f()
{
let b:bool := f()
}
}
// ====
// dialect: evmTyped
// ----

View File

@ -0,0 +1,5 @@
{ if let x:u256 {} }
// ====
// dialect: evmTyped
// ----
// ParserError 1856: (5-8): Literal or identifier expected.

View File

@ -0,0 +1,5 @@
{ if true:bool let x:u256 := 3:u256 }
// ====
// dialect: evmTyped
// ----
// ParserError 2314: (15-18): Expected '{' but got reserved keyword 'let'

View File

@ -0,0 +1,5 @@
{ if 42:u256 { } }
// ====
// dialect: evmTyped
// ----
// TypeError 1733: (5-12): Expected a value of boolean type "bool" but got "u256"

View File

@ -0,0 +1,9 @@
{
let x:u256
function f() -> a:u256, b:u256 {}
123:u256, x := f()
}
// ====
// dialect: evmTyped
// ----
// ParserError 2856: (58-59): Variable name must precede "," in multiple assignment.

View File

@ -0,0 +1,9 @@
{
let x:u256
function f() -> a:u256, b:u256 {}
x, 123:u256 := f()
}
// ====
// dialect: evmTyped
// ----
// ParserError 2856: (62-64): Variable name must precede ":=" in assignment.

View File

@ -0,0 +1,12 @@
{
function f(a:u256) -> r1:u256, r2:u256 {
r1 := a
r2 := 7:u256
}
let x:u256 := 9:u256
let y:u256 := 2:u256
x, y := f(x)
}
// ====
// dialect: evmTyped
// ----

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,8 @@
{
switch 0:u256
case 42:u256 {}
case 0x42:u256 {}
}
// ====
// dialect: evmTyped
// ----

View File

@ -0,0 +1,8 @@
{
switch 1:u256
case "1":u256 {}
case "2":u256 {}
}
// ====
// dialect: evmTyped
// ----

View File

@ -0,0 +1,9 @@
{
let x:u256
switch x
case "012345678901234567890123456789012":u256 {}
}
// ====
// dialect: evmTyped
// ----
// TypeError 3069: (30-70): String literal too long (33 > 32)

View File

@ -0,0 +1,8 @@
{
let x:u256
switch x
case "01234567890123456789012345678901":u256 {}
}
// ====
// dialect: evmTyped
// ----

View File

@ -0,0 +1,9 @@
{
switch 0:u256
case 0:u256 {}
case 0x0:u256 {}
}
// ====
// dialect: evmTyped
// ----
// DeclarationError 6792: (34-50): Duplicate case defined.

View File

@ -0,0 +1,9 @@
{
switch 0:u256
case 0:u256 {}
case "":u256 {}
}
// ====
// dialect: evmTyped
// ----
// DeclarationError 6792: (34-49): Duplicate case defined.