bound_values.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/array/bound_values.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/array/bound_values.carbon
  10. // --- addition.carbon
  11. library "[[@TEST_NAME]]";
  12. var a:
  13. //@dump-sem-ir-begin
  14. array(i32, 1 + 2)
  15. //@dump-sem-ir-end
  16. = (1, 2, 3);
  17. let b: array(i32, 3)* = &a;
  18. // --- unsigned.carbon
  19. library "[[@TEST_NAME]]";
  20. var a:
  21. //@dump-sem-ir-begin
  22. array(i32, 3 as u32)
  23. //@dump-sem-ir-end
  24. = (1, 2, 3);
  25. let b: array(i32, 3)* = &a;
  26. // --- fail_negative.carbon
  27. library "[[@TEST_NAME]]";
  28. // CHECK:STDERR: fail_negative.carbon:[[@LINE+4]]:19: error: array bound of -1 is negative [ArrayBoundNegative]
  29. // CHECK:STDERR: var a: array(i32, -1);
  30. // CHECK:STDERR: ^~
  31. // CHECK:STDERR:
  32. var a: array(i32, -1);
  33. // --- fail_overflow.carbon
  34. library "[[@TEST_NAME]]";
  35. // CHECK:STDERR: fail_overflow.carbon:[[@LINE+4]]:19: error: array bound of 39999999999999999993 is too large [ArrayBoundTooLarge]
  36. // CHECK:STDERR: var a: array(i32, 39999999999999999993);
  37. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~
  38. // CHECK:STDERR:
  39. var a: array(i32, 39999999999999999993);
  40. // --- fail_invalid_type_with_overflow.carbon
  41. library "[[@TEST_NAME]]";
  42. // CHECK:STDERR: fail_invalid_type_with_overflow.carbon:[[@LINE+7]]:14: error: cannot implicitly convert non-type value of type `Core.IntLiteral` to `type` [ConversionFailureNonTypeToFacet]
  43. // CHECK:STDERR: var b: array(1, 39999999999999999993);
  44. // CHECK:STDERR: ^
  45. // CHECK:STDERR: fail_invalid_type_with_overflow.carbon:[[@LINE+4]]:14: note: type `Core.IntLiteral` does not implement interface `Core.ImplicitAs(type)` [MissingImplInMemberAccessNote]
  46. // CHECK:STDERR: var b: array(1, 39999999999999999993);
  47. // CHECK:STDERR: ^
  48. // CHECK:STDERR:
  49. var b: array(1, 39999999999999999993);
  50. // CHECK:STDOUT: --- addition.carbon
  51. // CHECK:STDOUT:
  52. // CHECK:STDOUT: constants {
  53. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  54. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  55. // CHECK:STDOUT: %int_1.5b8: Core.IntLiteral = int_value 1 [concrete]
  56. // CHECK:STDOUT: %int_2.ecc: Core.IntLiteral = int_value 2 [concrete]
  57. // CHECK:STDOUT: %Add.type: type = facet_type <@Add> [concrete]
  58. // CHECK:STDOUT: %Op.type.545: type = fn_type @Op.1 [concrete]
  59. // CHECK:STDOUT: %Add.impl_witness.b91: <witness> = impl_witness imports.%Add.impl_witness_table.5a4 [concrete]
  60. // CHECK:STDOUT: %Add.facet: %Add.type = facet_value Core.IntLiteral, (%Add.impl_witness.b91) [concrete]
  61. // CHECK:STDOUT: %.066: type = fn_type_with_self_type %Op.type.545, %Add.facet [concrete]
  62. // CHECK:STDOUT: %Op.type.f28: type = fn_type @Op.2 [concrete]
  63. // CHECK:STDOUT: %Op.d0f: %Op.type.f28 = struct_value () [concrete]
  64. // CHECK:STDOUT: %Op.bound: <bound method> = bound_method %int_1.5b8, %Op.d0f [concrete]
  65. // CHECK:STDOUT: %int_3.1ba: Core.IntLiteral = int_value 3 [concrete]
  66. // CHECK:STDOUT: %array_type: type = array_type %int_3.1ba, %i32 [concrete]
  67. // CHECK:STDOUT: }
  68. // CHECK:STDOUT:
  69. // CHECK:STDOUT: imports {
  70. // CHECK:STDOUT: %Core.import_ref.3e4: %Op.type.f28 = import_ref Core//prelude/operators/arithmetic, loc78_42, loaded [concrete = constants.%Op.d0f]
  71. // CHECK:STDOUT: %Add.impl_witness_table.5a4 = impl_witness_table (%Core.import_ref.3e4), @impl.9e0 [concrete]
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT:
  74. // CHECK:STDOUT: file {
  75. // CHECK:STDOUT: %.loc6_21: type = splice_block %array_type.loc6 [concrete = constants.%array_type] {
  76. // CHECK:STDOUT: %int_32.loc6: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  77. // CHECK:STDOUT: %i32.loc6: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  78. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete = constants.%int_1.5b8]
  79. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [concrete = constants.%int_2.ecc]
  80. // CHECK:STDOUT: %impl.elem0: %.066 = impl_witness_access constants.%Add.impl_witness.b91, element0 [concrete = constants.%Op.d0f]
  81. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_1, %impl.elem0 [concrete = constants.%Op.bound]
  82. // CHECK:STDOUT: %int.sadd: init Core.IntLiteral = call %bound_method(%int_1, %int_2) [concrete = constants.%int_3.1ba]
  83. // CHECK:STDOUT: %.loc6_18.1: Core.IntLiteral = value_of_initializer %int.sadd [concrete = constants.%int_3.1ba]
  84. // CHECK:STDOUT: %.loc6_18.2: Core.IntLiteral = converted %int.sadd, %.loc6_18.1 [concrete = constants.%int_3.1ba]
  85. // CHECK:STDOUT: %array_type.loc6: type = array_type %.loc6_18.2, %i32.loc6 [concrete = constants.%array_type]
  86. // CHECK:STDOUT: }
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: fn @__global_init() {
  90. // CHECK:STDOUT: !entry:
  91. // CHECK:STDOUT: <elided>
  92. // CHECK:STDOUT: }
  93. // CHECK:STDOUT:
  94. // CHECK:STDOUT: --- unsigned.carbon
  95. // CHECK:STDOUT:
  96. // CHECK:STDOUT: constants {
  97. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  98. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  99. // CHECK:STDOUT: %int_3.1ba: Core.IntLiteral = int_value 3 [concrete]
  100. // CHECK:STDOUT: %u32: type = class_type @UInt, @UInt(%int_32) [concrete]
  101. // CHECK:STDOUT: %As.type.ce0: type = facet_type <@As, @As(%u32)> [concrete]
  102. // CHECK:STDOUT: %Convert.type.b94: type = fn_type @Convert.1, @As(%u32) [concrete]
  103. // CHECK:STDOUT: %To.c80: Core.IntLiteral = bind_symbolic_name To, 0 [symbolic]
  104. // CHECK:STDOUT: %ImplicitAs.type.2fd: type = facet_type <@ImplicitAs, @ImplicitAs(Core.IntLiteral)> [concrete]
  105. // CHECK:STDOUT: %From: Core.IntLiteral = bind_symbolic_name From, 0 [symbolic]
  106. // CHECK:STDOUT: %Convert.type.002: type = fn_type @Convert.4, @impl.750(%From) [symbolic]
  107. // CHECK:STDOUT: %Convert.819: %Convert.type.002 = struct_value () [symbolic]
  108. // CHECK:STDOUT: %Convert.type.71e: type = fn_type @Convert.2, @ImplicitAs(Core.IntLiteral) [concrete]
  109. // CHECK:STDOUT: %Convert.type.792: type = fn_type @Convert.5, @impl.2f2(%To.c80) [symbolic]
  110. // CHECK:STDOUT: %Convert.c02: %Convert.type.792 = struct_value () [symbolic]
  111. // CHECK:STDOUT: %As.impl_witness.d2d: <witness> = impl_witness imports.%As.impl_witness_table.afe, @impl.2f2(%int_32) [concrete]
  112. // CHECK:STDOUT: %Convert.type.70b: type = fn_type @Convert.5, @impl.2f2(%int_32) [concrete]
  113. // CHECK:STDOUT: %Convert.92a: %Convert.type.70b = struct_value () [concrete]
  114. // CHECK:STDOUT: %As.facet: %As.type.ce0 = facet_value Core.IntLiteral, (%As.impl_witness.d2d) [concrete]
  115. // CHECK:STDOUT: %.68c: type = fn_type_with_self_type %Convert.type.b94, %As.facet [concrete]
  116. // CHECK:STDOUT: %Convert.bound.7d9: <bound method> = bound_method %int_3.1ba, %Convert.92a [concrete]
  117. // CHECK:STDOUT: %Convert.specific_fn.4cf: <specific function> = specific_function %Convert.92a, @Convert.5(%int_32) [concrete]
  118. // CHECK:STDOUT: %bound_method.94d: <bound method> = bound_method %int_3.1ba, %Convert.specific_fn.4cf [concrete]
  119. // CHECK:STDOUT: %int_3.d14: %u32 = int_value 3 [concrete]
  120. // CHECK:STDOUT: %ImplicitAs.impl_witness.a11: <witness> = impl_witness imports.%ImplicitAs.impl_witness_table.6ad, @impl.750(%int_32) [concrete]
  121. // CHECK:STDOUT: %Convert.type.e06: type = fn_type @Convert.4, @impl.750(%int_32) [concrete]
  122. // CHECK:STDOUT: %Convert.47f: %Convert.type.e06 = struct_value () [concrete]
  123. // CHECK:STDOUT: %ImplicitAs.facet.84e: %ImplicitAs.type.2fd = facet_value %u32, (%ImplicitAs.impl_witness.a11) [concrete]
  124. // CHECK:STDOUT: %.268: type = fn_type_with_self_type %Convert.type.71e, %ImplicitAs.facet.84e [concrete]
  125. // CHECK:STDOUT: %Convert.bound.258: <bound method> = bound_method %int_3.d14, %Convert.47f [concrete]
  126. // CHECK:STDOUT: %Convert.specific_fn.2eb: <specific function> = specific_function %Convert.47f, @Convert.4(%int_32) [concrete]
  127. // CHECK:STDOUT: %bound_method.3a6: <bound method> = bound_method %int_3.d14, %Convert.specific_fn.2eb [concrete]
  128. // CHECK:STDOUT: %array_type: type = array_type %int_3.1ba, %i32 [concrete]
  129. // CHECK:STDOUT: }
  130. // CHECK:STDOUT:
  131. // CHECK:STDOUT: imports {
  132. // CHECK:STDOUT: %Core.import_ref.823: @impl.750.%Convert.type (%Convert.type.002) = import_ref Core//prelude/types/uint, loc24_44, loaded [symbolic = @impl.750.%Convert (constants.%Convert.819)]
  133. // CHECK:STDOUT: %ImplicitAs.impl_witness_table.6ad = impl_witness_table (%Core.import_ref.823), @impl.750 [concrete]
  134. // CHECK:STDOUT: %Core.import_ref.aef: @impl.2f2.%Convert.type (%Convert.type.792) = import_ref Core//prelude/types/uint, loc29_40, loaded [symbolic = @impl.2f2.%Convert (constants.%Convert.c02)]
  135. // CHECK:STDOUT: %As.impl_witness_table.afe = impl_witness_table (%Core.import_ref.aef), @impl.2f2 [concrete]
  136. // CHECK:STDOUT: }
  137. // CHECK:STDOUT:
  138. // CHECK:STDOUT: file {
  139. // CHECK:STDOUT: %.loc6_24: type = splice_block %array_type.loc6 [concrete = constants.%array_type] {
  140. // CHECK:STDOUT: %int_32.loc6_11: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  141. // CHECK:STDOUT: %i32.loc6: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  142. // CHECK:STDOUT: %int_3.loc6: Core.IntLiteral = int_value 3 [concrete = constants.%int_3.1ba]
  143. // CHECK:STDOUT: %int_32.loc6_21: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  144. // CHECK:STDOUT: %u32: type = class_type @UInt, @UInt(constants.%int_32) [concrete = constants.%u32]
  145. // CHECK:STDOUT: %impl.elem0.loc6_18.1: %.68c = impl_witness_access constants.%As.impl_witness.d2d, element0 [concrete = constants.%Convert.92a]
  146. // CHECK:STDOUT: %bound_method.loc6_18.1: <bound method> = bound_method %int_3.loc6, %impl.elem0.loc6_18.1 [concrete = constants.%Convert.bound.7d9]
  147. // CHECK:STDOUT: %specific_fn.loc6_18.1: <specific function> = specific_function %impl.elem0.loc6_18.1, @Convert.5(constants.%int_32) [concrete = constants.%Convert.specific_fn.4cf]
  148. // CHECK:STDOUT: %bound_method.loc6_18.2: <bound method> = bound_method %int_3.loc6, %specific_fn.loc6_18.1 [concrete = constants.%bound_method.94d]
  149. // CHECK:STDOUT: %int.convert_checked.loc6_18.1: init %u32 = call %bound_method.loc6_18.2(%int_3.loc6) [concrete = constants.%int_3.d14]
  150. // CHECK:STDOUT: %.loc6_18.1: %u32 = value_of_initializer %int.convert_checked.loc6_18.1 [concrete = constants.%int_3.d14]
  151. // CHECK:STDOUT: %.loc6_18.2: %u32 = converted %int_3.loc6, %.loc6_18.1 [concrete = constants.%int_3.d14]
  152. // CHECK:STDOUT: %impl.elem0.loc6_18.2: %.268 = impl_witness_access constants.%ImplicitAs.impl_witness.a11, element0 [concrete = constants.%Convert.47f]
  153. // CHECK:STDOUT: %bound_method.loc6_18.3: <bound method> = bound_method %.loc6_18.2, %impl.elem0.loc6_18.2 [concrete = constants.%Convert.bound.258]
  154. // CHECK:STDOUT: %specific_fn.loc6_18.2: <specific function> = specific_function %impl.elem0.loc6_18.2, @Convert.4(constants.%int_32) [concrete = constants.%Convert.specific_fn.2eb]
  155. // CHECK:STDOUT: %bound_method.loc6_18.4: <bound method> = bound_method %.loc6_18.2, %specific_fn.loc6_18.2 [concrete = constants.%bound_method.3a6]
  156. // CHECK:STDOUT: %int.convert_checked.loc6_18.2: init Core.IntLiteral = call %bound_method.loc6_18.4(%.loc6_18.2) [concrete = constants.%int_3.1ba]
  157. // CHECK:STDOUT: %.loc6_18.3: Core.IntLiteral = value_of_initializer %int.convert_checked.loc6_18.2 [concrete = constants.%int_3.1ba]
  158. // CHECK:STDOUT: %.loc6_18.4: Core.IntLiteral = converted %.loc6_18.2, %.loc6_18.3 [concrete = constants.%int_3.1ba]
  159. // CHECK:STDOUT: %array_type.loc6: type = array_type %.loc6_18.4, %i32.loc6 [concrete = constants.%array_type]
  160. // CHECK:STDOUT: }
  161. // CHECK:STDOUT: }
  162. // CHECK:STDOUT:
  163. // CHECK:STDOUT: fn @__global_init() {
  164. // CHECK:STDOUT: !entry:
  165. // CHECK:STDOUT: <elided>
  166. // CHECK:STDOUT: }
  167. // CHECK:STDOUT: