convert.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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/convert.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/builtins/int/convert.carbon
  12. // --- int_ops.carbon
  13. library "[[@TEST_NAME]]";
  14. // Size preserving
  15. fn Int32ToInt32(a: i32) -> i32 = "int.convert";
  16. fn Int32ToUint32(a: i32) -> u32 = "int.convert";
  17. fn Uint32ToInt32(a: u32) -> i32 = "int.convert";
  18. fn Uint32ToUint32(a: u32) -> u32 = "int.convert";
  19. fn IntLiteralToIntLiteral(a: Core.IntLiteral()) -> Core.IntLiteral() = "int.convert";
  20. // Narrowing
  21. fn Int32ToInt16(a: i32) -> i16 = "int.convert";
  22. fn Int32ToUint16(a: i32) -> u16 = "int.convert";
  23. fn Uint32ToInt16(a: u32) -> i16 = "int.convert";
  24. fn Uint32ToUint16(a: u32) -> u16 = "int.convert";
  25. fn IntLiteralToInt16(a: Core.IntLiteral()) -> i16 = "int.convert";
  26. fn IntLiteralToUint16(a: Core.IntLiteral()) -> u16 = "int.convert";
  27. // Widening
  28. fn Int32ToInt64(a: i32) -> i64 = "int.convert";
  29. fn Int32ToUint64(a: i32) -> u64 = "int.convert";
  30. fn Uint32ToInt64(a: u32) -> i64 = "int.convert";
  31. fn Uint32ToUint64(a: u32) -> u64 = "int.convert";
  32. fn Int32ToIntLiteral(a: i32) -> Core.IntLiteral() = "int.convert";
  33. fn Uint32ToIntLiteral(a: u32) -> Core.IntLiteral() = "int.convert";
  34. class Expect[T:! type](N:! T) {}
  35. fn Test[T:! type](N:! T) -> Expect(N) { return {}; }
  36. // --- runtime_call.carbon
  37. library "[[@TEST_NAME]]";
  38. import library "int_ops";
  39. fn SizePreserving(a: i32) -> u32 {
  40. //@dump-sem-ir-begin
  41. return Int32ToUint32(a);
  42. //@dump-sem-ir-end
  43. }
  44. fn Narrowing(a: i32) -> i16 {
  45. //@dump-sem-ir-begin
  46. return Int32ToInt16(a);
  47. //@dump-sem-ir-end
  48. }
  49. fn Widening(a: i32) -> i64 {
  50. //@dump-sem-ir-begin
  51. return Int32ToInt64(a);
  52. //@dump-sem-ir-end
  53. }
  54. // --- fail_self_test.carbon
  55. library "[[@TEST_NAME]]";
  56. import library "int_ops";
  57. fn F() {
  58. // Ensure our testing machinery works.
  59. // CHECK:STDERR: fail_self_test.carbon:[[@LINE+7]]:3: error: cannot convert expression of type `Expect(0)` to `Expect(1)` with `as` [ConversionFailure]
  60. // CHECK:STDERR: Test(Int32ToInt32(0)) as Expect(1 as i32);
  61. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  62. // CHECK:STDERR: fail_self_test.carbon:[[@LINE+4]]:3: note: type `Expect(0)` does not implement interface `Core.As(Expect(1))` [MissingImplInMemberAccessNote]
  63. // CHECK:STDERR: Test(Int32ToInt32(0)) as Expect(1 as i32);
  64. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  65. // CHECK:STDERR:
  66. Test(Int32ToInt32(0)) as Expect(1 as i32);
  67. }
  68. // --- identity.carbon
  69. library "[[@TEST_NAME]]";
  70. import library "int_ops";
  71. fn F() {
  72. Test(Int32ToInt32(-0x8000_0000)) as Expect(-0x8000_0000 as i32);
  73. Test(Int32ToInt32(-1)) as Expect(-1 as i32);
  74. Test(Int32ToInt32(0)) as Expect(0 as i32);
  75. Test(Int32ToInt32(0x7FFF_FFFF)) as Expect(0x7FFF_FFFF as i32);
  76. Test(Uint32ToUint32(0)) as Expect(0 as u32);
  77. Test(Uint32ToUint32(0x7FFF_FFFF)) as Expect(0x7FFF_FFFF as u32);
  78. Test(Uint32ToUint32(0x8000_0000)) as Expect(0x8000_0000 as u32);
  79. Test(Uint32ToUint32(0xFFFF_FFFF)) as Expect(0xFFFF_FFFF as u32);
  80. Test(IntLiteralToIntLiteral(0x1_0000_0000_0000_0000)) as
  81. Expect(0x1_0000_0000_0000_0000);
  82. Test(IntLiteralToIntLiteral(-1)) as Expect(-1);
  83. }
  84. // --- same_size.carbon
  85. library "[[@TEST_NAME]]";
  86. import library "int_ops";
  87. fn F() {
  88. Test(Int32ToUint32(-0x8000_0000)) as Expect(0x8000_0000 as u32);
  89. Test(Int32ToUint32(-1)) as Expect(0xFFFF_FFFF as u32);
  90. Test(Int32ToUint32(0)) as Expect(0 as u32);
  91. Test(Int32ToUint32(0x7FFF_FFFF)) as Expect(0x7FFF_FFFF as u32);
  92. Test(Uint32ToInt32(0)) as Expect(0 as i32);
  93. Test(Uint32ToInt32(0x7FFF_FFFF)) as Expect(0x7FFF_FFFF as i32);
  94. Test(Uint32ToInt32(0x8000_0000)) as Expect(-0x8000_0000 as i32);
  95. Test(Uint32ToInt32(0xFFFF_FFFF)) as Expect(-1 as i32);
  96. }
  97. // --- truncate.carbon
  98. library "[[@TEST_NAME]]";
  99. import library "int_ops";
  100. fn F() {
  101. Test(Int32ToInt16(-0x8000_0000)) as Expect(0 as i16);
  102. Test(Int32ToInt16(-0x7FFF_EDCC)) as Expect(0x1234 as i16);
  103. Test(Int32ToInt16(-0x7FFF_1234)) as Expect(-0x1234 as i16);
  104. Test(Int32ToInt16(-0x8000)) as Expect(-0x8000 as i16);
  105. Test(Int32ToInt16(-1)) as Expect(-1 as i16);
  106. Test(Int32ToInt16(0)) as Expect(0 as i16);
  107. Test(Int32ToInt16(0x7FFF)) as Expect(0x7FFF as i16);
  108. Test(Int32ToInt16(0xFFFF)) as Expect(-1 as i16);
  109. Test(Int32ToInt16(0x7FFF_1234)) as Expect(0x1234 as i16);
  110. Test(Int32ToInt16(0x7FFF_EDCC)) as Expect(-0x1234 as i16);
  111. Test(Int32ToInt16(0x7FFF_FFFF)) as Expect(-1 as i16);
  112. Test(Int32ToUint16(-0x8000_0000)) as Expect(0 as u16);
  113. Test(Int32ToUint16(-0x7FFF_EDCC)) as Expect(0x1234 as u16);
  114. Test(Int32ToUint16(-0x7FFF_1234)) as Expect(0xEDCC as u16);
  115. Test(Int32ToUint16(-0x8000)) as Expect(0x8000 as u16);
  116. Test(Int32ToUint16(-1)) as Expect(0xFFFF as u16);
  117. Test(Int32ToUint16(0)) as Expect(0 as u16);
  118. Test(Int32ToUint16(0x7FFF)) as Expect(0x7FFF as u16);
  119. Test(Int32ToUint16(0xFFFF)) as Expect(0xFFFF as u16);
  120. Test(Int32ToUint16(0x7FFF_1234)) as Expect(0x1234 as u16);
  121. Test(Int32ToUint16(0x7FFF_EDCC)) as Expect(0xEDCC as u16);
  122. Test(Int32ToUint16(0x7FFF_FFFF)) as Expect(0xFFFF as u16);
  123. Test(Uint32ToInt16(0x8000_0000)) as Expect(0 as i16);
  124. Test(Uint32ToInt16(0xFFFF_1234)) as Expect(0x1234 as i16);
  125. Test(Uint32ToInt16(0xFFFF_EDCC)) as Expect(-0x1234 as i16);
  126. Test(Uint32ToInt16(0xFFFF_8000)) as Expect(-0x8000 as i16);
  127. Test(Uint32ToInt16(0xFFFF_FFFF)) as Expect(-1 as i16);
  128. Test(Uint32ToInt16(0)) as Expect(0 as i16);
  129. Test(Uint32ToInt16(0x7FFF)) as Expect(0x7FFF as i16);
  130. Test(Uint32ToInt16(0xFFFF)) as Expect(-1 as i16);
  131. Test(Uint32ToInt16(0x7FFF_1234)) as Expect(0x1234 as i16);
  132. Test(Uint32ToInt16(0x7FFF_EDCC)) as Expect(-0x1234 as i16);
  133. Test(Uint32ToInt16(0x7FFF_FFFF)) as Expect(-1 as i16);
  134. Test(Uint32ToUint16(0x8000_0000)) as Expect(0 as u16);
  135. Test(Uint32ToUint16(0xFFFF_1234)) as Expect(0x1234 as u16);
  136. Test(Uint32ToUint16(0xFFFF_EDCC)) as Expect(0xEDCC as u16);
  137. Test(Uint32ToUint16(0xFFFF_8000)) as Expect(0x8000 as u16);
  138. Test(Uint32ToUint16(0xFFFF_FFFF)) as Expect(0xFFFF as u16);
  139. Test(Uint32ToUint16(0)) as Expect(0 as u16);
  140. Test(Uint32ToUint16(0x7FFF)) as Expect(0x7FFF as u16);
  141. Test(Uint32ToUint16(0xFFFF)) as Expect(0xFFFF as u16);
  142. Test(Uint32ToUint16(0x7FFF_1234)) as Expect(0x1234 as u16);
  143. Test(Uint32ToUint16(0x7FFF_EDCC)) as Expect(0xEDCC as u16);
  144. Test(Uint32ToUint16(0x7FFF_FFFF)) as Expect(0xFFFF as u16);
  145. Test(IntLiteralToInt16(0)) as Expect(0 as i16);
  146. Test(IntLiteralToInt16(0x7FFF)) as Expect(0x7FFF as i16);
  147. Test(IntLiteralToInt16(0x8000)) as Expect(-0x8000 as i16);
  148. Test(IntLiteralToInt16(0xFFFF)) as Expect(-1 as i16);
  149. Test(IntLiteralToInt16(0x1_2345)) as Expect(0x2345 as i16);
  150. Test(IntLiteralToInt16(-1)) as Expect(-1 as i16);
  151. Test(IntLiteralToUint16(0)) as Expect(0 as u16);
  152. Test(IntLiteralToUint16(0x7FFF)) as Expect(0x7FFF as u16);
  153. Test(IntLiteralToUint16(0x8000)) as Expect(0x8000 as u16);
  154. Test(IntLiteralToUint16(0xFFFF)) as Expect(0xFFFF as u16);
  155. Test(IntLiteralToUint16(0x1_2345)) as Expect(0x2345 as u16);
  156. Test(IntLiteralToUint16(-1)) as Expect(0xFFFF as u16);
  157. }
  158. // --- zero_extend.carbon
  159. library "[[@TEST_NAME]]";
  160. import library "int_ops";
  161. fn F() {
  162. Test(Uint32ToInt64(0)) as Expect(0 as i64);
  163. Test(Uint32ToInt64(0x1234_5678)) as Expect(0x1234_5678 as i64);
  164. Test(Uint32ToInt64(0x7FFF_FFFF)) as Expect(0x7FFF_FFFF as i64);
  165. Test(Uint32ToInt64(0x8000_0000)) as Expect(0x8000_0000 as i64);
  166. Test(Uint32ToInt64(0xFFFF_FFFF)) as Expect(0xFFFF_FFFF as i64);
  167. Test(Uint32ToUint64(0)) as Expect(0 as u64);
  168. Test(Uint32ToUint64(0x1234_5678)) as Expect(0x1234_5678 as u64);
  169. Test(Uint32ToUint64(0x7FFF_FFFF)) as Expect(0x7FFF_FFFF as u64);
  170. Test(Uint32ToUint64(0x8000_0000)) as Expect(0x8000_0000 as u64);
  171. Test(Uint32ToUint64(0xFFFF_FFFF)) as Expect(0xFFFF_FFFF as u64);
  172. Test(Uint32ToIntLiteral(0x1234_5678)) as Expect(0x1234_5678);
  173. Test(Uint32ToIntLiteral(0x8765_4321)) as Expect(0x8765_4321);
  174. Test(Uint32ToIntLiteral(0xFFFF_FFFF)) as Expect(0xFFFF_FFFF);
  175. }
  176. // --- sign_extend.carbon
  177. library "[[@TEST_NAME]]";
  178. import library "int_ops";
  179. fn F() {
  180. Test(Int32ToInt64(0)) as Expect(0 as i64);
  181. Test(Int32ToInt64(0x1234_5678)) as Expect(0x1234_5678 as i64);
  182. Test(Int32ToInt64(0x7FFF_FFFF)) as Expect(0x7FFF_FFFF as i64);
  183. Test(Int32ToInt64(-1)) as Expect(-1 as i64);
  184. Test(Int32ToUint64(0)) as Expect(0 as u64);
  185. Test(Int32ToUint64(0x1234_5678)) as Expect(0x1234_5678 as u64);
  186. Test(Int32ToUint64(0x7FFF_FFFF)) as Expect(0x7FFF_FFFF as u64);
  187. Test(Int32ToUint64(-1)) as Expect(0xFFFF_FFFF_FFFF_FFFF as u64);
  188. Test(Int32ToUint64(-0x8000_0000)) as Expect(0xFFFF_FFFF_8000_0000 as u64);
  189. Test(Int32ToIntLiteral(0x1234_5678)) as Expect(0x1234_5678);
  190. Test(Int32ToIntLiteral(-0x1234_5678)) as Expect(-0x1234_5678);
  191. Test(Int32ToIntLiteral(-1)) as Expect(-1);
  192. }
  193. // --- fail_not_constant.carbon
  194. library "[[@TEST_NAME]]";
  195. import library "int_ops";
  196. let not_constant: Core.IntLiteral() = 0;
  197. // CHECK:STDERR: fail_not_constant.carbon:[[@LINE+8]]:33: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  198. // CHECK:STDERR: let convert_not_constant: i16 = IntLiteralToInt16(not_constant);
  199. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  200. // CHECK:STDERR: fail_not_constant.carbon:[[@LINE-7]]:1: in import [InImport]
  201. // CHECK:STDERR: int_ops.carbon:16:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  202. // CHECK:STDERR: fn IntLiteralToInt16(a: Core.IntLiteral()) -> i16 = "int.convert";
  203. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  204. // CHECK:STDERR:
  205. let convert_not_constant: i16 = IntLiteralToInt16(not_constant);
  206. // CHECK:STDOUT: --- runtime_call.carbon
  207. // CHECK:STDOUT:
  208. // CHECK:STDOUT: constants {
  209. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  210. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  211. // CHECK:STDOUT: %u32: type = class_type @UInt, @UInt(%int_32) [concrete]
  212. // CHECK:STDOUT: %Int32ToUint32.type: type = fn_type @Int32ToUint32 [concrete]
  213. // CHECK:STDOUT: %Int32ToUint32: %Int32ToUint32.type = struct_value () [concrete]
  214. // CHECK:STDOUT: %int_16: Core.IntLiteral = int_value 16 [concrete]
  215. // CHECK:STDOUT: %i16: type = class_type @Int, @Int(%int_16) [concrete]
  216. // CHECK:STDOUT: %Int32ToInt16.type: type = fn_type @Int32ToInt16 [concrete]
  217. // CHECK:STDOUT: %Int32ToInt16: %Int32ToInt16.type = struct_value () [concrete]
  218. // CHECK:STDOUT: %int_64: Core.IntLiteral = int_value 64 [concrete]
  219. // CHECK:STDOUT: %i64: type = class_type @Int, @Int(%int_64) [concrete]
  220. // CHECK:STDOUT: %Int32ToInt64.type: type = fn_type @Int32ToInt64 [concrete]
  221. // CHECK:STDOUT: %Int32ToInt64: %Int32ToInt64.type = struct_value () [concrete]
  222. // CHECK:STDOUT: }
  223. // CHECK:STDOUT:
  224. // CHECK:STDOUT: imports {
  225. // CHECK:STDOUT: %Main.Int32ToUint32: %Int32ToUint32.type = import_ref Main//int_ops, Int32ToUint32, loaded [concrete = constants.%Int32ToUint32]
  226. // CHECK:STDOUT: %Main.Int32ToInt16: %Int32ToInt16.type = import_ref Main//int_ops, Int32ToInt16, loaded [concrete = constants.%Int32ToInt16]
  227. // CHECK:STDOUT: %Main.Int32ToInt64: %Int32ToInt64.type = import_ref Main//int_ops, Int32ToInt64, loaded [concrete = constants.%Int32ToInt64]
  228. // CHECK:STDOUT: }
  229. // CHECK:STDOUT:
  230. // CHECK:STDOUT: fn @SizePreserving(%a.param: %i32) -> %u32 {
  231. // CHECK:STDOUT: !entry:
  232. // CHECK:STDOUT: %Int32ToUint32.ref: %Int32ToUint32.type = name_ref Int32ToUint32, imports.%Main.Int32ToUint32 [concrete = constants.%Int32ToUint32]
  233. // CHECK:STDOUT: %a.ref: %i32 = name_ref a, %a
  234. // CHECK:STDOUT: %Int32ToUint32.call: init %u32 = call %Int32ToUint32.ref(%a.ref)
  235. // CHECK:STDOUT: return %Int32ToUint32.call to %return
  236. // CHECK:STDOUT: }
  237. // CHECK:STDOUT:
  238. // CHECK:STDOUT: fn @Narrowing(%a.param: %i32) -> %i16 {
  239. // CHECK:STDOUT: !entry:
  240. // CHECK:STDOUT: %Int32ToInt16.ref: %Int32ToInt16.type = name_ref Int32ToInt16, imports.%Main.Int32ToInt16 [concrete = constants.%Int32ToInt16]
  241. // CHECK:STDOUT: %a.ref: %i32 = name_ref a, %a
  242. // CHECK:STDOUT: %Int32ToInt16.call: init %i16 = call %Int32ToInt16.ref(%a.ref)
  243. // CHECK:STDOUT: return %Int32ToInt16.call to %return
  244. // CHECK:STDOUT: }
  245. // CHECK:STDOUT:
  246. // CHECK:STDOUT: fn @Widening(%a.param: %i32) -> %i64 {
  247. // CHECK:STDOUT: !entry:
  248. // CHECK:STDOUT: %Int32ToInt64.ref: %Int32ToInt64.type = name_ref Int32ToInt64, imports.%Main.Int32ToInt64 [concrete = constants.%Int32ToInt64]
  249. // CHECK:STDOUT: %a.ref: %i32 = name_ref a, %a
  250. // CHECK:STDOUT: %Int32ToInt64.call: init %i64 = call %Int32ToInt64.ref(%a.ref)
  251. // CHECK:STDOUT: return %Int32ToInt64.call to %return
  252. // CHECK:STDOUT: }
  253. // CHECK:STDOUT: