deduce_nested_facet_value.carbon 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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/facet_types.carbon
  6. // TODO: Add ranges and switch to "--dump-sem-ir-ranges=only".
  7. // EXTRA-ARGS: --dump-sem-ir-ranges=if-present
  8. //
  9. // AUTOUPDATE
  10. // TIP: To test this file alone, run:
  11. // TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/function/generic/deduce_nested_facet_value.carbon
  12. // TIP: To dump output, run:
  13. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/function/generic/deduce_nested_facet_value.carbon
  14. // --- deduce_nested_facet_value.carbon
  15. library "[[@TEST_NAME]]";
  16. interface Y {}
  17. interface W {}
  18. // DD implements both Y and W.
  19. class DD {}
  20. impl DD as Y {}
  21. impl DD as W {}
  22. // CC requires D to implement Y.
  23. class CC(D:! Y) {}
  24. interface Z {}
  25. // The `D` interface provides `Y` but not `W`, so we need to see that the
  26. // parameter to `CC` is `DD` which provides `Y & W`, not just a FacetValue
  27. // abstractly providing `Y.
  28. impl forall [E:! Y & W] CC(E) as Z {}
  29. fn F() {
  30. (CC(DD)) as Z;
  31. }
  32. // CHECK:STDOUT: --- deduce_nested_facet_value.carbon
  33. // CHECK:STDOUT:
  34. // CHECK:STDOUT: constants {
  35. // CHECK:STDOUT: %Y.type: type = facet_type <@Y> [concrete]
  36. // CHECK:STDOUT: %Self.b29: %Y.type = bind_symbolic_name Self, 0 [symbolic]
  37. // CHECK:STDOUT: %W.type: type = facet_type <@W> [concrete]
  38. // CHECK:STDOUT: %Self.f12: %W.type = bind_symbolic_name Self, 0 [symbolic]
  39. // CHECK:STDOUT: %DD: type = class_type @DD [concrete]
  40. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete]
  41. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete]
  42. // CHECK:STDOUT: %Y.impl_witness: <witness> = impl_witness file.%Y.impl_witness_table [concrete]
  43. // CHECK:STDOUT: %W.impl_witness: <witness> = impl_witness file.%W.impl_witness_table [concrete]
  44. // CHECK:STDOUT: %D: %Y.type = bind_symbolic_name D, 0 [symbolic]
  45. // CHECK:STDOUT: %pattern_type.667: type = pattern_type %Y.type [concrete]
  46. // CHECK:STDOUT: %CC.type: type = generic_class_type @CC [concrete]
  47. // CHECK:STDOUT: %CC.generic: %CC.type = struct_value () [concrete]
  48. // CHECK:STDOUT: %CC.3ba: type = class_type @CC, @CC(%D) [symbolic]
  49. // CHECK:STDOUT: %Z.type: type = facet_type <@Z> [concrete]
  50. // CHECK:STDOUT: %Self.6e6: %Z.type = bind_symbolic_name Self, 0 [symbolic]
  51. // CHECK:STDOUT: %BitAndWith.type.f2e: type = generic_interface_type @BitAndWith [concrete]
  52. // CHECK:STDOUT: %BitAndWith.generic: %BitAndWith.type.f2e = struct_value () [concrete]
  53. // CHECK:STDOUT: %BitAndWith.type.e8c: type = facet_type <@BitAndWith, @BitAndWith(type)> [concrete]
  54. // CHECK:STDOUT: %Op.type.9a3: type = fn_type @Op.1, @BitAndWith(type) [concrete]
  55. // CHECK:STDOUT: %BitAndWith.impl_witness: <witness> = impl_witness imports.%BitAndWith.impl_witness_table [concrete]
  56. // CHECK:STDOUT: %BitAndWith.facet: %BitAndWith.type.e8c = facet_value type, (%BitAndWith.impl_witness) [concrete]
  57. // CHECK:STDOUT: %.2af: type = fn_type_with_self_type %Op.type.9a3, %BitAndWith.facet [concrete]
  58. // CHECK:STDOUT: %Op.type.1cc: type = fn_type @Op.2 [concrete]
  59. // CHECK:STDOUT: %Op.ff4: %Op.type.1cc = struct_value () [concrete]
  60. // CHECK:STDOUT: %Op.bound: <bound method> = bound_method %Y.type, %Op.ff4 [concrete]
  61. // CHECK:STDOUT: %facet_type: type = facet_type <@Y & @W> [concrete]
  62. // CHECK:STDOUT: %E: %facet_type = bind_symbolic_name E, 0 [symbolic]
  63. // CHECK:STDOUT: %pattern_type.122: type = pattern_type %facet_type [concrete]
  64. // CHECK:STDOUT: %Y.lookup_impl_witness: <witness> = lookup_impl_witness %E, @Y [symbolic]
  65. // CHECK:STDOUT: %E.as_type: type = facet_access_type %E [symbolic]
  66. // CHECK:STDOUT: %Y.facet.7ec: %Y.type = facet_value %E.as_type, (%Y.lookup_impl_witness) [symbolic]
  67. // CHECK:STDOUT: %CC.d02: type = class_type @CC, @CC(%Y.facet.7ec) [symbolic]
  68. // CHECK:STDOUT: %Z.impl_witness.741: <witness> = impl_witness file.%Z.impl_witness_table, @Z.impl(%E) [symbolic]
  69. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  70. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  71. // CHECK:STDOUT: %Y.facet.268: %Y.type = facet_value %DD, (%Y.impl_witness) [concrete]
  72. // CHECK:STDOUT: %CC.7bf: type = class_type @CC, @CC(%Y.facet.268) [concrete]
  73. // CHECK:STDOUT: %facet_value: %facet_type = facet_value %DD, (%Y.impl_witness, %W.impl_witness) [concrete]
  74. // CHECK:STDOUT: %Z.impl_witness.3b0: <witness> = impl_witness file.%Z.impl_witness_table, @Z.impl(%facet_value) [concrete]
  75. // CHECK:STDOUT: %Z.facet: %Z.type = facet_value %CC.7bf, (%Z.impl_witness.3b0) [concrete]
  76. // CHECK:STDOUT: }
  77. // CHECK:STDOUT:
  78. // CHECK:STDOUT: imports {
  79. // CHECK:STDOUT: %Core: <namespace> = namespace file.%Core.import, [concrete] {
  80. // CHECK:STDOUT: .BitAndWith = %Core.BitAndWith
  81. // CHECK:STDOUT: import Core//prelude
  82. // CHECK:STDOUT: import Core//prelude/...
  83. // CHECK:STDOUT: }
  84. // CHECK:STDOUT: %Core.BitAndWith: %BitAndWith.type.f2e = import_ref Core//prelude, BitAndWith, loaded [concrete = constants.%BitAndWith.generic]
  85. // CHECK:STDOUT: %Core.import_ref.012: %Op.type.1cc = import_ref Core//prelude, loc13_42, loaded [concrete = constants.%Op.ff4]
  86. // CHECK:STDOUT: %BitAndWith.impl_witness_table = impl_witness_table (%Core.import_ref.012), @BitAndWith.impl [concrete]
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: file {
  90. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  91. // CHECK:STDOUT: .Core = imports.%Core
  92. // CHECK:STDOUT: .Y = %Y.decl
  93. // CHECK:STDOUT: .W = %W.decl
  94. // CHECK:STDOUT: .DD = %DD.decl
  95. // CHECK:STDOUT: .CC = %CC.decl
  96. // CHECK:STDOUT: .Z = %Z.decl
  97. // CHECK:STDOUT: .F = %F.decl
  98. // CHECK:STDOUT: }
  99. // CHECK:STDOUT: %Core.import = import Core
  100. // CHECK:STDOUT: %Y.decl: type = interface_decl @Y [concrete = constants.%Y.type] {} {}
  101. // CHECK:STDOUT: %W.decl: type = interface_decl @W [concrete = constants.%W.type] {} {}
  102. // CHECK:STDOUT: %DD.decl: type = class_decl @DD [concrete = constants.%DD] {} {}
  103. // CHECK:STDOUT: impl_decl @Y.impl [concrete] {} {
  104. // CHECK:STDOUT: %DD.ref: type = name_ref DD, file.%DD.decl [concrete = constants.%DD]
  105. // CHECK:STDOUT: %Y.ref: type = name_ref Y, file.%Y.decl [concrete = constants.%Y.type]
  106. // CHECK:STDOUT: }
  107. // CHECK:STDOUT: %Y.impl_witness_table = impl_witness_table (), @Y.impl [concrete]
  108. // CHECK:STDOUT: %Y.impl_witness: <witness> = impl_witness %Y.impl_witness_table [concrete = constants.%Y.impl_witness]
  109. // CHECK:STDOUT: impl_decl @W.impl [concrete] {} {
  110. // CHECK:STDOUT: %DD.ref: type = name_ref DD, file.%DD.decl [concrete = constants.%DD]
  111. // CHECK:STDOUT: %W.ref: type = name_ref W, file.%W.decl [concrete = constants.%W.type]
  112. // CHECK:STDOUT: }
  113. // CHECK:STDOUT: %W.impl_witness_table = impl_witness_table (), @W.impl [concrete]
  114. // CHECK:STDOUT: %W.impl_witness: <witness> = impl_witness %W.impl_witness_table [concrete = constants.%W.impl_witness]
  115. // CHECK:STDOUT: %CC.decl: %CC.type = class_decl @CC [concrete = constants.%CC.generic] {
  116. // CHECK:STDOUT: %D.patt: %pattern_type.667 = symbolic_binding_pattern D, 0 [concrete]
  117. // CHECK:STDOUT: } {
  118. // CHECK:STDOUT: %Y.ref: type = name_ref Y, file.%Y.decl [concrete = constants.%Y.type]
  119. // CHECK:STDOUT: %D.loc12_10.1: %Y.type = bind_symbolic_name D, 0 [symbolic = %D.loc12_10.2 (constants.%D)]
  120. // CHECK:STDOUT: }
  121. // CHECK:STDOUT: %Z.decl: type = interface_decl @Z [concrete = constants.%Z.type] {} {}
  122. // CHECK:STDOUT: impl_decl @Z.impl [concrete] {
  123. // CHECK:STDOUT: %E.patt: %pattern_type.122 = symbolic_binding_pattern E, 0 [concrete]
  124. // CHECK:STDOUT: } {
  125. // CHECK:STDOUT: %CC.ref: %CC.type = name_ref CC, file.%CC.decl [concrete = constants.%CC.generic]
  126. // CHECK:STDOUT: %E.ref: %facet_type = name_ref E, %E.loc19_14.1 [symbolic = %E.loc19_14.2 (constants.%E)]
  127. // CHECK:STDOUT: %E.as_type.loc19_29.1: type = facet_access_type constants.%E [symbolic = %E.as_type.loc19_29.2 (constants.%E.as_type)]
  128. // CHECK:STDOUT: %Y.facet.loc19_29.1: %Y.type = facet_value %E.as_type.loc19_29.1, (constants.%Y.lookup_impl_witness) [symbolic = %Y.facet.loc19_29.2 (constants.%Y.facet.7ec)]
  129. // CHECK:STDOUT: %.loc19_29: %Y.type = converted %E.ref, %Y.facet.loc19_29.1 [symbolic = %Y.facet.loc19_29.2 (constants.%Y.facet.7ec)]
  130. // CHECK:STDOUT: %CC.loc19_29.1: type = class_type @CC, @CC(constants.%Y.facet.7ec) [symbolic = %CC.loc19_29.2 (constants.%CC.d02)]
  131. // CHECK:STDOUT: %Z.ref: type = name_ref Z, file.%Z.decl [concrete = constants.%Z.type]
  132. // CHECK:STDOUT: %.loc19_20.1: type = splice_block %.loc19_20.3 [concrete = constants.%facet_type] {
  133. // CHECK:STDOUT: %Y.ref: type = name_ref Y, file.%Y.decl [concrete = constants.%Y.type]
  134. // CHECK:STDOUT: %W.ref: type = name_ref W, file.%W.decl [concrete = constants.%W.type]
  135. // CHECK:STDOUT: %impl.elem0: %.2af = impl_witness_access constants.%BitAndWith.impl_witness, element0 [concrete = constants.%Op.ff4]
  136. // CHECK:STDOUT: %bound_method: <bound method> = bound_method %Y.ref, %impl.elem0 [concrete = constants.%Op.bound]
  137. // CHECK:STDOUT: %type.and: init type = call %bound_method(%Y.ref, %W.ref) [concrete = constants.%facet_type]
  138. // CHECK:STDOUT: %.loc19_20.2: type = value_of_initializer %type.and [concrete = constants.%facet_type]
  139. // CHECK:STDOUT: %.loc19_20.3: type = converted %type.and, %.loc19_20.2 [concrete = constants.%facet_type]
  140. // CHECK:STDOUT: }
  141. // CHECK:STDOUT: %E.loc19_14.1: %facet_type = bind_symbolic_name E, 0 [symbolic = %E.loc19_14.2 (constants.%E)]
  142. // CHECK:STDOUT: }
  143. // CHECK:STDOUT: %Z.impl_witness_table = impl_witness_table (), @Z.impl [concrete]
  144. // CHECK:STDOUT: %Z.impl_witness: <witness> = impl_witness %Z.impl_witness_table, @Z.impl(constants.%E) [symbolic = @Z.impl.%Z.impl_witness (constants.%Z.impl_witness.741)]
  145. // CHECK:STDOUT: %F.decl: %F.type = fn_decl @F [concrete = constants.%F] {} {}
  146. // CHECK:STDOUT: }
  147. // CHECK:STDOUT:
  148. // CHECK:STDOUT: interface @Y {
  149. // CHECK:STDOUT: %Self: %Y.type = bind_symbolic_name Self, 0 [symbolic = constants.%Self.b29]
  150. // CHECK:STDOUT:
  151. // CHECK:STDOUT: !members:
  152. // CHECK:STDOUT: .Self = %Self
  153. // CHECK:STDOUT: witness = ()
  154. // CHECK:STDOUT: }
  155. // CHECK:STDOUT:
  156. // CHECK:STDOUT: interface @W {
  157. // CHECK:STDOUT: %Self: %W.type = bind_symbolic_name Self, 0 [symbolic = constants.%Self.f12]
  158. // CHECK:STDOUT:
  159. // CHECK:STDOUT: !members:
  160. // CHECK:STDOUT: .Self = %Self
  161. // CHECK:STDOUT: witness = ()
  162. // CHECK:STDOUT: }
  163. // CHECK:STDOUT:
  164. // CHECK:STDOUT: interface @Z {
  165. // CHECK:STDOUT: %Self: %Z.type = bind_symbolic_name Self, 0 [symbolic = constants.%Self.6e6]
  166. // CHECK:STDOUT:
  167. // CHECK:STDOUT: !members:
  168. // CHECK:STDOUT: .Self = %Self
  169. // CHECK:STDOUT: witness = ()
  170. // CHECK:STDOUT: }
  171. // CHECK:STDOUT:
  172. // CHECK:STDOUT: impl @Y.impl: %DD.ref as %Y.ref {
  173. // CHECK:STDOUT: !members:
  174. // CHECK:STDOUT: witness = file.%Y.impl_witness
  175. // CHECK:STDOUT: }
  176. // CHECK:STDOUT:
  177. // CHECK:STDOUT: impl @W.impl: %DD.ref as %W.ref {
  178. // CHECK:STDOUT: !members:
  179. // CHECK:STDOUT: witness = file.%W.impl_witness
  180. // CHECK:STDOUT: }
  181. // CHECK:STDOUT:
  182. // CHECK:STDOUT: generic impl @Z.impl(%E.loc19_14.1: %facet_type) {
  183. // CHECK:STDOUT: %E.loc19_14.2: %facet_type = bind_symbolic_name E, 0 [symbolic = %E.loc19_14.2 (constants.%E)]
  184. // CHECK:STDOUT: %Y.lookup_impl_witness: <witness> = lookup_impl_witness %E.loc19_14.2, @Y [symbolic = %Y.lookup_impl_witness (constants.%Y.lookup_impl_witness)]
  185. // CHECK:STDOUT: %E.as_type.loc19_29.2: type = facet_access_type %E.loc19_14.2 [symbolic = %E.as_type.loc19_29.2 (constants.%E.as_type)]
  186. // CHECK:STDOUT: %Y.facet.loc19_29.2: %Y.type = facet_value %E.as_type.loc19_29.2, (%Y.lookup_impl_witness) [symbolic = %Y.facet.loc19_29.2 (constants.%Y.facet.7ec)]
  187. // CHECK:STDOUT: %CC.loc19_29.2: type = class_type @CC, @CC(%Y.facet.loc19_29.2) [symbolic = %CC.loc19_29.2 (constants.%CC.d02)]
  188. // CHECK:STDOUT: %Z.impl_witness: <witness> = impl_witness file.%Z.impl_witness_table, @Z.impl(%E.loc19_14.2) [symbolic = %Z.impl_witness (constants.%Z.impl_witness.741)]
  189. // CHECK:STDOUT:
  190. // CHECK:STDOUT: !definition:
  191. // CHECK:STDOUT:
  192. // CHECK:STDOUT: impl: %CC.loc19_29.1 as %Z.ref {
  193. // CHECK:STDOUT: !members:
  194. // CHECK:STDOUT: witness = file.%Z.impl_witness
  195. // CHECK:STDOUT: }
  196. // CHECK:STDOUT: }
  197. // CHECK:STDOUT:
  198. // CHECK:STDOUT: class @DD {
  199. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete = constants.%empty_struct_type]
  200. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete = constants.%complete_type]
  201. // CHECK:STDOUT: complete_type_witness = %complete_type
  202. // CHECK:STDOUT:
  203. // CHECK:STDOUT: !members:
  204. // CHECK:STDOUT: .Self = constants.%DD
  205. // CHECK:STDOUT: }
  206. // CHECK:STDOUT:
  207. // CHECK:STDOUT: generic class @CC(%D.loc12_10.1: %Y.type) {
  208. // CHECK:STDOUT: %D.loc12_10.2: %Y.type = bind_symbolic_name D, 0 [symbolic = %D.loc12_10.2 (constants.%D)]
  209. // CHECK:STDOUT:
  210. // CHECK:STDOUT: !definition:
  211. // CHECK:STDOUT:
  212. // CHECK:STDOUT: class {
  213. // CHECK:STDOUT: %empty_struct_type: type = struct_type {} [concrete = constants.%empty_struct_type]
  214. // CHECK:STDOUT: %complete_type: <witness> = complete_type_witness %empty_struct_type [concrete = constants.%complete_type]
  215. // CHECK:STDOUT: complete_type_witness = %complete_type
  216. // CHECK:STDOUT:
  217. // CHECK:STDOUT: !members:
  218. // CHECK:STDOUT: .Self = constants.%CC.3ba
  219. // CHECK:STDOUT: }
  220. // CHECK:STDOUT: }
  221. // CHECK:STDOUT:
  222. // CHECK:STDOUT: fn @F() {
  223. // CHECK:STDOUT: !entry:
  224. // CHECK:STDOUT: %CC.ref: %CC.type = name_ref CC, file.%CC.decl [concrete = constants.%CC.generic]
  225. // CHECK:STDOUT: %DD.ref: type = name_ref DD, file.%DD.decl [concrete = constants.%DD]
  226. // CHECK:STDOUT: %Y.facet: %Y.type = facet_value constants.%DD, (constants.%Y.impl_witness) [concrete = constants.%Y.facet.268]
  227. // CHECK:STDOUT: %.loc22_9: %Y.type = converted %DD.ref, %Y.facet [concrete = constants.%Y.facet.268]
  228. // CHECK:STDOUT: %CC: type = class_type @CC, @CC(constants.%Y.facet.268) [concrete = constants.%CC.7bf]
  229. // CHECK:STDOUT: %Z.ref: type = name_ref Z, file.%Z.decl [concrete = constants.%Z.type]
  230. // CHECK:STDOUT: %facet_value: %facet_type = facet_value constants.%DD, (constants.%Y.impl_witness, constants.%W.impl_witness) [concrete = constants.%facet_value]
  231. // CHECK:STDOUT: %.loc22_12.1: %facet_type = converted constants.%DD, %facet_value [concrete = constants.%facet_value]
  232. // CHECK:STDOUT: %Z.facet: %Z.type = facet_value constants.%CC.7bf, (constants.%Z.impl_witness.3b0) [concrete = constants.%Z.facet]
  233. // CHECK:STDOUT: %.loc22_12.2: %Z.type = converted %CC, %Z.facet [concrete = constants.%Z.facet]
  234. // CHECK:STDOUT: return
  235. // CHECK:STDOUT: }
  236. // CHECK:STDOUT:
  237. // CHECK:STDOUT: specific @CC(constants.%D) {
  238. // CHECK:STDOUT: %D.loc12_10.2 => constants.%D
  239. // CHECK:STDOUT: }
  240. // CHECK:STDOUT:
  241. // CHECK:STDOUT: specific @CC(constants.%Y.facet.7ec) {
  242. // CHECK:STDOUT: %D.loc12_10.2 => constants.%Y.facet.7ec
  243. // CHECK:STDOUT: }
  244. // CHECK:STDOUT:
  245. // CHECK:STDOUT: specific @Z.impl(constants.%E) {
  246. // CHECK:STDOUT: %E.loc19_14.2 => constants.%E
  247. // CHECK:STDOUT: %Y.lookup_impl_witness => constants.%Y.lookup_impl_witness
  248. // CHECK:STDOUT: %E.as_type.loc19_29.2 => constants.%E.as_type
  249. // CHECK:STDOUT: %Y.facet.loc19_29.2 => constants.%Y.facet.7ec
  250. // CHECK:STDOUT: %CC.loc19_29.2 => constants.%CC.d02
  251. // CHECK:STDOUT: %Z.impl_witness => constants.%Z.impl_witness.741
  252. // CHECK:STDOUT: }
  253. // CHECK:STDOUT:
  254. // CHECK:STDOUT: specific @CC(constants.%Y.facet.268) {
  255. // CHECK:STDOUT: %D.loc12_10.2 => constants.%Y.facet.268
  256. // CHECK:STDOUT: }
  257. // CHECK:STDOUT:
  258. // CHECK:STDOUT: specific @Z.impl(constants.%facet_value) {
  259. // CHECK:STDOUT: %E.loc19_14.2 => constants.%facet_value
  260. // CHECK:STDOUT: %Y.lookup_impl_witness => constants.%Y.impl_witness
  261. // CHECK:STDOUT: %E.as_type.loc19_29.2 => constants.%DD
  262. // CHECK:STDOUT: %Y.facet.loc19_29.2 => constants.%Y.facet.268
  263. // CHECK:STDOUT: %CC.loc19_29.2 => constants.%CC.7bf
  264. // CHECK:STDOUT: %Z.impl_witness => constants.%Z.impl_witness.3b0
  265. // CHECK:STDOUT:
  266. // CHECK:STDOUT: !definition:
  267. // CHECK:STDOUT: }
  268. // CHECK:STDOUT: