2017-05-03 08:30:01 +00:00
/*
This file is part of solidity .
solidity is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
solidity is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with solidity . If not , see < http : //www.gnu.org/licenses/>.
*/
/**
* @ date 2017
2018-06-12 18:55:14 +00:00
* Unit tests for parsing Yul .
2017-05-03 08:30:01 +00:00
*/
2020-01-14 16:48:17 +00:00
# include <test/Common.h>
2017-05-03 08:30:01 +00:00
2017-05-24 16:34:19 +00:00
# include <test/libsolidity/ErrorCheck.h>
2018-12-04 10:23:28 +00:00
# include <test/libyul/Common.h>
2017-05-24 16:34:19 +00:00
2018-11-23 10:18:57 +00:00
# include <libyul/AsmParser.h>
2020-01-16 17:06:27 +00:00
# include <libyul/AsmPrinter.h>
2018-11-23 10:18:57 +00:00
# include <libyul/AsmAnalysis.h>
# include <libyul/AsmAnalysisInfo.h>
2018-12-04 10:23:28 +00:00
# include <libyul/Dialect.h>
2018-11-14 13:59:30 +00:00
# include <liblangutil/Scanner.h>
# include <liblangutil/ErrorReporter.h>
2017-05-03 08:30:01 +00:00
# include <boost/algorithm/string/replace.hpp>
2020-01-14 14:55:31 +00:00
# include <boost/test/unit_test.hpp>
2017-05-03 08:30:01 +00:00
# include <memory>
2019-10-28 10:39:30 +00:00
# include <optional>
# include <string>
2017-05-03 08:30:01 +00:00
using namespace std ;
2019-12-23 15:50:30 +00:00
using namespace solidity ;
using namespace solidity : : util ;
using namespace solidity : : langutil ;
2017-05-03 08:30:01 +00:00
2019-12-23 15:50:30 +00:00
namespace solidity : : yul : : test
2017-05-03 08:30:01 +00:00
{
namespace
{
2020-01-16 17:06:27 +00:00
shared_ptr < Block > parse ( string const & _source , Dialect const & _dialect , ErrorReporter & errorReporter )
2017-05-03 08:30:01 +00:00
{
try
{
2018-11-28 15:13:36 +00:00
auto scanner = make_shared < Scanner > ( CharStream ( _source , " " ) ) ;
2018-12-03 17:06:07 +00:00
auto parserResult = yul : : Parser ( errorReporter , _dialect ) . parse ( scanner , false ) ;
2017-05-03 08:30:01 +00:00
if ( parserResult )
2017-05-25 00:28:47 +00:00
{
2018-11-21 11:42:34 +00:00
yul : : AsmAnalysisInfo analysisInfo ;
2020-01-16 17:06:27 +00:00
if ( yul : : AsmAnalyzer (
2018-02-23 10:42:53 +00:00
analysisInfo ,
errorReporter ,
2018-12-03 17:06:07 +00:00
_dialect
2020-01-16 17:06:27 +00:00
) . analyze ( * parserResult ) )
return parserResult ;
2017-05-25 00:28:47 +00:00
}
2017-05-03 08:30:01 +00:00
}
catch ( FatalError const & )
{
BOOST_FAIL ( " Fatal error leaked. " ) ;
}
2020-01-16 17:06:27 +00:00
return { } ;
2017-05-03 08:30:01 +00:00
}
2019-10-28 10:39:30 +00:00
std : : optional < Error > parseAndReturnFirstError ( string const & _source , Dialect const & _dialect , bool _allowWarnings = true )
2017-05-03 08:30:01 +00:00
{
ErrorList errors ;
2017-05-11 13:26:35 +00:00
ErrorReporter errorReporter ( errors ) ;
2018-12-03 17:06:07 +00:00
if ( ! parse ( _source , _dialect , errorReporter ) )
2017-05-03 08:30:01 +00:00
{
2018-12-04 10:23:58 +00:00
BOOST_REQUIRE ( ! errors . empty ( ) ) ;
BOOST_CHECK_EQUAL ( errors . size ( ) , 1 ) ;
2017-05-03 08:30:01 +00:00
return * errors . front ( ) ;
}
else
{
// If success is true, there might still be an error in the assembly stage.
if ( _allowWarnings & & Error : : containsOnlyWarnings ( errors ) )
return { } ;
else if ( ! errors . empty ( ) )
{
if ( ! _allowWarnings )
BOOST_CHECK_EQUAL ( errors . size ( ) , 1 ) ;
return * errors . front ( ) ;
}
}
return { } ;
}
2020-01-29 17:14:03 +00:00
bool successParse ( std : : string const & _source , Dialect const & _dialect = Dialect : : yulDeprecated ( ) , bool _allowWarnings = true )
2017-05-03 08:30:01 +00:00
{
2018-12-03 17:06:07 +00:00
return ! parseAndReturnFirstError ( _source , _dialect , _allowWarnings ) ;
2017-05-03 08:30:01 +00:00
}
2020-01-29 17:14:03 +00:00
Error expectError ( std : : string const & _source , Dialect const & _dialect = Dialect : : yulDeprecated ( ) , bool _allowWarnings = false )
2017-05-03 08:30:01 +00:00
{
2018-12-03 17:06:07 +00:00
auto error = parseAndReturnFirstError ( _source , _dialect , _allowWarnings ) ;
2017-05-03 08:30:01 +00:00
BOOST_REQUIRE ( error ) ;
return * error ;
}
}
2018-12-03 17:06:07 +00:00
# define CHECK_ERROR_DIALECT(text, typ, substring, dialect) \
2017-05-03 08:30:01 +00:00
do \
{ \
2018-12-03 17:06:07 +00:00
Error err = expectError ( ( text ) , dialect , false ) ; \
2017-05-03 08:30:01 +00:00
BOOST_CHECK ( err . type ( ) = = ( Error : : Type : : typ ) ) ; \
2019-12-23 15:50:30 +00:00
BOOST_CHECK ( solidity : : frontend : : test : : searchErrorMessage ( err , ( substring ) ) ) ; \
2017-05-03 08:30:01 +00:00
} while ( 0 )
2020-01-29 17:14:03 +00:00
# define CHECK_ERROR(text, typ, substring) CHECK_ERROR_DIALECT(text, typ, substring, Dialect::yulDeprecated())
2018-12-03 17:06:07 +00:00
2018-06-14 22:29:45 +00:00
BOOST_AUTO_TEST_SUITE ( YulParser )
2017-05-03 08:30:01 +00:00
BOOST_AUTO_TEST_CASE ( smoke_test )
{
BOOST_CHECK ( successParse ( " { } " ) ) ;
}
BOOST_AUTO_TEST_CASE ( vardecl )
{
2017-05-05 13:51:36 +00:00
BOOST_CHECK ( successParse ( " { let x:u256 := 7:u256 } " ) ) ;
2017-05-03 08:30:01 +00:00
}
2017-05-17 12:20:24 +00:00
BOOST_AUTO_TEST_CASE ( vardecl_bool )
{
BOOST_CHECK ( successParse ( " { let x:bool := true:bool } " ) ) ;
BOOST_CHECK ( successParse ( " { let x:bool := false:bool } " ) ) ;
}
2017-05-05 15:46:26 +00:00
BOOST_AUTO_TEST_CASE ( vardecl_empty )
{
2017-07-03 10:01:07 +00:00
BOOST_CHECK ( successParse ( " { let x:u256 } " ) ) ;
2017-05-05 15:46:26 +00:00
}
2017-05-03 08:30:01 +00:00
BOOST_AUTO_TEST_CASE ( assignment )
{
2017-05-05 13:51:36 +00:00
BOOST_CHECK ( successParse ( " { let x:u256 := 2:u256 let y:u256 := x } " ) ) ;
2017-05-03 08:30:01 +00:00
}
2019-04-24 11:16:43 +00:00
BOOST_AUTO_TEST_CASE ( period_in_identifier )
{
BOOST_CHECK ( successParse ( " { let x.y:u256 := 2:u256 } " ) ) ;
}
BOOST_AUTO_TEST_CASE ( period_not_as_identifier_start )
{
CHECK_ERROR ( " { let .y:u256 } " , ParserError , " Expected identifier but got '.' " ) ;
}
BOOST_AUTO_TEST_CASE ( period_in_identifier_spaced )
{
2019-12-19 16:58:20 +00:00
CHECK_ERROR ( " { let x. y:u256 } " , ParserError , " Call or assignment expected " ) ;
CHECK_ERROR ( " { let x .y:u256 } " , ParserError , " Literal or identifier expected " ) ;
CHECK_ERROR ( " { let x . y:u256 } " , ParserError , " Literal or identifier expected " ) ;
2019-04-24 11:16:43 +00:00
}
BOOST_AUTO_TEST_CASE ( period_in_identifier_start )
{
BOOST_CHECK ( successParse ( " { x.y(2:u256) function x.y(a:u256) {} } " ) ) ;
}
BOOST_AUTO_TEST_CASE ( period_in_identifier_start_with_comment )
{
BOOST_CHECK ( successParse ( " /// comment \n { x.y(2:u256) function x.y(a:u256) {} } " ) ) ;
}
2017-05-03 08:30:01 +00:00
BOOST_AUTO_TEST_CASE ( vardecl_complex )
{
2017-05-25 00:28:47 +00:00
BOOST_CHECK ( successParse ( " { function add(a:u256, b:u256) -> c:u256 {} let y:u256 := 2:u256 let x:u256 := add(7:u256, add(6:u256, y)) } " ) ) ;
2017-05-03 08:30:01 +00:00
}
BOOST_AUTO_TEST_CASE ( blocks )
{
2017-05-05 13:51:36 +00:00
BOOST_CHECK ( successParse ( " { let x:u256 := 7:u256 { let y:u256 := 3:u256 } { let z:u256 := 2:u256 } } " ) ) ;
2017-05-03 08:30:01 +00:00
}
BOOST_AUTO_TEST_CASE ( function_definitions )
{
2017-05-05 13:51:36 +00:00
BOOST_CHECK ( successParse ( " { function f() { } function g(a:u256) -> x:u256 { } } " ) ) ;
2017-05-03 08:30:01 +00:00
}
BOOST_AUTO_TEST_CASE ( function_definitions_multiple_args )
{
2017-05-05 13:51:36 +00:00
BOOST_CHECK ( successParse ( " { function f(a:u256, d:u256) { } function g(a:u256, d:u256) -> x:u256, y:u256 { } } " ) ) ;
2017-05-03 08:30:01 +00:00
}
BOOST_AUTO_TEST_CASE ( function_calls )
{
2017-05-25 00:28:47 +00:00
BOOST_CHECK ( successParse ( " { function f(a:u256) -> b:u256 {} function g(a:u256, b:u256, c:u256) {} function x() { g(1:u256, 2:u256, f(3:u256)) x() } } " ) ) ;
2017-05-06 14:49:22 +00:00
}
BOOST_AUTO_TEST_CASE ( tuple_assignment )
{
BOOST_CHECK ( successParse ( " { function f() -> a:u256, b:u256, c:u256 {} let x:u256, y:u256, z:u256 := f() } " ) ) ;
2017-05-03 08:30:01 +00:00
}
BOOST_AUTO_TEST_CASE ( instructions )
{
CHECK_ERROR ( " { pop } " , ParserError , " Call or assignment expected. " ) ;
}
BOOST_AUTO_TEST_CASE ( push )
{
2017-05-05 13:51:36 +00:00
CHECK_ERROR ( " { 0x42:u256 } " , ParserError , " Call or assignment expected. " ) ;
2017-05-03 08:30:01 +00:00
}
BOOST_AUTO_TEST_CASE ( assign_from_stack )
{
2017-05-05 13:51:36 +00:00
CHECK_ERROR ( " { =: x:u256 } " , ParserError , " Literal or identifier expected. " ) ;
2017-05-03 08:30:01 +00:00
}
BOOST_AUTO_TEST_CASE ( empty_call )
{
CHECK_ERROR ( " { () } " , ParserError , " Literal or identifier expected. " ) ;
}
2018-01-04 23:25:31 +00:00
BOOST_AUTO_TEST_CASE ( tokens_as_identifers )
{
BOOST_CHECK ( successParse ( " { let return:u256 := 1:u256 } " ) ) ;
BOOST_CHECK ( successParse ( " { let byte:u256 := 1:u256 } " ) ) ;
BOOST_CHECK ( successParse ( " { let address:u256 := 1:u256 } " ) ) ;
BOOST_CHECK ( successParse ( " { let bool:u256 := 1:u256 } " ) ) ;
}
2019-12-19 16:58:20 +00:00
BOOST_AUTO_TEST_CASE ( optional_types )
2017-05-05 13:51:36 +00:00
{
2019-12-19 16:58:20 +00:00
BOOST_CHECK ( successParse ( " { let x := 1:u256 } " ) ) ;
BOOST_CHECK ( successParse ( " { let x:u256 := 1 } " ) ) ;
BOOST_CHECK ( successParse ( " { function f(a) {} } " ) ) ;
BOOST_CHECK ( successParse ( " { function f(a:u256) -> b {} } " ) ) ;
2017-05-05 13:51:36 +00:00
}
2017-08-21 10:08:29 +00:00
BOOST_AUTO_TEST_CASE ( number_literals )
{
BOOST_CHECK ( successParse ( " { let x:u256 := 1:u256 } " ) ) ;
CHECK_ERROR ( " { let x:u256 := .1:u256 } " , ParserError , " Invalid number literal. " ) ;
CHECK_ERROR ( " { let x:u256 := 1e5:u256 } " , ParserError , " Invalid number literal. " ) ;
CHECK_ERROR ( " { let x:u256 := 67.235:u256 } " , ParserError , " Invalid number literal. " ) ;
2018-01-04 23:25:41 +00:00
CHECK_ERROR ( " { let x:u256 := 0x1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff:u256 } " , TypeError , " Number literal too large (> 256 bits) " ) ;
2017-08-21 10:08:29 +00:00
}
2017-05-24 23:27:48 +00:00
BOOST_AUTO_TEST_CASE ( builtin_types )
{
BOOST_CHECK ( successParse ( " { let x:bool := true:bool } " ) ) ;
BOOST_CHECK ( successParse ( " { let x:u8 := 1:u8 } " ) ) ;
2020-01-22 19:11:33 +00:00
BOOST_CHECK ( successParse ( " { let x:s8 := 1:s8 } " ) ) ;
2017-05-24 23:27:48 +00:00
BOOST_CHECK ( successParse ( " { let x:u32 := 1:u32 } " ) ) ;
BOOST_CHECK ( successParse ( " { let x:s32 := 1:s32 } " ) ) ;
BOOST_CHECK ( successParse ( " { let x:u64 := 1:u64 } " ) ) ;
BOOST_CHECK ( successParse ( " { let x:s64 := 1:s64 } " ) ) ;
BOOST_CHECK ( successParse ( " { let x:u128 := 1:u128 } " ) ) ;
BOOST_CHECK ( successParse ( " { let x:s128 := 1:s128 } " ) ) ;
BOOST_CHECK ( successParse ( " { let x:u256 := 1:u256 } " ) ) ;
BOOST_CHECK ( successParse ( " { let x:s256 := 1:s256 } " ) ) ;
}
2017-08-21 10:29:04 +00:00
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 + = " } " ;
2017-08-21 10:33:29 +00:00
CHECK_ERROR ( input , ParserError , " recursion " ) ;
2017-08-21 10:29:04 +00:00
}
2017-05-19 16:06:26 +00:00
BOOST_AUTO_TEST_CASE ( multiple_assignment )
{
2019-02-18 14:07:15 +00:00
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. " ) ;
2017-05-19 16:06:26 +00:00
/// 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 ) ) ;
}
2017-11-22 15:24:59 +00:00
BOOST_AUTO_TEST_CASE ( if_statement )
{
2017-11-23 17:52:04 +00:00
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() } } " ) ) ;
2017-11-22 15:24:59 +00:00
}
2019-04-18 10:51:28 +00:00
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. " ,
2019-05-16 08:56:56 +00:00
EVMDialect : : strictAssemblyForEVMObjects ( EVMVersion : : constantinople ( ) )
2019-04-18 10:51:28 +00:00
) ;
}
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. " ,
2019-05-16 08:56:56 +00:00
EVMDialect : : strictAssemblyForEVMObjects ( EVMVersion : : constantinople ( ) )
2019-04-18 10:51:28 +00:00
) ;
}
2019-03-04 14:38:05 +00:00
BOOST_AUTO_TEST_CASE ( for_statement )
{
2019-05-16 08:56:56 +00:00
auto const & dialect = EVMDialect : : strictAssemblyForEVMObjects ( EVMVersion : : constantinople ( ) ) ;
2019-03-04 14:38:05 +00:00
BOOST_CHECK ( successParse ( " { for {let i := 0} iszero(eq(i, 10)) {i := add(i, 1)} {} } " , dialect ) ) ;
}
BOOST_AUTO_TEST_CASE ( for_statement_break )
{
2019-05-16 08:56:56 +00:00
auto const & dialect = EVMDialect : : strictAssemblyForEVMObjects ( EVMVersion : : constantinople ( ) ) ;
2019-03-04 14:38:05 +00:00
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 )
{
2019-05-16 08:56:56 +00:00
auto const & dialect = EVMDialect : : strictAssemblyForEVMObjects ( EVMVersion : : constantinople ( ) ) ;
2019-03-04 14:38:05 +00:00
CHECK_ERROR_DIALECT (
" { for {let i := 0 break} iszero(eq(i, 10)) {i := add(i, 1)} {} } " ,
SyntaxError ,
2019-04-18 10:51:28 +00:00
" Keyword \" break \" in for-loop init block is not allowed. " ,
dialect
) ;
2019-03-04 14:38:05 +00:00
}
BOOST_AUTO_TEST_CASE ( for_statement_break_post )
{
2019-05-16 08:56:56 +00:00
auto const & dialect = EVMDialect : : strictAssemblyForEVMObjects ( EVMVersion : : constantinople ( ) ) ;
2019-03-04 14:38:05 +00:00
CHECK_ERROR_DIALECT (
" { for {let i := 0} iszero(eq(i, 10)) {i := add(i, 1) break} {} } " ,
SyntaxError ,
2019-04-18 10:51:28 +00:00
" Keyword \" break \" in for-loop post block is not allowed. " ,
dialect
) ;
2019-03-04 14:38:05 +00:00
}
BOOST_AUTO_TEST_CASE ( for_statement_nested_break )
{
2019-05-16 08:56:56 +00:00
auto const & dialect = EVMDialect : : strictAssemblyForEVMObjects ( EVMVersion : : constantinople ( ) ) ;
2019-03-04 14:38:05 +00:00
CHECK_ERROR_DIALECT (
" { for {let i := 0} iszero(eq(i, 10)) {} { function f() { break } } } " ,
SyntaxError ,
2019-04-18 10:51:28 +00:00
" Keyword \" break \" needs to be inside a for-loop body. " ,
dialect
) ;
2019-03-04 14:38:05 +00:00
}
BOOST_AUTO_TEST_CASE ( for_statement_continue )
{
2019-05-16 08:56:56 +00:00
auto const & dialect = EVMDialect : : strictAssemblyForEVMObjects ( EVMVersion : : constantinople ( ) ) ;
2019-03-04 14:38:05 +00:00
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 )
{
2019-05-16 08:56:56 +00:00
auto const & dialect = EVMDialect : : strictAssemblyForEVMObjects ( EVMVersion : : constantinople ( ) ) ;
2019-03-04 14:38:05 +00:00
CHECK_ERROR_DIALECT (
" { for {let i := 0 continue} iszero(eq(i, 10)) {i := add(i, 1)} {} } " ,
SyntaxError ,
2019-04-18 10:51:28 +00:00
" Keyword \" continue \" in for-loop init block is not allowed. " ,
dialect
) ;
2019-03-04 14:38:05 +00:00
}
BOOST_AUTO_TEST_CASE ( for_statement_continue_fail_post )
{
2019-05-16 08:56:56 +00:00
auto const & dialect = EVMDialect : : strictAssemblyForEVMObjects ( EVMVersion : : constantinople ( ) ) ;
2019-03-04 14:38:05 +00:00
CHECK_ERROR_DIALECT (
" { for {let i := 0} iszero(eq(i, 10)) {i := add(i, 1) continue} {} } " ,
SyntaxError ,
2019-04-18 10:51:28 +00:00
" Keyword \" continue \" in for-loop post block is not allowed. " ,
dialect
) ;
}
2019-04-18 16:10:45 +00:00
BOOST_AUTO_TEST_CASE ( for_statement_nested_continue )
{
2019-05-16 08:56:56 +00:00
auto const & dialect = EVMDialect : : strictAssemblyForEVMObjects ( EVMVersion : : constantinople ( ) ) ;
2019-04-18 16:10:45 +00:00
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
) ;
}
2019-04-18 10:51:28 +00:00
BOOST_AUTO_TEST_CASE ( for_statement_continue_nested_init_in_body )
{
2019-05-16 08:56:56 +00:00
auto const & dialect = EVMDialect : : strictAssemblyForEVMObjects ( EVMVersion : : constantinople ( ) ) ;
2019-04-18 10:51:28 +00:00
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 )
{
2019-05-16 08:56:56 +00:00
auto const & dialect = EVMDialect : : strictAssemblyForEVMObjects ( EVMVersion { } ) ;
2019-04-18 10:51:28 +00:00
BOOST_CHECK ( successParse ( " { for {let x for {} x {} { continue }} 1 {} {} } " , dialect ) ) ;
}
BOOST_AUTO_TEST_CASE ( for_statement_break_nested_body_in_init )
{
2019-05-16 08:56:56 +00:00
auto const & dialect = EVMDialect : : strictAssemblyForEVMObjects ( EVMVersion { } ) ;
2019-04-18 10:51:28 +00:00
BOOST_CHECK ( successParse ( " { for {let x for {} x {} { break }} 1 {} {} } " , dialect ) ) ;
}
BOOST_AUTO_TEST_CASE ( for_statement_continue_nested_body_in_post )
{
2019-05-16 08:56:56 +00:00
auto const & dialect = EVMDialect : : strictAssemblyForEVMObjects ( EVMVersion { } ) ;
2019-04-18 10:51:28 +00:00
BOOST_CHECK ( successParse ( " { for {} 1 {let x for {} x {} { continue }} {} } " , dialect ) ) ;
}
BOOST_AUTO_TEST_CASE ( for_statement_break_nested_body_in_post )
{
2019-05-16 08:56:56 +00:00
auto const & dialect = EVMDialect : : strictAssemblyForEVMObjects ( EVMVersion { } ) ;
2019-04-18 10:51:28 +00:00
BOOST_CHECK ( successParse ( " { for {} 1 {let x for {} x {} { break }} {} } " , dialect ) ) ;
2019-03-04 14:38:05 +00:00
}
2019-04-18 16:10:45 +00:00
BOOST_AUTO_TEST_CASE ( function_defined_in_init_block )
{
2019-05-16 08:56:56 +00:00
auto const & dialect = EVMDialect : : strictAssemblyForEVMObjects ( EVMVersion { } ) ;
2019-04-18 16:10:45 +00:00
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 )
{
2019-05-16 08:56:56 +00:00
auto const & dialect = EVMDialect : : strictAssemblyForEVMObjects ( EVMVersion { } ) ;
2019-04-18 16:10:45 +00:00
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
) ;
}
2017-11-22 15:24:59 +00:00
BOOST_AUTO_TEST_CASE ( if_statement_invalid )
{
CHECK_ERROR ( " { if let x:u256 {} } " , ParserError , " Literal or identifier expected. " ) ;
2018-05-02 18:49:36 +00:00
CHECK_ERROR ( " { if true:bool let x:u256 := 3:u256 } " , ParserError , " Expected '{' but got reserved keyword 'let' " ) ;
2020-01-22 19:11:33 +00:00
CHECK_ERROR ( " { if 42:u256 { } } " , TypeError , " Expected a value of boolean type " ) ;
2019-01-15 12:40:10 +00:00
}
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 {} } " ) ) ;
}
2019-03-25 11:30:05 +00:00
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 {} } " ) ) ;
}
2019-04-17 13:05:44 +00:00
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) " ) ;
}
2019-04-04 15:48:41 +00:00
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 {} } " ) ) ;
}
2018-12-03 17:15:32 +00:00
BOOST_AUTO_TEST_CASE ( builtins_parser )
{
2018-12-06 23:56:16 +00:00
struct SimpleDialect : public Dialect
2018-12-03 17:15:32 +00:00
{
2018-12-06 23:56:16 +00:00
BuiltinFunction const * builtin ( YulString _name ) const override
2018-12-03 17:15:32 +00:00
{
2018-12-10 03:25:51 +00:00
return _name = = " builtin " _yulstring ? & f : nullptr ;
2018-12-03 17:15:32 +00:00
}
BuiltinFunction f ;
} ;
2019-05-16 08:56:56 +00:00
SimpleDialect dialect ;
2018-12-03 17:15:32 +00:00
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 ) ;
2019-05-20 12:30:32 +00:00
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 ) ;
2018-12-03 17:15:32 +00:00
}
2018-12-04 10:23:28 +00:00
BOOST_AUTO_TEST_CASE ( builtins_analysis )
{
2018-12-06 23:56:16 +00:00
struct SimpleDialect : public Dialect
2018-12-04 10:23:28 +00:00
{
2018-12-06 23:56:16 +00:00
BuiltinFunction const * builtin ( YulString _name ) const override
2018-12-04 10:23:28 +00:00
{
2018-12-10 03:25:51 +00:00
return _name = = " builtin " _yulstring ? & f : nullptr ;
2018-12-04 10:23:28 +00:00
}
2020-04-06 12:47:44 +00:00
BuiltinFunction f { " builtin " _yulstring , vector < Type > ( 2 ) , vector < Type > ( 3 ) , { } , { } , false , { } } ;
2018-12-04 10:23:28 +00:00
} ;
2019-05-16 08:56:56 +00:00
SimpleDialect dialect ;
2018-12-04 10:23:28 +00:00
BOOST_CHECK ( successParse ( " { let a, b, c := builtin(1, 2) } " , dialect ) ) ;
CHECK_ERROR_DIALECT ( " { let a, b, c := builtin(1) } " , TypeError , " Function expects 2 arguments but got 1 " , dialect ) ;
CHECK_ERROR_DIALECT ( " { let a, b := builtin(1, 2) } " , DeclarationError , " Variable count mismatch: 2 variables and 3 values. " , dialect ) ;
}
2020-01-16 17:06:27 +00:00
BOOST_AUTO_TEST_CASE ( default_types_set )
{
ErrorList errorList ;
ErrorReporter reporter ( errorList ) ;
shared_ptr < Block > result = parse (
" { "
" let x:bool := true:bool "
2020-01-29 17:14:20 +00:00
" let z:bool := true "
2020-01-16 17:06:27 +00:00
" let y := add(1, 2) "
" switch y case 0 {} default {} "
" } " ,
EVMDialectTyped : : instance ( EVMVersion { } ) ,
reporter
) ;
BOOST_REQUIRE ( ! ! result ) ;
// Use no dialect so that all types are printed.
// This tests that the default types are properly assigned.
BOOST_CHECK_EQUAL ( AsmPrinter { } ( * result ) ,
" { \n "
" let x:bool := true:bool \n "
2020-01-29 17:14:20 +00:00
" let z:bool := true:bool \n "
2020-01-16 17:06:27 +00:00
" let y:u256 := add(1:u256, 2:u256) \n "
" switch y \n "
" case 0:u256 { } \n "
" default { } \n "
" } "
) ;
// Now test again with type dialect. Now the default types
// should be omitted.
BOOST_CHECK_EQUAL ( AsmPrinter { EVMDialectTyped : : instance ( EVMVersion { } ) } ( * result ) ,
" { \n "
2020-01-29 17:14:20 +00:00
" let x:bool := true \n "
" let z:bool := true \n "
2020-01-16 17:06:27 +00:00
" let y := add(1, 2) \n "
" switch y \n "
" case 0 { } \n "
" default { } \n "
" } "
) ;
}
2019-07-09 15:23:14 +00:00
2017-05-03 08:30:01 +00:00
BOOST_AUTO_TEST_SUITE_END ( )
} // end namespaces