impl_as_named_constraint.carbon 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. // CHECK:STDOUT: --- fail_error_self_in_require.carbon
  136. // CHECK:STDOUT:
  137. // CHECK:STDOUT: constants {
  138. // CHECK:STDOUT: %B.type: type = generic_named_constaint_type @B [concrete]
  139. // CHECK:STDOUT: %empty_struct: %B.type = struct_value () [concrete]
  140. // CHECK:STDOUT: }
  141. // CHECK:STDOUT:
  142. // CHECK:STDOUT: imports {
  143. // CHECK:STDOUT: }
  144. // CHECK:STDOUT:
  145. // CHECK:STDOUT: file {
  146. // CHECK:STDOUT: %B.decl: %B.type = constraint_decl @B [concrete = constants.%empty_struct] {
  147. // CHECK:STDOUT: %X.patt: <error> = symbolic_binding_pattern X, 0 [concrete]
  148. // CHECK:STDOUT: } {
  149. // CHECK:STDOUT: <elided>
  150. // CHECK:STDOUT: %X: <error> = symbolic_binding X, 0 [concrete = <error>]
  151. // CHECK:STDOUT: }
  152. // CHECK:STDOUT: }
  153. // CHECK:STDOUT:
  154. // CHECK:STDOUT: generic constraint @B(%X: <error>) {
  155. // CHECK:STDOUT: !definition:
  156. // CHECK:STDOUT:
  157. // CHECK:STDOUT: constraint {
  158. // CHECK:STDOUT: %Self: <error> = symbolic_binding Self, 1 [concrete = <error>]
  159. // CHECK:STDOUT:
  160. // CHECK:STDOUT: !members:
  161. // CHECK:STDOUT: .Self = %Self
  162. // CHECK:STDOUT: .A = <poisoned>
  163. // CHECK:STDOUT: .X = <poisoned>
  164. // CHECK:STDOUT:
  165. // CHECK:STDOUT: !requires:
  166. // CHECK:STDOUT: }
  167. // CHECK:STDOUT: }
  168. // CHECK:STDOUT:
  169. // CHECK:STDOUT: specific @B(<error>) {}
  170. // CHECK:STDOUT: