impl_as_named_constraint.carbon 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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/convert.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/impl/impl_as_named_constraint.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/impl_as_named_constraint.carbon
  12. // --- fail_empty_constraint.carbon
  13. library "[[@TEST_NAME]]";
  14. constraint A {}
  15. // CHECK:STDERR: fail_empty_constraint.carbon:[[@LINE+4]]:1: error: impl as 0 interfaces, expected 1 [ImplOfNotOneInterface]
  16. // CHECK:STDERR: impl () as A {}
  17. // CHECK:STDERR: ^~~~~~~~~~~~~~
  18. // CHECK:STDERR:
  19. impl () as A {}
  20. // --- fail_too_many_interfaces_in_constraint.carbon
  21. library "[[@TEST_NAME]]";
  22. interface A1 {}
  23. interface A2 {}
  24. constraint B {
  25. extend require impls A1;
  26. extend require impls A2;
  27. }
  28. // CHECK:STDERR: fail_too_many_interfaces_in_constraint.carbon:[[@LINE+4]]:1: error: impl as 2 interfaces, expected 1 [ImplOfNotOneInterface]
  29. // CHECK:STDERR: impl () as B {}
  30. // CHECK:STDERR: ^~~~~~~~~~~~~~
  31. // CHECK:STDERR:
  32. impl () as B {}
  33. // --- one_extend_impls_interface_in_constraint.carbon
  34. library "[[@TEST_NAME]]";
  35. interface A {}
  36. constraint B {
  37. extend require impls A;
  38. }
  39. impl () as B {}
  40. // --- fail_one_impls_interface_in_constraint.carbon
  41. library "[[@TEST_NAME]]";
  42. interface A;
  43. constraint B {
  44. require impls A;
  45. }
  46. // CHECK:STDERR: fail_one_impls_interface_in_constraint.carbon:[[@LINE+4]]:1: error: impl as 0 interfaces, expected 1 [ImplOfNotOneInterface]
  47. // CHECK:STDERR: impl () as B {}
  48. // CHECK:STDERR: ^~~~~~~~~~~~~~
  49. // CHECK:STDERR:
  50. impl () as B {}
  51. // --- nested_constraints.carbon
  52. library "[[@TEST_NAME]]";
  53. interface A {}
  54. constraint B {
  55. extend require impls A;
  56. }
  57. constraint C {
  58. extend require impls B;
  59. }
  60. impl () as C {}
  61. // --- fail_nested_constraints_not_extend_outer.carbon
  62. library "[[@TEST_NAME]]";
  63. interface A {}
  64. constraint B {
  65. extend require impls A;
  66. }
  67. constraint C {
  68. require impls B;
  69. }
  70. // CHECK:STDERR: fail_nested_constraints_not_extend_outer.carbon:[[@LINE+4]]:1: error: impl as 0 interfaces, expected 1 [ImplOfNotOneInterface]
  71. // CHECK:STDERR: impl () as C {}
  72. // CHECK:STDERR: ^~~~~~~~~~~~~~
  73. // CHECK:STDERR:
  74. impl () as C {}
  75. // --- fail_nested_constraints_not_extend_inner.carbon
  76. library "[[@TEST_NAME]]";
  77. interface A {}
  78. constraint B {
  79. require impls A;
  80. }
  81. constraint C {
  82. extend require impls B;
  83. }
  84. // CHECK:STDERR: fail_nested_constraints_not_extend_inner.carbon:[[@LINE+4]]:1: error: impl as 0 interfaces, expected 1 [ImplOfNotOneInterface]
  85. // CHECK:STDERR: impl () as C {}
  86. // CHECK:STDERR: ^~~~~~~~~~~~~~
  87. // CHECK:STDERR:
  88. impl () as C {}
  89. // --- fail_nested_constraints_not_extend_both.carbon
  90. library "[[@TEST_NAME]]";
  91. interface A {}
  92. constraint B {
  93. require impls A;
  94. }
  95. constraint C {
  96. require impls B;
  97. }
  98. // CHECK:STDERR: fail_nested_constraints_not_extend_both.carbon:[[@LINE+4]]:1: error: impl as 0 interfaces, expected 1 [ImplOfNotOneInterface]
  99. // CHECK:STDERR: impl () as C {}
  100. // CHECK:STDERR: ^~~~~~~~~~~~~~
  101. // CHECK:STDERR:
  102. impl () as C {}
  103. // --- fail_duplicate_through_generic_constraint.carbon
  104. library "[[@TEST_NAME]]";
  105. interface A(T:! type) {}
  106. constraint B(X:! type, Y:! type) {
  107. extend require impls A(Y);
  108. }
  109. // This should impl () as A({}).
  110. impl () as B((), {}) {}
  111. // CHECK:STDERR: fail_duplicate_through_generic_constraint.carbon:[[@LINE+7]]:1: error: found non-final `impl` with the same type structure as another non-final `impl` [ImplNonFinalSameTypeStructure]
  112. // CHECK:STDERR: impl () as A({}) {}
  113. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~
  114. // CHECK:STDERR: fail_duplicate_through_generic_constraint.carbon:[[@LINE-5]]:1: note: other `impl` here [ImplNonFinalSameTypeStructureNote]
  115. // CHECK:STDERR: impl () as B((), {}) {}
  116. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~
  117. // CHECK:STDERR:
  118. impl () as A({}) {}
  119. // --- fail_error_self_in_require.carbon
  120. library "[[@TEST_NAME]]";
  121. interface A(T:! type) {}
  122. // This makes the type of `Self` into an ErrorInst. No `require` is added to
  123. // the constraint.
  124. //@dump-sem-ir-begin
  125. //
  126. // CHECK:STDERR: fail_error_self_in_require.carbon:[[@LINE+4]]:18: error: name `Undefined` not found [NameNotFound]
  127. // CHECK:STDERR: constraint B(X:! Undefined) {
  128. // CHECK:STDERR: ^~~~~~~~~
  129. // CHECK:STDERR:
  130. constraint B(X:! Undefined) {
  131. extend require impls A(X);
  132. }
  133. //@dump-sem-ir-end
  134. impl () as B(1) {}
  135. // --- impl_as_constraint_where_rewrite.carbon
  136. library "[[@TEST_NAME]]";
  137. interface I {
  138. let I1:! type;
  139. }
  140. constraint N {
  141. extend require impls I;
  142. }
  143. class C;
  144. impl C as N where .I1 = C {}
  145. fn F(_:! I where .I1 = .Self) {}
  146. fn G() {
  147. // TODO: This crashes.
  148. // F(C);
  149. }
  150. // --- impl_as_concrete_generic_constraint_where_rewrite.carbon
  151. library "[[@TEST_NAME]]";
  152. interface I(T:! type) {
  153. let I1:! type;
  154. }
  155. class C;
  156. constraint N {
  157. extend require impls I(C);
  158. }
  159. impl C as N where .I1 = C {}
  160. fn F(_:! I(.Self) where .I1 = .Self) {}
  161. fn G() {
  162. // TODO: This crashes.
  163. // F(C);
  164. }
  165. // --- todo_impl_as_symbolic_generic_constraint_where_rewrite.carbon
  166. library "[[@TEST_NAME]]";
  167. interface I(T:! type) {
  168. let I1:! type;
  169. }
  170. constraint N {
  171. extend require impls I(Self);
  172. }
  173. class C;
  174. impl C as N where .I1 = C {}
  175. fn F(_:! I(.Self) where .I1 = .Self) {}
  176. fn G() {
  177. // TODO: This crashes.
  178. // F(C);
  179. }
  180. // --- fail_todo_impl_as_constraint_containing_rewrite_for_concrete_interface.carbon
  181. library "[[@TEST_NAME]]";
  182. interface I {
  183. let I1:! type;
  184. }
  185. constraint N {
  186. extend require impls I where .I1 = Self;
  187. }
  188. class C;
  189. // CHECK:STDERR: fail_todo_impl_as_constraint_containing_rewrite_for_concrete_interface.carbon:[[@LINE+7]]:1: error: associated constant I1 not given a value in impl of interface I [ImplAssociatedConstantNeedsValue]
  190. // CHECK:STDERR: impl C as N {}
  191. // CHECK:STDERR: ^~~~~~~~~~~~~
  192. // CHECK:STDERR: fail_todo_impl_as_constraint_containing_rewrite_for_concrete_interface.carbon:[[@LINE-11]]:7: note: associated constant declared here [AssociatedConstantHere]
  193. // CHECK:STDERR: let I1:! type;
  194. // CHECK:STDERR: ^~~~~~~~~
  195. // CHECK:STDERR:
  196. impl C as N {}
  197. fn F(_:! I where .I1 = .Self) {}
  198. fn G() {
  199. // TODO: This crashes.
  200. // F(C);
  201. }
  202. // --- fail_todo_impl_as_constraint_containing_rewrite_for_generic_interface.carbon
  203. library "[[@TEST_NAME]]";
  204. interface I(T:! type) {
  205. let I1:! type;
  206. }
  207. constraint N {
  208. extend require impls I(Self) where .I1 = Self;
  209. }
  210. class C;
  211. // CHECK:STDERR: fail_todo_impl_as_constraint_containing_rewrite_for_generic_interface.carbon:[[@LINE+7]]:1: error: associated constant I1 not given a value in impl of interface I [ImplAssociatedConstantNeedsValue]
  212. // CHECK:STDERR: impl C as N {}
  213. // CHECK:STDERR: ^~~~~~~~~~~~~
  214. // CHECK:STDERR: fail_todo_impl_as_constraint_containing_rewrite_for_generic_interface.carbon:[[@LINE-11]]:7: note: associated constant declared here [AssociatedConstantHere]
  215. // CHECK:STDERR: let I1:! type;
  216. // CHECK:STDERR: ^~~~~~~~~
  217. // CHECK:STDERR:
  218. impl C as N {}
  219. fn F(_:! I(.Self) where .I1 = .Self) {}
  220. fn G() {
  221. // TODO: This crashes.
  222. // F(C);
  223. }
  224. // CHECK:STDOUT: --- fail_error_self_in_require.carbon
  225. // CHECK:STDOUT:
  226. // CHECK:STDOUT: constants {
  227. // CHECK:STDOUT: %B.type: type = generic_named_constaint_type @B [concrete]
  228. // CHECK:STDOUT: %empty_struct: %B.type = struct_value () [concrete]
  229. // CHECK:STDOUT: }
  230. // CHECK:STDOUT:
  231. // CHECK:STDOUT: file {
  232. // CHECK:STDOUT: %B.decl: %B.type = constraint_decl @B [concrete = constants.%empty_struct] {
  233. // CHECK:STDOUT: %X.patt: <error> = symbolic_binding_pattern X, 0 [concrete]
  234. // CHECK:STDOUT: } {
  235. // CHECK:STDOUT: <elided>
  236. // CHECK:STDOUT: %X: <error> = symbolic_binding X, 0 [concrete = <error>]
  237. // CHECK:STDOUT: }
  238. // CHECK:STDOUT: }
  239. // CHECK:STDOUT:
  240. // CHECK:STDOUT: generic constraint @B(%X: <error>) {
  241. // CHECK:STDOUT: !definition:
  242. // CHECK:STDOUT:
  243. // CHECK:STDOUT: constraint {
  244. // CHECK:STDOUT: %Self: <error> = symbolic_binding Self, 1 [concrete = <error>]
  245. // CHECK:STDOUT: %B.WithSelf.decl = constraint_with_self_decl @B [concrete]
  246. // CHECK:STDOUT:
  247. // CHECK:STDOUT: !members:
  248. // CHECK:STDOUT: .Self = %Self
  249. // CHECK:STDOUT: .A = <poisoned>
  250. // CHECK:STDOUT: .X = <poisoned>
  251. // CHECK:STDOUT: .A = <poisoned>
  252. // CHECK:STDOUT: .X = <poisoned>
  253. // CHECK:STDOUT: has_error
  254. // CHECK:STDOUT:
  255. // CHECK:STDOUT: !requires:
  256. // CHECK:STDOUT: }
  257. // CHECK:STDOUT: }
  258. // CHECK:STDOUT:
  259. // CHECK:STDOUT: specific @B(<error>) {}
  260. // CHECK:STDOUT:
  261. // CHECK:STDOUT: specific @B.WithSelf(<error>, <error>) {}
  262. // CHECK:STDOUT: