impl_overlap.carbon 15 KB

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