generic.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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/none.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/named_constraint/generic.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/named_constraint/generic.carbon
  12. // --- generic.carbon
  13. library "[[@TEST_NAME]]";
  14. //@include-in-dumps
  15. constraint GenericType(T:! type) {}
  16. interface Z {}
  17. constraint GenericZ(U:! Z) {}
  18. constraint ForwardDeclaredGeneric(T:! type);
  19. constraint ForwardDeclaredGeneric(T:! type) {}
  20. // --- fail_mismatched_generic_param_types.carbon
  21. library "[[@TEST_NAME]]";
  22. constraint ForwardDeclaredGeneric(T:! ());
  23. // CHECK:STDERR: fail_mismatched_generic_param_types.carbon:[[@LINE+7]]:35: error: type `<pattern for type>` of parameter 1 in redeclaration differs from previous parameter type `<pattern for ()>` [RedeclParamDiffersType]
  24. // CHECK:STDERR: constraint ForwardDeclaredGeneric(T:! type) {}
  25. // CHECK:STDERR: ^
  26. // CHECK:STDERR: fail_mismatched_generic_param_types.carbon:[[@LINE-4]]:35: note: previous declaration's corresponding parameter here [RedeclParamPrevious]
  27. // CHECK:STDERR: constraint ForwardDeclaredGeneric(T:! ());
  28. // CHECK:STDERR: ^
  29. // CHECK:STDERR:
  30. constraint ForwardDeclaredGeneric(T:! type) {}
  31. // --- fail_mismatched_generic_param_arity.carbon
  32. library "[[@TEST_NAME]]";
  33. constraint ForwardDeclaredGeneric(T:! type, U:! type);
  34. // CHECK:STDERR: fail_mismatched_generic_param_arity.carbon:[[@LINE+7]]:1: error: redeclaration differs because of parameter count of 1 [RedeclParamCountDiffers]
  35. // CHECK:STDERR: constraint ForwardDeclaredGeneric(T:! type) {}
  36. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  37. // CHECK:STDERR: fail_mismatched_generic_param_arity.carbon:[[@LINE-4]]:1: note: previously declared with parameter count of 2 [RedeclParamCountPrevious]
  38. // CHECK:STDERR: constraint ForwardDeclaredGeneric(T:! type, U:! type);
  39. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  40. // CHECK:STDERR:
  41. constraint ForwardDeclaredGeneric(T:! type) {}
  42. // --- fail_inside_definition.carbon
  43. library "[[@TEST_NAME]]";
  44. constraint Generic(T:! type) {}
  45. // CHECK:STDERR: fail_inside_definition.carbon:[[@LINE+7]]:1: error: redefinition of `constraint Generic` [RedeclRedef]
  46. // CHECK:STDERR: constraint Generic(T:! type) {}
  47. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  48. // CHECK:STDERR: fail_inside_definition.carbon:[[@LINE-4]]:1: note: previously defined here [RedeclPrevDef]
  49. // CHECK:STDERR: constraint Generic(T:! type) {}
  50. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  51. // CHECK:STDERR:
  52. constraint Generic(T:! type) {}
  53. // --- fail_args_count_mismatch.carbon
  54. library "[[@TEST_NAME]]";
  55. constraint Generic(T:! type) {}
  56. // CHECK:STDERR: fail_args_count_mismatch.carbon:[[@LINE+7]]:10: error: 2 arguments passed to generic constraint expecting 1 argument [CallArgCountMismatch]
  57. // CHECK:STDERR: fn F(T:! Generic((), ())) {}
  58. // CHECK:STDERR: ^~~~~~~~~~~~~~~
  59. // CHECK:STDERR: fail_args_count_mismatch.carbon:[[@LINE-5]]:1: note: calling generic constraint declared here [InCallToEntity]
  60. // CHECK:STDERR: constraint Generic(T:! type) {}
  61. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  62. // CHECK:STDERR:
  63. fn F(T:! Generic((), ())) {}
  64. // CHECK:STDOUT: --- generic.carbon
  65. // CHECK:STDOUT:
  66. // CHECK:STDOUT: constants {
  67. // CHECK:STDOUT: %type: type = facet_type <type> [concrete]
  68. // CHECK:STDOUT: %.Self: %type = symbolic_binding .Self [symbolic_self]
  69. // CHECK:STDOUT: %T: type = symbolic_binding T, 0 [symbolic]
  70. // CHECK:STDOUT: %pattern_type.98f: type = pattern_type type [concrete]
  71. // CHECK:STDOUT: %GenericType.type.ace: type = generic_named_constaint_type @GenericType [concrete]
  72. // CHECK:STDOUT: %empty_struct.bd5: %GenericType.type.ace = struct_value () [concrete]
  73. // CHECK:STDOUT: %GenericType.type.b8d: type = facet_type <@GenericType, @GenericType(%T)> [symbolic]
  74. // CHECK:STDOUT: %Self.aa1546.1: %GenericType.type.b8d = symbolic_binding Self, 1 [symbolic]
  75. // CHECK:STDOUT: %Z.type: type = facet_type <@Z> [concrete]
  76. // CHECK:STDOUT: %Self.3b3: %Z.type = symbolic_binding Self, 0 [symbolic]
  77. // CHECK:STDOUT: %U: %Z.type = symbolic_binding U, 0 [symbolic]
  78. // CHECK:STDOUT: %pattern_type.4a0: type = pattern_type %Z.type [concrete]
  79. // CHECK:STDOUT: %GenericZ.type.e48: type = generic_named_constaint_type @GenericZ [concrete]
  80. // CHECK:STDOUT: %empty_struct.05e: %GenericZ.type.e48 = struct_value () [concrete]
  81. // CHECK:STDOUT: %GenericZ.type.b8d: type = facet_type <@GenericZ, @GenericZ(%U)> [symbolic]
  82. // CHECK:STDOUT: %Self.aa1546.2: %GenericZ.type.b8d = symbolic_binding Self, 1 [symbolic]
  83. // CHECK:STDOUT: %ForwardDeclaredGeneric.type.341: type = generic_named_constaint_type @ForwardDeclaredGeneric [concrete]
  84. // CHECK:STDOUT: %empty_struct.1a7: %ForwardDeclaredGeneric.type.341 = struct_value () [concrete]
  85. // CHECK:STDOUT: %ForwardDeclaredGeneric.type.b8d: type = facet_type <@ForwardDeclaredGeneric, @ForwardDeclaredGeneric(%T)> [symbolic]
  86. // CHECK:STDOUT: %Self.aa1546.3: %ForwardDeclaredGeneric.type.b8d = symbolic_binding Self, 1 [symbolic]
  87. // CHECK:STDOUT: }
  88. // CHECK:STDOUT:
  89. // CHECK:STDOUT: file {
  90. // CHECK:STDOUT: package: <namespace> = namespace [concrete] {
  91. // CHECK:STDOUT: .GenericType = %GenericType.decl
  92. // CHECK:STDOUT: .Z = %Z.decl
  93. // CHECK:STDOUT: .GenericZ = %GenericZ.decl
  94. // CHECK:STDOUT: .ForwardDeclaredGeneric = %ForwardDeclaredGeneric.decl.loc9
  95. // CHECK:STDOUT: }
  96. // CHECK:STDOUT: %GenericType.decl: %GenericType.type.ace = constraint_decl @GenericType [concrete = constants.%empty_struct.bd5] {
  97. // CHECK:STDOUT: %T.patt: %pattern_type.98f = symbolic_binding_pattern T, 0 [concrete]
  98. // CHECK:STDOUT: } {
  99. // CHECK:STDOUT: %.Self: %type = symbolic_binding .Self [symbolic_self = constants.%.Self]
  100. // CHECK:STDOUT: %T.loc4_24.2: type = symbolic_binding T, 0 [symbolic = %T.loc4_24.1 (constants.%T)]
  101. // CHECK:STDOUT: }
  102. // CHECK:STDOUT: %Z.decl: type = interface_decl @Z [concrete = constants.%Z.type] {} {}
  103. // CHECK:STDOUT: %GenericZ.decl: %GenericZ.type.e48 = constraint_decl @GenericZ [concrete = constants.%empty_struct.05e] {
  104. // CHECK:STDOUT: %U.patt: %pattern_type.4a0 = symbolic_binding_pattern U, 0 [concrete]
  105. // CHECK:STDOUT: } {
  106. // CHECK:STDOUT: %.loc7: type = splice_block %Z.ref [concrete = constants.%Z.type] {
  107. // CHECK:STDOUT: %.Self: %type = symbolic_binding .Self [symbolic_self = constants.%.Self]
  108. // CHECK:STDOUT: %Z.ref: type = name_ref Z, file.%Z.decl [concrete = constants.%Z.type]
  109. // CHECK:STDOUT: }
  110. // CHECK:STDOUT: %U.loc7_21.2: %Z.type = symbolic_binding U, 0 [symbolic = %U.loc7_21.1 (constants.%U)]
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT: %ForwardDeclaredGeneric.decl.loc9: %ForwardDeclaredGeneric.type.341 = constraint_decl @ForwardDeclaredGeneric [concrete = constants.%empty_struct.1a7] {
  113. // CHECK:STDOUT: %T.patt: %pattern_type.98f = symbolic_binding_pattern T, 0 [concrete]
  114. // CHECK:STDOUT: } {
  115. // CHECK:STDOUT: %.Self.2: %type = symbolic_binding .Self [symbolic_self = constants.%.Self]
  116. // CHECK:STDOUT: %T.loc9_35.2: type = symbolic_binding T, 0 [symbolic = %T.loc9_35.1 (constants.%T)]
  117. // CHECK:STDOUT: }
  118. // CHECK:STDOUT: %ForwardDeclaredGeneric.decl.loc10: %ForwardDeclaredGeneric.type.341 = constraint_decl @ForwardDeclaredGeneric [concrete = constants.%empty_struct.1a7] {
  119. // CHECK:STDOUT: %T.patt: %pattern_type.98f = symbolic_binding_pattern T, 0 [concrete]
  120. // CHECK:STDOUT: } {
  121. // CHECK:STDOUT: %.Self.1: %type = symbolic_binding .Self [symbolic_self = constants.%.Self]
  122. // CHECK:STDOUT: %T.loc10: type = symbolic_binding T, 0 [symbolic = %T.loc9_35.1 (constants.%T)]
  123. // CHECK:STDOUT: }
  124. // CHECK:STDOUT: }
  125. // CHECK:STDOUT:
  126. // CHECK:STDOUT: interface @Z {
  127. // CHECK:STDOUT: %Self: %Z.type = symbolic_binding Self, 0 [symbolic = constants.%Self.3b3]
  128. // CHECK:STDOUT:
  129. // CHECK:STDOUT: !members:
  130. // CHECK:STDOUT: .Self = %Self
  131. // CHECK:STDOUT: witness = ()
  132. // CHECK:STDOUT:
  133. // CHECK:STDOUT: !requires:
  134. // CHECK:STDOUT: }
  135. // CHECK:STDOUT:
  136. // CHECK:STDOUT: generic constraint @GenericType(%T.loc4_24.2: type) {
  137. // CHECK:STDOUT: %T.loc4_24.1: type = symbolic_binding T, 0 [symbolic = %T.loc4_24.1 (constants.%T)]
  138. // CHECK:STDOUT:
  139. // CHECK:STDOUT: !definition:
  140. // CHECK:STDOUT: %GenericType.type: type = facet_type <@GenericType, @GenericType(%T.loc4_24.1)> [symbolic = %GenericType.type (constants.%GenericType.type.b8d)]
  141. // CHECK:STDOUT: %Self.loc4_34.2: @GenericType.%GenericType.type (%GenericType.type.b8d) = symbolic_binding Self, 1 [symbolic = %Self.loc4_34.2 (constants.%Self.aa1546.1)]
  142. // CHECK:STDOUT:
  143. // CHECK:STDOUT: constraint {
  144. // CHECK:STDOUT: %Self.loc4_34.1: @GenericType.%GenericType.type (%GenericType.type.b8d) = symbolic_binding Self, 1 [symbolic = %Self.loc4_34.2 (constants.%Self.aa1546.1)]
  145. // CHECK:STDOUT:
  146. // CHECK:STDOUT: !members:
  147. // CHECK:STDOUT: .Self = %Self.loc4_34.1
  148. // CHECK:STDOUT:
  149. // CHECK:STDOUT: !requires:
  150. // CHECK:STDOUT: }
  151. // CHECK:STDOUT: }
  152. // CHECK:STDOUT:
  153. // CHECK:STDOUT: generic constraint @GenericZ(%U.loc7_21.2: %Z.type) {
  154. // CHECK:STDOUT: %U.loc7_21.1: %Z.type = symbolic_binding U, 0 [symbolic = %U.loc7_21.1 (constants.%U)]
  155. // CHECK:STDOUT:
  156. // CHECK:STDOUT: !definition:
  157. // CHECK:STDOUT: %GenericZ.type: type = facet_type <@GenericZ, @GenericZ(%U.loc7_21.1)> [symbolic = %GenericZ.type (constants.%GenericZ.type.b8d)]
  158. // CHECK:STDOUT: %Self.loc7_28.2: @GenericZ.%GenericZ.type (%GenericZ.type.b8d) = symbolic_binding Self, 1 [symbolic = %Self.loc7_28.2 (constants.%Self.aa1546.2)]
  159. // CHECK:STDOUT:
  160. // CHECK:STDOUT: constraint {
  161. // CHECK:STDOUT: %Self.loc7_28.1: @GenericZ.%GenericZ.type (%GenericZ.type.b8d) = symbolic_binding Self, 1 [symbolic = %Self.loc7_28.2 (constants.%Self.aa1546.2)]
  162. // CHECK:STDOUT:
  163. // CHECK:STDOUT: !members:
  164. // CHECK:STDOUT: .Self = %Self.loc7_28.1
  165. // CHECK:STDOUT:
  166. // CHECK:STDOUT: !requires:
  167. // CHECK:STDOUT: }
  168. // CHECK:STDOUT: }
  169. // CHECK:STDOUT:
  170. // CHECK:STDOUT: generic constraint @ForwardDeclaredGeneric(%T.loc9_35.2: type) {
  171. // CHECK:STDOUT: %T.loc9_35.1: type = symbolic_binding T, 0 [symbolic = %T.loc9_35.1 (constants.%T)]
  172. // CHECK:STDOUT:
  173. // CHECK:STDOUT: !definition:
  174. // CHECK:STDOUT: %ForwardDeclaredGeneric.type: type = facet_type <@ForwardDeclaredGeneric, @ForwardDeclaredGeneric(%T.loc9_35.1)> [symbolic = %ForwardDeclaredGeneric.type (constants.%ForwardDeclaredGeneric.type.b8d)]
  175. // CHECK:STDOUT: %Self.loc10_45.2: @ForwardDeclaredGeneric.%ForwardDeclaredGeneric.type (%ForwardDeclaredGeneric.type.b8d) = symbolic_binding Self, 1 [symbolic = %Self.loc10_45.2 (constants.%Self.aa1546.3)]
  176. // CHECK:STDOUT:
  177. // CHECK:STDOUT: constraint {
  178. // CHECK:STDOUT: %Self.loc10_45.1: @ForwardDeclaredGeneric.%ForwardDeclaredGeneric.type (%ForwardDeclaredGeneric.type.b8d) = symbolic_binding Self, 1 [symbolic = %Self.loc10_45.2 (constants.%Self.aa1546.3)]
  179. // CHECK:STDOUT:
  180. // CHECK:STDOUT: !members:
  181. // CHECK:STDOUT: .Self = %Self.loc10_45.1
  182. // CHECK:STDOUT:
  183. // CHECK:STDOUT: !requires:
  184. // CHECK:STDOUT: }
  185. // CHECK:STDOUT: }
  186. // CHECK:STDOUT:
  187. // CHECK:STDOUT: specific @GenericType(constants.%T) {
  188. // CHECK:STDOUT: %T.loc4_24.1 => constants.%T
  189. // CHECK:STDOUT: }
  190. // CHECK:STDOUT:
  191. // CHECK:STDOUT: specific @GenericZ(constants.%U) {
  192. // CHECK:STDOUT: %U.loc7_21.1 => constants.%U
  193. // CHECK:STDOUT: }
  194. // CHECK:STDOUT:
  195. // CHECK:STDOUT: specific @ForwardDeclaredGeneric(constants.%T) {
  196. // CHECK:STDOUT: %T.loc9_35.1 => constants.%T
  197. // CHECK:STDOUT: }
  198. // CHECK:STDOUT: