convert_checked.carbon 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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_checked.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_checked.carbon
  12. // --- int_ops.carbon
  13. library "[[@TEST_NAME]]";
  14. fn NegateI32(a: i32) -> i32 = "int.snegate";
  15. fn SubI32(a: i32, b: i32) -> i32 = "int.ssub";
  16. fn AddU32(a: u32, b: u32) -> u32 = "int.uadd";
  17. fn IntLiteral() -> type = "int_literal.make_type";
  18. // Size preserving
  19. fn Int32ToInt32(a: i32) -> i32 = "int.convert_checked";
  20. fn Int32ToUint32(a: i32) -> u32 = "int.convert_checked";
  21. fn Uint32ToInt32(a: u32) -> i32 = "int.convert_checked";
  22. fn Uint32ToUint32(a: u32) -> u32 = "int.convert_checked";
  23. fn IntLiteralToIntLiteral(a: IntLiteral()) -> IntLiteral() =
  24. "int.convert_checked";
  25. // Narrowing
  26. fn Int32ToInt16(a: i32) -> i16 = "int.convert_checked";
  27. fn Int32ToUint16(a: i32) -> u16 = "int.convert_checked";
  28. fn Uint32ToInt16(a: u32) -> i16 = "int.convert_checked";
  29. fn Uint32ToUint16(a: u32) -> u16 = "int.convert_checked";
  30. fn IntLiteralToInt16(a: IntLiteral()) -> i16 = "int.convert_checked";
  31. fn IntLiteralToUint16(a: IntLiteral()) -> u16 = "int.convert_checked";
  32. // Widening
  33. fn Int32ToInt64(a: i32) -> i64 = "int.convert_checked";
  34. fn Int32ToUint64(a: i32) -> u64 = "int.convert_checked";
  35. fn Uint32ToInt64(a: u32) -> i64 = "int.convert_checked";
  36. fn Uint32ToUint64(a: u32) -> u64 = "int.convert_checked";
  37. fn Int32ToIntLiteral(a: i32) -> IntLiteral() = "int.convert_checked";
  38. fn Uint32ToUintLiteral(a: u32) -> IntLiteral() = "int.convert_checked";
  39. // --- identity.carbon
  40. library "[[@TEST_NAME]]";
  41. import library "int_ops";
  42. let a: i32 = Int32ToInt32(0);
  43. let b: i32 = Int32ToInt32(0x7FFF_FFFF);
  44. let c: i32 = Int32ToInt32(SubI32(NegateI32(0x7FFF_FFFF), 1));
  45. let d: IntLiteral() = IntLiteralToIntLiteral(Int32ToIntLiteral(NegateI32(1)));
  46. // --- same_size.carbon
  47. library "[[@TEST_NAME]]";
  48. import library "int_ops";
  49. let max: u32 = Int32ToUint32(0x7FFF_FFFF);
  50. let max_roundtrip: i32 = Uint32ToInt32(Int32ToUint32(0x7FFF_FFFF));
  51. // --- truncate.carbon
  52. library "[[@TEST_NAME]]";
  53. import library "int_ops";
  54. let a: u16 = Int32ToUint16(0);
  55. let b: u16 = Int32ToUint16(0xFFFF);
  56. let c: i16 = Int32ToInt16(0x7FFF);
  57. let d: i16 = Int32ToInt16(NegateI32(0x8000));
  58. let e: u16 = Uint32ToUint16(Int32ToUint32(0));
  59. let f: u16 = Uint32ToUint16(Int32ToUint32(0xFFFF));
  60. let g: i16 = Uint32ToInt16(Int32ToUint32(0));
  61. let h: i16 = Uint32ToInt16(Int32ToUint32(0x7FFF));
  62. let lit_i16_min: i16 = IntLiteralToInt16(Int32ToIntLiteral(NegateI32(0x8000)));
  63. let lit_i16_max: i16 = IntLiteralToInt16(Int32ToIntLiteral(0x7FFF));
  64. let lit_u16_min: u16 = IntLiteralToUint16(Int32ToIntLiteral(0));
  65. let lit_u16_max: u16 = IntLiteralToUint16(Int32ToIntLiteral(0xFFFF));
  66. // --- zero_extend.carbon
  67. library "[[@TEST_NAME]]";
  68. import library "int_ops";
  69. let a: u64 = Uint32ToUint64(Int32ToUint32(0));
  70. let b: u64 = Uint32ToUint64(
  71. AddU32(
  72. AddU32(Int32ToUint32(0x7FFF_FFFF), Int32ToUint32(0x7FFF_FFFF)),
  73. Int32ToUint32(1)));
  74. let c: i64 = Uint32ToInt64(Int32ToUint32(0));
  75. let d: i64 = Uint32ToInt64(
  76. AddU32(
  77. AddU32(Int32ToUint32(0x7FFF_FFFF), Int32ToUint32(0x7FFF_FFFF)),
  78. Int32ToUint32(1)));
  79. // --- sign_extend.carbon
  80. library "[[@TEST_NAME]]";
  81. import library "int_ops";
  82. let a: u64 = Int32ToUint64(0);
  83. let b: u64 = Int32ToUint64(0x7FFF_FFFF);
  84. let c: i64 = Int32ToInt64(SubI32(NegateI32(0x7FFF_FFFF), 1));
  85. let d: i64 = Int32ToInt64(0x7FFF_FFFF);
  86. // --- fail_too_large_u32_for_i32.carbon
  87. library "[[@TEST_NAME]]";
  88. import library "int_ops";
  89. let max_plus_one: i32 =
  90. // CHECK:STDERR: fail_too_large_u32_for_i32.carbon:[[@LINE+4]]:3: error: integer value 2147483648 too large for type `i32` [IntTooLargeForType]
  91. // CHECK:STDERR: Uint32ToInt32(
  92. // CHECK:STDERR: ^~~~~~~~~~~~~~
  93. // CHECK:STDERR:
  94. Uint32ToInt32(
  95. AddU32(Int32ToUint32(0x7FFF_FFFF),
  96. Int32ToUint32(1)));
  97. // --- fail_too_large_i32_for_i16.carbon
  98. library "[[@TEST_NAME]]";
  99. import library "int_ops";
  100. // CHECK:STDERR: fail_too_large_i32_for_i16.carbon:[[@LINE+4]]:25: error: integer value 32768 too large for type `i16` [IntTooLargeForType]
  101. // CHECK:STDERR: let max_plus_one: i16 = Int32ToInt16(0x8000);
  102. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~
  103. // CHECK:STDERR:
  104. let max_plus_one: i16 = Int32ToInt16(0x8000);
  105. // --- fail_too_large_i32_for_u16.carbon
  106. library "[[@TEST_NAME]]";
  107. import library "int_ops";
  108. // CHECK:STDERR: fail_too_large_i32_for_u16.carbon:[[@LINE+4]]:25: error: integer value 65536 too large for type `u16` [IntTooLargeForType]
  109. // CHECK:STDERR: let max_plus_one: u16 = Int32ToUint16(0x1_0000);
  110. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~
  111. // CHECK:STDERR:
  112. let max_plus_one: u16 = Int32ToUint16(0x1_0000);
  113. // --- fail_too_large_u32_for_i16.carbon
  114. library "[[@TEST_NAME]]";
  115. import library "int_ops";
  116. // CHECK:STDERR: fail_too_large_u32_for_i16.carbon:[[@LINE+4]]:25: error: integer value 32768 too large for type `i16` [IntTooLargeForType]
  117. // CHECK:STDERR: let max_plus_one: i16 = Uint32ToInt16(Int32ToUint32(0x8000));
  118. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  119. // CHECK:STDERR:
  120. let max_plus_one: i16 = Uint32ToInt16(Int32ToUint32(0x8000));
  121. // --- fail_too_large_u32_for_u16.carbon
  122. library "[[@TEST_NAME]]";
  123. import library "int_ops";
  124. // CHECK:STDERR: fail_too_large_u32_for_u16.carbon:[[@LINE+4]]:25: error: integer value 65536 too large for type `u16` [IntTooLargeForType]
  125. // CHECK:STDERR: let max_plus_one: u16 = Uint32ToUint16(Int32ToUint32(0x1_0000));
  126. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  127. // CHECK:STDERR:
  128. let max_plus_one: u16 = Uint32ToUint16(Int32ToUint32(0x1_0000));
  129. // --- fail_negative_i32_to_u16.carbon
  130. library "[[@TEST_NAME]]";
  131. import library "int_ops";
  132. // CHECK:STDERR: fail_negative_i32_to_u16.carbon:[[@LINE+4]]:29: error: negative integer value -1 converted to unsigned type `u16` [NegativeIntInUnsignedType]
  133. // CHECK:STDERR: let minus_one_to_u16: u16 = Int32ToUint16(SubI32(0, 1));
  134. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~
  135. // CHECK:STDERR:
  136. let minus_one_to_u16: u16 = Int32ToUint16(SubI32(0, 1));
  137. // --- fail_negative_i32_to_u32.carbon
  138. library "[[@TEST_NAME]]";
  139. import library "int_ops";
  140. // CHECK:STDERR: fail_negative_i32_to_u32.carbon:[[@LINE+4]]:29: error: negative integer value -1 converted to unsigned type `u32` [NegativeIntInUnsignedType]
  141. // CHECK:STDERR: let minus_one_to_u32: u32 = Int32ToUint32(SubI32(0, 1));
  142. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~
  143. // CHECK:STDERR:
  144. let minus_one_to_u32: u32 = Int32ToUint32(SubI32(0, 1));
  145. // --- fail_negative_i32_to_u64.carbon
  146. library "[[@TEST_NAME]]";
  147. import library "int_ops";
  148. // CHECK:STDERR: fail_negative_i32_to_u64.carbon:[[@LINE+4]]:29: error: negative integer value -1 converted to unsigned type `u64` [NegativeIntInUnsignedType]
  149. // CHECK:STDERR: let minus_one_to_u64: u64 = Int32ToUint64(SubI32(0, 1));
  150. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~
  151. // CHECK:STDERR:
  152. let minus_one_to_u64: u64 = Int32ToUint64(SubI32(0, 1));
  153. // --- fail_too_small_i32_for_i16.carbon
  154. library "[[@TEST_NAME]]";
  155. import library "int_ops";
  156. // CHECK:STDERR: fail_too_small_i32_for_i16.carbon:[[@LINE+4]]:26: error: integer value -32769 too large for type `i16` [IntTooLargeForType]
  157. // CHECK:STDERR: let min_minus_one: i16 = Int32ToInt16(NegateI32(0x8001));
  158. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  159. // CHECK:STDERR:
  160. let min_minus_one: i16 = Int32ToInt16(NegateI32(0x8001));
  161. // --- fail_not_constant.carbon
  162. library "[[@TEST_NAME]]";
  163. import library "int_ops";
  164. let not_constant: i32 = 0;
  165. // CHECK:STDERR: fail_not_constant.carbon:[[@LINE+8]]:40: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  166. // CHECK:STDERR: let convert_not_constant_narrow: i16 = Int32ToInt16(not_constant);
  167. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~
  168. // CHECK:STDERR: fail_not_constant.carbon:[[@LINE-7]]:1: in import [InImport]
  169. // CHECK:STDERR: int_ops.carbon:18:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  170. // CHECK:STDERR: fn Int32ToInt16(a: i32) -> i16 = "int.convert_checked";
  171. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  172. // CHECK:STDERR:
  173. let convert_not_constant_narrow: i16 = Int32ToInt16(not_constant);
  174. // CHECK:STDERR: fail_not_constant.carbon:[[@LINE+8]]:38: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  175. // CHECK:STDERR: let convert_not_constant_same: i32 = Int32ToInt32(not_constant);
  176. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~
  177. // CHECK:STDERR: fail_not_constant.carbon:[[@LINE-17]]:1: in import [InImport]
  178. // CHECK:STDERR: int_ops.carbon:10:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  179. // CHECK:STDERR: fn Int32ToInt32(a: i32) -> i32 = "int.convert_checked";
  180. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  181. // CHECK:STDERR:
  182. let convert_not_constant_same: i32 = Int32ToInt32(not_constant);
  183. // CHECK:STDERR: fail_not_constant.carbon:[[@LINE+8]]:39: error: non-constant call to compile-time-only function [NonConstantCallToCompTimeOnlyFunction]
  184. // CHECK:STDERR: let convert_not_constant_widen: i64 = Int32ToInt64(not_constant);
  185. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~
  186. // CHECK:STDERR: fail_not_constant.carbon:[[@LINE-27]]:1: in import [InImport]
  187. // CHECK:STDERR: int_ops.carbon:26:1: note: compile-time-only function declared here [CompTimeOnlyFunctionHere]
  188. // CHECK:STDERR: fn Int32ToInt64(a: i32) -> i64 = "int.convert_checked";
  189. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  190. // CHECK:STDERR:
  191. let convert_not_constant_widen: i64 = Int32ToInt64(not_constant);