| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481 |
- use crate::{build_solidity, first_error, no_errors, parse_and_resolve};
- use solang::Target;
- #[test]
- fn variable_size() {
- let ns = parse_and_resolve(
- "contract x {
- function foo(int[12131231313213] memory y) public {}
- }
- ",
- Target::Substrate {
- address_length: 32,
- value_length: 16,
- },
- );
- assert_eq!(
- first_error(ns.diagnostics),
- "type is too large to fit into memory"
- );
- let ns = parse_and_resolve(
- "contract x {
- function foo() public returns (int[12131231313213] memory y) {}
- }
- ",
- Target::Substrate {
- address_length: 32,
- value_length: 16,
- },
- );
- assert_eq!(
- first_error(ns.diagnostics),
- "type is too large to fit into memory"
- );
- let ns = parse_and_resolve(
- "contract x {
- function foo() public {
- int[64*1024] memory y;
- }
- }
- ",
- Target::Substrate {
- address_length: 32,
- value_length: 16,
- },
- );
- assert_eq!(
- first_error(ns.diagnostics),
- "type is too large to fit into memory"
- );
- }
- #[test]
- fn immutable() {
- let ns = parse_and_resolve(
- "contract x {
- int public immutable y = 1;
- function foo() public {
- y = 2;
- }
- }
- ",
- Target::Substrate {
- address_length: 32,
- value_length: 16,
- },
- );
- assert_eq!(
- first_error(ns.diagnostics),
- "cannot assign to immutable ‘y’ outside of constructor"
- );
- let ns = parse_and_resolve(
- "contract x {
- int public immutable y = 1;
- function foo() public {
- y += 1;
- }
- }
- ",
- Target::Substrate {
- address_length: 32,
- value_length: 16,
- },
- );
- assert_eq!(
- first_error(ns.diagnostics),
- "cannot assign to immutable outside of constructor"
- );
- let ns = parse_and_resolve(
- "contract x {
- int public immutable y = 1;
- function foo() public {
- y++;
- }
- }
- ",
- Target::Substrate {
- address_length: 32,
- value_length: 16,
- },
- );
- assert_eq!(
- first_error(ns.diagnostics),
- "cannot assign to immutable outside of constructor"
- );
- let ns = parse_and_resolve(
- "contract x {
- int[] public immutable y;
- function foo() public {
- y.push();
- }
- }
- ",
- Target::Substrate {
- address_length: 32,
- value_length: 16,
- },
- );
- assert_eq!(
- first_error(ns.diagnostics),
- "cannot call method on immutable array outside of constructor"
- );
- let ns = parse_and_resolve(
- "contract x {
- int public immutable y;
- function foo() public {
- int a;
- (y, a) = (1, 2);
- }
- }
- ",
- Target::Substrate {
- address_length: 32,
- value_length: 16,
- },
- );
- assert_eq!(
- first_error(ns.diagnostics),
- "cannot assign to immutable ‘y’ outside of constructor"
- );
- let ns = parse_and_resolve(
- "contract x {
- int immutable public immutable y = 1;
- }
- ",
- Target::Substrate {
- address_length: 32,
- value_length: 16,
- },
- );
- assert_eq!(
- first_error(ns.diagnostics),
- "duplicate ‘immutable’ attribute"
- );
- }
- #[test]
- fn override_attribute() {
- let ns = parse_and_resolve(
- "contract x {
- int override y = 1;
- }
- ",
- Target::Substrate {
- address_length: 32,
- value_length: 16,
- },
- );
- assert_eq!(
- first_error(ns.diagnostics),
- "only public variable can be declared ‘override’"
- );
- let ns = parse_and_resolve(
- "contract x {
- int override internal y = 1;
- }
- ",
- Target::Substrate {
- address_length: 32,
- value_length: 16,
- },
- );
- assert_eq!(
- first_error(ns.diagnostics),
- "only public variable can be declared ‘override’"
- );
- let ns = parse_and_resolve(
- "contract x {
- int override private y = 1;
- }
- ",
- Target::Substrate {
- address_length: 32,
- value_length: 16,
- },
- );
- assert_eq!(
- first_error(ns.diagnostics),
- "only public variable can be declared ‘override’"
- );
- let ns = parse_and_resolve(
- "contract x {
- int override override y = 1;
- }
- ",
- Target::Substrate {
- address_length: 32,
- value_length: 16,
- },
- );
- assert_eq!(
- first_error(ns.diagnostics),
- "duplicate ‘override’ attribute"
- );
- let ns = parse_and_resolve(
- "contract x is y {
- int public foo;
- }
- contract y {
- function foo() public virtual returns (int) {
- return 102;
- }
- }
- ",
- Target::Substrate {
- address_length: 32,
- value_length: 16,
- },
- );
- assert_eq!(
- first_error(ns.diagnostics),
- "function ‘foo’ with this signature already defined"
- );
- let ns = parse_and_resolve(
- "contract x is y {
- int public override foo;
- }
- contract y {
- function foo() public virtual returns (int) {
- return 102;
- }
- }
- ",
- Target::Substrate {
- address_length: 32,
- value_length: 16,
- },
- );
- no_errors(ns.diagnostics);
- }
- #[test]
- fn test_variable_errors() {
- let ns = parse_and_resolve(
- "contract test {
- // solc 0.4.25 compiles this to 30.
- function foo() public pure returns (int32) {
- int32 a = b + 3;
- int32 b = a + 7;
- return a * b;
- }
- }",
- Target::Substrate {
- address_length: 32,
- value_length: 16,
- },
- );
- assert_eq!(first_error(ns.diagnostics), "`b' is not found");
- }
- #[test]
- fn test_variable_initializer_errors() {
- // cannot read contract storage in constant
- let ns = parse_and_resolve(
- "contract test {
- uint x = 102;
- uint constant y = x + 5;
- }",
- Target::Substrate {
- address_length: 32,
- value_length: 16,
- },
- );
- assert_eq!(
- first_error(ns.diagnostics),
- "cannot read contract variable ‘x’ in constant expression"
- );
- // cannot read contract storage in constant
- let ns = parse_and_resolve(
- "contract test {
- function foo() public pure returns (uint) {
- return 102;
- }
- uint constant y = foo() + 5;
- }",
- Target::Substrate {
- address_length: 32,
- value_length: 16,
- },
- );
- assert_eq!(
- first_error(ns.diagnostics),
- "cannot call function in constant expression"
- );
- // cannot refer to variable declared later
- let ns = parse_and_resolve(
- "contract test {
- uint x = y + 102;
- uint y = 102;
- }",
- Target::Substrate {
- address_length: 32,
- value_length: 16,
- },
- );
- assert_eq!(first_error(ns.diagnostics), "`y' is not found");
- // cannot refer to variable declared later (constant)
- let ns = parse_and_resolve(
- "contract test {
- uint x = y + 102;
- uint constant y = 102;
- }",
- Target::Substrate {
- address_length: 32,
- value_length: 16,
- },
- );
- assert_eq!(first_error(ns.diagnostics), "`y' is not found");
- // cannot refer to yourself
- let ns = parse_and_resolve(
- "contract test {
- uint x = x + 102;
- }",
- Target::Substrate {
- address_length: 32,
- value_length: 16,
- },
- );
- assert_eq!(first_error(ns.diagnostics), "`x' is not found");
- }
- #[test]
- fn global_constants() {
- let ns = parse_and_resolve(
- "uint x = 102;",
- Target::Substrate {
- address_length: 32,
- value_length: 16,
- },
- );
- assert_eq!(
- first_error(ns.diagnostics),
- "global variable must be constant"
- );
- let ns = parse_and_resolve(
- "uint constant public x = 102;",
- Target::Substrate {
- address_length: 32,
- value_length: 16,
- },
- );
- assert_eq!(
- first_error(ns.diagnostics),
- "‘public’: global variable cannot have visibility specifier"
- );
- let ns = parse_and_resolve(
- "uint constant external x = 102;",
- Target::Substrate {
- address_length: 32,
- value_length: 16,
- },
- );
- assert_eq!(
- first_error(ns.diagnostics),
- "‘external’: global variable cannot have visibility specifier"
- );
- let ns = parse_and_resolve(
- "uint constant x;",
- Target::Substrate {
- address_length: 32,
- value_length: 16,
- },
- );
- assert_eq!(
- first_error(ns.diagnostics),
- "missing initializer for constant"
- );
- let ns = parse_and_resolve(
- "uint constant test = 5; contract test {}",
- Target::Substrate {
- address_length: 32,
- value_length: 16,
- },
- );
- assert_eq!(
- first_error(ns.diagnostics),
- "test is already defined as a contract name"
- );
- let mut runtime = build_solidity(
- r##"
- int32 constant foo = 102 + 104;
- contract a {
- function test() public payable {
- assert(foo == 206);
- }
- }"##,
- );
- runtime.constructor(0, Vec::new());
- runtime.function("test", Vec::new());
- let mut runtime = build_solidity(
- r##"
- string constant foo = "FOO";
- contract a {
- function test() public payable {
- assert(foo == "FOO");
- }
- }"##,
- );
- runtime.constructor(0, Vec::new());
- runtime.function("test", Vec::new());
- }
|