convert.carbon 13 KB

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