impl_overlap.carbon 15 KB

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