constraints.carbon 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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/where_expr/constraints.carbon
  8. // TIP: To dump output, run:
  9. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/where_expr/constraints.carbon
  10. // --- state_constraints.carbon
  11. library "[[@TEST_NAME]]";
  12. interface J {}
  13. interface I {
  14. let Member:! type;
  15. let Second:! J;
  16. }
  17. //@dump-sem-ir-begin
  18. fn EqualEqual(U:! I where .Self == ());
  19. fn Impls(V:! J where .Self impls I);
  20. fn And(W:! I where .Self impls J and .Member == ());
  21. //@dump-sem-ir-end
  22. // --- associated_type_impls.carbon
  23. library "[[@TEST_NAME]]";
  24. interface L {}
  25. interface M {}
  26. interface K {
  27. let Associated:! L;
  28. }
  29. //@dump-sem-ir-begin
  30. fn AssociatedTypeImpls(W:! K where .Associated impls M);
  31. //@dump-sem-ir-end
  32. // --- fail_left_of_impls_non_type.carbon
  33. library "[[@TEST_NAME]]";
  34. // CHECK:STDERR: fail_left_of_impls_non_type.carbon:[[@LINE+7]]:32: error: cannot implicitly convert non-type value of type `Core.IntLiteral` to `type` [ConversionFailureNonTypeToFacet]
  35. // CHECK:STDERR: fn NonTypeImpls(U:! type where 7 impls type);
  36. // CHECK:STDERR: ^
  37. // CHECK:STDERR: fail_left_of_impls_non_type.carbon:[[@LINE+4]]:32: note: type `Core.IntLiteral` does not implement interface `Core.ImplicitAs(type)` [MissingImplInMemberAccessNote]
  38. // CHECK:STDERR: fn NonTypeImpls(U:! type where 7 impls type);
  39. // CHECK:STDERR: ^
  40. // CHECK:STDERR:
  41. fn NonTypeImpls(U:! type where 7 impls type);
  42. // --- fail_right_of_impls_non_type.carbon
  43. library "[[@TEST_NAME]]";
  44. // CHECK:STDERR: fail_right_of_impls_non_type.carbon:[[@LINE+7]]:44: error: cannot implicitly convert non-type value of type `Core.IntLiteral` to `type` [ConversionFailureNonTypeToFacet]
  45. // CHECK:STDERR: fn ImplsNonType(U:! type where .Self impls 7);
  46. // CHECK:STDERR: ^
  47. // CHECK:STDERR: fail_right_of_impls_non_type.carbon:[[@LINE+4]]:44: note: type `Core.IntLiteral` does not implement interface `Core.ImplicitAs(type)` [MissingImplInMemberAccessNote]
  48. // CHECK:STDERR: fn ImplsNonType(U:! type where .Self impls 7);
  49. // CHECK:STDERR: ^
  50. // CHECK:STDERR:
  51. fn ImplsNonType(U:! type where .Self impls 7);
  52. // --- fail_right_of_impls_non_facet_type.carbon
  53. library "[[@TEST_NAME]]";
  54. // CHECK:STDERR: fail_right_of_impls_non_facet_type.carbon:[[@LINE+4]]:51: error: right argument of `impls` requirement must be a facet type [ImplsOnNonFacetType]
  55. // CHECK:STDERR: fn ImplsOfNonFacetType(U:! type where .Self impls i32);
  56. // CHECK:STDERR: ^~~
  57. // CHECK:STDERR:
  58. fn ImplsOfNonFacetType(U:! type where .Self impls i32);
  59. // --- fail_enforce_impls_constraint.carbon
  60. library "[[@TEST_NAME]]";
  61. import library "state_constraints";
  62. // C implements J but not I.
  63. class C {}
  64. impl C as J {}
  65. fn DoesNotImplI() {
  66. // CHECK:STDERR: fail_enforce_impls_constraint.carbon:[[@LINE+8]]:3: error: cannot convert type `C` into type implementing `J where .Self impls I` [ConversionFailureTypeToFacet]
  67. // CHECK:STDERR: Impls(C);
  68. // CHECK:STDERR: ^~~~~~~~
  69. // CHECK:STDERR: fail_enforce_impls_constraint.carbon:[[@LINE-10]]:1: in import [InImport]
  70. // CHECK:STDERR: state_constraints.carbon:14:10: note: initializing generic parameter `V` declared here [InitializingGenericParam]
  71. // CHECK:STDERR: fn Impls(V:! J where .Self impls I);
  72. // CHECK:STDERR: ^
  73. // CHECK:STDERR:
  74. Impls(C);
  75. }
  76. // --- fail_todo_enforce_equality_constraint.carbon
  77. library "[[@TEST_NAME]]";
  78. import library "state_constraints";
  79. class C {}
  80. impl C as J {}
  81. fn EmptyStruct(Y:! J where .Self == {});
  82. // TODO: Should report that `C` does not meeet the `==` constraint. Right
  83. // now there is no support for `==` constraints so it rejects it as an
  84. // "other currently unsupported constraint."
  85. fn NotEmptyStruct() {
  86. // CHECK:STDERR: fail_todo_enforce_equality_constraint.carbon:[[@LINE+7]]:3: error: cannot convert type `C` into type implementing `J where...` [ConversionFailureTypeToFacet]
  87. // CHECK:STDERR: EmptyStruct(C);
  88. // CHECK:STDERR: ^~~~~~~~~~~~~~
  89. // CHECK:STDERR: fail_todo_enforce_equality_constraint.carbon:[[@LINE-9]]:16: note: initializing generic parameter `Y` declared here [InitializingGenericParam]
  90. // CHECK:STDERR: fn EmptyStruct(Y:! J where .Self == {});
  91. // CHECK:STDERR: ^
  92. // CHECK:STDERR:
  93. EmptyStruct(C);
  94. }
  95. // CHECK:STDOUT: --- state_constraints.carbon
  96. // CHECK:STDOUT:
  97. // CHECK:STDOUT: constants {
  98. // CHECK:STDOUT: %J.type: type = facet_type <@J> [concrete]
  99. // CHECK:STDOUT: %I.type: type = facet_type <@I> [concrete]
  100. // CHECK:STDOUT: %I.assoc_type: type = assoc_entity_type @I [concrete]
  101. // CHECK:STDOUT: %assoc0: %I.assoc_type = assoc_entity element0, @I.%Member [concrete]
  102. // CHECK:STDOUT: %.Self.258: %I.type = bind_symbolic_name .Self [symbolic_self]
  103. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  104. // CHECK:STDOUT: %I_where.type.cad: type = facet_type <@I where TODO> [concrete]
  105. // CHECK:STDOUT: %U: %I_where.type.cad = bind_symbolic_name U, 0 [symbolic]
  106. // CHECK:STDOUT: %pattern_type.f03: type = pattern_type %I_where.type.cad [concrete]
  107. // CHECK:STDOUT: %EqualEqual.type: type = fn_type @EqualEqual [concrete]
  108. // CHECK:STDOUT: %EqualEqual: %EqualEqual.type = struct_value () [concrete]
  109. // CHECK:STDOUT: %.Self.968: %J.type = bind_symbolic_name .Self [symbolic_self]
  110. // CHECK:STDOUT: %.Self.as_type.78d: type = facet_access_type %.Self.968 [symbolic_self]
  111. // CHECK:STDOUT: %J_where.type: type = facet_type <@J where .Self impls @I> [concrete]
  112. // CHECK:STDOUT: %V: %J_where.type = bind_symbolic_name V, 0 [symbolic]
  113. // CHECK:STDOUT: %pattern_type.d2b: type = pattern_type %J_where.type [concrete]
  114. // CHECK:STDOUT: %Impls.type: type = fn_type @Impls [concrete]
  115. // CHECK:STDOUT: %Impls: %Impls.type = struct_value () [concrete]
  116. // CHECK:STDOUT: %.Self.as_type.541: type = facet_access_type %.Self.258 [symbolic_self]
  117. // CHECK:STDOUT: %I.lookup_impl_witness: <witness> = lookup_impl_witness %.Self.258, @I [symbolic_self]
  118. // CHECK:STDOUT: %impl.elem0: type = impl_witness_access %I.lookup_impl_witness, element0 [symbolic_self]
  119. // CHECK:STDOUT: %I_where.type.427: type = facet_type <@I where .Self impls @J and TODO> [concrete]
  120. // CHECK:STDOUT: %W: %I_where.type.427 = bind_symbolic_name W, 0 [symbolic]
  121. // CHECK:STDOUT: %pattern_type.127: type = pattern_type %I_where.type.427 [concrete]
  122. // CHECK:STDOUT: %And.type: type = fn_type @And [concrete]
  123. // CHECK:STDOUT: %And: %And.type = struct_value () [concrete]
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT:
  126. // CHECK:STDOUT: imports {
  127. // CHECK:STDOUT: }
  128. // CHECK:STDOUT:
  129. // CHECK:STDOUT: file {
  130. // CHECK:STDOUT: %EqualEqual.decl: %EqualEqual.type = fn_decl @EqualEqual [concrete = constants.%EqualEqual] {
  131. // CHECK:STDOUT: %U.patt: %pattern_type.f03 = symbolic_binding_pattern U, 0 [concrete]
  132. // CHECK:STDOUT: } {
  133. // CHECK:STDOUT: %.loc12_21.1: type = splice_block %.loc12_21.2 [concrete = constants.%I_where.type.cad] {
  134. // CHECK:STDOUT: %I.ref: type = name_ref I, file.%I.decl [concrete = constants.%I.type]
  135. // CHECK:STDOUT: <elided>
  136. // CHECK:STDOUT: %.Self.ref: %I.type = name_ref .Self, %.Self [symbolic_self = constants.%.Self.258]
  137. // CHECK:STDOUT: %.loc12_37: %empty_tuple.type = tuple_literal ()
  138. // CHECK:STDOUT: %.loc12_21.2: type = where_expr %.Self [concrete = constants.%I_where.type.cad] {
  139. // CHECK:STDOUT: requirement_equivalent %.Self.ref, %.loc12_37
  140. // CHECK:STDOUT: }
  141. // CHECK:STDOUT: }
  142. // CHECK:STDOUT: %U.loc12_15.1: %I_where.type.cad = bind_symbolic_name U, 0 [symbolic = %U.loc12_15.2 (constants.%U)]
  143. // CHECK:STDOUT: }
  144. // CHECK:STDOUT: %Impls.decl: %Impls.type = fn_decl @Impls [concrete = constants.%Impls] {
  145. // CHECK:STDOUT: %V.patt: %pattern_type.d2b = symbolic_binding_pattern V, 0 [concrete]
  146. // CHECK:STDOUT: } {
  147. // CHECK:STDOUT: %.loc14_16.1: type = splice_block %.loc14_16.2 [concrete = constants.%J_where.type] {
  148. // CHECK:STDOUT: %J.ref: type = name_ref J, file.%J.decl [concrete = constants.%J.type]
  149. // CHECK:STDOUT: <elided>
  150. // CHECK:STDOUT: %.Self.ref: %J.type = name_ref .Self, %.Self [symbolic_self = constants.%.Self.968]
  151. // CHECK:STDOUT: %I.ref: type = name_ref I, file.%I.decl [concrete = constants.%I.type]
  152. // CHECK:STDOUT: %.Self.as_type: type = facet_access_type %.Self.ref [symbolic_self = constants.%.Self.as_type.78d]
  153. // CHECK:STDOUT: %.loc14_22: type = converted %.Self.ref, %.Self.as_type [symbolic_self = constants.%.Self.as_type.78d]
  154. // CHECK:STDOUT: %.loc14_16.2: type = where_expr %.Self [concrete = constants.%J_where.type] {
  155. // CHECK:STDOUT: requirement_impls %.loc14_22, %I.ref
  156. // CHECK:STDOUT: }
  157. // CHECK:STDOUT: }
  158. // CHECK:STDOUT: %V.loc14_10.1: %J_where.type = bind_symbolic_name V, 0 [symbolic = %V.loc14_10.2 (constants.%V)]
  159. // CHECK:STDOUT: }
  160. // CHECK:STDOUT: %And.decl: %And.type = fn_decl @And [concrete = constants.%And] {
  161. // CHECK:STDOUT: %W.patt: %pattern_type.127 = symbolic_binding_pattern W, 0 [concrete]
  162. // CHECK:STDOUT: } {
  163. // CHECK:STDOUT: %.loc16_14.1: type = splice_block %.loc16_14.2 [concrete = constants.%I_where.type.427] {
  164. // CHECK:STDOUT: %I.ref: type = name_ref I, file.%I.decl [concrete = constants.%I.type]
  165. // CHECK:STDOUT: <elided>
  166. // CHECK:STDOUT: %.Self.ref.loc16_20: %I.type = name_ref .Self, %.Self [symbolic_self = constants.%.Self.258]
  167. // CHECK:STDOUT: %J.ref: type = name_ref J, file.%J.decl [concrete = constants.%J.type]
  168. // CHECK:STDOUT: %.Self.as_type.loc16_20: type = facet_access_type %.Self.ref.loc16_20 [symbolic_self = constants.%.Self.as_type.541]
  169. // CHECK:STDOUT: %.loc16_20: type = converted %.Self.ref.loc16_20, %.Self.as_type.loc16_20 [symbolic_self = constants.%.Self.as_type.541]
  170. // CHECK:STDOUT: %.Self.ref.loc16_38: %I.type = name_ref .Self, %.Self [symbolic_self = constants.%.Self.258]
  171. // CHECK:STDOUT: %Member.ref: %I.assoc_type = name_ref Member, @Member.%assoc0 [concrete = constants.%assoc0]
  172. // CHECK:STDOUT: %.Self.as_type.loc16_38: type = facet_access_type %.Self.ref.loc16_38 [symbolic_self = constants.%.Self.as_type.541]
  173. // CHECK:STDOUT: %.loc16_38: type = converted %.Self.ref.loc16_38, %.Self.as_type.loc16_38 [symbolic_self = constants.%.Self.as_type.541]
  174. // CHECK:STDOUT: %impl.elem0: type = impl_witness_access constants.%I.lookup_impl_witness, element0 [symbolic_self = constants.%impl.elem0]
  175. // CHECK:STDOUT: %.loc16_50: %empty_tuple.type = tuple_literal ()
  176. // CHECK:STDOUT: %.loc16_14.2: type = where_expr %.Self [concrete = constants.%I_where.type.427] {
  177. // CHECK:STDOUT: requirement_impls %.loc16_20, %J.ref
  178. // CHECK:STDOUT: requirement_equivalent %impl.elem0, %.loc16_50
  179. // CHECK:STDOUT: }
  180. // CHECK:STDOUT: }
  181. // CHECK:STDOUT: %W.loc16_8.1: %I_where.type.427 = bind_symbolic_name W, 0 [symbolic = %W.loc16_8.2 (constants.%W)]
  182. // CHECK:STDOUT: }
  183. // CHECK:STDOUT: }
  184. // CHECK:STDOUT:
  185. // CHECK:STDOUT: generic fn @EqualEqual(%U.loc12_15.1: %I_where.type.cad) {
  186. // CHECK:STDOUT: %U.loc12_15.2: %I_where.type.cad = bind_symbolic_name U, 0 [symbolic = %U.loc12_15.2 (constants.%U)]
  187. // CHECK:STDOUT:
  188. // CHECK:STDOUT: fn();
  189. // CHECK:STDOUT: }
  190. // CHECK:STDOUT:
  191. // CHECK:STDOUT: generic fn @Impls(%V.loc14_10.1: %J_where.type) {
  192. // CHECK:STDOUT: %V.loc14_10.2: %J_where.type = bind_symbolic_name V, 0 [symbolic = %V.loc14_10.2 (constants.%V)]
  193. // CHECK:STDOUT:
  194. // CHECK:STDOUT: fn();
  195. // CHECK:STDOUT: }
  196. // CHECK:STDOUT:
  197. // CHECK:STDOUT: generic fn @And(%W.loc16_8.1: %I_where.type.427) {
  198. // CHECK:STDOUT: %W.loc16_8.2: %I_where.type.427 = bind_symbolic_name W, 0 [symbolic = %W.loc16_8.2 (constants.%W)]
  199. // CHECK:STDOUT:
  200. // CHECK:STDOUT: fn();
  201. // CHECK:STDOUT: }
  202. // CHECK:STDOUT:
  203. // CHECK:STDOUT: specific @EqualEqual(constants.%U) {
  204. // CHECK:STDOUT: %U.loc12_15.2 => constants.%U
  205. // CHECK:STDOUT: }
  206. // CHECK:STDOUT:
  207. // CHECK:STDOUT: specific @Impls(constants.%V) {
  208. // CHECK:STDOUT: %V.loc14_10.2 => constants.%V
  209. // CHECK:STDOUT: }
  210. // CHECK:STDOUT:
  211. // CHECK:STDOUT: specific @And(constants.%W) {
  212. // CHECK:STDOUT: %W.loc16_8.2 => constants.%W
  213. // CHECK:STDOUT: }
  214. // CHECK:STDOUT:
  215. // CHECK:STDOUT: --- associated_type_impls.carbon
  216. // CHECK:STDOUT:
  217. // CHECK:STDOUT: constants {
  218. // CHECK:STDOUT: %L.type: type = facet_type <@L> [concrete]
  219. // CHECK:STDOUT: %M.type: type = facet_type <@M> [concrete]
  220. // CHECK:STDOUT: %K.type: type = facet_type <@K> [concrete]
  221. // CHECK:STDOUT: %K.assoc_type: type = assoc_entity_type @K [concrete]
  222. // CHECK:STDOUT: %assoc0: %K.assoc_type = assoc_entity element0, @K.%Associated [concrete]
  223. // CHECK:STDOUT: %.Self: %K.type = bind_symbolic_name .Self [symbolic_self]
  224. // CHECK:STDOUT: %.Self.as_type: type = facet_access_type %.Self [symbolic_self]
  225. // CHECK:STDOUT: %K.lookup_impl_witness: <witness> = lookup_impl_witness %.Self, @K [symbolic_self]
  226. // CHECK:STDOUT: %impl.elem0: %L.type = impl_witness_access %K.lookup_impl_witness, element0 [symbolic_self]
  227. // CHECK:STDOUT: %as_type: type = facet_access_type %impl.elem0 [symbolic_self]
  228. // CHECK:STDOUT: %K_where.type: type = facet_type <@K where TODO> [concrete]
  229. // CHECK:STDOUT: %W: %K_where.type = bind_symbolic_name W, 0 [symbolic]
  230. // CHECK:STDOUT: %pattern_type: type = pattern_type %K_where.type [concrete]
  231. // CHECK:STDOUT: %AssociatedTypeImpls.type: type = fn_type @AssociatedTypeImpls [concrete]
  232. // CHECK:STDOUT: %AssociatedTypeImpls: %AssociatedTypeImpls.type = struct_value () [concrete]
  233. // CHECK:STDOUT: }
  234. // CHECK:STDOUT:
  235. // CHECK:STDOUT: imports {
  236. // CHECK:STDOUT: }
  237. // CHECK:STDOUT:
  238. // CHECK:STDOUT: file {
  239. // CHECK:STDOUT: %AssociatedTypeImpls.decl: %AssociatedTypeImpls.type = fn_decl @AssociatedTypeImpls [concrete = constants.%AssociatedTypeImpls] {
  240. // CHECK:STDOUT: %W.patt: %pattern_type = symbolic_binding_pattern W, 0 [concrete]
  241. // CHECK:STDOUT: } {
  242. // CHECK:STDOUT: %.loc12_30.1: type = splice_block %.loc12_30.2 [concrete = constants.%K_where.type] {
  243. // CHECK:STDOUT: %K.ref: type = name_ref K, file.%K.decl [concrete = constants.%K.type]
  244. // CHECK:STDOUT: <elided>
  245. // CHECK:STDOUT: %.Self.ref: %K.type = name_ref .Self, %.Self [symbolic_self = constants.%.Self]
  246. // CHECK:STDOUT: %Associated.ref: %K.assoc_type = name_ref Associated, @Associated.%assoc0 [concrete = constants.%assoc0]
  247. // CHECK:STDOUT: %.Self.as_type: type = facet_access_type %.Self.ref [symbolic_self = constants.%.Self.as_type]
  248. // CHECK:STDOUT: %.loc12_36.1: type = converted %.Self.ref, %.Self.as_type [symbolic_self = constants.%.Self.as_type]
  249. // CHECK:STDOUT: %impl.elem0: %L.type = impl_witness_access constants.%K.lookup_impl_witness, element0 [symbolic_self = constants.%impl.elem0]
  250. // CHECK:STDOUT: %M.ref: type = name_ref M, file.%M.decl [concrete = constants.%M.type]
  251. // CHECK:STDOUT: %as_type: type = facet_access_type %impl.elem0 [symbolic_self = constants.%as_type]
  252. // CHECK:STDOUT: %.loc12_36.2: type = converted %impl.elem0, %as_type [symbolic_self = constants.%as_type]
  253. // CHECK:STDOUT: %.loc12_30.2: type = where_expr %.Self [concrete = constants.%K_where.type] {
  254. // CHECK:STDOUT: requirement_impls %.loc12_36.2, %M.ref
  255. // CHECK:STDOUT: }
  256. // CHECK:STDOUT: }
  257. // CHECK:STDOUT: %W.loc12_24.1: %K_where.type = bind_symbolic_name W, 0 [symbolic = %W.loc12_24.2 (constants.%W)]
  258. // CHECK:STDOUT: }
  259. // CHECK:STDOUT: }
  260. // CHECK:STDOUT:
  261. // CHECK:STDOUT: generic fn @AssociatedTypeImpls(%W.loc12_24.1: %K_where.type) {
  262. // CHECK:STDOUT: %W.loc12_24.2: %K_where.type = bind_symbolic_name W, 0 [symbolic = %W.loc12_24.2 (constants.%W)]
  263. // CHECK:STDOUT:
  264. // CHECK:STDOUT: fn();
  265. // CHECK:STDOUT: }
  266. // CHECK:STDOUT:
  267. // CHECK:STDOUT: specific @AssociatedTypeImpls(constants.%W) {
  268. // CHECK:STDOUT: %W.loc12_24.2 => constants.%W
  269. // CHECK:STDOUT: }
  270. // CHECK:STDOUT: