overflow_constant_propagation.sol 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Note: none of these errors will occur without --no-constant-folding
  2. contract foo {
  3. function test() public pure {
  4. uint32 x = 2147483648;
  5. uint32 y = 2147483648;
  6. uint32 z = x + y;
  7. print("z: {}".format(z));
  8. uint16 a1 = 256;
  9. uint16 b1 = 257;
  10. uint16 c1 = a1 * b1;
  11. print("c1: {}".format(c1));
  12. uint16 a2 = 10;
  13. uint16 b2 = 5;
  14. uint16 c2 = a2 ** b2;
  15. print("c2: {}".format(c2));
  16. uint16 a3 = 256;
  17. uint16 b3 = 257;
  18. uint16 c3 = a3 - b3;
  19. print("c3: {}".format(c3));
  20. int16 a4 = -0x8000;
  21. int16 c4 = -a4;
  22. print("c4: {}".format(c4));
  23. }
  24. function test_unchecked() public pure {
  25. unchecked {
  26. uint32 x = 2147483648;
  27. uint32 y = 2147483648;
  28. uint32 z = x + y;
  29. print("z: {}".format(z));
  30. uint16 a1 = 256;
  31. uint16 b1 = 257;
  32. uint16 c1 = a1 * b1;
  33. print("c1: {}".format(c1));
  34. uint16 a2 = 10;
  35. uint16 b2 = 5;
  36. uint16 c2 = a2 ** b2;
  37. print("c2: {}".format(c2));
  38. uint16 a3 = 256;
  39. uint16 b3 = 257;
  40. uint16 c3 = a3 - b3;
  41. print("c3: {}".format(c3));
  42. int16 a4 = -0x8000;
  43. int16 c4 = -a4;
  44. print("c4: {}".format(c4));
  45. }
  46. }
  47. function test_big() public pure returns (uint256) {
  48. uint256 a1 = 1 << 255;
  49. uint64 b1 = 1 << 31;
  50. uint256 c1 = a1**b1;
  51. print("c1: {}".format(c1));
  52. uint256 a2 = 1 << 255;
  53. uint64 b2 = 1 << 3;
  54. uint256 c2 = a2**b2;
  55. print("c2: {}".format(c2));
  56. }
  57. }
  58. // ---- Expect: diagnostics ----
  59. // error: 6:20-25: value 4294967296 does not fit into type uint32.
  60. // error: 11:21-28: value 65792 does not fit into type uint16.
  61. // error: 16:21-29: value 100000 does not fit into type uint16.
  62. // error: 21:21-28: negative value -1 does not fit into type uint16. Cannot implicitly convert signed literal to unsigned type.
  63. // error: 25:20-23: value 32768 does not fit into type int16.
  64. // error: 60:26-32: power 2147483648 not possible
  65. // error: 65:26-32: value is too large to fit into type uint256