left_shift.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. // INCLUDE-FILE: toolchain/testing/testdata/min_prelude/uint.carbon
  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. //@dump-sem-ir-begin
  29. return LeftShift(a, b);
  30. //@dump-sem-ir-end
  31. }
  32. // --- u32.carbon
  33. library "[[@TEST_NAME]]";
  34. class Expect(N:! u32) {}
  35. fn Test(N:! u32) -> Expect(N) { return {}; }
  36. fn LeftShift(a: u32, b: i32) -> u32 = "int.left_shift";
  37. fn F() {
  38. Test(LeftShift(0, 0)) as Expect(0);
  39. Test(LeftShift(0, 1)) as Expect(0);
  40. Test(LeftShift(0, 30)) as Expect(0);
  41. Test(LeftShift(1, 30)) as Expect(0x4000_0000);
  42. Test(LeftShift(5, 2)) as Expect(20);
  43. Test(LeftShift(0xFFFF_FFFF, 1)) as Expect(0xFFFF_FFFE);
  44. Test(LeftShift(0xFFFF_FFFE, 1)) as Expect(0xFFFF_FFFC);
  45. Test(LeftShift(0xABCD_EF01, 8)) as Expect(0xCDEF_0100);
  46. }
  47. fn RuntimeCall(a: u32, b: i32) -> u32 {
  48. //@dump-sem-ir-begin
  49. return LeftShift(a, b);
  50. //@dump-sem-ir-end
  51. }
  52. // --- literal.carbon
  53. library "[[@TEST_NAME]]";
  54. fn LeftShift(a: Core.IntLiteral(), b: Core.IntLiteral()) -> Core.IntLiteral() = "int.left_shift";
  55. class Expect(N:! Core.IntLiteral()) {}
  56. fn Test(N:! Core.IntLiteral()) -> Expect(N) { return {}; }
  57. fn F() {
  58. // Zero can be shifted by any amount.
  59. Test(LeftShift(0, 0)) as Expect(0);
  60. Test(LeftShift(0, 0)) as Expect(0);
  61. Test(LeftShift(0, 1)) as Expect(0);
  62. Test(LeftShift(0, 30)) as Expect(0);
  63. Test(LeftShift(0, 1_000_000_000)) as Expect(0);
  64. // Positive numbers can be shifted.
  65. Test(LeftShift(1, 0)) as Expect(1);
  66. Test(LeftShift(1, 1)) as Expect(2);
  67. Test(LeftShift(2, 1)) as Expect(4);
  68. Test(LeftShift(1, 2)) as Expect(4);
  69. Test(LeftShift(3, 2)) as Expect(12);
  70. Test(LeftShift(1, 30)) as Expect(0x4000_0000);
  71. Test(LeftShift(5, 2)) as Expect(20);
  72. // Negative numbers can be shifted too.
  73. Test(LeftShift(-1, 0)) as Expect(-1);
  74. Test(LeftShift(-1, 1)) as Expect(-2);
  75. Test(LeftShift(-2, 1)) as Expect(-4);
  76. Test(LeftShift(-3, 1)) as Expect(-6);
  77. // Large numbers can be shifted losslessly.
  78. Test(LeftShift(0xFFFF_FFFF, 1)) as Expect(0x1_FFFF_FFFE);
  79. Test(LeftShift(0xFFFF_FFFE, 1)) as Expect(0x1_FFFF_FFFC);
  80. Test(LeftShift(0xABCD_EF01, 8)) as Expect(0xAB_CDEF_0100);
  81. Test(LeftShift(0x7FFF_FFFF_FFFF_FFFF, 1)) as Expect(0xFFFF_FFFF_FFFF_FFFE);
  82. Test(LeftShift(0xFFFF_FFFF_FFFF_FFFF, 1)) as Expect(0x1_FFFF_FFFF_FFFF_FFFE);
  83. }
  84. // --- fail_bad_shift.carbon
  85. library "[[@TEST_NAME]]";
  86. fn LeftShift(a: i32, b: i32) -> i32 = "int.left_shift";
  87. fn LeftShiftLit(a: Core.IntLiteral(), b: i32) -> Core.IntLiteral() = "int.left_shift";
  88. // Shift greater than size is disallowed for sized types.
  89. let size_1: i32 = LeftShift(1, 31);
  90. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:19: error: shift distance >= type width of 32 in `1 << 32` [CompileTimeShiftOutOfRange]
  91. // CHECK:STDERR: let size_2: i32 = LeftShift(1, 32);
  92. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  93. // CHECK:STDERR:
  94. let size_2: i32 = LeftShift(1, 32);
  95. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:19: error: shift distance >= type width of 32 in `1 << 33` [CompileTimeShiftOutOfRange]
  96. // CHECK:STDERR: let size_3: i32 = LeftShift(1, 33);
  97. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  98. // CHECK:STDERR:
  99. let size_3: i32 = LeftShift(1, 33);
  100. // Overflow is allowed if the shift distance is in bounds.
  101. let overflow_1: i32 = LeftShift(1000, 31);
  102. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:23: error: shift distance >= type width of 32 in `1000 << 32` [CompileTimeShiftOutOfRange]
  103. // CHECK:STDERR: let overflow_2: i32 = LeftShift(1000, 32);
  104. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~
  105. // CHECK:STDERR:
  106. let overflow_2: i32 = LeftShift(1000, 32);
  107. // Oversize shifts aren't allowed even if there's no overflow.
  108. let no_overflow_1: i32 = LeftShift(0, 31);
  109. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:26: error: shift distance >= type width of 32 in `0 << 32` [CompileTimeShiftOutOfRange]
  110. // CHECK:STDERR: let no_overflow_2: i32 = LeftShift(0, 32);
  111. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  112. // CHECK:STDERR:
  113. let no_overflow_2: i32 = LeftShift(0, 32);
  114. // Negative shifts aren't allowed either, even for literals, even if the lhs is zero.
  115. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:21: error: shift distance >= type width of 32 in `1 << -1` [CompileTimeShiftOutOfRange]
  116. // CHECK:STDERR: let negative: i32 = LeftShift(1, -1);
  117. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  118. // CHECK:STDERR:
  119. let negative: i32 = LeftShift(1, -1);
  120. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:26: error: shift distance >= type width of 32 in `0 << -1` [CompileTimeShiftOutOfRange]
  121. // CHECK:STDERR: let negative_zero: i32 = LeftShift(0, -1);
  122. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  123. // CHECK:STDERR:
  124. let negative_zero: i32 = LeftShift(0, -1);
  125. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:39: error: shift distance negative in `1 << -1` [CompileTimeShiftNegative]
  126. // CHECK:STDERR: let negative_lit: Core.IntLiteral() = LeftShiftLit(1, -1);
  127. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~
  128. // CHECK:STDERR:
  129. let negative_lit: Core.IntLiteral() = LeftShiftLit(1, -1);
  130. // CHECK:STDERR: fail_bad_shift.carbon:[[@LINE+4]]:44: error: shift distance negative in `0 << -1` [CompileTimeShiftNegative]
  131. // CHECK:STDERR: let negative_lit_zero: Core.IntLiteral() = LeftShiftLit(0, -1);
  132. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~
  133. // CHECK:STDERR:
  134. let negative_lit_zero: Core.IntLiteral() = LeftShiftLit(0, -1);
  135. // --- fail_literal_overflow.carbon
  136. library "[[@TEST_NAME]]";
  137. fn LeftShift(a: Core.IntLiteral(), b: Core.IntLiteral()) -> Core.IntLiteral() = "int.left_shift";
  138. // 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]
  139. // CHECK:STDERR: let bad: i32 = LeftShift(1, 1_000_000_000);
  140. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~
  141. // CHECK:STDERR:
  142. let bad: i32 = LeftShift(1, 1_000_000_000);
  143. // 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]
  144. // CHECK:STDERR: let bad_negative: i32 = LeftShift(-1, 1_000_000_000);
  145. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
  146. // CHECK:STDERR:
  147. let bad_negative: i32 = LeftShift(-1, 1_000_000_000);
  148. // --- fail_comp_time_only_shift.carbon
  149. library "[[@TEST_NAME]]";
  150. fn LeftShiftByLit(a: i32, b: Core.IntLiteral()) -> i32 = "int.left_shift";
  151. fn LeftShiftOfLit(a: Core.IntLiteral(), b: i32) -> Core.IntLiteral() = "int.left_shift";
  152. var a_lit: Core.IntLiteral() = 12;
  153. var an_i32: i32 = 34;
  154. // This can't be valid: we don't have a compile-time or runtime integer value for `a_lit`.
  155. // CHECK:STDERR: fail_comp_time_only_shift.carbon:[[@LINE+7]]:17: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  156. // CHECK:STDERR: let bad1: i32 = LeftShiftByLit(an_i32, a_lit);
  157. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  158. // CHECK:STDERR: fail_comp_time_only_shift.carbon:[[@LINE-10]]:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  159. // CHECK:STDERR: fn LeftShiftByLit(a: i32, b: Core.IntLiteral()) -> i32 = "int.left_shift";
  160. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  161. // CHECK:STDERR:
  162. let bad1: i32 = LeftShiftByLit(an_i32, a_lit);
  163. // TODO: This could be valid because we don't actually need the return value at runtime.
  164. // CHECK:STDERR: fail_comp_time_only_shift.carbon:[[@LINE+7]]:31: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  165. // CHECK:STDERR: let bad2: Core.IntLiteral() = LeftShiftOfLit(a_lit, an_i32);
  166. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  167. // CHECK:STDERR: fail_comp_time_only_shift.carbon:[[@LINE-19]]:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  168. // CHECK:STDERR: fn LeftShiftOfLit(a: Core.IntLiteral(), b: i32) -> Core.IntLiteral() = "int.left_shift";
  169. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  170. // CHECK:STDERR:
  171. let bad2: Core.IntLiteral() = LeftShiftOfLit(a_lit, an_i32);
  172. // TODO: This could be valid because the literal argument has a constant value.
  173. // CHECK:STDERR: fail_comp_time_only_shift.carbon:[[@LINE+7]]:17: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  174. // CHECK:STDERR: let bad3: i32 = LeftShiftByLit(an_i32, 12);
  175. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~
  176. // CHECK:STDERR: fail_comp_time_only_shift.carbon:[[@LINE-30]]:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  177. // CHECK:STDERR: fn LeftShiftByLit(a: i32, b: Core.IntLiteral()) -> i32 = "int.left_shift";
  178. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  179. // CHECK:STDERR:
  180. let bad3: i32 = LeftShiftByLit(an_i32, 12);
  181. // TODO: This could be valid because we don't actually need the return value at runtime.
  182. // CHECK:STDERR: fail_comp_time_only_shift.carbon:[[@LINE+7]]:31: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  183. // CHECK:STDERR: let bad4: Core.IntLiteral() = LeftShiftOfLit(12, an_i32);
  184. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~
  185. // CHECK:STDERR: fail_comp_time_only_shift.carbon:[[@LINE-39]]:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  186. // CHECK:STDERR: fn LeftShiftOfLit(a: Core.IntLiteral(), b: i32) -> Core.IntLiteral() = "int.left_shift";
  187. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  188. // CHECK:STDERR:
  189. let bad4: Core.IntLiteral() = LeftShiftOfLit(12, an_i32);
  190. // CHECK:STDOUT: --- i32.carbon
  191. // CHECK:STDOUT:
  192. // CHECK:STDOUT: constants {
  193. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  194. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  195. // CHECK:STDOUT: %LeftShift.type: type = fn_type @LeftShift [concrete]
  196. // CHECK:STDOUT: %LeftShift: %LeftShift.type = struct_value () [concrete]
  197. // CHECK:STDOUT: }
  198. // CHECK:STDOUT:
  199. // CHECK:STDOUT: imports {
  200. // CHECK:STDOUT: }
  201. // CHECK:STDOUT:
  202. // CHECK:STDOUT: fn @RuntimeCallIsValid(%a.param: %i32, %b.param: %i32) -> %i32 {
  203. // CHECK:STDOUT: !entry:
  204. // CHECK:STDOUT: %LeftShift.ref: %LeftShift.type = name_ref LeftShift, file.%LeftShift.decl [concrete = constants.%LeftShift]
  205. // CHECK:STDOUT: %a.ref: %i32 = name_ref a, %a
  206. // CHECK:STDOUT: %b.ref: %i32 = name_ref b, %b
  207. // CHECK:STDOUT: %LeftShift.call: init %i32 = call %LeftShift.ref(%a.ref, %b.ref)
  208. // CHECK:STDOUT: return %LeftShift.call to %return.param
  209. // CHECK:STDOUT: }
  210. // CHECK:STDOUT:
  211. // CHECK:STDOUT: --- u32.carbon
  212. // CHECK:STDOUT:
  213. // CHECK:STDOUT: constants {
  214. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  215. // CHECK:STDOUT: %u32: type = class_type @UInt, @UInt(%int_32) [concrete]
  216. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  217. // CHECK:STDOUT: %LeftShift.type: type = fn_type @LeftShift [concrete]
  218. // CHECK:STDOUT: %LeftShift: %LeftShift.type = struct_value () [concrete]
  219. // CHECK:STDOUT: }
  220. // CHECK:STDOUT:
  221. // CHECK:STDOUT: imports {
  222. // CHECK:STDOUT: }
  223. // CHECK:STDOUT:
  224. // CHECK:STDOUT: fn @RuntimeCall(%a.param: %u32, %b.param: %i32) -> %u32 {
  225. // CHECK:STDOUT: !entry:
  226. // CHECK:STDOUT: %LeftShift.ref: %LeftShift.type = name_ref LeftShift, file.%LeftShift.decl [concrete = constants.%LeftShift]
  227. // CHECK:STDOUT: %a.ref: %u32 = name_ref a, %a
  228. // CHECK:STDOUT: %b.ref: %i32 = name_ref b, %b
  229. // CHECK:STDOUT: %LeftShift.call: init %u32 = call %LeftShift.ref(%a.ref, %b.ref)
  230. // CHECK:STDOUT: return %LeftShift.call to %return.param
  231. // CHECK:STDOUT: }
  232. // CHECK:STDOUT: