inheritance.rs 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421
  1. use crate::{build_solidity, first_error, first_warning, no_errors, parse_and_resolve};
  2. use parity_scale_codec::Encode;
  3. use parity_scale_codec_derive::{Decode, Encode};
  4. use solang::file_resolver::FileResolver;
  5. use solang::Target;
  6. #[test]
  7. fn test_virtual() {
  8. let ns = parse_and_resolve(
  9. r#"
  10. contract c {
  11. function test() public;
  12. }"#,
  13. Target::Substrate {
  14. address_length: 32,
  15. value_length: 16,
  16. },
  17. );
  18. assert_eq!(
  19. first_error(ns.diagnostics),
  20. "function with no body must be marked ‘virtual’"
  21. );
  22. let ns = parse_and_resolve(
  23. r#"
  24. contract c {
  25. function test() virtual public {}
  26. }"#,
  27. Target::Substrate {
  28. address_length: 32,
  29. value_length: 16,
  30. },
  31. );
  32. no_errors(ns.diagnostics);
  33. let ns = parse_and_resolve(
  34. r#"
  35. contract c {
  36. function test() virtual public;
  37. function test2() virtual public;
  38. }"#,
  39. Target::Substrate {
  40. address_length: 32,
  41. value_length: 16,
  42. },
  43. );
  44. assert_eq!(
  45. first_error(ns.diagnostics),
  46. "contract should be marked ‘abstract contract’ since it has 2 functions with no body"
  47. );
  48. }
  49. #[test]
  50. fn test_abstract() {
  51. let ns = parse_and_resolve(
  52. r#"
  53. abstract contract foo {
  54. constructor(int arg1) public {
  55. }
  56. function f1() public {
  57. }
  58. }
  59. contract bar {
  60. function test() public {
  61. foo x = new foo(1);
  62. }
  63. }
  64. "#,
  65. Target::Substrate {
  66. address_length: 32,
  67. value_length: 16,
  68. },
  69. );
  70. assert_eq!(
  71. first_error(ns.diagnostics),
  72. "cannot construct ‘foo’ of type ‘abstract contract’"
  73. );
  74. let ns = parse_and_resolve(
  75. r#"
  76. abstract contract foo {
  77. constructor(int arg1) public {
  78. }
  79. function f1() public {
  80. }
  81. }
  82. contract bar {
  83. function test() public {
  84. foo x = new foo({arg: 1});
  85. }
  86. }
  87. "#,
  88. Target::Substrate {
  89. address_length: 32,
  90. value_length: 16,
  91. },
  92. );
  93. assert_eq!(
  94. first_error(ns.diagnostics),
  95. "cannot construct ‘foo’ of type ‘abstract contract’"
  96. );
  97. let mut cache = FileResolver::new();
  98. cache.set_file_contents(
  99. "a.sol",
  100. r#"
  101. abstract contract foo {
  102. constructor(int arg1) public {
  103. }
  104. function f1() public {
  105. }
  106. }
  107. contract bar {
  108. function test() public returns (uint32) {
  109. return 102;
  110. }
  111. }
  112. "#
  113. .to_string(),
  114. );
  115. let (contracts, ns) = solang::compile(
  116. "a.sol",
  117. &mut cache,
  118. inkwell::OptimizationLevel::Default,
  119. Target::Substrate {
  120. address_length: 32,
  121. value_length: 16,
  122. },
  123. false,
  124. );
  125. no_errors(ns.diagnostics);
  126. assert_eq!(contracts.len(), 1);
  127. let mut cache = FileResolver::new();
  128. cache.set_file_contents(
  129. "a.sol",
  130. r#"
  131. contract foo {
  132. function f1() public {
  133. }
  134. }"#
  135. .to_string(),
  136. );
  137. cache.set_file_contents(
  138. "b.sol",
  139. r#"
  140. import "a.sol";
  141. contract bar is foo {
  142. function test() public returns (uint32) {
  143. return 102;
  144. }
  145. }
  146. "#
  147. .to_string(),
  148. );
  149. let (contracts, ns) = solang::compile(
  150. "a.sol",
  151. &mut cache,
  152. inkwell::OptimizationLevel::Default,
  153. Target::Substrate {
  154. address_length: 32,
  155. value_length: 16,
  156. },
  157. false,
  158. );
  159. no_errors(ns.diagnostics);
  160. assert_eq!(contracts.len(), 1);
  161. }
  162. #[test]
  163. fn test_interface() {
  164. let ns = parse_and_resolve(
  165. r#"
  166. interface foo {
  167. constructor(int arg1) public {
  168. }
  169. }
  170. "#,
  171. Target::Substrate {
  172. address_length: 32,
  173. value_length: 16,
  174. },
  175. );
  176. assert_eq!(
  177. first_error(ns.diagnostics),
  178. "constructor not allowed in an interface"
  179. );
  180. let ns = parse_and_resolve(
  181. r#"
  182. interface foo {
  183. function bar() external {}
  184. }
  185. "#,
  186. Target::Substrate {
  187. address_length: 32,
  188. value_length: 16,
  189. },
  190. );
  191. assert_eq!(
  192. first_error(ns.diagnostics),
  193. "function in an interface cannot have a body"
  194. );
  195. let ns = parse_and_resolve(
  196. r#"
  197. interface foo {
  198. function bar() private;
  199. }
  200. "#,
  201. Target::Substrate {
  202. address_length: 32,
  203. value_length: 16,
  204. },
  205. );
  206. assert_eq!(
  207. first_error(ns.diagnostics),
  208. "functions must be declared ‘external’ in an interface"
  209. );
  210. let ns = parse_and_resolve(
  211. r#"
  212. interface foo {
  213. function bar() internal;
  214. }
  215. "#,
  216. Target::Substrate {
  217. address_length: 32,
  218. value_length: 16,
  219. },
  220. );
  221. assert_eq!(
  222. first_error(ns.diagnostics),
  223. "functions must be declared ‘external’ in an interface"
  224. );
  225. let ns = parse_and_resolve(
  226. r#"
  227. interface foo is a {
  228. function bar() internal;
  229. }
  230. abstract contract a {
  231. function f() internal {}
  232. }
  233. "#,
  234. Target::Substrate {
  235. address_length: 32,
  236. value_length: 16,
  237. },
  238. );
  239. assert_eq!(
  240. first_error(ns.diagnostics),
  241. "interface ‘foo’ cannot have abstract contract ‘a’ as a base"
  242. );
  243. let ns = parse_and_resolve(
  244. r#"
  245. interface foo is a {
  246. function bar() internal;
  247. }
  248. contract a {
  249. function f() internal {}
  250. }
  251. "#,
  252. Target::Substrate {
  253. address_length: 32,
  254. value_length: 16,
  255. },
  256. );
  257. assert_eq!(
  258. first_error(ns.diagnostics),
  259. "interface ‘foo’ cannot have contract ‘a’ as a base"
  260. );
  261. let ns = parse_and_resolve(
  262. r#"
  263. interface bar {
  264. function foo() virtual external;
  265. }
  266. "#,
  267. Target::Substrate {
  268. address_length: 32,
  269. value_length: 16,
  270. },
  271. );
  272. assert_eq!(
  273. first_warning(ns.diagnostics),
  274. "functions in an interface are implicitly virtual"
  275. );
  276. let ns = parse_and_resolve(
  277. r#"
  278. interface bar {
  279. int x;
  280. }
  281. "#,
  282. Target::Substrate {
  283. address_length: 32,
  284. value_length: 16,
  285. },
  286. );
  287. assert_eq!(
  288. first_error(ns.diagnostics),
  289. "interface ‘bar’ is not allowed to have contract variable ‘x’"
  290. );
  291. let ns = parse_and_resolve(
  292. r#"
  293. interface bar {
  294. int constant x = 1;
  295. }
  296. "#,
  297. Target::Substrate {
  298. address_length: 32,
  299. value_length: 16,
  300. },
  301. );
  302. assert_eq!(
  303. first_error(ns.diagnostics),
  304. "interface ‘bar’ is not allowed to have contract variable ‘x’"
  305. );
  306. // 1. implementing an interface does not require an override
  307. // 2. interface is function implemented by base
  308. let ns = parse_and_resolve(
  309. r#"
  310. interface bar {
  311. function f1(address a) external;
  312. }
  313. interface bar2 {
  314. function f1(address a) external;
  315. }
  316. contract x is bar {
  317. function f1(address a) public {}
  318. }
  319. contract y is bar2, x {
  320. function f2(address a) public {}
  321. }
  322. "#,
  323. Target::Substrate {
  324. address_length: 32,
  325. value_length: 16,
  326. },
  327. );
  328. no_errors(ns.diagnostics);
  329. }
  330. #[test]
  331. fn inherit() {
  332. let ns = parse_and_resolve(
  333. r#"
  334. contract a is a {
  335. constructor(int arg1) public {
  336. }
  337. }
  338. "#,
  339. Target::Substrate {
  340. address_length: 32,
  341. value_length: 16,
  342. },
  343. );
  344. assert_eq!(
  345. first_error(ns.diagnostics),
  346. "contract ‘a’ cannot have itself as a base contract"
  347. );
  348. let ns = parse_and_resolve(
  349. r#"
  350. contract a is foo {
  351. constructor(int arg1) public {
  352. }
  353. }
  354. "#,
  355. Target::Substrate {
  356. address_length: 32,
  357. value_length: 16,
  358. },
  359. );
  360. assert_eq!(first_error(ns.diagnostics), "contract ‘foo’ not found");
  361. let ns = parse_and_resolve(
  362. r#"
  363. contract a is b {
  364. constructor(int arg1) public {
  365. }
  366. }
  367. contract b is a {
  368. constructor(int arg1) public {
  369. }
  370. }
  371. "#,
  372. Target::Substrate {
  373. address_length: 32,
  374. value_length: 16,
  375. },
  376. );
  377. assert_eq!(
  378. first_error(ns.diagnostics),
  379. "base ‘a’ from contract ‘b’ is cyclic"
  380. );
  381. let ns = parse_and_resolve(
  382. r#"
  383. contract a {
  384. constructor(int arg1) public {
  385. }
  386. }
  387. contract b is a, a {
  388. constructor(int arg1) public {
  389. }
  390. }
  391. "#,
  392. Target::Substrate {
  393. address_length: 32,
  394. value_length: 16,
  395. },
  396. );
  397. assert_eq!(
  398. first_error(ns.diagnostics),
  399. "contract ‘b’ duplicate base ‘a’"
  400. );
  401. let ns = parse_and_resolve(
  402. r#"
  403. contract a is b {
  404. constructor(int arg1) public {
  405. }
  406. }
  407. contract b is c {
  408. constructor(int arg1) public {
  409. }
  410. }
  411. contract c is a {
  412. constructor(int arg1) public {
  413. }
  414. }
  415. "#,
  416. Target::Substrate {
  417. address_length: 32,
  418. value_length: 16,
  419. },
  420. );
  421. assert_eq!(
  422. first_error(ns.diagnostics),
  423. "base ‘a’ from contract ‘c’ is cyclic"
  424. );
  425. let ns = parse_and_resolve(
  426. r#"
  427. contract a is b {
  428. constructor(int arg1) public {
  429. }
  430. }
  431. contract b is c {
  432. constructor(int arg1) public {
  433. }
  434. }
  435. contract d {
  436. constructor(int arg1) public {
  437. }
  438. }
  439. contract c is d, a {
  440. constructor(int arg1) public {
  441. }
  442. }
  443. "#,
  444. Target::Substrate {
  445. address_length: 32,
  446. value_length: 16,
  447. },
  448. );
  449. assert_eq!(
  450. first_error(ns.diagnostics),
  451. "base ‘a’ from contract ‘c’ is cyclic"
  452. );
  453. }
  454. #[test]
  455. fn inherit_types() {
  456. let ns = parse_and_resolve(
  457. r#"
  458. contract a is b {
  459. function test() public returns (enum_x) {
  460. return enum_x.x2;
  461. }
  462. }
  463. contract b {
  464. enum enum_x { x1, x2 }
  465. }
  466. "#,
  467. Target::Substrate {
  468. address_length: 32,
  469. value_length: 16,
  470. },
  471. );
  472. no_errors(ns.diagnostics);
  473. let ns = parse_and_resolve(
  474. r#"
  475. contract a is b {
  476. function test() public returns (enum_x) {
  477. return enum_x.x2;
  478. }
  479. function test2() public returns (enum_y) {
  480. return enum_y.y2;
  481. }
  482. }
  483. contract b is c {
  484. enum enum_y { y1, y2 }
  485. }
  486. contract c {
  487. enum enum_x { x1, x2 }
  488. }
  489. "#,
  490. Target::Substrate {
  491. address_length: 32,
  492. value_length: 16,
  493. },
  494. );
  495. no_errors(ns.diagnostics);
  496. let ns = parse_and_resolve(
  497. r#"
  498. contract a is b, c {
  499. function test() public returns (enum_x) {
  500. return enum_x.x2;
  501. }
  502. function test2() public returns (enum_y) {
  503. return enum_y.y2;
  504. }
  505. }
  506. contract b is c {
  507. enum enum_y { y1, y2 }
  508. }
  509. contract c {
  510. enum enum_x { x1, x2 }
  511. }
  512. "#,
  513. Target::Substrate {
  514. address_length: 32,
  515. value_length: 16,
  516. },
  517. );
  518. no_errors(ns.diagnostics);
  519. let ns = parse_and_resolve(
  520. r#"
  521. contract a {
  522. function test() public returns (enum_x) {
  523. return enum_x.x2;
  524. }
  525. }
  526. contract b {
  527. enum enum_x { x1, x2 }
  528. }
  529. "#,
  530. Target::Substrate {
  531. address_length: 32,
  532. value_length: 16,
  533. },
  534. );
  535. assert_eq!(first_error(ns.diagnostics), "type ‘enum_x’ not found");
  536. let ns = parse_and_resolve(
  537. r#"
  538. contract a is b {
  539. foo public var1;
  540. }
  541. contract b {
  542. struct foo {
  543. uint32 f1;
  544. uint32 f2;
  545. }
  546. }
  547. "#,
  548. Target::Substrate {
  549. address_length: 32,
  550. value_length: 16,
  551. },
  552. );
  553. no_errors(ns.diagnostics);
  554. let ns = parse_and_resolve(
  555. r#"
  556. contract b {
  557. struct foo {
  558. uint32 f1;
  559. uint32 f2;
  560. }
  561. }
  562. contract c {
  563. enum foo { f1, f2 }
  564. }
  565. contract a is b, c {
  566. function test(foo x) public {
  567. }
  568. }
  569. "#,
  570. Target::Substrate {
  571. address_length: 32,
  572. value_length: 16,
  573. },
  574. );
  575. assert_eq!(first_error(ns.diagnostics), "already defined ‘foo’");
  576. }
  577. #[test]
  578. fn inherit_variables() {
  579. let ns = parse_and_resolve(
  580. r#"
  581. contract b {
  582. int foo;
  583. }
  584. contract c is b {
  585. function getFoo() public returns (int) {
  586. return foo;
  587. }
  588. }
  589. "#,
  590. Target::Substrate {
  591. address_length: 32,
  592. value_length: 16,
  593. },
  594. );
  595. no_errors(ns.diagnostics);
  596. let ns = parse_and_resolve(
  597. r#"
  598. contract b {
  599. int private foo;
  600. }
  601. contract c is b {
  602. function getFoo() public returns (int) {
  603. return foo;
  604. }
  605. }
  606. "#,
  607. Target::Substrate {
  608. address_length: 32,
  609. value_length: 16,
  610. },
  611. );
  612. assert_eq!(first_error(ns.diagnostics), "`foo\' is not found");
  613. let ns = parse_and_resolve(
  614. r#"
  615. contract a {
  616. int public foo;
  617. }
  618. contract b is a {
  619. int public bar;
  620. }
  621. contract c is b {
  622. function getFoo() public returns (int) {
  623. return foo;
  624. }
  625. }
  626. "#,
  627. Target::Substrate {
  628. address_length: 32,
  629. value_length: 16,
  630. },
  631. );
  632. no_errors(ns.diagnostics);
  633. let ns = parse_and_resolve(
  634. r#"
  635. contract a {
  636. int private foo;
  637. }
  638. contract b is a {
  639. int public foo;
  640. }
  641. contract c is b {
  642. function getFoo() public returns (int) {
  643. return foo;
  644. }
  645. }
  646. "#,
  647. Target::Substrate {
  648. address_length: 32,
  649. value_length: 16,
  650. },
  651. );
  652. no_errors(ns.diagnostics);
  653. let ns = parse_and_resolve(
  654. r#"
  655. contract a {
  656. int public constant foo = 0xbffe;
  657. }
  658. contract c is a {
  659. function getFoo() public returns (int) {
  660. return foo;
  661. }
  662. }
  663. "#,
  664. Target::Substrate {
  665. address_length: 32,
  666. value_length: 16,
  667. },
  668. );
  669. no_errors(ns.diagnostics);
  670. let mut runtime = build_solidity(
  671. r##"
  672. contract b is a {
  673. uint16 public foo = 65535;
  674. }
  675. contract a {
  676. uint16 private foo = 102;
  677. }"##,
  678. );
  679. runtime.constructor(0, Vec::new());
  680. let mut slot = [0u8; 32];
  681. assert_eq!(
  682. runtime.store.get(&(runtime.vm.address, slot)).unwrap(),
  683. &vec!(102, 0)
  684. );
  685. slot[0] = 1;
  686. assert_eq!(
  687. runtime.store.get(&(runtime.vm.address, slot)).unwrap(),
  688. &vec!(0xff, 0xff)
  689. );
  690. let mut runtime = build_solidity(
  691. r##"
  692. contract b is a {
  693. uint16 public var_b;
  694. function test() public {
  695. var_a = 102;
  696. var_b = 65535;
  697. }
  698. }
  699. contract a {
  700. uint16 public var_a;
  701. }"##,
  702. );
  703. runtime.constructor(0, Vec::new());
  704. runtime.function("test", Vec::new());
  705. let mut slot = [0u8; 32];
  706. assert_eq!(
  707. runtime.store.get(&(runtime.vm.address, slot)).unwrap(),
  708. &vec!(102, 0)
  709. );
  710. slot[0] = 1;
  711. assert_eq!(
  712. runtime.store.get(&(runtime.vm.address, slot)).unwrap(),
  713. &vec!(0xff, 0xff)
  714. );
  715. }
  716. #[test]
  717. fn call_inherited_function() {
  718. #[derive(Debug, PartialEq, Encode, Decode)]
  719. struct Val(u64);
  720. let mut runtime = build_solidity(
  721. r##"
  722. contract apex is base {
  723. function bar() public returns (uint64) {
  724. return foo() + 3;
  725. }
  726. }
  727. contract base {
  728. function foo() public returns (uint64) {
  729. return 102;
  730. }
  731. }"##,
  732. );
  733. runtime.constructor(0, Vec::new());
  734. runtime.function("bar", Vec::new());
  735. assert_eq!(runtime.vm.output, Val(105).encode());
  736. let ns = parse_and_resolve(
  737. r#"
  738. contract base {
  739. function foo() private returns (uint64) {
  740. return 102;
  741. }
  742. }
  743. contract apex is base {
  744. function bar() public returns (uint64) {
  745. return foo() + 3;
  746. }
  747. }
  748. "#,
  749. Target::Substrate {
  750. address_length: 32,
  751. value_length: 16,
  752. },
  753. );
  754. assert_eq!(first_error(ns.diagnostics), "cannot call private function");
  755. let ns = parse_and_resolve(
  756. r#"
  757. contract base {
  758. function foo(uint64 a) private returns (uint64) {
  759. return a + 102;
  760. }
  761. }
  762. contract apex is base {
  763. function bar() public returns (uint64) {
  764. return foo({a: 3}) + 3;
  765. }
  766. }
  767. "#,
  768. Target::Substrate {
  769. address_length: 32,
  770. value_length: 16,
  771. },
  772. );
  773. assert_eq!(first_error(ns.diagnostics), "cannot call private function");
  774. let ns = parse_and_resolve(
  775. r#"
  776. contract base {
  777. function foo(uint64 a) public returns (uint64) {
  778. return a + 102;
  779. }
  780. }
  781. contract apex is base {
  782. function foo(uint64 a) public returns (uint64) {
  783. return a + 64;
  784. }
  785. function bar() public returns (uint64) {
  786. return foo({a: 3}) + 3;
  787. }
  788. }
  789. "#,
  790. Target::Substrate {
  791. address_length: 32,
  792. value_length: 16,
  793. },
  794. );
  795. assert_eq!(
  796. first_error(ns.diagnostics),
  797. "function ‘foo’ with this signature already defined"
  798. );
  799. let mut runtime = build_solidity(
  800. r##"
  801. contract apex is base {
  802. uint64 private x = 7;
  803. function bar() public returns (uint64) {
  804. return foo() + x + 13;
  805. }
  806. }
  807. contract base {
  808. uint64 private x = 5;
  809. function foo() public returns (uint64) {
  810. return x + 11;
  811. }
  812. }"##,
  813. );
  814. runtime.constructor(0, Vec::new());
  815. runtime.function("bar", Vec::new());
  816. assert_eq!(runtime.vm.output, Val(36).encode());
  817. let mut runtime = build_solidity(
  818. r##"
  819. contract apex is base, base2 {
  820. uint64 private x = 7;
  821. function bar() public returns (uint64) {
  822. return foo() + foo2() + x + 13;
  823. }
  824. }
  825. contract base {
  826. uint64 private x = 50000;
  827. function foo() public returns (uint64) {
  828. return x + 110000;
  829. }
  830. }
  831. contract base2 {
  832. uint64 private x = 600;
  833. function foo2() public returns (uint64) {
  834. return x + 1100;
  835. }
  836. }"##,
  837. );
  838. runtime.constructor(0, Vec::new());
  839. runtime.function("bar", Vec::new());
  840. assert_eq!(runtime.vm.output, Val(161720).encode());
  841. let mut runtime = build_solidity(
  842. r##"
  843. contract apex is base, base2 {
  844. function foo(int64 x) public returns (uint64) {
  845. return 3;
  846. }
  847. }
  848. contract base {
  849. function foo() public returns (uint64) {
  850. return 1;
  851. }
  852. }
  853. contract base2 {
  854. function foo(bool) public returns (uint64) {
  855. return 2;
  856. }
  857. }"##,
  858. );
  859. runtime.constructor(0, Vec::new());
  860. runtime.raw_function([0xC2, 0x98, 0x55, 0x78].to_vec());
  861. assert_eq!(runtime.vm.output, Val(1).encode());
  862. runtime.raw_function([0x45, 0x55, 0x75, 0x78, 1].to_vec());
  863. assert_eq!(runtime.vm.output, Val(2).encode());
  864. runtime.raw_function([0x36, 0x8E, 0x4A, 0x7F, 1, 2, 3, 4, 5, 6, 7, 8].to_vec());
  865. assert_eq!(runtime.vm.output, Val(3).encode());
  866. }
  867. #[test]
  868. fn test_override() {
  869. let ns = parse_and_resolve(
  870. r#"
  871. contract base {
  872. function foo(uint64 a) override override private returns (uint64) {
  873. return a + 102;
  874. }
  875. }
  876. "#,
  877. Target::Substrate {
  878. address_length: 32,
  879. value_length: 16,
  880. },
  881. );
  882. assert_eq!(
  883. first_error(ns.diagnostics),
  884. "function redeclared ‘override’"
  885. );
  886. let ns = parse_and_resolve(
  887. r#"
  888. contract base {
  889. function foo(uint64 a) override(bar) private returns (uint64) {
  890. return a + 102;
  891. }
  892. }
  893. "#,
  894. Target::Substrate {
  895. address_length: 32,
  896. value_length: 16,
  897. },
  898. );
  899. assert_eq!(
  900. first_error(ns.diagnostics),
  901. "contract ‘bar’ in override list not found"
  902. );
  903. let ns = parse_and_resolve(
  904. r#"
  905. contract base {
  906. function foo(uint64 a) override(bar) private returns (uint64) {
  907. return a + 102;
  908. }
  909. }
  910. contract bar {
  911. function f() private {}
  912. }
  913. "#,
  914. Target::Substrate {
  915. address_length: 32,
  916. value_length: 16,
  917. },
  918. );
  919. assert_eq!(
  920. first_error(ns.diagnostics),
  921. "override ‘bar’ is not a base contract of ‘base’"
  922. );
  923. let ns = parse_and_resolve(
  924. r#"
  925. contract base {
  926. function foo(uint64 a) override private returns (uint64) {
  927. return a + 102;
  928. }
  929. }
  930. "#,
  931. Target::Substrate {
  932. address_length: 32,
  933. value_length: 16,
  934. },
  935. );
  936. assert_eq!(
  937. first_error(ns.diagnostics),
  938. "function ‘foo’ does not override anything"
  939. );
  940. let ns = parse_and_resolve(
  941. r#"
  942. contract base is bar {
  943. function foo(uint64 a) override(bar) private returns (uint64) {
  944. return a + 102;
  945. }
  946. }
  947. contract bar {
  948. function foo(uint64 a) private returns (uint64) {
  949. return a + 102;
  950. }
  951. }
  952. "#,
  953. Target::Substrate {
  954. address_length: 32,
  955. value_length: 16,
  956. },
  957. );
  958. assert_eq!(
  959. first_error(ns.diagnostics),
  960. "function ‘foo’ overrides function which is not virtual"
  961. );
  962. let ns = parse_and_resolve(
  963. r#"
  964. contract base is bar, bar2 {
  965. function foo(uint64 a) override(bar2) internal returns (uint64) {
  966. return a + 102;
  967. }
  968. }
  969. contract bar {
  970. function foo(uint64 a) virtual internal returns (uint64) {
  971. return a + 102;
  972. }
  973. }
  974. contract bar2 {
  975. uint64 public x;
  976. }
  977. "#,
  978. Target::Substrate {
  979. address_length: 32,
  980. value_length: 16,
  981. },
  982. );
  983. assert_eq!(
  984. first_error(ns.diagnostics),
  985. "function ‘foo’ override list does not contain ‘bar’"
  986. );
  987. let ns = parse_and_resolve(
  988. r#"
  989. contract a {
  990. int64 public x = 3;
  991. function f() virtual payable external {
  992. x = 1;
  993. }
  994. function f() override payable external {
  995. x = 2;
  996. }
  997. }"#,
  998. Target::Substrate {
  999. address_length: 32,
  1000. value_length: 16,
  1001. },
  1002. );
  1003. assert_eq!(
  1004. first_error(ns.diagnostics),
  1005. "function ‘f’ overrides function in same contract"
  1006. );
  1007. let ns = parse_and_resolve(
  1008. r#"
  1009. contract a {
  1010. function foo() virtual public returns (int32) {
  1011. return 1;
  1012. }
  1013. }
  1014. contract b is a {
  1015. function foo() virtual override public returns (int32) {
  1016. return 2;
  1017. }
  1018. }
  1019. contract c is b {
  1020. function foo() override public returns (int32) {
  1021. return 3;
  1022. }
  1023. }
  1024. "#,
  1025. Target::Substrate {
  1026. address_length: 32,
  1027. value_length: 16,
  1028. },
  1029. );
  1030. no_errors(ns.diagnostics);
  1031. let mut runtime = build_solidity(
  1032. r##"
  1033. contract b is a {
  1034. receive() override payable external {
  1035. x = 2;
  1036. }
  1037. }
  1038. contract a {
  1039. int8 public x = 3;
  1040. receive() virtual payable external {
  1041. x = 1;
  1042. }
  1043. }
  1044. contract c is b {
  1045. function test() public returns (int8) {
  1046. return x;
  1047. }
  1048. }"##,
  1049. );
  1050. runtime.constructor(0, Vec::new());
  1051. let slot = [0u8; 32];
  1052. assert_eq!(
  1053. runtime.store.get(&(runtime.vm.address, slot)).unwrap(),
  1054. &vec!(3)
  1055. );
  1056. runtime.vm.value = 1;
  1057. runtime.raw_function([0xC2, 0x98, 0x55, 0x78].to_vec());
  1058. let slot = [0u8; 32];
  1059. assert_eq!(
  1060. runtime.store.get(&(runtime.vm.address, slot)).unwrap(),
  1061. &vec!(2)
  1062. );
  1063. let mut runtime = build_solidity(
  1064. r##"
  1065. contract b is a {
  1066. fallback() override external {
  1067. x = 2;
  1068. }
  1069. }
  1070. contract a {
  1071. int8 public x = 3;
  1072. fallback() virtual external {
  1073. x = 1;
  1074. }
  1075. }"##,
  1076. );
  1077. runtime.constructor(0, Vec::new());
  1078. let slot = [0u8; 32];
  1079. assert_eq!(
  1080. runtime.store.get(&(runtime.vm.address, slot)).unwrap(),
  1081. &vec!(3)
  1082. );
  1083. runtime.raw_function([0xC2, 0x98, 0x55, 0x78].to_vec());
  1084. let slot = [0u8; 32];
  1085. assert_eq!(
  1086. runtime.store.get(&(runtime.vm.address, slot)).unwrap(),
  1087. &vec!(2)
  1088. );
  1089. let ns = parse_and_resolve(
  1090. r#"
  1091. interface b {
  1092. function bar(int64 x) external;
  1093. }
  1094. contract a is b {
  1095. function bar(int x) public { print ("foo"); }
  1096. }
  1097. "#,
  1098. Target::Substrate {
  1099. address_length: 32,
  1100. value_length: 16,
  1101. },
  1102. );
  1103. assert_eq!(
  1104. first_error(ns.diagnostics),
  1105. "contract ‘a’ missing override for function ‘bar’"
  1106. );
  1107. let ns = parse_and_resolve(
  1108. r#"
  1109. interface b {
  1110. function bar(int64 x) external;
  1111. }
  1112. contract a is b {
  1113. function bar(int64 x) public override;
  1114. }
  1115. "#,
  1116. Target::Substrate {
  1117. address_length: 32,
  1118. value_length: 16,
  1119. },
  1120. );
  1121. assert_eq!(
  1122. first_error(ns.diagnostics),
  1123. "function with no body must be marked ‘virtual’"
  1124. );
  1125. }
  1126. #[test]
  1127. fn multiple_override() {
  1128. let ns = parse_and_resolve(
  1129. r#"
  1130. contract base is bar, bar2 {
  1131. function foo(uint64 a) override internal returns (uint64) {
  1132. return a + 102;
  1133. }
  1134. }
  1135. contract bar {
  1136. function foo(uint64 a) virtual internal returns (uint64) {
  1137. return a + 102;
  1138. }
  1139. }
  1140. contract bar2 {
  1141. function foo(uint64 a) virtual internal returns (uint64) {
  1142. return a + 103;
  1143. }
  1144. }
  1145. "#,
  1146. Target::Substrate {
  1147. address_length: 32,
  1148. value_length: 16,
  1149. },
  1150. );
  1151. assert_eq!(
  1152. first_error(ns.diagnostics),
  1153. "function ‘foo’ should specify override list ‘override(bar2,bar)’"
  1154. );
  1155. let ns = parse_and_resolve(
  1156. r#"
  1157. contract base is bar, bar2 {
  1158. function foo(uint64 a) override(bar) internal returns (uint64) {
  1159. return a + 102;
  1160. }
  1161. }
  1162. contract bar {
  1163. function foo(uint64 a) virtual internal returns (uint64) {
  1164. return a + 102;
  1165. }
  1166. }
  1167. contract bar2 {
  1168. function foo(uint64 a) virtual internal returns (uint64) {
  1169. return a + 103;
  1170. }
  1171. }
  1172. "#,
  1173. Target::Substrate {
  1174. address_length: 32,
  1175. value_length: 16,
  1176. },
  1177. );
  1178. assert_eq!(
  1179. first_error(ns.diagnostics),
  1180. "function ‘foo’ missing overrides ‘bar2’, specify ‘override(bar2,bar)’"
  1181. );
  1182. let ns = parse_and_resolve(
  1183. r#"
  1184. contract base is bar, bar2, bar3 {
  1185. function foo(uint64 a) override(bar,bar2,bar3) internal returns (uint64) {
  1186. return a + 102;
  1187. }
  1188. }
  1189. contract bar {
  1190. function foo(uint64 a) virtual internal returns (uint64) {
  1191. return a + 102;
  1192. }
  1193. }
  1194. contract bar2 {
  1195. function foo(uint64 a) virtual internal returns (uint64) {
  1196. return a + 103;
  1197. }
  1198. }
  1199. contract bar3 {
  1200. function f() public {
  1201. }
  1202. }
  1203. "#,
  1204. Target::Substrate {
  1205. address_length: 32,
  1206. value_length: 16,
  1207. },
  1208. );
  1209. assert_eq!(
  1210. first_error(ns.diagnostics),
  1211. "function ‘foo’ includes extraneous overrides ‘bar3’, specify ‘override(bar2,bar)’"
  1212. );
  1213. let ns = parse_and_resolve(
  1214. r#"
  1215. contract base is bar, bar2 {
  1216. function foo(uint64 a) override(bar,bar2) internal returns (uint64) {
  1217. return a + 102;
  1218. }
  1219. }
  1220. contract bar {
  1221. function foo(uint64 a) internal returns (uint64) {
  1222. return a + 102;
  1223. }
  1224. }
  1225. contract bar2 {
  1226. function foo(uint64 a) virtual internal returns (uint64) {
  1227. return a + 103;
  1228. }
  1229. }
  1230. "#,
  1231. Target::Substrate {
  1232. address_length: 32,
  1233. value_length: 16,
  1234. },
  1235. );
  1236. assert_eq!(
  1237. first_error(ns.diagnostics),
  1238. "function ‘foo’ overrides functions which are not ‘virtual’"
  1239. );
  1240. let ns = parse_and_resolve(
  1241. r#"
  1242. contract base is bar, bar2 {
  1243. function foo(uint64 a) internal returns (uint64) {
  1244. return a + 102;
  1245. }
  1246. }
  1247. contract bar {
  1248. function foo(uint64 a) virtual internal returns (uint64) {
  1249. return a + 102;
  1250. }
  1251. }
  1252. contract bar2 {
  1253. function foo(uint64 a) virtual internal returns (uint64) {
  1254. return a + 103;
  1255. }
  1256. }
  1257. "#,
  1258. Target::Substrate {
  1259. address_length: 32,
  1260. value_length: 16,
  1261. },
  1262. );
  1263. assert_eq!(
  1264. first_error(ns.diagnostics),
  1265. "function ‘foo’ should specify override list ‘override(bar2,bar)’"
  1266. );
  1267. let ns = parse_and_resolve(
  1268. r#"
  1269. contract base is bar, bar2 {
  1270. function foo(uint64 a) virtual internal returns (uint64) {
  1271. return a + 104;
  1272. }
  1273. function foo(uint64 a) override(bar,bar2) internal returns (uint64) {
  1274. return a + 102;
  1275. }
  1276. }
  1277. contract bar {
  1278. function foo(uint64 a) virtual internal returns (uint64) {
  1279. return a + 102;
  1280. }
  1281. }
  1282. contract bar2 {
  1283. function foo(uint64 a) virtual internal returns (uint64) {
  1284. return a + 103;
  1285. }
  1286. }
  1287. "#,
  1288. Target::Substrate {
  1289. address_length: 32,
  1290. value_length: 16,
  1291. },
  1292. );
  1293. assert_eq!(
  1294. first_error(ns.diagnostics),
  1295. "function ‘foo’ should specify override list ‘override(bar2,bar)’"
  1296. );
  1297. let ns = parse_and_resolve(
  1298. r#"
  1299. contract base is bar, bar2 {
  1300. function foo(uint64 a) override(bar,bar2) internal returns (uint64) {
  1301. return a + 104;
  1302. }
  1303. function foo(uint64 a) override(bar,bar2) internal returns (uint64) {
  1304. return a + 102;
  1305. }
  1306. }
  1307. contract bar {
  1308. function foo(uint64 a) virtual internal returns (uint64) {
  1309. return a + 102;
  1310. }
  1311. }
  1312. contract bar2 {
  1313. function foo(uint64 a) virtual internal returns (uint64) {
  1314. return a + 103;
  1315. }
  1316. }
  1317. "#,
  1318. Target::Substrate {
  1319. address_length: 32,
  1320. value_length: 16,
  1321. },
  1322. );
  1323. assert_eq!(
  1324. first_error(ns.diagnostics),
  1325. "function ‘foo’ overrides function in same contract"
  1326. );
  1327. }
  1328. #[test]
  1329. fn base_contract() {
  1330. #[derive(Debug, PartialEq, Encode, Decode)]
  1331. struct Val(u32);
  1332. let ns = parse_and_resolve(
  1333. r#"
  1334. contract base {
  1335. constructor(uint64 a) {}
  1336. }
  1337. contract apex is base {
  1338. constructor() {}
  1339. function foo(uint64 a) virtual internal returns (uint64) {
  1340. return a + 102;
  1341. }
  1342. }"#,
  1343. Target::Substrate {
  1344. address_length: 32,
  1345. value_length: 16,
  1346. },
  1347. );
  1348. assert_eq!(
  1349. first_error(ns.diagnostics),
  1350. "missing arguments to contract ‘base’ constructor"
  1351. );
  1352. let ns = parse_and_resolve(
  1353. r#"
  1354. contract base {
  1355. constructor(uint64 a) public {}
  1356. }
  1357. contract apex is base(true) {
  1358. function foo(uint64 a) virtual internal returns (uint64) {
  1359. return a + 102;
  1360. }
  1361. }"#,
  1362. Target::Substrate {
  1363. address_length: 32,
  1364. value_length: 16,
  1365. },
  1366. );
  1367. assert_eq!(
  1368. first_error(ns.diagnostics),
  1369. "conversion from bool to uint64 not possible"
  1370. );
  1371. let mut runtime = build_solidity(
  1372. r##"
  1373. contract b is a(foo) {
  1374. int32 constant foo = 102;
  1375. function f() public returns (int32) {
  1376. return bar;
  1377. }
  1378. }
  1379. contract a {
  1380. int32 public bar;
  1381. constructor(int32 x) public {
  1382. bar = x;
  1383. }
  1384. }"##,
  1385. );
  1386. runtime.constructor(0, Vec::new());
  1387. runtime.function("f", Vec::new());
  1388. assert_eq!(runtime.vm.output, Val(102).encode());
  1389. }
  1390. #[test]
  1391. fn base_contract_on_constructor() {
  1392. #[derive(Debug, PartialEq, Encode, Decode)]
  1393. struct Val(i32);
  1394. #[derive(Debug, PartialEq, Encode, Decode)]
  1395. struct Val64(u64);
  1396. let ns = parse_and_resolve(
  1397. r#"
  1398. contract base {
  1399. struct s { uint32 f1; }
  1400. }
  1401. contract b {
  1402. struct s { uint32 f1; }
  1403. }
  1404. contract apex is base {
  1405. constructor() public b {
  1406. }
  1407. }"#,
  1408. Target::Substrate {
  1409. address_length: 32,
  1410. value_length: 16,
  1411. },
  1412. );
  1413. assert_eq!(
  1414. first_error(ns.diagnostics),
  1415. "contract ‘b’ is not a base contract of ‘apex’"
  1416. );
  1417. let ns = parse_and_resolve(
  1418. r#"
  1419. contract base {
  1420. struct s { uint32 f1; }
  1421. }
  1422. contract b {
  1423. struct s { uint32 f1; }
  1424. }
  1425. contract apex is base {
  1426. constructor() public b {
  1427. }
  1428. }"#,
  1429. Target::Substrate {
  1430. address_length: 32,
  1431. value_length: 16,
  1432. },
  1433. );
  1434. assert_eq!(
  1435. first_error(ns.diagnostics),
  1436. "contract ‘b’ is not a base contract of ‘apex’"
  1437. );
  1438. let ns = parse_and_resolve(
  1439. r#"
  1440. contract apex {
  1441. constructor() public apex {
  1442. }
  1443. }"#,
  1444. Target::Substrate {
  1445. address_length: 32,
  1446. value_length: 16,
  1447. },
  1448. );
  1449. assert_eq!(
  1450. first_error(ns.diagnostics),
  1451. "contract ‘apex’ is not a base contract of ‘apex’"
  1452. );
  1453. let ns = parse_and_resolve(
  1454. r#"
  1455. contract apex {
  1456. constructor() oosda public {
  1457. }
  1458. }"#,
  1459. Target::Substrate {
  1460. address_length: 32,
  1461. value_length: 16,
  1462. },
  1463. );
  1464. assert_eq!(
  1465. first_error(ns.diagnostics),
  1466. "unknown function attribute ‘oosda’"
  1467. );
  1468. let ns = parse_and_resolve(
  1469. r#"
  1470. contract base {
  1471. constructor(bool x) {}
  1472. }
  1473. contract apex is base {
  1474. function foo() pure public {}
  1475. }"#,
  1476. Target::Substrate {
  1477. address_length: 32,
  1478. value_length: 16,
  1479. },
  1480. );
  1481. assert_eq!(
  1482. first_error(ns.diagnostics),
  1483. "missing arguments to base contract ‘base’ constructor"
  1484. );
  1485. let ns = parse_and_resolve(
  1486. r#"
  1487. contract base {
  1488. constructor(bool x) {}
  1489. }
  1490. contract apex is base {
  1491. constructor() base(true) base(false) {}
  1492. function foo() pure public {}
  1493. }"#,
  1494. Target::Substrate {
  1495. address_length: 32,
  1496. value_length: 16,
  1497. },
  1498. );
  1499. assert_eq!(
  1500. first_error(ns.diagnostics),
  1501. "duplicate base contract ‘base’"
  1502. );
  1503. let mut runtime = build_solidity(
  1504. r##"
  1505. contract b is a {
  1506. int32 constant BAR = 102;
  1507. int64 public foo;
  1508. constructor(int64 i) a(BAR) { foo = i; }
  1509. function get_x() public returns (int32) {
  1510. return x;
  1511. }
  1512. }
  1513. contract a {
  1514. int32 public x;
  1515. constructor(int32 i) { x = i; }
  1516. }"##,
  1517. );
  1518. runtime.constructor(0, Val64(0xbffe).encode());
  1519. runtime.function("get_x", Vec::new());
  1520. assert_eq!(runtime.vm.output, Val(102).encode());
  1521. let mut runtime = build_solidity(
  1522. r##"
  1523. contract c is b(2) {
  1524. constructor() {
  1525. }
  1526. }
  1527. contract a {
  1528. int32 public x;
  1529. constructor(int32 i) { x = i; }
  1530. }
  1531. contract b is a {
  1532. int32 constant BAR = 102;
  1533. int64 public foo;
  1534. constructor(int64 i) a(BAR + int32(i)) { foo = i; }
  1535. function get_x() public view returns (int32) {
  1536. return x;
  1537. }
  1538. function get_foo() public view returns (int64) {
  1539. return foo;
  1540. }
  1541. }"##,
  1542. );
  1543. runtime.constructor(0, Vec::new());
  1544. runtime.function("get_x", Vec::new());
  1545. assert_eq!(runtime.vm.output, Val(104).encode());
  1546. let mut runtime = build_solidity(
  1547. r##"
  1548. contract c is b {
  1549. constructor(int64 x) b(x+3) {}
  1550. }
  1551. contract b is a {
  1552. constructor(int64 y) a(y+2) {}
  1553. }
  1554. contract a {
  1555. int64 foo;
  1556. function get_foo() public returns (int64) { return foo; }
  1557. constructor(int64 z) { foo = z; }
  1558. }"##,
  1559. );
  1560. runtime.constructor(0, Val64(7).encode());
  1561. runtime.function("get_foo", Vec::new());
  1562. assert_eq!(runtime.vm.output, Val64(12).encode());
  1563. let mut runtime = build_solidity(
  1564. r##"
  1565. contract c is b {
  1566. constructor(int64 x) b(x+3) a(x+5){}
  1567. }
  1568. abstract contract b is a {
  1569. constructor(int64 y) {}
  1570. }
  1571. contract a {
  1572. int64 foo;
  1573. function get_foo() public returns (int64) { return foo; }
  1574. constructor(int64 z) { foo = z; }
  1575. }"##,
  1576. );
  1577. runtime.constructor(0, Val64(7).encode());
  1578. runtime.function("get_foo", Vec::new());
  1579. assert_eq!(runtime.vm.output, Val64(12).encode());
  1580. let ns = parse_and_resolve(
  1581. r##"
  1582. contract c is b {
  1583. constructor(int64 x) b(x+3) {}
  1584. }
  1585. abstract contract b is a {
  1586. constructor(int64 y) {}
  1587. }
  1588. contract a {
  1589. int64 foo;
  1590. function get_foo() public returns (int64) { return foo; }
  1591. constructor(int64 z) { foo = z; }
  1592. }"##,
  1593. Target::Substrate {
  1594. address_length: 32,
  1595. value_length: 16,
  1596. },
  1597. );
  1598. assert_eq!(
  1599. first_error(ns.diagnostics),
  1600. "missing arguments to base contract ‘a’ constructor"
  1601. );
  1602. let ns = parse_and_resolve(
  1603. r##"
  1604. contract c is b {
  1605. constructor(int64 x) b(x+3) b(0) {}
  1606. }
  1607. abstract contract b is a {
  1608. constructor(int64 y) {}
  1609. }
  1610. contract a {
  1611. int64 foo;
  1612. function get_foo() public returns (int64) { return foo; }
  1613. constructor(int64 z) { foo = z; }
  1614. }"##,
  1615. Target::Substrate {
  1616. address_length: 32,
  1617. value_length: 16,
  1618. },
  1619. );
  1620. assert_eq!(first_error(ns.diagnostics), "duplicate base contract ‘b’");
  1621. let ns = parse_and_resolve(
  1622. r##"
  1623. contract c is b {
  1624. constructor(int64 x) b(x+3) a(0) {}
  1625. }
  1626. abstract contract b is a(2) {
  1627. constructor(int64 y) {}
  1628. }
  1629. contract a {
  1630. int64 foo;
  1631. function get_foo() public returns (int64) { return foo; }
  1632. constructor(int64 z) { foo = z; }
  1633. }"##,
  1634. Target::Substrate {
  1635. address_length: 32,
  1636. value_length: 16,
  1637. },
  1638. );
  1639. assert_eq!(
  1640. first_error(ns.diagnostics),
  1641. "duplicate argument for base contract ‘a’"
  1642. );
  1643. }
  1644. #[test]
  1645. fn call_base_function_via_basename() {
  1646. #[derive(Debug, PartialEq, Encode, Decode)]
  1647. struct Val(i32);
  1648. #[derive(Debug, PartialEq, Encode, Decode)]
  1649. struct Val64(u64);
  1650. let mut runtime = build_solidity(
  1651. r##"
  1652. contract c is b {
  1653. function bar() public returns (uint64) {
  1654. return a.foo();
  1655. }
  1656. }
  1657. contract b is a {
  1658. function foo() internal override returns (uint64) {
  1659. return 2;
  1660. }
  1661. }
  1662. contract a {
  1663. function foo() internal virtual returns (uint64) {
  1664. return 1;
  1665. }
  1666. }"##,
  1667. );
  1668. runtime.constructor(0, Vec::new());
  1669. runtime.function("bar", Vec::new());
  1670. assert_eq!(runtime.vm.output, Val64(1).encode());
  1671. let mut runtime = build_solidity(
  1672. r##"
  1673. contract c is b {
  1674. uint64 constant private C = 100;
  1675. function bar() public returns (uint64) {
  1676. return a.foo({ x: C });
  1677. }
  1678. }
  1679. contract b is a {
  1680. uint64 constant private C = 300;
  1681. function foo(uint64 x) internal override returns (uint64) {
  1682. return 2;
  1683. }
  1684. }
  1685. contract a {
  1686. uint64 constant private C = 200;
  1687. function foo(uint64 x) internal virtual returns (uint64) {
  1688. return 1 + x;
  1689. }
  1690. }"##,
  1691. );
  1692. runtime.constructor(0, Vec::new());
  1693. runtime.function("bar", Vec::new());
  1694. assert_eq!(runtime.vm.output, Val64(101).encode());
  1695. }
  1696. #[test]
  1697. fn simple_interface() {
  1698. let ns = parse_and_resolve(
  1699. r#"
  1700. interface IFoo {
  1701. function bar(uint32) external pure returns (uint32);
  1702. }
  1703. contract foo is IFoo {
  1704. function bar(uint32 a) public pure returns (uint32) {
  1705. return a * 2;
  1706. }
  1707. }"#,
  1708. Target::Substrate {
  1709. address_length: 32,
  1710. value_length: 16,
  1711. },
  1712. );
  1713. no_errors(ns.diagnostics);
  1714. let mut runtime = build_solidity(
  1715. r##"
  1716. contract foo is IFoo {
  1717. function bar(uint32 a) public pure override returns (uint32) {
  1718. return a * 2;
  1719. }
  1720. }
  1721. interface IFoo {
  1722. function bar(uint32) external pure returns (uint32);
  1723. }"##,
  1724. );
  1725. runtime.constructor(0, Vec::new());
  1726. runtime.function("bar", 100u32.encode());
  1727. assert_eq!(runtime.vm.output, 200u32.encode());
  1728. }
  1729. #[test]
  1730. fn cast_contract() {
  1731. let ns = parse_and_resolve(
  1732. r#"
  1733. interface operator {
  1734. function op1(int32 a, int32 b) external returns (int32);
  1735. function op2(int32 a, int32 b) external returns (int32);
  1736. }
  1737. contract ferqu {
  1738. operator op;
  1739. constructor(bool do_adds) {
  1740. if (do_adds) {
  1741. op = new m1();
  1742. } else {
  1743. op = new m2();
  1744. }
  1745. }
  1746. function x(int32 b) public returns (int32) {
  1747. return op.op1(102, b);
  1748. }
  1749. }
  1750. contract m1 is operator {
  1751. function op1(int32 a, int32 b) public override returns (int32) {
  1752. return a + b;
  1753. }
  1754. function op2(int32 a, int32 b) public override returns (int32) {
  1755. return a - b;
  1756. }
  1757. }
  1758. contract m2 is operator {
  1759. function op1(int32 a, int32 b) public override returns (int32) {
  1760. return a * b;
  1761. }
  1762. function op2(int32 a, int32 b) public override returns (int32) {
  1763. return a / b;
  1764. }
  1765. }"#,
  1766. Target::Substrate {
  1767. address_length: 32,
  1768. value_length: 16,
  1769. },
  1770. );
  1771. no_errors(ns.diagnostics);
  1772. let ns = parse_and_resolve(
  1773. r#"
  1774. interface IFoo {
  1775. function bar(uint32) external pure returns (uint32);
  1776. }
  1777. contract foo {
  1778. function bar(IFoo x) public pure returns (uint32) {
  1779. foo y = x;
  1780. }
  1781. }"#,
  1782. Target::Substrate {
  1783. address_length: 32,
  1784. value_length: 16,
  1785. },
  1786. );
  1787. assert_eq!(
  1788. first_error(ns.diagnostics),
  1789. "implicit conversion not allowed since contract foo is not a base contract of contract IFoo"
  1790. );
  1791. }
  1792. #[test]
  1793. fn test_super() {
  1794. let ns = parse_and_resolve(
  1795. r#"contract super {}"#,
  1796. Target::Substrate {
  1797. address_length: 32,
  1798. value_length: 16,
  1799. },
  1800. );
  1801. assert_eq!(
  1802. first_error(ns.diagnostics),
  1803. "‘super’ shadows name of a builtin"
  1804. );
  1805. let ns = parse_and_resolve(
  1806. r#"
  1807. function f1() { super.a(); }
  1808. "#,
  1809. Target::Substrate {
  1810. address_length: 32,
  1811. value_length: 16,
  1812. },
  1813. );
  1814. assert_eq!(
  1815. first_error(ns.diagnostics),
  1816. "super not available outside contracts"
  1817. );
  1818. let ns = parse_and_resolve(
  1819. r#"
  1820. contract a {
  1821. function f1() public {}
  1822. }
  1823. contract b is a {
  1824. function f2() public {
  1825. super.f2();
  1826. }
  1827. }"#,
  1828. Target::Substrate {
  1829. address_length: 32,
  1830. value_length: 16,
  1831. },
  1832. );
  1833. assert_eq!(first_error(ns.diagnostics), "unknown function or type ‘f2’");
  1834. let mut runtime = build_solidity(
  1835. r##"
  1836. contract b is a {
  1837. function bar() public returns (uint64) {
  1838. super.foo();
  1839. return var;
  1840. }
  1841. function foo() internal override {
  1842. var = 103;
  1843. }
  1844. }
  1845. contract a {
  1846. uint64 var;
  1847. function foo() internal virtual {
  1848. var = 102;
  1849. }
  1850. }"##,
  1851. );
  1852. runtime.constructor(0, Vec::new());
  1853. runtime.function("bar", Vec::new());
  1854. assert_eq!(runtime.vm.output, 102u64.encode());
  1855. let mut runtime = build_solidity(
  1856. r##"
  1857. contract b is a {
  1858. function bar() public returns (uint64) {
  1859. super.foo({x: 10});
  1860. return var;
  1861. }
  1862. function foo2(uint64 x) internal {
  1863. var = 103 + x;
  1864. }
  1865. }
  1866. contract a {
  1867. uint64 var;
  1868. function foo(uint64 x) internal virtual {
  1869. var = 102 + x;
  1870. }
  1871. }"##,
  1872. );
  1873. runtime.constructor(0, Vec::new());
  1874. runtime.function("bar", Vec::new());
  1875. assert_eq!(runtime.vm.output, 112u64.encode());
  1876. let mut runtime = build_solidity(
  1877. r##"
  1878. contract b is a, aa {
  1879. function bar() public returns (uint64) {
  1880. return super.foo({x: 10});
  1881. }
  1882. function foo(uint64 x) public override(a, aa) returns (uint64) {
  1883. return 103 + x;
  1884. }
  1885. }
  1886. contract a {
  1887. function foo(uint64 x) public virtual returns (uint64) {
  1888. return 102 + x;
  1889. }
  1890. }
  1891. contract aa {
  1892. function foo(uint64 x) public virtual returns (uint64) {
  1893. return 202 + x;
  1894. }
  1895. }"##,
  1896. );
  1897. runtime.constructor(0, Vec::new());
  1898. runtime.function("bar", Vec::new());
  1899. assert_eq!(runtime.vm.output, 112u64.encode());
  1900. }
  1901. #[test]
  1902. fn mutability() {
  1903. let ns = parse_and_resolve(
  1904. r#"
  1905. contract y {
  1906. function foo() external pure virtual returns (int) {
  1907. return 102;
  1908. }
  1909. }
  1910. contract x is y {
  1911. function foo() external override returns (int) {
  1912. return 102;
  1913. }
  1914. }
  1915. "#,
  1916. Target::Substrate {
  1917. address_length: 32,
  1918. value_length: 16,
  1919. },
  1920. );
  1921. assert_eq!(
  1922. first_error(ns.diagnostics),
  1923. "mutability ‘nonpayable’ of function ‘foo’ is not compatible with mutability ‘pure’"
  1924. );
  1925. let ns = parse_and_resolve(
  1926. r#"
  1927. abstract contract y {
  1928. function foo() external view virtual returns (int);
  1929. }
  1930. contract x is y {
  1931. function foo() external payable override returns (int) {
  1932. return 102;
  1933. }
  1934. }
  1935. "#,
  1936. Target::Substrate {
  1937. address_length: 32,
  1938. value_length: 16,
  1939. },
  1940. );
  1941. assert_eq!(
  1942. first_error(ns.diagnostics),
  1943. "mutability ‘payable’ of function ‘foo’ is not compatible with mutability ‘view’"
  1944. );
  1945. }
  1946. #[test]
  1947. fn visibility() {
  1948. let ns = parse_and_resolve(
  1949. r#"
  1950. contract y {
  1951. function foo() external virtual returns (int) {
  1952. return 102;
  1953. }
  1954. }
  1955. contract x is y {
  1956. function foo() public override returns (int) {
  1957. return 102;
  1958. }
  1959. }
  1960. "#,
  1961. Target::Substrate {
  1962. address_length: 32,
  1963. value_length: 16,
  1964. },
  1965. );
  1966. no_errors(ns.diagnostics);
  1967. let ns = parse_and_resolve(
  1968. r#"
  1969. abstract contract y {
  1970. function foo() external virtual returns (int);
  1971. }
  1972. contract x is y {
  1973. function foo() internal override returns (int) {
  1974. return 102;
  1975. }
  1976. }
  1977. "#,
  1978. Target::Substrate {
  1979. address_length: 32,
  1980. value_length: 16,
  1981. },
  1982. );
  1983. assert_eq!(
  1984. first_error(ns.diagnostics),
  1985. "visibility ‘internal’ of function ‘foo’ is not compatible with visibility ‘external’"
  1986. );
  1987. let ns = parse_and_resolve(
  1988. r#"
  1989. abstract contract y {
  1990. function foo() internal virtual returns (int);
  1991. }
  1992. contract x is y {
  1993. function foo() private override returns (int) {
  1994. return 102;
  1995. }
  1996. }
  1997. "#,
  1998. Target::Substrate {
  1999. address_length: 32,
  2000. value_length: 16,
  2001. },
  2002. );
  2003. assert_eq!(
  2004. first_error(ns.diagnostics),
  2005. "visibility ‘private’ of function ‘foo’ is not compatible with visibility ‘internal’"
  2006. );
  2007. }
  2008. #[test]
  2009. fn var_or_function() {
  2010. let mut runtime = build_solidity(
  2011. r##"
  2012. contract x is c {
  2013. function f1() public returns (int64) {
  2014. return c.selector();
  2015. }
  2016. function f2() public returns (int64) {
  2017. function() internal returns (int64) a = c.selector;
  2018. return a();
  2019. }
  2020. }
  2021. contract c {
  2022. int64 public selector = 102;
  2023. }"##,
  2024. );
  2025. runtime.constructor(0, Vec::new());
  2026. runtime.function("f1", Vec::new());
  2027. assert_eq!(runtime.vm.output, 102u64.encode());
  2028. runtime.function("f2", Vec::new());
  2029. assert_eq!(runtime.vm.output, 102u64.encode());
  2030. }