overload_functions.sol 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. contract Test1 {
  2. function sum(int32 a, int32 b) public pure returns (int32) {
  3. return a + b;
  4. }
  5. // This is not allowed on Solana, because the discriminator for both functions is exactly the same.
  6. function sum(int64 a, int64 b) public pure returns (int64) {
  7. return a + b;
  8. }
  9. }
  10. contract Test2 {
  11. constructor() {
  12. }
  13. function sub(int32 d) public pure returns (int32) {
  14. return d-2;
  15. }
  16. function multiply(int32 a, int32 b) public pure returns (int32) {
  17. return a*b;
  18. }
  19. }
  20. contract Test3 is Test2 {
  21. int32 state;
  22. constructor(int32 state_var) {
  23. state = state_var;
  24. }
  25. function multiply(int32 c) public pure returns (int32) {
  26. return c*state;
  27. }
  28. }
  29. contract Test4 is Test2 {
  30. int32 state;
  31. constructor(int32 state_var) {
  32. state = state_var;
  33. }
  34. function multiply(int32 a, int32 b) public pure returns (int32) {
  35. return a*state*b;
  36. }
  37. }
  38. contract Test5 is Test3 {
  39. constructor(int32 state_var) Test3(state_var) {}
  40. function sub(int64 e) public pure returns (int64) {
  41. return e-2;
  42. }
  43. }
  44. abstract contract Test6 {
  45. constructor() {}
  46. function doThis() public virtual returns (int32);
  47. }
  48. contract Test7 is Test6 {
  49. constructor() {
  50. }
  51. function doThis() public override(Test6) returns (int32) {
  52. return 7;
  53. }
  54. }
  55. contract Base1
  56. {
  57. function foo() virtual public {}
  58. }
  59. contract Base2
  60. {
  61. function foo() virtual public {}
  62. }
  63. contract Inherited is Base1, Base2
  64. {
  65. // This should be allowed
  66. function foo() public override(Base1, Base2) {}
  67. }
  68. contract ManglingInvalid {
  69. function foo_bool() public pure returns (int32) {
  70. return 2;
  71. }
  72. // This should not be allowed
  73. function foo(bool a) public pure returns (int32) {
  74. if (a) {
  75. return 1;
  76. } else {
  77. return 3;
  78. }
  79. }
  80. }
  81. // ---- Expect: diagnostics ----
  82. // error: 22:5-68: function 'multiply' with this signature already defined
  83. // note 44:5-68: previous definition of function 'multiply'
  84. // error: 95:5-53: mangling the symbol of overloaded function 'foo' with signature 'foo(bool)' results in a new symbol 'foo_bool' but this symbol already exists
  85. // note 90:5-52: this function declaration conflicts with mangled name