impl_overlap.carbon 15 KB

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