assoc_const_in_generic.carbon 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/interface/no_prelude/assoc_const_in_generic.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interface/no_prelude/assoc_const_in_generic.carbon
  10. interface I(T:! type) {
  11. fn F(U:! type) -> U;
  12. }
  13. fn G(T:! type) {
  14. // This should not result in a `fn_decl` instruction being added to the eval
  15. // block for the generic G. This used to crash when printing formatted SemIR
  16. // because the same instruction ended up in multiple scopes.
  17. I(T).F;
  18. }
  19. fn H() {
  20. G({});
  21. }
  22. // CHECK:STDOUT: --- assoc_const_in_generic.carbon
  23. // CHECK:STDOUT:
  24. // CHECK:STDOUT: constants {
  25. // CHECK:STDOUT: %T: type = bind_symbolic_name T, 0 [symbolic]
  26. // CHECK:STDOUT: %T.patt.1: type = symbolic_binding_pattern T, 0 [symbolic]
  27. // CHECK:STDOUT: %I.type.1: type = generic_interface_type @I [template]
  28. // CHECK:STDOUT: %.1: type = tuple_type () [template]
  29. // CHECK:STDOUT: %I: %I.type.1 = struct_value () [template]
  30. // CHECK:STDOUT: %I.type.2: type = interface_type @I, @I(%T) [symbolic]
  31. // CHECK:STDOUT: %Self: %I.type.2 = bind_symbolic_name Self, 1 [symbolic]
  32. // CHECK:STDOUT: %U: type = bind_symbolic_name U, 2 [symbolic]
  33. // CHECK:STDOUT: %U.patt: type = symbolic_binding_pattern U, 2 [symbolic]
  34. // CHECK:STDOUT: %F.type.1: type = fn_type @F, @I(%T) [symbolic]
  35. // CHECK:STDOUT: %F: %F.type.1 = struct_value () [symbolic]
  36. // CHECK:STDOUT: %.2: type = assoc_entity_type %I.type.2, %F.type.1 [symbolic]
  37. // CHECK:STDOUT: %.3: %.2 = assoc_entity element0, @I.%F.decl [symbolic]
  38. // CHECK:STDOUT: %T.patt.2: type = symbolic_binding_pattern T, 0 [symbolic]
  39. // CHECK:STDOUT: %G.type: type = fn_type @G [template]
  40. // CHECK:STDOUT: %G: %G.type = struct_value () [template]
  41. // CHECK:STDOUT: %H.type: type = fn_type @H [template]
  42. // CHECK:STDOUT: %H: %H.type = struct_value () [template]
  43. // CHECK:STDOUT: %.4: type = struct_type {} [template]
  44. // CHECK:STDOUT: %.5: <specific function> = specific_function %G, @G(%.4) [template]
  45. // CHECK:STDOUT: %I.type.3: type = interface_type @I, @I(%.4) [template]
  46. // CHECK:STDOUT: %F.type.2: type = fn_type @F, @I(%.4) [template]
  47. // CHECK:STDOUT: %.6: type = assoc_entity_type %I.type.3, %F.type.2 [template]
  48. // CHECK:STDOUT: %.7: %.6 = assoc_entity element0, @I.%F.decl [template]
  49. // CHECK:STDOUT: }
  50. // CHECK:STDOUT:
  51. // CHECK:STDOUT: file {
  52. // CHECK:STDOUT: package: <namespace> = namespace [template] {
  53. // CHECK:STDOUT: .I = %I.decl
  54. // CHECK:STDOUT: .G = %G.decl
  55. // CHECK:STDOUT: .H = %H.decl
  56. // CHECK:STDOUT: }
  57. // CHECK:STDOUT: %I.decl: %I.type.1 = interface_decl @I [template = constants.%I] {
  58. // CHECK:STDOUT: %T.patt.loc11_13.1: type = symbolic_binding_pattern T, 0 [symbolic = %T.patt.loc11_13.2 (constants.%T.patt.1)]
  59. // CHECK:STDOUT: %T.param_patt: type = param_pattern %T.patt.loc11_13.1, runtime_param<invalid> [symbolic = %T.patt.loc11_13.2 (constants.%T.patt.1)]
  60. // CHECK:STDOUT: } {
  61. // CHECK:STDOUT: %param: type = param runtime_param<invalid>
  62. // CHECK:STDOUT: %T.loc11_13.1: type = bind_symbolic_name T, 0, %param [symbolic = %T.loc11_13.2 (constants.%T)]
  63. // CHECK:STDOUT: }
  64. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [template = constants.%G] {
  65. // CHECK:STDOUT: %T.patt.loc15_6.1: type = symbolic_binding_pattern T, 0 [symbolic = %T.patt.loc15_6.2 (constants.%T.patt.2)]
  66. // CHECK:STDOUT: %T.param_patt: type = param_pattern %T.patt.loc15_6.1, runtime_param<invalid> [symbolic = %T.patt.loc15_6.2 (constants.%T.patt.2)]
  67. // CHECK:STDOUT: } {
  68. // CHECK:STDOUT: %param: type = param runtime_param<invalid>
  69. // CHECK:STDOUT: %T.loc15_6.1: type = bind_symbolic_name T, 0, %param [symbolic = %T.loc15_6.2 (constants.%T)]
  70. // CHECK:STDOUT: }
  71. // CHECK:STDOUT: %H.decl: %H.type = fn_decl @H [template = constants.%H] {} {}
  72. // CHECK:STDOUT: }
  73. // CHECK:STDOUT:
  74. // CHECK:STDOUT: generic interface @I(%T.loc11_13.1: type) {
  75. // CHECK:STDOUT: %T.loc11_13.2: type = bind_symbolic_name T, 0 [symbolic = %T.loc11_13.2 (constants.%T)]
  76. // CHECK:STDOUT: %T.patt.loc11_13.2: type = symbolic_binding_pattern T, 0 [symbolic = %T.patt.loc11_13.2 (constants.%T.patt.1)]
  77. // CHECK:STDOUT:
  78. // CHECK:STDOUT: !definition:
  79. // CHECK:STDOUT: %I.type: type = interface_type @I, @I(%T.loc11_13.2) [symbolic = %I.type (constants.%I.type.2)]
  80. // CHECK:STDOUT: %Self.2: %I.type.2 = bind_symbolic_name Self, 1 [symbolic = %Self.2 (constants.%Self)]
  81. // CHECK:STDOUT: %F.type: type = fn_type @F, @I(%T.loc11_13.2) [symbolic = %F.type (constants.%F.type.1)]
  82. // CHECK:STDOUT: %F: @I.%F.type (%F.type.1) = struct_value () [symbolic = %F (constants.%F)]
  83. // CHECK:STDOUT: %.loc12_22.2: type = assoc_entity_type @I.%I.type (%I.type.2), @I.%F.type (%F.type.1) [symbolic = %.loc12_22.2 (constants.%.2)]
  84. // CHECK:STDOUT: %.loc12_22.3: @I.%.loc12_22.2 (%.2) = assoc_entity element0, %F.decl [symbolic = %.loc12_22.3 (constants.%.3)]
  85. // CHECK:STDOUT:
  86. // CHECK:STDOUT: interface {
  87. // CHECK:STDOUT: %Self.1: @I.%I.type (%I.type.2) = bind_symbolic_name Self, 1 [symbolic = %Self.2 (constants.%Self)]
  88. // CHECK:STDOUT: %F.decl: @I.%F.type (%F.type.1) = fn_decl @F [symbolic = @I.%F (constants.%F)] {
  89. // CHECK:STDOUT: %U.patt.loc12_8.2: type = symbolic_binding_pattern U, 2 [symbolic = %U.patt.loc12_8.1 (constants.%U.patt)]
  90. // CHECK:STDOUT: %U.param_patt: type = param_pattern %U.patt.loc12_8.2, runtime_param<invalid> [symbolic = %U.patt.loc12_8.1 (constants.%U.patt)]
  91. // CHECK:STDOUT: } {
  92. // CHECK:STDOUT: %U.ref: type = name_ref U, %U.loc12_8.2 [symbolic = %U.loc12_8.1 (constants.%U)]
  93. // CHECK:STDOUT: %return: ref @F.%U.loc12_8.1 (%U) = var <return slot>
  94. // CHECK:STDOUT: %param: type = param runtime_param<invalid>
  95. // CHECK:STDOUT: %U.loc12_8.2: type = bind_symbolic_name U, 2, %param [symbolic = %U.loc12_8.1 (constants.%U)]
  96. // CHECK:STDOUT: }
  97. // CHECK:STDOUT: %.loc12_22.1: @I.%.loc12_22.2 (%.2) = assoc_entity element0, %F.decl [symbolic = %.loc12_22.3 (constants.%.3)]
  98. // CHECK:STDOUT:
  99. // CHECK:STDOUT: !members:
  100. // CHECK:STDOUT: .Self = %Self.1
  101. // CHECK:STDOUT: .F = %.loc12_22.1
  102. // CHECK:STDOUT: witness = (%F.decl)
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT: }
  105. // CHECK:STDOUT:
  106. // CHECK:STDOUT: generic fn @F(@I.%T.loc11_13.1: type, @I.%Self.1: @I.%I.type (%I.type.2), %U.loc12_8.2: type) {
  107. // CHECK:STDOUT: %U.loc12_8.1: type = bind_symbolic_name U, 2 [symbolic = %U.loc12_8.1 (constants.%U)]
  108. // CHECK:STDOUT: %U.patt.loc12_8.1: type = symbolic_binding_pattern U, 2 [symbolic = %U.patt.loc12_8.1 (constants.%U.patt)]
  109. // CHECK:STDOUT:
  110. // CHECK:STDOUT: fn(%U.param_patt: type) -> @F.%U.loc12_8.1 (%U);
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: generic fn @G(%T.loc15_6.1: type) {
  114. // CHECK:STDOUT: %T.loc15_6.2: type = bind_symbolic_name T, 0 [symbolic = %T.loc15_6.2 (constants.%T)]
  115. // CHECK:STDOUT: %T.patt.loc15_6.2: type = symbolic_binding_pattern T, 0 [symbolic = %T.patt.loc15_6.2 (constants.%T.patt.2)]
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: !definition:
  118. // CHECK:STDOUT: %I.type.loc19_4.2: type = interface_type @I, @I(%T.loc15_6.2) [symbolic = %I.type.loc19_4.2 (constants.%I.type.2)]
  119. // CHECK:STDOUT: %F.type: type = fn_type @F, @I(%T.loc15_6.2) [symbolic = %F.type (constants.%F.type.1)]
  120. // CHECK:STDOUT: %.loc19_7.2: type = assoc_entity_type @G.%I.type.loc19_4.2 (%I.type.2), @G.%F.type (%F.type.1) [symbolic = %.loc19_7.2 (constants.%.2)]
  121. // CHECK:STDOUT: %.loc19_7.3: @G.%.loc19_7.2 (%.2) = assoc_entity element0, @I.%F.decl [symbolic = %.loc19_7.3 (constants.%.3)]
  122. // CHECK:STDOUT:
  123. // CHECK:STDOUT: fn(%T.param_patt: type) {
  124. // CHECK:STDOUT: !entry:
  125. // CHECK:STDOUT: %I.ref: %I.type.1 = name_ref I, file.%I.decl [template = constants.%I]
  126. // CHECK:STDOUT: %T.ref: type = name_ref T, %T.loc15_6.1 [symbolic = %T.loc15_6.2 (constants.%T)]
  127. // CHECK:STDOUT: %I.type.loc19_4.1: type = interface_type @I, @I(constants.%T) [symbolic = %I.type.loc19_4.2 (constants.%I.type.2)]
  128. // CHECK:STDOUT: %.loc19_7.1: @G.%.loc19_7.2 (%.2) = specific_constant @I.%.loc12_22.1, @I(constants.%T) [symbolic = %.loc19_7.3 (constants.%.3)]
  129. // CHECK:STDOUT: %F.ref: @G.%.loc19_7.2 (%.2) = name_ref F, %.loc19_7.1 [symbolic = %.loc19_7.3 (constants.%.3)]
  130. // CHECK:STDOUT: return
  131. // CHECK:STDOUT: }
  132. // CHECK:STDOUT: }
  133. // CHECK:STDOUT:
  134. // CHECK:STDOUT: fn @H() {
  135. // CHECK:STDOUT: !entry:
  136. // CHECK:STDOUT: %G.ref: %G.type = name_ref G, file.%G.decl [template = constants.%G]
  137. // CHECK:STDOUT: %.loc23_6: %.4 = struct_literal ()
  138. // CHECK:STDOUT: %.loc23_4: type = converted %.loc23_6, constants.%.4 [template = constants.%.4]
  139. // CHECK:STDOUT: %.loc23_3: <specific function> = specific_function %G.ref, @G(constants.%.4) [template = constants.%.5]
  140. // CHECK:STDOUT: %G.call: init %.1 = call %.loc23_3()
  141. // CHECK:STDOUT: return
  142. // CHECK:STDOUT: }
  143. // CHECK:STDOUT:
  144. // CHECK:STDOUT: specific @I(constants.%T) {
  145. // CHECK:STDOUT: %T.loc11_13.2 => constants.%T
  146. // CHECK:STDOUT: %T.patt.loc11_13.2 => constants.%T
  147. // CHECK:STDOUT:
  148. // CHECK:STDOUT: !definition:
  149. // CHECK:STDOUT: %I.type => constants.%I.type.2
  150. // CHECK:STDOUT: %Self.2 => constants.%Self
  151. // CHECK:STDOUT: %F.type => constants.%F.type.1
  152. // CHECK:STDOUT: %F => constants.%F
  153. // CHECK:STDOUT: %.loc12_22.2 => constants.%.2
  154. // CHECK:STDOUT: %.loc12_22.3 => constants.%.3
  155. // CHECK:STDOUT: }
  156. // CHECK:STDOUT:
  157. // CHECK:STDOUT: specific @F(constants.%T, constants.%Self, constants.%U) {
  158. // CHECK:STDOUT: %U.loc12_8.1 => constants.%U
  159. // CHECK:STDOUT: %U.patt.loc12_8.1 => constants.%U
  160. // CHECK:STDOUT: }
  161. // CHECK:STDOUT:
  162. // CHECK:STDOUT: specific @I(@I.%T.loc11_13.2) {
  163. // CHECK:STDOUT: %T.loc11_13.2 => constants.%T
  164. // CHECK:STDOUT: %T.patt.loc11_13.2 => constants.%T
  165. // CHECK:STDOUT: }
  166. // CHECK:STDOUT:
  167. // CHECK:STDOUT: specific @G(constants.%T) {
  168. // CHECK:STDOUT: %T.loc15_6.2 => constants.%T
  169. // CHECK:STDOUT: %T.patt.loc15_6.2 => constants.%T
  170. // CHECK:STDOUT: }
  171. // CHECK:STDOUT:
  172. // CHECK:STDOUT: specific @I(@G.%T.loc15_6.2) {
  173. // CHECK:STDOUT: %T.loc11_13.2 => constants.%T
  174. // CHECK:STDOUT: %T.patt.loc11_13.2 => constants.%T
  175. // CHECK:STDOUT: }
  176. // CHECK:STDOUT:
  177. // CHECK:STDOUT: specific @G(constants.%.4) {
  178. // CHECK:STDOUT: %T.loc15_6.2 => constants.%.4
  179. // CHECK:STDOUT: %T.patt.loc15_6.2 => constants.%.4
  180. // CHECK:STDOUT:
  181. // CHECK:STDOUT: !definition:
  182. // CHECK:STDOUT: %I.type.loc19_4.2 => constants.%I.type.3
  183. // CHECK:STDOUT: %F.type => constants.%F.type.2
  184. // CHECK:STDOUT: %.loc19_7.2 => constants.%.6
  185. // CHECK:STDOUT: %.loc19_7.3 => constants.%.7
  186. // CHECK:STDOUT: }
  187. // CHECK:STDOUT:
  188. // CHECK:STDOUT: specific @I(constants.%.4) {
  189. // CHECK:STDOUT: %T.loc11_13.2 => constants.%.4
  190. // CHECK:STDOUT: %T.patt.loc11_13.2 => constants.%.4
  191. // CHECK:STDOUT: }
  192. // CHECK:STDOUT: