impl_overlap.carbon 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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/lookup/impl_overlap.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/lookup/impl_overlap.carbon
  12. // ============================================================================
  13. // Setup files
  14. // ============================================================================
  15. // --- interface_z.carbon
  16. library "[[@TEST_NAME]]";
  17. interface Z(T:! type) {}
  18. // --- interface_z_with_impl.carbon
  19. library "[[@TEST_NAME]]";
  20. // Add an extra unused interface to bump `Z`'s id up so that it doesn't
  21. // accidentally match the id assigned to the imported copy of `Z`.
  22. interface Other(T:! type) {}
  23. interface Z(T:! type) {}
  24. final impl forall [T:! type] T as Z(T) {}
  25. // --- type_d.carbon
  26. library "[[@TEST_NAME]]";
  27. class D(T:! type) {}
  28. // ============================================================================
  29. // Test files
  30. // ============================================================================
  31. // --- final_impl_with_interface_generic_self.carbon
  32. library "[[@TEST_NAME]]";
  33. interface Z {}
  34. final impl forall [T:! type] T as Z {}
  35. // --- final_impl_with_interface_concrete_self_same_file.carbon
  36. library "[[@TEST_NAME]]";
  37. interface Z {}
  38. class C;
  39. final impl C as Z {}
  40. // --- final_impl_with_interface_concrete_self_from_other_file.carbon
  41. library "[[@TEST_NAME]]";
  42. import library "type_d";
  43. interface Z {}
  44. // The final impl is for a root self type from another file, but the interface
  45. // is in the same file, so this is valid.
  46. final impl D(()) as Z {}
  47. // --- final_impl_with_interface_symbolic_self_from_other_file.carbon
  48. library "[[@TEST_NAME]]";
  49. import library "type_d";
  50. interface Z {}
  51. // The root self type is from another file, but the interface is from this file.
  52. // This tests the self type being a symbolic type due to the type parameter T.
  53. final impl forall [T:! type] D(T) as Z {}
  54. // --- fail_multiple_finals_overlap_with_interface.carbon
  55. library "[[@TEST_NAME]]";
  56. interface Z(T:! type) {}
  57. final impl forall [T:! type] T as Z(T) {}
  58. // The final impl here overlaps with the final impl above, but is not in a
  59. // match_first.
  60. //
  61. // CHECK:STDERR: fail_multiple_finals_overlap_with_interface.carbon:[[@LINE+7]]:1: error: `final impl` overlaps with `final impl` from same file outside a `match_first` block [FinalImplOverlapsSameFile]
  62. // CHECK:STDERR: final impl forall [T:! type] T as Z(()) {}
  63. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  64. // CHECK:STDERR: fail_multiple_finals_overlap_with_interface.carbon:[[@LINE-8]]:1: note: other `final impl` here [FinalImplOverlapsSameFileNote]
  65. // CHECK:STDERR: final impl forall [T:! type] T as Z(T) {}
  66. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  67. // CHECK:STDERR:
  68. final impl forall [T:! type] T as Z(()) {}
  69. class C;
  70. // The final impl here overlaps with the first final impl above, but is not in
  71. // a match_first.
  72. //
  73. // CHECK:STDERR: fail_multiple_finals_overlap_with_interface.carbon:[[@LINE+7]]:1: error: `final impl` overlaps with `final impl` from same file outside a `match_first` block [FinalImplOverlapsSameFile]
  74. // CHECK:STDERR: final impl C as Z({}) {}
  75. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~
  76. // CHECK:STDERR: fail_multiple_finals_overlap_with_interface.carbon:[[@LINE-22]]:1: note: other `final impl` here [FinalImplOverlapsSameFileNote]
  77. // CHECK:STDERR: final impl forall [T:! type] T as Z(T) {}
  78. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  79. // CHECK:STDERR:
  80. final impl C as Z({}) {}
  81. // --- fail_multiple_finals_overlap_with_self_type.carbon
  82. library "[[@TEST_NAME]]";
  83. import library "interface_z";
  84. class C;
  85. final impl C as Z(()) {}
  86. // The final impl here overlaps with the first final impl above, but is not in
  87. // a match_first.
  88. //
  89. // CHECK:STDERR: fail_multiple_finals_overlap_with_self_type.carbon:[[@LINE+7]]:1: error: `final impl` overlaps with `final impl` from same file outside a `match_first` block [FinalImplOverlapsSameFile]
  90. // CHECK:STDERR: final impl forall [T:! type] C as Z(T) {}
  91. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  92. // CHECK:STDERR: fail_multiple_finals_overlap_with_self_type.carbon:[[@LINE-8]]:1: note: other `final impl` here [FinalImplOverlapsSameFileNote]
  93. // CHECK:STDERR: final impl C as Z(()) {}
  94. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~
  95. // CHECK:STDERR:
  96. final impl forall [T:! type] C as Z(T) {}
  97. // --- multiple_finals_with_nonoverlapping_with_interface.carbon
  98. library "[[@TEST_NAME]]";
  99. import library "type_d";
  100. interface Z {}
  101. // Two final impls for D as Z, but they are not overlapping so they are allowed
  102. // outside of match_first.
  103. final impl D(()) as Z {}
  104. final impl D({}) as Z {}
  105. // --- multiple_finals_with_nonoverlapping_with_self_type.carbon
  106. library "[[@TEST_NAME]]";
  107. import library "interface_z";
  108. class C;
  109. // Two final impls for C as Z, but they are not overlapping so they are allowed
  110. // outside of match_first.
  111. final impl C as Z(()) {}
  112. final impl C as Z({}) {}
  113. // --- final_impl_with_root_self_type.carbon
  114. library "[[@TEST_NAME]]";
  115. import library "interface_z";
  116. class C(T:! type);
  117. // Can provide a specialized final blanket impl for a type defined in the same
  118. // file.
  119. final impl forall [T:! type] C(T) as Z(T) {}
  120. // --- fail_final_impl_with_both_interface_and_self_but_different_files.carbon
  121. library "[[@TEST_NAME]]";
  122. import library "interface_z_with_impl";
  123. class C;
  124. // Can't write a final impl in both the interface's file and the root self
  125. // type's file (when they are different files).
  126. //
  127. // CHECK:STDERR: fail_final_impl_with_both_interface_and_self_but_different_files.carbon:[[@LINE+8]]:1: error: `final impl` overlaps with `final impl` from another file [FinalImplOverlapsDifferentFile]
  128. // CHECK:STDERR: final impl C as Z(()) {}
  129. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~
  130. // CHECK:STDERR: fail_final_impl_with_both_interface_and_self_but_different_files.carbon:[[@LINE-10]]:1: in import [InImport]
  131. // CHECK:STDERR: interface_z_with_impl.carbon:9:1: note: imported `final impl` here [FinalImplOverlapsDifferentFileNote]
  132. // CHECK:STDERR: final impl forall [T:! type] T as Z(T) {}
  133. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  134. // CHECK:STDERR:
  135. final impl C as Z(()) {}
  136. // --- fail_final_overlaps_final_from_other_file.carbon
  137. library "[[@TEST_NAME]]";
  138. import library "interface_z_with_impl";
  139. class C;
  140. // This final impl is overlapped by a final impl in the interface file, and you
  141. // can't write a final impl in two different files.
  142. //
  143. // CHECK:STDERR: fail_final_overlaps_final_from_other_file.carbon:[[@LINE+8]]:1: error: `final impl` overlaps with `final impl` from another file [FinalImplOverlapsDifferentFile]
  144. // CHECK:STDERR: final impl C as Z(C) {}
  145. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~
  146. // CHECK:STDERR: fail_final_overlaps_final_from_other_file.carbon:[[@LINE-10]]:1: in import [InImport]
  147. // CHECK:STDERR: interface_z_with_impl.carbon:9:1: note: imported `final impl` here [FinalImplOverlapsDifferentFileNote]
  148. // CHECK:STDERR: final impl forall [T:! type] T as Z(T) {}
  149. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  150. // CHECK:STDERR:
  151. final impl C as Z(C) {}
  152. // --- fail_final_overlaps_non_final_from_other_file.carbon
  153. library "[[@TEST_NAME]]";
  154. import library "interface_z_with_impl";
  155. class C;
  156. // This non-final impl is subsumed by a final impl in the interface file.
  157. //
  158. // CHECK:STDERR: fail_final_overlaps_non_final_from_other_file.carbon:[[@LINE+8]]:1: error: `impl` will never be used [ImplFinalOverlapsNonFinal]
  159. // CHECK:STDERR: impl C as Z(C) {}
  160. // CHECK:STDERR: ^~~~~~~~~~~~~~~~
  161. // CHECK:STDERR: fail_final_overlaps_non_final_from_other_file.carbon:[[@LINE-9]]:1: in import [InImport]
  162. // CHECK:STDERR: interface_z_with_impl.carbon:9:1: note: `final impl` declared here would always be used instead [ImplFinalOverlapsNonFinalNote]
  163. // CHECK:STDERR: final impl forall [T:! type] T as Z(T) {}
  164. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  165. // CHECK:STDERR:
  166. impl C as Z(C) {}
  167. // --- fail_final_different_file_from_self_and_interface.carbon
  168. library "[[@TEST_NAME]]";
  169. import library "interface_z";
  170. import library "type_d";
  171. class C;
  172. // Can't make a final impl that is not in the same file as the self type nor
  173. // the interface.
  174. //
  175. // CHECK:STDERR: fail_final_different_file_from_self_and_interface.carbon:[[@LINE+4]]:1: error: `final impl` found in file that does not contain the root self type nor the interface definition [FinalImplInvalidFile]
  176. // CHECK:STDERR: final impl D(C) as Z(()) {}
  177. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~
  178. // CHECK:STDERR:
  179. final impl D(C) as Z(()) {}
  180. // --- fail_final_different_file_from_self_and_interface_with_generic_self.carbon
  181. library "[[@TEST_NAME]]";
  182. import library "interface_z";
  183. class C;
  184. // Can't make a final impl that is not in the same file as the self type nor
  185. // the interface.
  186. //
  187. // CHECK:STDERR: fail_final_different_file_from_self_and_interface_with_generic_self.carbon:[[@LINE+4]]:1: error: `final impl` found in file that does not contain the root self type nor the interface definition [FinalImplInvalidFile]
  188. // CHECK:STDERR: final impl forall [T:! type] T as Z(C) {}
  189. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  190. // CHECK:STDERR:
  191. final impl forall [T:! type] T as Z(C) {}
  192. // --- fail_final_overlaps_earlier_non_final_impl.carbon
  193. library "[[@TEST_NAME]]";
  194. interface Z(T:! type) {}
  195. // CHECK:STDERR: fail_final_overlaps_earlier_non_final_impl.carbon:[[@LINE+3]]:1: error: `impl` will never be used [ImplFinalOverlapsNonFinal]
  196. // CHECK:STDERR: impl () as Z(()) {}
  197. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~
  198. impl () as Z(()) {}
  199. // CHECK:STDERR: fail_final_overlaps_earlier_non_final_impl.carbon:[[@LINE+4]]:1: note: `final impl` declared here would always be used instead [ImplFinalOverlapsNonFinalNote]
  200. // CHECK:STDERR: final impl forall [T:! type] T as Z(T) {}
  201. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  202. // CHECK:STDERR:
  203. final impl forall [T:! type] T as Z(T) {}
  204. // --- fail_final_overlaps_later_non_final_impl.carbon
  205. library "[[@TEST_NAME]]";
  206. interface Z(T:! type) {}
  207. final impl forall [T:! type] T as Z(T) {}
  208. // CHECK:STDERR: fail_final_overlaps_later_non_final_impl.carbon:[[@LINE+7]]:1: error: `impl` will never be used [ImplFinalOverlapsNonFinal]
  209. // CHECK:STDERR: impl () as Z(()) {}
  210. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~
  211. // CHECK:STDERR: fail_final_overlaps_later_non_final_impl.carbon:[[@LINE-5]]:1: note: `final impl` declared here would always be used instead [ImplFinalOverlapsNonFinalNote]
  212. // CHECK:STDERR: final impl forall [T:! type] T as Z(T) {}
  213. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  214. // CHECK:STDERR:
  215. impl () as Z(()) {}
  216. // --- fail_final_overlaps_earlier_final_impl.carbon
  217. library "[[@TEST_NAME]]";
  218. interface Z(T:! type) {}
  219. class C;
  220. final impl C as Z(C) {}
  221. // CHECK:STDERR: fail_final_overlaps_earlier_final_impl.carbon:[[@LINE+7]]:1: error: `final impl` overlaps with `final impl` from same file outside a `match_first` block [FinalImplOverlapsSameFile]
  222. // CHECK:STDERR: final impl forall [T:! type] T as Z(T) {}
  223. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  224. // CHECK:STDERR: fail_final_overlaps_earlier_final_impl.carbon:[[@LINE-5]]:1: note: other `final impl` here [FinalImplOverlapsSameFileNote]
  225. // CHECK:STDERR: final impl C as Z(C) {}
  226. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~
  227. // CHECK:STDERR:
  228. final impl forall [T:! type] T as Z(T) {}
  229. // --- fail_final_overlaps_later_final_impl.carbon
  230. library "[[@TEST_NAME]]";
  231. interface Z(T:! type) {}
  232. class C;
  233. final impl forall [T:! type] T as Z(T) {}
  234. // CHECK:STDERR: fail_final_overlaps_later_final_impl.carbon:[[@LINE+7]]:1: error: `final impl` overlaps with `final impl` from same file outside a `match_first` block [FinalImplOverlapsSameFile]
  235. // CHECK:STDERR: final impl C as Z(C) {}
  236. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~
  237. // CHECK:STDERR: fail_final_overlaps_later_final_impl.carbon:[[@LINE-5]]:1: note: other `final impl` here [FinalImplOverlapsSameFileNote]
  238. // CHECK:STDERR: final impl forall [T:! type] T as Z(T) {}
  239. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  240. // CHECK:STDERR:
  241. final impl C as Z(C) {}
  242. // --- fail_non_final_type_structure_matches_non_final_impl.carbon
  243. library "[[@TEST_NAME]]";
  244. interface Z(T:! type) {}
  245. interface Y {}
  246. impl forall [T:! Y] T as Z(T) {}
  247. class C(T:! type);
  248. // No diagnosis here as the type structure is unique.
  249. impl forall [T:! type] T as Z(C(T)) {}
  250. // CHECK:STDERR: fail_non_final_type_structure_matches_non_final_impl.carbon:[[@LINE+7]]:1: error: found non-final `impl` with the same type structure as another non-final `impl` [ImplNonFinalSameTypeStructure]
  251. // CHECK:STDERR: impl forall [T:! type] T as Z(T) {}
  252. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  253. // CHECK:STDERR: fail_non_final_type_structure_matches_non_final_impl.carbon:[[@LINE-10]]:1: note: other `impl` here [ImplNonFinalSameTypeStructureNote]
  254. // CHECK:STDERR: impl forall [T:! Y] T as Z(T) {}
  255. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  256. // CHECK:STDERR:
  257. impl forall [T:! type] T as Z(T) {}
  258. // --- non_final_type_structure_same_shape_but_different_concrete_types.carbon
  259. library "[[@TEST_NAME]]";
  260. interface Z {}
  261. class C;
  262. class D;
  263. impl C as Z {}
  264. impl D as Z {}
  265. // --- partial_overlap_type_structure_of_non_final_impl.carbon
  266. library "[[@TEST_NAME]]";
  267. interface Z(T:! type) {}
  268. impl forall [T:! type] T as Z(T) {}
  269. class C;
  270. // Partially overlaps the blanket impl, which is fine.
  271. final impl C as Z(C) {}
  272. class D;
  273. // Partially overlaps the blanket impl, which is fine.
  274. impl D as Z(D) {}
  275. // --- fail_final_overlap_where_each_is_more_specific_than_the_other.carbon
  276. interface Z(T:! type) {}
  277. class C(T:! type) {}
  278. // This should be diagnosed as overlapping final impls outside a `match_first`.
  279. // The first impl is more specific in the interface constraint. The second is
  280. // more specific in the self type. They overlap for the query `C(()) as Z(())`
  281. // but neither impl is completely more specific than the other.
  282. final impl forall [T:! type] C(T) as Z(()) {}
  283. // CHECK:STDERR: fail_final_overlap_where_each_is_more_specific_than_the_other.carbon:[[@LINE+7]]:1: error: `final impl` overlaps with `final impl` from same file outside a `match_first` block [FinalImplOverlapsSameFile]
  284. // CHECK:STDERR: final impl forall [T:! type] C(()) as Z(T) {}
  285. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  286. // CHECK:STDERR: fail_final_overlap_where_each_is_more_specific_than_the_other.carbon:[[@LINE-4]]:1: note: other `final impl` here [FinalImplOverlapsSameFileNote]
  287. // CHECK:STDERR: final impl forall [T:! type] C(T) as Z(()) {}
  288. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  289. // CHECK:STDERR:
  290. final impl forall [T:! type] C(()) as Z(T) {}