| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736 |
- // SPDX-License-Identifier: Apache-2.0
- use solang::codegen::{codegen, OptimizationLevel, Options};
- use solang::file_resolver::FileResolver;
- use solang::sema::ast::Diagnostic;
- use solang::sema::ast::Namespace;
- use solang::{parse_and_resolve, Target};
- use std::ffi::OsStr;
- fn parse_and_codegen(src: &'static str) -> Namespace {
- let mut cache = FileResolver::new();
- cache.set_file_contents("test.sol", src.to_string());
- let mut ns = parse_and_resolve(OsStr::new("test.sol"), &mut cache, Target::EVM);
- let opt = Options {
- dead_storage: false,
- constant_folding: false,
- strength_reduce: false,
- vector_to_slice: false,
- common_subexpression_elimination: false,
- opt_level: OptimizationLevel::Default,
- math_overflow_check: false,
- generate_debug_information: false,
- log_api_return_codes: false,
- log_runtime_errors: false,
- };
- codegen(&mut ns, &opt);
- ns
- }
- fn contains_error_message_and_notes(
- errors: &[&Diagnostic],
- message: &str,
- notes_no: usize,
- ) -> bool {
- for error in errors {
- if error.message == message {
- return error.notes.len() == notes_no;
- }
- }
- false
- }
- #[test]
- fn used_before_being_defined() {
- let file = r#"
- contract Test {
- bytes byteArr;
- bytes32 baRR;
- function get() public {
- string memory s = "Test";
- byteArr = bytes(s);
- uint16 a = 1;
- uint8 b;
- b = uint8(a);
- uint256 c;
- c = b;
- bytes32 b32;
- bytes memory char = bytes(bytes32(uint(a) * 2 ** (8 * b)));
- baRR = bytes32(c);
- bytes32 cdr = bytes32(char);
- assert(b32 == baRR);
- if(b32 != cdr) {
- }
- }
- }
- "#;
- let ns = parse_and_codegen(file);
- let errors = ns.diagnostics.errors();
- assert_eq!(errors.len(), 1);
- assert_eq!(errors[0].message, "Variable 'b32' is undefined");
- assert_eq!(errors[0].notes.len(), 2);
- assert_eq!(
- errors[0].notes[0].message,
- "Variable read before being defined"
- );
- assert_eq!(
- errors[0].notes[1].message,
- "Variable read before being defined"
- );
- }
- #[test]
- fn struct_as_ref() {
- let file = r#"
- contract test_struct_parsing {
- struct foo {
- bool x;
- uint32 y;
- }
- function func(foo f) private {
- // assigning to f members dereferences f
- f.x = true;
- f.y = 64;
- // assigning to f changes the reference
- f = foo({ x: false, y: 256 });
- // f no longer point to f in caller function
- f.x = false;
- f.y = 98123;
- }
- function test() public {
- foo f;
- func(f);
- assert(f.x == true);
- assert(f.y == 64);
- }
- }
- "#;
- let ns = parse_and_codegen(file);
- let errors = ns.diagnostics.errors();
- assert_eq!(errors.len(), 1);
- assert_eq!(errors[0].message, "Variable 'f' is undefined");
- assert_eq!(errors[0].notes.len(), 3);
- assert_eq!(
- errors[0].notes[0].message,
- "Variable read before being defined"
- );
- assert_eq!(
- errors[0].notes[1].message,
- "Variable read before being defined"
- );
- assert_eq!(
- errors[0].notes[2].message,
- "Variable read before being defined"
- );
- let file = r#"
- contract test_struct_parsing {
- struct foo {
- bool x;
- uint32 y;
- }
- function func(foo f) private {
- // assigning to f members dereferences f
- f.x = true;
- f.y = 64;
- // assigning to f changes the reference
- f = foo({ x: false, y: 256 });
- // f no longer point to f in caller function
- f.x = false;
- f.y = 98123;
- }
- function test() public {
- foo f = foo(false, 2);
- func(f);
- assert(f.x == true);
- assert(f.y == 64);
- }
- }
- "#;
- let ns = parse_and_codegen(file);
- let errors = ns.diagnostics.errors();
- assert_eq!(errors.len(), 0);
- }
- #[test]
- fn while_loop() {
- let file = r#"
- contract testing {
- function test(int x) public pure returns (string) {
- string s;
- while(x > 0){
- s = "testing_string";
- x--;
- }
- return s;
- }
- }
- "#;
- let ns = parse_and_codegen(file);
- let errors = ns.diagnostics.errors();
- assert_eq!(errors.len(), 1);
- assert_eq!(errors[0].message, "Variable 's' is undefined");
- assert_eq!(errors[0].notes.len(), 1);
- assert_eq!(
- errors[0].notes[0].message,
- "Variable read before being defined"
- );
- let file = r#"
- contract testing {
- function test(int x) public pure returns (string) {
- string s;
- while(x > 0){
- s = "testing_string";
- x--;
- }
- if(x < 0) {
- s = "another_test";
- }
- return s;
- }
- }
- "#;
- let ns = parse_and_codegen(file);
- let errors = ns.diagnostics.errors();
- assert_eq!(errors.len(), 1);
- assert_eq!(errors[0].message, "Variable 's' is undefined");
- assert_eq!(errors[0].notes.len(), 1);
- assert_eq!(
- errors[0].notes[0].message,
- "Variable read before being defined"
- );
- let file = r#"
- contract testing {
- function test(int x) public pure returns (string) {
- string s;
- while(x > 0){
- s = "testing_string";
- x--;
- }
- if(x < 0) {
- s = "another_test";
- } else {
- s = "should_work";
- }
- return s;
- }
- }
- "#;
- let ns = parse_and_codegen(file);
- let errors = ns.diagnostics.errors();
- assert_eq!(errors.len(), 0);
- }
- #[test]
- fn for_loop() {
- let file = r#"
- contract testing {
- function test(int x) public pure returns (int) {
- int s;
- for(int i=0; i<x; i++) {
- s = 5;
- }
- int p;
- if(x < 0) {
- p = s + 2;
- }
- return p;
- }
- }
- "#;
- let ns = parse_and_codegen(file);
- let errors = ns.diagnostics.errors();
- assert_eq!(errors.len(), 2);
- assert!(contains_error_message_and_notes(
- &errors,
- "Variable 'p' is undefined",
- 1
- ));
- assert!(contains_error_message_and_notes(
- &errors,
- "Variable 's' is undefined",
- 1
- ));
- let file = r#"
- contract testing {
- function test(int x) public pure returns (int) {
- int s;
- for(int i=0; i<x; i++) {
- s = 5;
- }
- s=5;
- int p;
- if(x < 0) {
- p = s + 2;
- } else {
- p = 2;
- s = 2;
- }
- return p;
- }
- }
- "#;
- let ns = parse_and_codegen(file);
- let errors = ns.diagnostics.errors();
- assert_eq!(errors.len(), 0);
- }
- #[test]
- fn do_while_loop() {
- let file = r#"
- contract testing {
- struct other {
- int a;
- }
- function test(int x) public pure returns (int) {
- other o;
- do {
- x--;
- o = other(1);
- }while(x > 0);
- return o.a;
- }
- }
- "#;
- let ns = parse_and_codegen(file);
- let errors = ns.diagnostics.errors();
- assert_eq!(errors.len(), 0);
- }
- #[test]
- fn if_else_condition() {
- let file = r#"
- contract testing {
- struct other {
- int a;
- }
- function test(int x) public pure returns (int) {
- other o;
- if(x > 0) {
- o = other(2);
- }
- return o.a;
- }
- }
- "#;
- let ns = parse_and_codegen(file);
- let errors = ns.diagnostics.errors();
- assert_eq!(errors.len(), 1);
- assert_eq!(errors[0].message, "Variable 'o' is undefined");
- assert_eq!(errors[0].notes.len(), 1);
- assert_eq!(
- errors[0].notes[0].message,
- "Variable read before being defined"
- );
- let file = r#"
- contract testing {
- struct other {
- int a;
- }
- function test(int x) public pure returns (int) {
- other o;
- if(x > 0) {
- x += 2;
- } else if(x < 0) {
- o = other(2);
- } else {
- x++;
- }
- return o.a;
- }
- }
- "#;
- let ns = parse_and_codegen(file);
- let errors = ns.diagnostics.errors();
- assert_eq!(errors.len(), 1);
- assert_eq!(errors[0].message, "Variable 'o' is undefined");
- assert_eq!(errors[0].notes.len(), 1);
- assert_eq!(
- errors[0].notes[0].message,
- "Variable read before being defined"
- );
- let file = r#"
- contract testing {
- struct other {
- int a;
- }
- function test(int x) public pure returns (int) {
- other o;
- if(x > 0) {
- o = other(2);
- } else {
- o = other(2);
- }
- return o.a;
- }
- }
- "#;
- let ns = parse_and_codegen(file);
- let errors = ns.diagnostics.errors();
- assert_eq!(errors.len(), 0);
- }
- #[test]
- fn array() {
- let file = r#"
- contract test {
- function testing(int x) public pure returns (int) {
- int[] vec;
- return vec[0];
- }
- }
- "#;
- let ns = parse_and_codegen(file);
- let errors = ns.diagnostics.errors();
- assert_eq!(errors.len(), 0);
- let file = r#"
- contract test {
- function testing(int x) public pure returns (int) {
- int[] vec;
- if(x > 0) {
- vec.push(2);
- }
- return vec[0];
- }
- }
- "#;
- let ns = parse_and_codegen(file);
- let errors = ns.diagnostics.errors();
- assert_eq!(errors.len(), 0);
- }
- #[test]
- fn contract_and_enum() {
- let file = r#"
- contract other {
- int public a;
- function testing() public returns (int) {
- return 2;
- }
- }
- contract test {
- enum FreshJuiceSize{ SMALL, MEDIUM, LARGE }
- function testing(int x) public returns (int) {
- other o;
- FreshJuiceSize choice;
- if(x > 0 && o.testing() < 5) {
- o = new other();
- }
- assert(choice == FreshJuiceSize.LARGE);
- return o.a();
- }
- }
- "#;
- let ns = parse_and_codegen(file);
- let errors = ns.diagnostics.errors();
- assert!(contains_error_message_and_notes(
- &errors,
- "Variable 'o' is undefined",
- 2
- ));
- assert!(contains_error_message_and_notes(
- &errors,
- "Variable 'choice' is undefined",
- 1
- ));
- }
- #[test]
- fn basic_types() {
- let file = r#"
- contract test {
- function testing(int x) public returns (address, int, uint, bool, bytes, int) {
- address a;
- int i;
- uint u;
- bool b;
- bytes bt;
- int[5] vec;
- while(x > 0) {
- x--;
- a = address(this);
- i = -2;
- u = 2;
- b = true;
- bt = hex"1234";
- vec[0] = 2;
- }
- return (a, i, u, b, bt, vec[1]);
- }
- }
- "#;
- let ns = parse_and_codegen(file);
- let errors = ns.diagnostics.errors();
- assert!(contains_error_message_and_notes(
- &errors,
- "Variable 'bt' is undefined",
- 1
- ));
- assert!(contains_error_message_and_notes(
- &errors,
- "Variable 'b' is undefined",
- 1
- ));
- assert!(contains_error_message_and_notes(
- &errors,
- "Variable 'a' is undefined",
- 1
- ));
- assert!(contains_error_message_and_notes(
- &errors,
- "Variable 'i' is undefined",
- 1
- ));
- assert!(contains_error_message_and_notes(
- &errors,
- "Variable 'u' is undefined",
- 1
- ));
- }
- #[test]
- fn nested_branches() {
- let file = r#"
- contract test {
- function testing(int x) public returns (int) {
- int i;
- while(x > 0) {
- int b;
- if(x > 5) {
- b = 2;
- }
- i = b;
- }
- int a;
- if(x < 5) {
- if(x < 2) {
- a = 2;
- } else {
- a = 1;
- }
- } else if(x < 4) {
- a = 5;
- }
- return i + a;
- }
- }
- "#;
- let ns = parse_and_codegen(file);
- let errors = ns.diagnostics.errors();
- assert!(contains_error_message_and_notes(
- &errors,
- "Variable 'b' is undefined",
- 1
- ));
- assert!(contains_error_message_and_notes(
- &errors,
- "Variable 'a' is undefined",
- 1
- ));
- assert!(contains_error_message_and_notes(
- &errors,
- "Variable 'i' is undefined",
- 2
- ));
- }
- #[test]
- fn try_catch() {
- //TODO: Fix this test case
- // let file = r#"
- // contract AddNumbers { function add(uint256 a, uint256 b) external pure returns (uint256 c) {c = b;} }
- // contract Example {
- // AddNumbers addContract;
- // event StringFailure(string stringFailure);
- // event BytesFailure(bytes bytesFailure);
- //
- // function exampleFunction(uint256 _a, uint256 _b) public returns (bytes c) {
- // bytes r;
- // try addContract.add(_a, _b) returns (uint256 _value) {
- // r = hex"ABCD";
- // return r;
- // } catch Error(string memory _err) {
- // r = hex"ABCD";
- // emit StringFailure(_err);
- // } catch (bytes memory _err) {
- // emit BytesFailure(_err);
- // }
- //
- // return r;
- // }
- //
- // }
- // "#;
- //
- // let ns = parse_and_codegen(file);
- // let errors = ns.diagnostics.errors();
- // assert_eq!(errors.len(), 1);
- // assert_eq!(errors[0].message, "Variable 'r' is undefined");
- // assert_eq!(errors[0].notes.len(), 1);
- // assert_eq!(errors[0].notes[0].message, "Variable read before being defined");
- let file = r#"
- contract AddNumbers { function add(uint256 a, uint256 b) external pure returns (uint256 c) {c = b;} }
- contract Example {
- AddNumbers addContract;
- event StringFailure(string stringFailure);
- event BytesFailure(bytes bytesFailure);
- function exampleFunction(uint256 _a, uint256 _b) public returns (bytes c) {
- bytes r;
- try addContract.add(_a, _b) returns (uint256 _value) {
- r = hex"ABCD";
- return r;
- } catch Error(string memory _err) {
- r = hex"ABCD";
- emit StringFailure(_err);
- } catch (bytes memory _err) {
- r = hex"ABCD";
- emit BytesFailure(_err);
- }
- return r;
- }
- }
- "#;
- let ns = parse_and_codegen(file);
- let errors = ns.diagnostics.errors();
- assert_eq!(errors.len(), 0);
- let file = r#"
- contract AddNumbers { function add(uint256 a, uint256 b) external pure returns (uint256 c) {c = b;} }
- contract Example {
- AddNumbers addContract;
- event StringFailure(string stringFailure);
- event BytesFailure(bytes bytesFailure);
- function exampleFunction(uint256 _a, uint256 _b) public returns (bytes c) {
- bytes r;
- try addContract.add(_a, _b) returns (uint256 _value) {
- return r;
- } catch Error(string memory _err) {
- r = hex"ABCD";
- emit StringFailure(_err);
- } catch (bytes memory _err) {
- emit BytesFailure(_err);
- }
- return r;
- }
- }
- "#;
- let ns = parse_and_codegen(file);
- let errors = ns.diagnostics.errors();
- assert_eq!(errors.len(), 1);
- assert_eq!(errors[0].message, "Variable 'r' is undefined");
- assert_eq!(errors[0].notes.len(), 1);
- assert_eq!(
- errors[0].notes[0].message,
- "Variable read before being defined"
- );
- let file = r#"
- contract AddNumbers { function add(uint256 a, uint256 b) external pure returns (uint256 c) {c = b;} }
- contract Example {
- AddNumbers addContract;
- event StringFailure(string stringFailure);
- event BytesFailure(bytes bytesFailure);
- function exampleFunction(uint256 _a, uint256 _b) public returns (bytes c) {
- bytes r;
- try addContract.add(_a, _b) returns (uint256 _value) {
- r = hex"ABCD";
- return r;
- } catch Error(string memory _err) {
- emit StringFailure(_err);
- } catch (bytes memory _err) {
- emit BytesFailure(_err);
- }
- return r;
- }
- }
- "#;
- let ns = parse_and_codegen(file);
- let errors = ns.diagnostics.errors();
- assert_eq!(errors.len(), 1);
- assert_eq!(errors[0].message, "Variable 'r' is undefined");
- assert_eq!(errors[0].notes.len(), 1);
- assert_eq!(
- errors[0].notes[0].message,
- "Variable read before being defined"
- );
- }
|