left_shift.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. //
  5. // EXTRA-ARGS: --no-dump-sem-ir
  6. //
  7. // AUTOUPDATE
  8. // TIP: To test this file alone, run:
  9. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/builtins/int/left_shift.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/builtins/int/left_shift.carbon
  12. // --- i32.carbon
  13. library "[[@TEST_NAME]]";
  14. class Expect(N:! i32) {}
  15. fn Test(N:! i32) -> Expect(N) { return {}; }
  16. fn LeftShift(a: i32, b: i32) -> i32 = "int.left_shift";
  17. fn F() {
  18. Test(LeftShift(0, 0)) as Expect(0);
  19. Test(LeftShift(0, 1)) as Expect(0);
  20. Test(LeftShift(0, 30)) as Expect(0);
  21. Test(LeftShift(1, 30)) as Expect(0x4000_0000);
  22. Test(LeftShift(5, 2)) as Expect(20);
  23. Test(LeftShift(-1, 1)) as Expect(-2);
  24. Test(LeftShift(-2, 1)) as Expect(-4);
  25. Test(LeftShift(-3, 1)) as Expect(-6);
  26. }
  27. fn RuntimeCallIsValid(a: i32, b: i32) -> i32 {
  28. return LeftShift(a, b);
  29. }
  30. // --- u32.carbon
  31. library "[[@TEST_NAME]]";
  32. class Expect(N:! u32) {}
  33. fn Test(N:! u32) -> Expect(N) { return {}; }
  34. fn LeftShift(a: u32, b: i32) -> u32 = "int.left_shift";
  35. fn F() {
  36. Test(LeftShift(0, 0)) as Expect(0);
  37. Test(LeftShift(0, 1)) as Expect(0);
  38. Test(LeftShift(0, 30)) as Expect(0);
  39. Test(LeftShift(1, 30)) as Expect(0x4000_0000);
  40. Test(LeftShift(5, 2)) as Expect(20);
  41. Test(LeftShift(0xFFFF_FFFF, 1)) as Expect(0xFFFF_FFFE);
  42. Test(LeftShift(0xFFFF_FFFE, 1)) as Expect(0xFFFF_FFFC);
  43. Test(LeftShift(0xABCD_EF01, 8)) as Expect(0xCDEF_0100);
  44. }
  45. fn RuntimeCall(a: u32, b: i32) -> u32 {
  46. return LeftShift(a, b);
  47. }
  48. // --- literal.carbon
  49. library "[[@TEST_NAME]]";
  50. fn LeftShift(a: Core.IntLiteral(), b: Core.IntLiteral()) -> Core.IntLiteral() = "int.left_shift";
  51. class Expect(N:! Core.IntLiteral()) {}
  52. fn Test(N:! Core.IntLiteral()) -> Expect(N) { return {}; }
  53. fn F() {
  54. // Zero can be shifted by any amount.
  55. Test(LeftShift(0, 0)) as Expect(0);
  56. Test(LeftShift(0, 0)) as Expect(0);
  57. Test(LeftShift(0, 1)) as Expect(0);
  58. Test(LeftShift(0, 30)) as Expect(0);
  59. Test(LeftShift(0, 1_000_000_000)) as Expect(0);
  60. // Positive numbers can be shifted.
  61. Test(LeftShift(1, 0)) as Expect(1);
  62. Test(LeftShift(1, 1)) as Expect(2);
  63. Test(LeftShift(2, 1)) as Expect(4);
  64. Test(LeftShift(1, 2)) as Expect(4);
  65. Test(LeftShift(3, 2)) as Expect(12);
  66. Test(LeftShift(1, 30)) as Expect(0x4000_0000);
  67. Test(LeftShift(5, 2)) as Expect(20);
  68. // Negative numbers can be shifted too.
  69. Test(LeftShift(-1, 0)) as Expect(-1);
  70. Test(LeftShift(-1, 1)) as Expect(-2);
  71. Test(LeftShift(-2, 1)) as Expect(-4);
  72. Test(LeftShift(-3, 1)) as Expect(-6);
  73. // Large numbers can be shifted losslessly.
  74. Test(LeftShift(0xFFFF_FFFF, 1)) as Expect(0x1_FFFF_FFFE);
  75. Test(LeftShift(0xFFFF_FFFE, 1)) as Expect(0x1_FFFF_FFFC);
  76. Test(LeftShift(0xABCD_EF01, 8)) as Expect(0xAB_CDEF_0100);
  77. Test(LeftShift(0x7FFF_FFFF_FFFF_FFFF, 1)) as Expect(0xFFFF_FFFF_FFFF_FFFE);
  78. Test(LeftShift(0xFFFF_FFFF_FFFF_FFFF, 1)) as Expect(0x1_FFFF_FFFF_FFFF_FFFE);
  79. }
  80. // --- fail_bad_shift.carbon
  81. library "[[@TEST_NAME]]";
  82. fn LeftShift(a: i32, b: i32) -> i32 = "int.left_shift";
  83. fn LeftShiftLit(a: Core.IntLiteral(), b: i32) -> Core.IntLiteral() = "int.left_shift";
  84. // Shift greater than size is disallowed for sized types.
  85. let size_1: i32 = LeftShift(1, 31);
  86. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:19: error: shift distance >= type width of 32 in `1 << 32` [CompileTimeShiftOutOfRange]
  87. // CHECK:STDERR: let size_2: i32 = LeftShift(1, 32);
  88. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  89. // CHECK:STDERR:
  90. let size_2: i32 = LeftShift(1, 32);
  91. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:19: error: shift distance >= type width of 32 in `1 << 33` [CompileTimeShiftOutOfRange]
  92. // CHECK:STDERR: let size_3: i32 = LeftShift(1, 33);
  93. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  94. // CHECK:STDERR:
  95. let size_3: i32 = LeftShift(1, 33);
  96. // Overflow is allowed if the shift distance is in bounds.
  97. let overflow_1: i32 = LeftShift(1000, 31);
  98. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:23: error: shift distance >= type width of 32 in `1000 << 32` [CompileTimeShiftOutOfRange]
  99. // CHECK:STDERR: let overflow_2: i32 = LeftShift(1000, 32);
  100. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~
  101. // CHECK:STDERR:
  102. let overflow_2: i32 = LeftShift(1000, 32);
  103. // Oversize shifts aren't allowed even if there's no overflow.
  104. let no_overflow_1: i32 = LeftShift(0, 31);
  105. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:26: error: shift distance >= type width of 32 in `0 << 32` [CompileTimeShiftOutOfRange]
  106. // CHECK:STDERR: let no_overflow_2: i32 = LeftShift(0, 32);
  107. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  108. // CHECK:STDERR:
  109. let no_overflow_2: i32 = LeftShift(0, 32);
  110. // Negative shifts aren't allowed either, even for literals, even if the lhs is zero.
  111. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:21: error: shift distance >= type width of 32 in `1 << -1` [CompileTimeShiftOutOfRange]
  112. // CHECK:STDERR: let negative: i32 = LeftShift(1, -1);
  113. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  114. // CHECK:STDERR:
  115. let negative: i32 = LeftShift(1, -1);
  116. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:26: error: shift distance >= type width of 32 in `0 << -1` [CompileTimeShiftOutOfRange]
  117. // CHECK:STDERR: let negative_zero: i32 = LeftShift(0, -1);
  118. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  119. // CHECK:STDERR:
  120. let negative_zero: i32 = LeftShift(0, -1);
  121. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:39: error: shift distance negative in `1 << -1` [CompileTimeShiftNegative]
  122. // CHECK:STDERR: let negative_lit: Core.IntLiteral() = LeftShiftLit(1, -1);
  123. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~
  124. // CHECK:STDERR:
  125. let negative_lit: Core.IntLiteral() = LeftShiftLit(1, -1);
  126. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:44: error: shift distance negative in `0 << -1` [CompileTimeShiftNegative]
  127. // CHECK:STDERR: let negative_lit_zero: Core.IntLiteral() = LeftShiftLit(0, -1);
  128. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~
  129. // CHECK:STDERR:
  130. let negative_lit_zero: Core.IntLiteral() = LeftShiftLit(0, -1);
  131. // --- fail_literal_overflow.carbon
  132. library "[[@TEST_NAME]]";
  133. fn LeftShift(a: Core.IntLiteral(), b: Core.IntLiteral()) -> Core.IntLiteral() = "int.left_shift";
  134. // CHECK:STDERR: fail_literal_overflow.carbon:[[@LINE+4]]:16: error: shift distance of 1000000000 would result in an integer whose width is greater than the maximum supported width of 8388608 [CompileTimeUnsizedShiftOutOfRange]
  135. // CHECK:STDERR: let bad: i32 = LeftShift(1, 1_000_000_000);
  136. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~
  137. // CHECK:STDERR:
  138. let bad: i32 = LeftShift(1, 1_000_000_000);
  139. // CHECK:STDERR: fail_literal_overflow.carbon:[[@LINE+4]]:25: error: shift distance of 1000000000 would result in an integer whose width is greater than the maximum supported width of 8388608 [CompileTimeUnsizedShiftOutOfRange]
  140. // CHECK:STDERR: let bad_negative: i32 = LeftShift(-1, 1_000_000_000);
  141. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
  142. // CHECK:STDERR:
  143. let bad_negative: i32 = LeftShift(-1, 1_000_000_000);
  144. // --- fail_comp_time_only_shift.carbon
  145. library "[[@TEST_NAME]]";
  146. fn LeftShiftByLit(a: i32, b: Core.IntLiteral()) -> i32 = "int.left_shift";
  147. fn LeftShiftOfLit(a: Core.IntLiteral(), b: i32) -> Core.IntLiteral() = "int.left_shift";
  148. var a_lit: Core.IntLiteral() = 12;
  149. var an_i32: i32 = 34;
  150. // This can't be valid: we don't have a compile-time or runtime integer value for `a_lit`.
  151. // CHECK:STDERR: fail_comp_time_only_shift.carbon:[[@LINE+7]]:17: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  152. // CHECK:STDERR: let bad1: i32 = LeftShiftByLit(an_i32, a_lit);
  153. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  154. // CHECK:STDERR: fail_comp_time_only_shift.carbon:[[@LINE-10]]:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  155. // CHECK:STDERR: fn LeftShiftByLit(a: i32, b: Core.IntLiteral()) -> i32 = "int.left_shift";
  156. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  157. // CHECK:STDERR:
  158. let bad1: i32 = LeftShiftByLit(an_i32, a_lit);
  159. // TODO: This could be valid because we don't actually need the return value at runtime.
  160. // CHECK:STDERR: fail_comp_time_only_shift.carbon:[[@LINE+7]]:31: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  161. // CHECK:STDERR: let bad2: Core.IntLiteral() = LeftShiftOfLit(a_lit, an_i32);
  162. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  163. // CHECK:STDERR: fail_comp_time_only_shift.carbon:[[@LINE-19]]:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  164. // CHECK:STDERR: fn LeftShiftOfLit(a: Core.IntLiteral(), b: i32) -> Core.IntLiteral() = "int.left_shift";
  165. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  166. // CHECK:STDERR:
  167. let bad2: Core.IntLiteral() = LeftShiftOfLit(a_lit, an_i32);
  168. // TODO: This could be valid because the literal argument has a constant value.
  169. // CHECK:STDERR: fail_comp_time_only_shift.carbon:[[@LINE+7]]:17: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  170. // CHECK:STDERR: let bad3: i32 = LeftShiftByLit(an_i32, 12);
  171. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~
  172. // CHECK:STDERR: fail_comp_time_only_shift.carbon:[[@LINE-30]]:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  173. // CHECK:STDERR: fn LeftShiftByLit(a: i32, b: Core.IntLiteral()) -> i32 = "int.left_shift";
  174. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  175. // CHECK:STDERR:
  176. let bad3: i32 = LeftShiftByLit(an_i32, 12);
  177. // TODO: This could be valid because we don't actually need the return value at runtime.
  178. // CHECK:STDERR: fail_comp_time_only_shift.carbon:[[@LINE+7]]:31: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  179. // CHECK:STDERR: let bad4: Core.IntLiteral() = LeftShiftOfLit(12, an_i32);
  180. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~
  181. // CHECK:STDERR: fail_comp_time_only_shift.carbon:[[@LINE-39]]:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  182. // CHECK:STDERR: fn LeftShiftOfLit(a: Core.IntLiteral(), b: i32) -> Core.IntLiteral() = "int.left_shift";
  183. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  184. // CHECK:STDERR:
  185. let bad4: Core.IntLiteral() = LeftShiftOfLit(12, an_i32);