init_dependent_bound.carbon 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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/int.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/init_dependent_bound.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/array/init_dependent_bound.carbon
  12. // --- generic_empty.carbon
  13. library "[[@TEST_NAME]]";
  14. fn G(T:! type) {
  15. // We can initialize this without knowing T.
  16. //@dump-sem-ir-begin
  17. var unused arr: array(T, 0) = ();
  18. //@dump-sem-ir-end
  19. }
  20. class C {}
  21. fn H() {
  22. G(C);
  23. }
  24. // --- fail_init_dependent_bound.carbon
  25. library "[[@TEST_NAME]]";
  26. fn F(N:! i32) {
  27. // CHECK:STDERR: fail_init_dependent_bound.carbon:[[@LINE+4]]:35: error: cannot initialize array with dependent bound from a list of initializers [ArrayInitDependentBound]
  28. // CHECK:STDERR: var unused arr: array(i32, N) = (1, 2, 3);
  29. // CHECK:STDERR: ^~~~~~~~~
  30. // CHECK:STDERR:
  31. var unused arr: array(i32, N) = (1, 2, 3);
  32. }
  33. // --- fail_todo_init_template_dependent_bound.carbon
  34. library "[[@TEST_NAME]]";
  35. // TODO: This should be valid.
  36. fn G(template N:! i32) {
  37. //@dump-sem-ir-begin
  38. // CHECK:STDERR: fail_todo_init_template_dependent_bound.carbon:[[@LINE+4]]:19: error: cannot evaluate type expression [TypeExprEvaluationFailure]
  39. // CHECK:STDERR: var unused arr: array(i32, N) = (1, 2, 3);
  40. // CHECK:STDERR: ^~~~~~~~~~~~~
  41. // CHECK:STDERR:
  42. var unused arr: array(i32, N) = (1, 2, 3);
  43. //@dump-sem-ir-end
  44. }
  45. fn H() { G(3); }
  46. // CHECK:STDOUT: --- generic_empty.carbon
  47. // CHECK:STDOUT:
  48. // CHECK:STDOUT: constants {
  49. // CHECK:STDOUT: %T: type = symbolic_binding T, 0 [symbolic]
  50. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  51. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [concrete]
  52. // CHECK:STDOUT: %array_type.1b3: type = array_type %int_0, %T [symbolic]
  53. // CHECK:STDOUT: %require_complete: <witness> = require_complete_type %array_type.1b3 [symbolic]
  54. // CHECK:STDOUT: %pattern_type.bd6: type = pattern_type %array_type.1b3 [symbolic]
  55. // CHECK:STDOUT: %empty_tuple: %empty_tuple.type = tuple_value () [concrete]
  56. // CHECK:STDOUT: %array.ca4: %array_type.1b3 = tuple_value () [symbolic]
  57. // CHECK:STDOUT: %Destroy.type: type = facet_type <@Destroy> [concrete]
  58. // CHECK:STDOUT: %Destroy.lookup_impl_witness: <witness> = lookup_impl_witness %array_type.1b3, @Destroy [symbolic]
  59. // CHECK:STDOUT: %Destroy.facet.15e: %Destroy.type = facet_value %array_type.1b3, (%Destroy.lookup_impl_witness) [symbolic]
  60. // CHECK:STDOUT: %Destroy.WithSelf.Op.type.427: type = fn_type @Destroy.WithSelf.Op, @Destroy.WithSelf(%Destroy.facet.15e) [symbolic]
  61. // CHECK:STDOUT: %.6d6: type = fn_type_with_self_type %Destroy.WithSelf.Op.type.427, %Destroy.facet.15e [symbolic]
  62. // CHECK:STDOUT: %impl.elem0: %.6d6 = impl_witness_access %Destroy.lookup_impl_witness, element0 [symbolic]
  63. // CHECK:STDOUT: %specific_impl_fn: <specific function> = specific_impl_function %impl.elem0, @Destroy.WithSelf.Op(%Destroy.facet.15e) [symbolic]
  64. // CHECK:STDOUT: %C: type = class_type @C [concrete]
  65. // CHECK:STDOUT: %array_type.70a: type = array_type %int_0, %C [concrete]
  66. // CHECK:STDOUT: %complete_type.95b: <witness> = complete_type_witness %array_type.70a [concrete]
  67. // CHECK:STDOUT: %pattern_type.f2c: type = pattern_type %array_type.70a [concrete]
  68. // CHECK:STDOUT: %array.40f: %array_type.70a = tuple_value () [concrete]
  69. // CHECK:STDOUT: %Destroy.Op.type: type = fn_type @Destroy.Op [concrete]
  70. // CHECK:STDOUT: %Destroy.Op: %Destroy.Op.type = struct_value () [concrete]
  71. // CHECK:STDOUT: %custom_witness.8d7: <witness> = custom_witness (%Destroy.Op), @Destroy [concrete]
  72. // CHECK:STDOUT: %Destroy.facet.00d: %Destroy.type = facet_value %array_type.70a, (%custom_witness.8d7) [concrete]
  73. // CHECK:STDOUT: %Destroy.WithSelf.Op.type.ed3: type = fn_type @Destroy.WithSelf.Op, @Destroy.WithSelf(%Destroy.facet.00d) [concrete]
  74. // CHECK:STDOUT: %.23f: type = fn_type_with_self_type %Destroy.WithSelf.Op.type.ed3, %Destroy.facet.00d [concrete]
  75. // CHECK:STDOUT: }
  76. // CHECK:STDOUT:
  77. // CHECK:STDOUT: generic fn @G(%T.loc4_7.2: type) {
  78. // CHECK:STDOUT: <elided>
  79. // CHECK:STDOUT:
  80. // CHECK:STDOUT: !definition:
  81. // CHECK:STDOUT: %array_type.loc7_29.2: type = array_type constants.%int_0, %T.loc4_7.1 [symbolic = %array_type.loc7_29.2 (constants.%array_type.1b3)]
  82. // CHECK:STDOUT: %require_complete: <witness> = require_complete_type %array_type.loc7_29.2 [symbolic = %require_complete (constants.%require_complete)]
  83. // CHECK:STDOUT: %pattern_type: type = pattern_type %array_type.loc7_29.2 [symbolic = %pattern_type (constants.%pattern_type.bd6)]
  84. // CHECK:STDOUT: %array: @G.%array_type.loc7_29.2 (%array_type.1b3) = tuple_value () [symbolic = %array (constants.%array.ca4)]
  85. // CHECK:STDOUT: %Destroy.lookup_impl_witness: <witness> = lookup_impl_witness %array_type.loc7_29.2, @Destroy [symbolic = %Destroy.lookup_impl_witness (constants.%Destroy.lookup_impl_witness)]
  86. // CHECK:STDOUT: %Destroy.facet: %Destroy.type = facet_value %array_type.loc7_29.2, (%Destroy.lookup_impl_witness) [symbolic = %Destroy.facet (constants.%Destroy.facet.15e)]
  87. // CHECK:STDOUT: %Destroy.WithSelf.Op.type: type = fn_type @Destroy.WithSelf.Op, @Destroy.WithSelf(%Destroy.facet) [symbolic = %Destroy.WithSelf.Op.type (constants.%Destroy.WithSelf.Op.type.427)]
  88. // CHECK:STDOUT: %.loc7_3.2: type = fn_type_with_self_type %Destroy.WithSelf.Op.type, %Destroy.facet [symbolic = %.loc7_3.2 (constants.%.6d6)]
  89. // CHECK:STDOUT: %impl.elem0.loc7_3.2: @G.%.loc7_3.2 (%.6d6) = impl_witness_access %Destroy.lookup_impl_witness, element0 [symbolic = %impl.elem0.loc7_3.2 (constants.%impl.elem0)]
  90. // CHECK:STDOUT: %specific_impl_fn.loc7_3.2: <specific function> = specific_impl_function %impl.elem0.loc7_3.2, @Destroy.WithSelf.Op(%Destroy.facet) [symbolic = %specific_impl_fn.loc7_3.2 (constants.%specific_impl_fn)]
  91. // CHECK:STDOUT:
  92. // CHECK:STDOUT: fn() {
  93. // CHECK:STDOUT: !entry:
  94. // CHECK:STDOUT: name_binding_decl {
  95. // CHECK:STDOUT: %arr.patt: @G.%pattern_type (%pattern_type.bd6) = ref_binding_pattern arr [concrete]
  96. // CHECK:STDOUT: %arr.var_patt: @G.%pattern_type (%pattern_type.bd6) = var_pattern %arr.patt [concrete]
  97. // CHECK:STDOUT: }
  98. // CHECK:STDOUT: %arr.var: ref @G.%array_type.loc7_29.2 (%array_type.1b3) = var %arr.var_patt
  99. // CHECK:STDOUT: %.loc7_34.1: %empty_tuple.type = tuple_literal () [concrete = constants.%empty_tuple]
  100. // CHECK:STDOUT: %.loc7_34.2: init @G.%array_type.loc7_29.2 (%array_type.1b3) to %arr.var = array_init () [symbolic = %array (constants.%array.ca4)]
  101. // CHECK:STDOUT: %.loc7_3.1: init @G.%array_type.loc7_29.2 (%array_type.1b3) = converted %.loc7_34.1, %.loc7_34.2 [symbolic = %array (constants.%array.ca4)]
  102. // CHECK:STDOUT: assign %arr.var, %.loc7_3.1
  103. // CHECK:STDOUT: %.loc7_29: type = splice_block %array_type.loc7_29.1 [symbolic = %array_type.loc7_29.2 (constants.%array_type.1b3)] {
  104. // CHECK:STDOUT: %T.ref: type = name_ref T, %T.loc4_7.2 [symbolic = %T.loc4_7.1 (constants.%T)]
  105. // CHECK:STDOUT: %int_0: Core.IntLiteral = int_value 0 [concrete = constants.%int_0]
  106. // CHECK:STDOUT: %array_type.loc7_29.1: type = array_type %int_0, %T.ref [symbolic = %array_type.loc7_29.2 (constants.%array_type.1b3)]
  107. // CHECK:STDOUT: }
  108. // CHECK:STDOUT: %arr: ref @G.%array_type.loc7_29.2 (%array_type.1b3) = ref_binding arr, %arr.var
  109. // CHECK:STDOUT: %impl.elem0.loc7_3.1: @G.%.loc7_3.2 (%.6d6) = impl_witness_access constants.%Destroy.lookup_impl_witness, element0 [symbolic = %impl.elem0.loc7_3.2 (constants.%impl.elem0)]
  110. // CHECK:STDOUT: %bound_method.loc7_3.1: <bound method> = bound_method %arr.var, %impl.elem0.loc7_3.1
  111. // CHECK:STDOUT: %specific_impl_fn.loc7_3.1: <specific function> = specific_impl_function %impl.elem0.loc7_3.1, @Destroy.WithSelf.Op(constants.%Destroy.facet.15e) [symbolic = %specific_impl_fn.loc7_3.2 (constants.%specific_impl_fn)]
  112. // CHECK:STDOUT: %bound_method.loc7_3.2: <bound method> = bound_method %arr.var, %specific_impl_fn.loc7_3.1
  113. // CHECK:STDOUT: %Destroy.WithSelf.Op.call: init %empty_tuple.type = call %bound_method.loc7_3.2(%arr.var)
  114. // CHECK:STDOUT: <elided>
  115. // CHECK:STDOUT: }
  116. // CHECK:STDOUT: }
  117. // CHECK:STDOUT:
  118. // CHECK:STDOUT: fn @Destroy.Op(%self.param: ref %array_type.70a) = "no_op";
  119. // CHECK:STDOUT:
  120. // CHECK:STDOUT: specific @G(constants.%T) {
  121. // CHECK:STDOUT: %T.loc4_7.1 => constants.%T
  122. // CHECK:STDOUT: }
  123. // CHECK:STDOUT:
  124. // CHECK:STDOUT: specific @G(constants.%C) {
  125. // CHECK:STDOUT: %T.loc4_7.1 => constants.%C
  126. // CHECK:STDOUT:
  127. // CHECK:STDOUT: !definition:
  128. // CHECK:STDOUT: %array_type.loc7_29.2 => constants.%array_type.70a
  129. // CHECK:STDOUT: %require_complete => constants.%complete_type.95b
  130. // CHECK:STDOUT: %pattern_type => constants.%pattern_type.f2c
  131. // CHECK:STDOUT: %array => constants.%array.40f
  132. // CHECK:STDOUT: %Destroy.lookup_impl_witness => constants.%custom_witness.8d7
  133. // CHECK:STDOUT: %Destroy.facet => constants.%Destroy.facet.00d
  134. // CHECK:STDOUT: %Destroy.WithSelf.Op.type => constants.%Destroy.WithSelf.Op.type.ed3
  135. // CHECK:STDOUT: %.loc7_3.2 => constants.%.23f
  136. // CHECK:STDOUT: %impl.elem0.loc7_3.2 => constants.%Destroy.Op
  137. // CHECK:STDOUT: %specific_impl_fn.loc7_3.2 => constants.%Destroy.Op
  138. // CHECK:STDOUT: }
  139. // CHECK:STDOUT:
  140. // CHECK:STDOUT: --- fail_todo_init_template_dependent_bound.carbon
  141. // CHECK:STDOUT:
  142. // CHECK:STDOUT: constants {
  143. // CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
  144. // CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
  145. // CHECK:STDOUT: %N.5de: %i32 = symbolic_binding N, 0, template [template]
  146. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete]
  147. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [concrete]
  148. // CHECK:STDOUT: %int_3.1ba: Core.IntLiteral = int_value 3 [concrete]
  149. // CHECK:STDOUT: %tuple.type: type = tuple_type (Core.IntLiteral, Core.IntLiteral, Core.IntLiteral) [concrete]
  150. // CHECK:STDOUT: %tuple: %tuple.type = tuple_value (%int_1, %int_2, %int_3.1ba) [concrete]
  151. // CHECK:STDOUT: %ImplicitAs.type.139: type = facet_type <@ImplicitAs, @ImplicitAs(Core.IntLiteral)> [concrete]
  152. // CHECK:STDOUT: %From: Core.IntLiteral = symbolic_binding From, 0 [symbolic]
  153. // CHECK:STDOUT: %Int.as.ImplicitAs.impl.Convert.type.2ed: type = fn_type @Int.as.ImplicitAs.impl.Convert, @Int.as.ImplicitAs.impl(%From) [symbolic]
  154. // CHECK:STDOUT: %Int.as.ImplicitAs.impl.Convert.d29: %Int.as.ImplicitAs.impl.Convert.type.2ed = struct_value () [symbolic]
  155. // CHECK:STDOUT: %int_3.822: %i32 = int_value 3 [concrete]
  156. // CHECK:STDOUT: %ImplicitAs.impl_witness.640: <witness> = impl_witness imports.%ImplicitAs.impl_witness_table.ea2, @Int.as.ImplicitAs.impl(%int_32) [concrete]
  157. // CHECK:STDOUT: %Int.as.ImplicitAs.impl.Convert.type.240: type = fn_type @Int.as.ImplicitAs.impl.Convert, @Int.as.ImplicitAs.impl(%int_32) [concrete]
  158. // CHECK:STDOUT: %Int.as.ImplicitAs.impl.Convert.dd4: %Int.as.ImplicitAs.impl.Convert.type.240 = struct_value () [concrete]
  159. // CHECK:STDOUT: %ImplicitAs.facet.290: %ImplicitAs.type.139 = facet_value %i32, (%ImplicitAs.impl_witness.640) [concrete]
  160. // CHECK:STDOUT: %ImplicitAs.WithSelf.Convert.type.462: type = fn_type @ImplicitAs.WithSelf.Convert, @ImplicitAs.WithSelf(Core.IntLiteral, %ImplicitAs.facet.290) [concrete]
  161. // CHECK:STDOUT: %.0a7: type = fn_type_with_self_type %ImplicitAs.WithSelf.Convert.type.462, %ImplicitAs.facet.290 [concrete]
  162. // CHECK:STDOUT: %Int.as.ImplicitAs.impl.Convert.bound: <bound method> = bound_method %int_3.822, %Int.as.ImplicitAs.impl.Convert.dd4 [concrete]
  163. // CHECK:STDOUT: %Int.as.ImplicitAs.impl.Convert.specific_fn: <specific function> = specific_function %Int.as.ImplicitAs.impl.Convert.dd4, @Int.as.ImplicitAs.impl.Convert(%int_32) [concrete]
  164. // CHECK:STDOUT: %bound_method.17a: <bound method> = bound_method %int_3.822, %Int.as.ImplicitAs.impl.Convert.specific_fn [concrete]
  165. // CHECK:STDOUT: %inst.splice_block: <instruction> = inst_value [concrete] {
  166. // CHECK:STDOUT: %.3e5: Core.IntLiteral = splice_block %.502 [concrete = %int_3.1ba] {
  167. // CHECK:STDOUT: %impl.elem0: %.0a7 = impl_witness_access %ImplicitAs.impl_witness.640, element0 [concrete = %Int.as.ImplicitAs.impl.Convert.dd4]
  168. // CHECK:STDOUT: %bound_method.372: <bound method> = bound_method %int_3.822, %impl.elem0 [concrete = %Int.as.ImplicitAs.impl.Convert.bound]
  169. // CHECK:STDOUT: %specific_fn: <specific function> = specific_function %impl.elem0, @Int.as.ImplicitAs.impl.Convert(%int_32) [concrete = %Int.as.ImplicitAs.impl.Convert.specific_fn]
  170. // CHECK:STDOUT: %bound_method.28e: <bound method> = bound_method %int_3.822, %specific_fn [concrete = %bound_method.17a]
  171. // CHECK:STDOUT: %Int.as.ImplicitAs.impl.Convert.call: init Core.IntLiteral = call %bound_method.28e(%int_3.822) [concrete = %int_3.1ba]
  172. // CHECK:STDOUT: %.14d: Core.IntLiteral = value_of_initializer %Int.as.ImplicitAs.impl.Convert.call [concrete = %int_3.1ba]
  173. // CHECK:STDOUT: %.502: Core.IntLiteral = converted %int_3.822, %.14d [concrete = %int_3.1ba]
  174. // CHECK:STDOUT: }
  175. // CHECK:STDOUT: }
  176. // CHECK:STDOUT: }
  177. // CHECK:STDOUT:
  178. // CHECK:STDOUT: imports {
  179. // CHECK:STDOUT: %Core.import_ref.0bc: @Int.as.ImplicitAs.impl.%Int.as.ImplicitAs.impl.Convert.type (%Int.as.ImplicitAs.impl.Convert.type.2ed) = import_ref Core//prelude/parts/int, loc{{\d+_\d+}}, loaded [symbolic = @Int.as.ImplicitAs.impl.%Int.as.ImplicitAs.impl.Convert (constants.%Int.as.ImplicitAs.impl.Convert.d29)]
  180. // CHECK:STDOUT: %ImplicitAs.impl_witness_table.ea2 = impl_witness_table (%Core.import_ref.0bc), @Int.as.ImplicitAs.impl [concrete]
  181. // CHECK:STDOUT: }
  182. // CHECK:STDOUT:
  183. // CHECK:STDOUT: generic fn @G(%N.loc5_16.2: %i32) {
  184. // CHECK:STDOUT: <elided>
  185. // CHECK:STDOUT:
  186. // CHECK:STDOUT: !definition:
  187. // CHECK:STDOUT: %.loc11_30.2: <instruction> = convert_to_value_action %N.ref, Core.IntLiteral [template]
  188. // CHECK:STDOUT:
  189. // CHECK:STDOUT: fn() {
  190. // CHECK:STDOUT: !entry:
  191. // CHECK:STDOUT: name_binding_decl {
  192. // CHECK:STDOUT: %arr.patt: <error> = ref_binding_pattern arr [concrete]
  193. // CHECK:STDOUT: %arr.var_patt: <error> = var_pattern %arr.patt [concrete]
  194. // CHECK:STDOUT: }
  195. // CHECK:STDOUT: %arr.var: ref <error> = var %arr.var_patt [concrete = <error>]
  196. // CHECK:STDOUT: %int_1: Core.IntLiteral = int_value 1 [concrete = constants.%int_1]
  197. // CHECK:STDOUT: %int_2: Core.IntLiteral = int_value 2 [concrete = constants.%int_2]
  198. // CHECK:STDOUT: %int_3: Core.IntLiteral = int_value 3 [concrete = constants.%int_3.1ba]
  199. // CHECK:STDOUT: %.loc11_43: %tuple.type = tuple_literal (%int_1, %int_2, %int_3) [concrete = constants.%tuple]
  200. // CHECK:STDOUT: assign %arr.var, <error>
  201. // CHECK:STDOUT: <elided>
  202. // CHECK:STDOUT: %arr: ref <error> = ref_binding arr, <error> [concrete = <error>]
  203. // CHECK:STDOUT: <elided>
  204. // CHECK:STDOUT: }
  205. // CHECK:STDOUT: }
  206. // CHECK:STDOUT:
  207. // CHECK:STDOUT: specific @G(constants.%N.5de) {
  208. // CHECK:STDOUT: %N.loc5_16.1 => constants.%N.5de
  209. // CHECK:STDOUT: }
  210. // CHECK:STDOUT:
  211. // CHECK:STDOUT: specific @G(constants.%int_3.822) {
  212. // CHECK:STDOUT: %N.loc5_16.1 => constants.%int_3.822
  213. // CHECK:STDOUT:
  214. // CHECK:STDOUT: !definition:
  215. // CHECK:STDOUT: %.loc11_30.2 => constants.%inst.splice_block
  216. // CHECK:STDOUT: }
  217. // CHECK:STDOUT: