bound_values.carbon 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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/full.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/array/bound_values.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/array/bound_values.carbon
  12. // --- addition.carbon
  13. library "[[@TEST_NAME]]";
  14. var a:
  15. //@dump-sem-ir-begin
  16. array(i32, 1 + 2)
  17. //@dump-sem-ir-end
  18. = (1, 2, 3);
  19. let b: array(i32, 3)* = &a;
  20. // --- unsigned.carbon
  21. library "[[@TEST_NAME]]";
  22. var a:
  23. //@dump-sem-ir-begin
  24. array(i32, 3 as u32)
  25. //@dump-sem-ir-end
  26. = (1, 2, 3);
  27. let b: array(i32, 3)* = &a;
  28. // --- fail_negative.carbon
  29. library "[[@TEST_NAME]]";
  30. // CHECK:STDERR: fail_negative.carbon:[[@LINE+4]]:19: error: array bound of -1 is negative [ArrayBoundNegative]
  31. // CHECK:STDERR: var a: array(i32, -1);
  32. // CHECK:STDERR: ^~
  33. // CHECK:STDERR:
  34. var a: array(i32, -1);
  35. // --- fail_overflow.carbon
  36. library "[[@TEST_NAME]]";
  37. // CHECK:STDERR: fail_overflow.carbon:[[@LINE+4]]:19: error: array bound of 39999999999999999993 is too large [ArrayBoundTooLarge]
  38. // CHECK:STDERR: var a: array(i32, 39999999999999999993);
  39. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~
  40. // CHECK:STDERR:
  41. var a: array(i32, 39999999999999999993);
  42. // --- fail_invalid_type_with_overflow.carbon
  43. library "[[@TEST_NAME]]";
  44. // 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]
  45. // CHECK:STDERR: var b: array(1, 39999999999999999993);
  46. // CHECK:STDERR: ^
  47. // CHECK:STDERR: fail_invalid_type_with_overflow.carbon:[[@LINE+4]]:14: note: type `Core.IntLiteral` does not implement interface `Core.ImplicitAs(type)` [MissingImplInMemberAccessNote]
  48. // CHECK:STDERR: var b: array(1, 39999999999999999993);
  49. // CHECK:STDERR: ^
  50. // CHECK:STDERR:
  51. var b: array(1, 39999999999999999993);
  52. // CHECK:STDOUT: --- addition.carbon
  53. // CHECK:STDOUT:
  54. // CHECK:STDOUT: constants {
  55. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  56. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  57. // CHECK:STDOUT: %int_1.5b8: Core.IntLiteral = int_value 1 [concrete]
  58. // CHECK:STDOUT: %int_2.ecc: Core.IntLiteral = int_value 2 [concrete]
  59. // CHECK:STDOUT: %AddWith.type.a45: type = facet_type <@AddWith, @AddWith(Core.IntLiteral)> [concrete]
  60. // CHECK:STDOUT: %AddWith.Op.type.0ee: type = fn_type @AddWith.Op, @AddWith(Core.IntLiteral) [concrete]
  61. // CHECK:STDOUT: %AddWith.impl_witness: <witness> = impl_witness imports.%AddWith.impl_witness_table [concrete]
  62. // CHECK:STDOUT: %AddWith.facet: %AddWith.type.a45 = facet_value Core.IntLiteral, (%AddWith.impl_witness) [concrete]
  63. // CHECK:STDOUT: %.133: type = fn_type_with_self_type %AddWith.Op.type.0ee, %AddWith.facet [concrete]
  64. // CHECK:STDOUT: %Core.IntLiteral.as.AddWith.impl.Op.type: type = fn_type @Core.IntLiteral.as.AddWith.impl.Op [concrete]
  65. // CHECK:STDOUT: %Core.IntLiteral.as.AddWith.impl.Op: %Core.IntLiteral.as.AddWith.impl.Op.type = struct_value () [concrete]
  66. // CHECK:STDOUT: %Core.IntLiteral.as.AddWith.impl.Op.bound: <bound method> = bound_method %int_1.5b8, %Core.IntLiteral.as.AddWith.impl.Op [concrete]
  67. // CHECK:STDOUT: %int_3.1ba: Core.IntLiteral = int_value 3 [concrete]
  68. // CHECK:STDOUT: %array_type: type = array_type %int_3.1ba, %i32 [concrete]
  69. // CHECK:STDOUT: }
  70. // CHECK:STDOUT:
  71. // CHECK:STDOUT: imports {
  72. // CHECK:STDOUT: %Core.import_ref.abd = import_ref Core//prelude/operators/arithmetic, loc95_57, unloaded
  73. // CHECK:STDOUT: %Core.import_ref.fdc: %Core.IntLiteral.as.AddWith.impl.Op.type = import_ref Core//prelude/operators/arithmetic, loc96_42, loaded [concrete = constants.%Core.IntLiteral.as.AddWith.impl.Op]
  74. // CHECK:STDOUT: %AddWith.impl_witness_table = impl_witness_table (%Core.import_ref.abd, %Core.import_ref.fdc), @Core.IntLiteral.as.AddWith.impl [concrete]
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT:
  77. // CHECK:STDOUT: file {
  78. // CHECK:STDOUT: %.loc6_21: type = splice_block %array_type.loc6 [concrete = constants.%array_type] {
  79. // CHECK:STDOUT: %int_32.loc6: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  80. // CHECK:STDOUT: %i32.loc6: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  81. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete = constants.%int_1.5b8]
  82. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [concrete = constants.%int_2.ecc]
  83. // CHECK:STDOUT: %impl.elem1: %.133 = impl_witness_access constants.%AddWith.impl_witness, element1 [concrete = constants.%Core.IntLiteral.as.AddWith.impl.Op]
  84. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %int_1, %impl.elem1 [concrete = constants.%Core.IntLiteral.as.AddWith.impl.Op.bound]
  85. // CHECK:STDOUT: %Core.IntLiteral.as.AddWith.impl.Op.call: init Core.IntLiteral = call %bound_method(%int_1, %int_2) [concrete = constants.%int_3.1ba]
  86. // CHECK:STDOUT: %.loc6_18.1: Core.IntLiteral = value_of_initializer %Core.IntLiteral.as.AddWith.impl.Op.call [concrete = constants.%int_3.1ba]
  87. // CHECK:STDOUT: %.loc6_18.2: Core.IntLiteral = converted %Core.IntLiteral.as.AddWith.impl.Op.call, %.loc6_18.1 [concrete = constants.%int_3.1ba]
  88. // CHECK:STDOUT: %array_type.loc6: type = array_type %.loc6_18.2, %i32.loc6 [concrete = constants.%array_type]
  89. // CHECK:STDOUT: }
  90. // CHECK:STDOUT: }
  91. // CHECK:STDOUT:
  92. // CHECK:STDOUT: fn @__global_init() {
  93. // CHECK:STDOUT: !entry:
  94. // CHECK:STDOUT: <elided>
  95. // CHECK:STDOUT: }
  96. // CHECK:STDOUT:
  97. // CHECK:STDOUT: --- unsigned.carbon
  98. // CHECK:STDOUT:
  99. // CHECK:STDOUT: constants {
  100. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  101. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  102. // CHECK:STDOUT: %int_3.1ba: Core.IntLiteral = int_value 3 [concrete]
  103. // CHECK:STDOUT: %u32: type = class_type @UInt, @UInt(%int_32) [concrete]
  104. // CHECK:STDOUT: %As.type.45b: type = facet_type <@As, @As(%u32)> [concrete]
  105. // CHECK:STDOUT: %As.Convert.type.b94: type = fn_type @As.Convert, @As(%u32) [concrete]
  106. // CHECK:STDOUT: %To.c80: Core.IntLiteral = bind_symbolic_name To, 0 [symbolic]
  107. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.type.972: type = fn_type @Core.IntLiteral.as.As.impl.Convert, @Core.IntLiteral.as.As.impl(%To.c80) [symbolic]
  108. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.276: %Core.IntLiteral.as.As.impl.Convert.type.972 = struct_value () [symbolic]
  109. // CHECK:STDOUT: %From: Core.IntLiteral = bind_symbolic_name From, 0 [symbolic]
  110. // CHECK:STDOUT: %As.impl_witness.556: <witness> = impl_witness imports.%As.impl_witness_table.e4e, @Core.IntLiteral.as.As.impl(%int_32) [concrete]
  111. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.type.847: type = fn_type @Core.IntLiteral.as.As.impl.Convert, @Core.IntLiteral.as.As.impl(%int_32) [concrete]
  112. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.51e: %Core.IntLiteral.as.As.impl.Convert.type.847 = struct_value () [concrete]
  113. // CHECK:STDOUT: %As.facet: %As.type.45b = facet_value Core.IntLiteral, (%As.impl_witness.556) [concrete]
  114. // CHECK:STDOUT: %.97b: type = fn_type_with_self_type %As.Convert.type.b94, %As.facet [concrete]
  115. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.bound: <bound method> = bound_method %int_3.1ba, %Core.IntLiteral.as.As.impl.Convert.51e [concrete]
  116. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.specific_fn: <specific function> = specific_function %Core.IntLiteral.as.As.impl.Convert.51e, @Core.IntLiteral.as.As.impl.Convert(%int_32) [concrete]
  117. // CHECK:STDOUT: %bound_method.4fe: <bound method> = bound_method %int_3.1ba, %Core.IntLiteral.as.As.impl.Convert.specific_fn [concrete]
  118. // CHECK:STDOUT: %int_3.d14: %u32 = int_value 3 [concrete]
  119. // CHECK:STDOUT: %ImplicitAs.type.7a9: type = facet_type <@ImplicitAs, @ImplicitAs(Core.IntLiteral)> [concrete]
  120. // CHECK:STDOUT: %ImplicitAs.Convert.type.71e: type = fn_type @ImplicitAs.Convert, @ImplicitAs(Core.IntLiteral) [concrete]
  121. // CHECK:STDOUT: %UInt.as.ImplicitAs.impl.Convert.type.b7e: type = fn_type @UInt.as.ImplicitAs.impl.Convert, @UInt.as.ImplicitAs.impl(%From) [symbolic]
  122. // CHECK:STDOUT: %UInt.as.ImplicitAs.impl.Convert.138: %UInt.as.ImplicitAs.impl.Convert.type.b7e = struct_value () [symbolic]
  123. // CHECK:STDOUT: %ImplicitAs.impl_witness.f6b: <witness> = impl_witness imports.%ImplicitAs.impl_witness_table.bd4, @UInt.as.ImplicitAs.impl(%int_32) [concrete]
  124. // CHECK:STDOUT: %UInt.as.ImplicitAs.impl.Convert.type.af7: type = fn_type @UInt.as.ImplicitAs.impl.Convert, @UInt.as.ImplicitAs.impl(%int_32) [concrete]
  125. // CHECK:STDOUT: %UInt.as.ImplicitAs.impl.Convert.e1c: %UInt.as.ImplicitAs.impl.Convert.type.af7 = struct_value () [concrete]
  126. // CHECK:STDOUT: %ImplicitAs.facet.701: %ImplicitAs.type.7a9 = facet_value %u32, (%ImplicitAs.impl_witness.f6b) [concrete]
  127. // CHECK:STDOUT: %.a92: type = fn_type_with_self_type %ImplicitAs.Convert.type.71e, %ImplicitAs.facet.701 [concrete]
  128. // CHECK:STDOUT: %UInt.as.ImplicitAs.impl.Convert.bound: <bound method> = bound_method %int_3.d14, %UInt.as.ImplicitAs.impl.Convert.e1c [concrete]
  129. // CHECK:STDOUT: %UInt.as.ImplicitAs.impl.Convert.specific_fn: <specific function> = specific_function %UInt.as.ImplicitAs.impl.Convert.e1c, @UInt.as.ImplicitAs.impl.Convert(%int_32) [concrete]
  130. // CHECK:STDOUT: %bound_method.709: <bound method> = bound_method %int_3.d14, %UInt.as.ImplicitAs.impl.Convert.specific_fn [concrete]
  131. // CHECK:STDOUT: %array_type: type = array_type %int_3.1ba, %i32 [concrete]
  132. // CHECK:STDOUT: }
  133. // CHECK:STDOUT:
  134. // CHECK:STDOUT: imports {
  135. // CHECK:STDOUT: %Core.import_ref.611: @Core.IntLiteral.as.As.impl.%Core.IntLiteral.as.As.impl.Convert.type (%Core.IntLiteral.as.As.impl.Convert.type.972) = import_ref Core//prelude/types/uint, loc37_40, loaded [symbolic = @Core.IntLiteral.as.As.impl.%Core.IntLiteral.as.As.impl.Convert (constants.%Core.IntLiteral.as.As.impl.Convert.276)]
  136. // CHECK:STDOUT: %As.impl_witness_table.e4e = impl_witness_table (%Core.import_ref.611), @Core.IntLiteral.as.As.impl [concrete]
  137. // CHECK:STDOUT: %Core.import_ref.4da: @UInt.as.ImplicitAs.impl.%UInt.as.ImplicitAs.impl.Convert.type (%UInt.as.ImplicitAs.impl.Convert.type.b7e) = import_ref Core//prelude/types/uint, loc32_44, loaded [symbolic = @UInt.as.ImplicitAs.impl.%UInt.as.ImplicitAs.impl.Convert (constants.%UInt.as.ImplicitAs.impl.Convert.138)]
  138. // CHECK:STDOUT: %ImplicitAs.impl_witness_table.bd4 = impl_witness_table (%Core.import_ref.4da), @UInt.as.ImplicitAs.impl [concrete]
  139. // CHECK:STDOUT: }
  140. // CHECK:STDOUT:
  141. // CHECK:STDOUT: file {
  142. // CHECK:STDOUT: %.loc6_24: type = splice_block %array_type.loc6 [concrete = constants.%array_type] {
  143. // CHECK:STDOUT: %int_32.loc6_11: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  144. // CHECK:STDOUT: %i32.loc6: type = class_type @Int, @Int(constants.%int_32) [concrete = constants.%i32]
  145. // CHECK:STDOUT: %int_3.loc6: Core.IntLiteral = int_value 3 [concrete = constants.%int_3.1ba]
  146. // CHECK:STDOUT: %int_32.loc6_21: Core.IntLiteral = int_value 32 [concrete = constants.%int_32]
  147. // CHECK:STDOUT: %u32: type = class_type @UInt, @UInt(constants.%int_32) [concrete = constants.%u32]
  148. // CHECK:STDOUT: %impl.elem0.loc6_18.1: %.97b = impl_witness_access constants.%As.impl_witness.556, element0 [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.51e]
  149. // CHECK:STDOUT: %bound_method.loc6_18.1: <bound method> = bound_method %int_3.loc6, %impl.elem0.loc6_18.1 [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.bound]
  150. // CHECK:STDOUT: %specific_fn.loc6_18.1: <specific function> = specific_function %impl.elem0.loc6_18.1, @Core.IntLiteral.as.As.impl.Convert(constants.%int_32) [concrete = constants.%Core.IntLiteral.as.As.impl.Convert.specific_fn]
  151. // CHECK:STDOUT: %bound_method.loc6_18.2: <bound method> = bound_method %int_3.loc6, %specific_fn.loc6_18.1 [concrete = constants.%bound_method.4fe]
  152. // CHECK:STDOUT: %Core.IntLiteral.as.As.impl.Convert.call: init %u32 = call %bound_method.loc6_18.2(%int_3.loc6) [concrete = constants.%int_3.d14]
  153. // CHECK:STDOUT: %.loc6_18.1: %u32 = value_of_initializer %Core.IntLiteral.as.As.impl.Convert.call [concrete = constants.%int_3.d14]
  154. // CHECK:STDOUT: %.loc6_18.2: %u32 = converted %int_3.loc6, %.loc6_18.1 [concrete = constants.%int_3.d14]
  155. // CHECK:STDOUT: %impl.elem0.loc6_18.2: %.a92 = impl_witness_access constants.%ImplicitAs.impl_witness.f6b, element0 [concrete = constants.%UInt.as.ImplicitAs.impl.Convert.e1c]
  156. // CHECK:STDOUT: %bound_method.loc6_18.3: <bound method> = bound_method %.loc6_18.2, %impl.elem0.loc6_18.2 [concrete = constants.%UInt.as.ImplicitAs.impl.Convert.bound]
  157. // CHECK:STDOUT: %specific_fn.loc6_18.2: <specific function> = specific_function %impl.elem0.loc6_18.2, @UInt.as.ImplicitAs.impl.Convert(constants.%int_32) [concrete = constants.%UInt.as.ImplicitAs.impl.Convert.specific_fn]
  158. // CHECK:STDOUT: %bound_method.loc6_18.4: <bound method> = bound_method %.loc6_18.2, %specific_fn.loc6_18.2 [concrete = constants.%bound_method.709]
  159. // CHECK:STDOUT: %UInt.as.ImplicitAs.impl.Convert.call: init Core.IntLiteral = call %bound_method.loc6_18.4(%.loc6_18.2) [concrete = constants.%int_3.1ba]
  160. // CHECK:STDOUT: %.loc6_18.3: Core.IntLiteral = value_of_initializer %UInt.as.ImplicitAs.impl.Convert.call [concrete = constants.%int_3.1ba]
  161. // CHECK:STDOUT: %.loc6_18.4: Core.IntLiteral = converted %.loc6_18.2, %.loc6_18.3 [concrete = constants.%int_3.1ba]
  162. // CHECK:STDOUT: %array_type.loc6: type = array_type %.loc6_18.4, %i32.loc6 [concrete = constants.%array_type]
  163. // CHECK:STDOUT: }
  164. // CHECK:STDOUT: }
  165. // CHECK:STDOUT:
  166. // CHECK:STDOUT: fn @__global_init() {
  167. // CHECK:STDOUT: !entry:
  168. // CHECK:STDOUT: <elided>
  169. // CHECK:STDOUT: }
  170. // CHECK:STDOUT: