convert.carbon 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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/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. // --- fail_self_test.carbon
  37. library "[[@TEST_NAME]]";
  38. import library "int_ops";
  39. fn F() {
  40. // Ensure our testing machinery works.
  41. // CHECK:STDERR: fail_self_test.carbon:[[@LINE+7]]:3: error: cannot convert from `Expect(0)` to `Expect(1)` with `as` [ExplicitAsConversionFailure]
  42. // CHECK:STDERR: Test(Int32ToInt32(0)) as Expect(1 as i32);
  43. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  44. // CHECK:STDERR: fail_self_test.carbon:[[@LINE+4]]:3: note: type `Expect(0)` does not implement interface `Core.As(Expect(1))` [MissingImplInMemberAccessNote]
  45. // CHECK:STDERR: Test(Int32ToInt32(0)) as Expect(1 as i32);
  46. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  47. // CHECK:STDERR:
  48. Test(Int32ToInt32(0)) as Expect(1 as i32);
  49. }
  50. // --- identity.carbon
  51. library "[[@TEST_NAME]]";
  52. import library "int_ops";
  53. fn F() {
  54. Test(Int32ToInt32(-0x8000_0000)) as Expect(-0x8000_0000 as i32);
  55. Test(Int32ToInt32(-1)) as Expect(-1 as i32);
  56. Test(Int32ToInt32(0)) as Expect(0 as i32);
  57. Test(Int32ToInt32(0x7FFF_FFFF)) as Expect(0x7FFF_FFFF as i32);
  58. Test(Uint32ToUint32(0)) as Expect(0 as u32);
  59. Test(Uint32ToUint32(0x7FFF_FFFF)) as Expect(0x7FFF_FFFF as u32);
  60. Test(Uint32ToUint32(0x8000_0000)) as Expect(0x8000_0000 as u32);
  61. Test(Uint32ToUint32(0xFFFF_FFFF)) as Expect(0xFFFF_FFFF as u32);
  62. Test(IntLiteralToIntLiteral(0x1_0000_0000_0000_0000)) as
  63. Expect(0x1_0000_0000_0000_0000);
  64. Test(IntLiteralToIntLiteral(-1)) as Expect(-1);
  65. }
  66. // --- same_size.carbon
  67. library "[[@TEST_NAME]]";
  68. import library "int_ops";
  69. fn F() {
  70. Test(Int32ToUint32(-0x8000_0000)) as Expect(0x8000_0000 as u32);
  71. Test(Int32ToUint32(-1)) as Expect(0xFFFF_FFFF as u32);
  72. Test(Int32ToUint32(0)) as Expect(0 as u32);
  73. Test(Int32ToUint32(0x7FFF_FFFF)) as Expect(0x7FFF_FFFF as u32);
  74. Test(Uint32ToInt32(0)) as Expect(0 as i32);
  75. Test(Uint32ToInt32(0x7FFF_FFFF)) as Expect(0x7FFF_FFFF as i32);
  76. Test(Uint32ToInt32(0x8000_0000)) as Expect(-0x8000_0000 as i32);
  77. Test(Uint32ToInt32(0xFFFF_FFFF)) as Expect(-1 as i32);
  78. }
  79. // --- truncate.carbon
  80. library "[[@TEST_NAME]]";
  81. import library "int_ops";
  82. fn F() {
  83. Test(Int32ToInt16(-0x8000_0000)) as Expect(0 as i16);
  84. Test(Int32ToInt16(-0x7FFF_EDCC)) as Expect(0x1234 as i16);
  85. Test(Int32ToInt16(-0x7FFF_1234)) as Expect(-0x1234 as i16);
  86. Test(Int32ToInt16(-0x8000)) as Expect(-0x8000 as i16);
  87. Test(Int32ToInt16(-1)) as Expect(-1 as i16);
  88. Test(Int32ToInt16(0)) as Expect(0 as i16);
  89. Test(Int32ToInt16(0x7FFF)) as Expect(0x7FFF as i16);
  90. Test(Int32ToInt16(0xFFFF)) as Expect(-1 as i16);
  91. Test(Int32ToInt16(0x7FFF_1234)) as Expect(0x1234 as i16);
  92. Test(Int32ToInt16(0x7FFF_EDCC)) as Expect(-0x1234 as i16);
  93. Test(Int32ToInt16(0x7FFF_FFFF)) as Expect(-1 as i16);
  94. Test(Int32ToUint16(-0x8000_0000)) as Expect(0 as u16);
  95. Test(Int32ToUint16(-0x7FFF_EDCC)) as Expect(0x1234 as u16);
  96. Test(Int32ToUint16(-0x7FFF_1234)) as Expect(0xEDCC as u16);
  97. Test(Int32ToUint16(-0x8000)) as Expect(0x8000 as u16);
  98. Test(Int32ToUint16(-1)) as Expect(0xFFFF as u16);
  99. Test(Int32ToUint16(0)) as Expect(0 as u16);
  100. Test(Int32ToUint16(0x7FFF)) as Expect(0x7FFF as u16);
  101. Test(Int32ToUint16(0xFFFF)) as Expect(0xFFFF as u16);
  102. Test(Int32ToUint16(0x7FFF_1234)) as Expect(0x1234 as u16);
  103. Test(Int32ToUint16(0x7FFF_EDCC)) as Expect(0xEDCC as u16);
  104. Test(Int32ToUint16(0x7FFF_FFFF)) as Expect(0xFFFF as u16);
  105. Test(Uint32ToInt16(0x8000_0000)) as Expect(0 as i16);
  106. Test(Uint32ToInt16(0xFFFF_1234)) as Expect(0x1234 as i16);
  107. Test(Uint32ToInt16(0xFFFF_EDCC)) as Expect(-0x1234 as i16);
  108. Test(Uint32ToInt16(0xFFFF_8000)) as Expect(-0x8000 as i16);
  109. Test(Uint32ToInt16(0xFFFF_FFFF)) as Expect(-1 as i16);
  110. Test(Uint32ToInt16(0)) as Expect(0 as i16);
  111. Test(Uint32ToInt16(0x7FFF)) as Expect(0x7FFF as i16);
  112. Test(Uint32ToInt16(0xFFFF)) as Expect(-1 as i16);
  113. Test(Uint32ToInt16(0x7FFF_1234)) as Expect(0x1234 as i16);
  114. Test(Uint32ToInt16(0x7FFF_EDCC)) as Expect(-0x1234 as i16);
  115. Test(Uint32ToInt16(0x7FFF_FFFF)) as Expect(-1 as i16);
  116. Test(Uint32ToUint16(0x8000_0000)) as Expect(0 as u16);
  117. Test(Uint32ToUint16(0xFFFF_1234)) as Expect(0x1234 as u16);
  118. Test(Uint32ToUint16(0xFFFF_EDCC)) as Expect(0xEDCC as u16);
  119. Test(Uint32ToUint16(0xFFFF_8000)) as Expect(0x8000 as u16);
  120. Test(Uint32ToUint16(0xFFFF_FFFF)) as Expect(0xFFFF as u16);
  121. Test(Uint32ToUint16(0)) as Expect(0 as u16);
  122. Test(Uint32ToUint16(0x7FFF)) as Expect(0x7FFF as u16);
  123. Test(Uint32ToUint16(0xFFFF)) as Expect(0xFFFF as u16);
  124. Test(Uint32ToUint16(0x7FFF_1234)) as Expect(0x1234 as u16);
  125. Test(Uint32ToUint16(0x7FFF_EDCC)) as Expect(0xEDCC as u16);
  126. Test(Uint32ToUint16(0x7FFF_FFFF)) as Expect(0xFFFF as u16);
  127. Test(IntLiteralToInt16(0)) as Expect(0 as i16);
  128. Test(IntLiteralToInt16(0x7FFF)) as Expect(0x7FFF as i16);
  129. Test(IntLiteralToInt16(0x8000)) as Expect(-0x8000 as i16);
  130. Test(IntLiteralToInt16(0xFFFF)) as Expect(-1 as i16);
  131. Test(IntLiteralToInt16(0x1_2345)) as Expect(0x2345 as i16);
  132. Test(IntLiteralToInt16(-1)) as Expect(-1 as i16);
  133. Test(IntLiteralToUint16(0)) as Expect(0 as u16);
  134. Test(IntLiteralToUint16(0x7FFF)) as Expect(0x7FFF as u16);
  135. Test(IntLiteralToUint16(0x8000)) as Expect(0x8000 as u16);
  136. Test(IntLiteralToUint16(0xFFFF)) as Expect(0xFFFF as u16);
  137. Test(IntLiteralToUint16(0x1_2345)) as Expect(0x2345 as u16);
  138. Test(IntLiteralToUint16(-1)) as Expect(0xFFFF as u16);
  139. }
  140. // --- zero_extend.carbon
  141. library "[[@TEST_NAME]]";
  142. import library "int_ops";
  143. fn F() {
  144. Test(Uint32ToInt64(0)) as Expect(0 as i64);
  145. Test(Uint32ToInt64(0x1234_5678)) as Expect(0x1234_5678 as i64);
  146. Test(Uint32ToInt64(0x7FFF_FFFF)) as Expect(0x7FFF_FFFF as i64);
  147. Test(Uint32ToInt64(0x8000_0000)) as Expect(0x8000_0000 as i64);
  148. Test(Uint32ToInt64(0xFFFF_FFFF)) as Expect(0xFFFF_FFFF as i64);
  149. Test(Uint32ToUint64(0)) as Expect(0 as u64);
  150. Test(Uint32ToUint64(0x1234_5678)) as Expect(0x1234_5678 as u64);
  151. Test(Uint32ToUint64(0x7FFF_FFFF)) as Expect(0x7FFF_FFFF as u64);
  152. Test(Uint32ToUint64(0x8000_0000)) as Expect(0x8000_0000 as u64);
  153. Test(Uint32ToUint64(0xFFFF_FFFF)) as Expect(0xFFFF_FFFF as u64);
  154. Test(Uint32ToIntLiteral(0x1234_5678)) as Expect(0x1234_5678);
  155. Test(Uint32ToIntLiteral(0x8765_4321)) as Expect(0x8765_4321);
  156. Test(Uint32ToIntLiteral(0xFFFF_FFFF)) as Expect(0xFFFF_FFFF);
  157. }
  158. // --- sign_extend.carbon
  159. library "[[@TEST_NAME]]";
  160. import library "int_ops";
  161. fn F() {
  162. Test(Int32ToInt64(0)) as Expect(0 as i64);
  163. Test(Int32ToInt64(0x1234_5678)) as Expect(0x1234_5678 as i64);
  164. Test(Int32ToInt64(0x7FFF_FFFF)) as Expect(0x7FFF_FFFF as i64);
  165. Test(Int32ToInt64(-1)) as Expect(-1 as i64);
  166. Test(Int32ToUint64(0)) as Expect(0 as u64);
  167. Test(Int32ToUint64(0x1234_5678)) as Expect(0x1234_5678 as u64);
  168. Test(Int32ToUint64(0x7FFF_FFFF)) as Expect(0x7FFF_FFFF as u64);
  169. Test(Int32ToUint64(-1)) as Expect(0xFFFF_FFFF_FFFF_FFFF as u64);
  170. Test(Int32ToUint64(-0x8000_0000)) as Expect(0xFFFF_FFFF_8000_0000 as u64);
  171. Test(Int32ToIntLiteral(0x1234_5678)) as Expect(0x1234_5678);
  172. Test(Int32ToIntLiteral(-0x1234_5678)) as Expect(-0x1234_5678);
  173. Test(Int32ToIntLiteral(-1)) as Expect(-1);
  174. }
  175. // --- fail_not_constant.carbon
  176. library "[[@TEST_NAME]]";
  177. import library "int_ops";
  178. let not_constant: Core.IntLiteral() = 0;
  179. // CHECK:STDERR: fail_not_constant.carbon:[[@LINE+8]]:33: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  180. // CHECK:STDERR: let convert_not_constant: i16 = IntLiteralToInt16(not_constant);
  181. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  182. // CHECK:STDERR: fail_not_constant.carbon:[[@LINE-7]]:1: in import [InImport]
  183. // CHECK:STDERR: int_ops.carbon:16:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  184. // CHECK:STDERR: fn IntLiteralToInt16(a: Core.IntLiteral()) -> i16 = "int.convert";
  185. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  186. // CHECK:STDERR:
  187. let convert_not_constant: i16 = IntLiteralToInt16(not_constant);