specialization.carbon 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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/specialization.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/specialization.carbon
  13. // --- specialized_self_first.carbon
  14. library "[[@TEST_NAME]]";
  15. interface Z(T:! type) {
  16. let X:! type;
  17. }
  18. class C {}
  19. impl C as Z(C) where .X = C {}
  20. impl forall [T:! type] T as Z(T) where .X = () {}
  21. fn F() {
  22. // The specialization of `Z(C)` should match in preference to the blanket impl
  23. // of `Z(T)`. If the blanket impl is chosen, then `a` will have type `()`
  24. // which will fail to typecheck here.
  25. let a: C.(Z(C).X) = {} as C;
  26. }
  27. // --- specialized_self_second.carbon
  28. library "[[@TEST_NAME]]";
  29. interface Z(T:! type) {
  30. let X:! type;
  31. }
  32. impl forall [T:! type] T as Z(T) where .X = () {}
  33. class C {}
  34. impl C as Z(C) where .X = C {}
  35. fn F() {
  36. // The specialization of `Z(C)` should match in preference to the blanket impl
  37. // of `Z(T)`. If the blanket impl is chosen, then `a` will have type `()`
  38. // which will fail to typecheck here.
  39. let a: C.(Z(C).X) = {} as C;
  40. }
  41. // --- specialized_constraint_first.carbon
  42. library "[[@TEST_NAME]]";
  43. interface Z(T:! type) {
  44. let X:! type;
  45. }
  46. class C {}
  47. impl C as Z(C) where .X = C {}
  48. impl forall [T:! type] C as Z(T) where .X = () {}
  49. fn F() {
  50. // The specialization of `Z(C)` should match in preference to the blanket impl
  51. // of `Z(T)`. If the blanket impl is chosen, then `a` will have type `()`
  52. // which will fail to typecheck here.
  53. let a: C.(Z(C).X) = {} as C;
  54. }
  55. // --- specialized_constraint_second.carbon
  56. library "[[@TEST_NAME]]";
  57. interface Z(T:! type) {
  58. let X:! type;
  59. }
  60. class C {}
  61. impl forall [T:! type] C as Z(T) where .X = () {}
  62. impl C as Z(C) where .X = C {}
  63. fn F() {
  64. // The specialization of `Z(C)` should match in preference to the blanket impl
  65. // of `Z(T)`. If the blanket impl is chosen, then `a` will have type `()`
  66. // which will fail to typecheck here.
  67. let a: C.(Z(C).X) = {} as C;
  68. }
  69. // --- specialized_self_vs_constraint_self_first.carbon
  70. library "[[@TEST_NAME]]";
  71. interface Z(T:! type) {
  72. let X:! type;
  73. }
  74. class C(T:! type) {}
  75. impl forall [T:! type] C(()) as Z(T) where .X = C(()) {}
  76. impl forall [T:! type] C(T) as Z(C(())) where .X = () {}
  77. fn F() {
  78. // The specialization of `C(())` should match in preference to the blanket impl
  79. // of `C(T)`. If the blanket impl is chosen, then `a` will have type `()`
  80. // which will fail to typecheck here.
  81. let a: C(()).(Z(C(())).X) = {} as C(());
  82. }
  83. // --- specialized_self_vs_constraint_self_second.carbon
  84. library "[[@TEST_NAME]]";
  85. interface Z(T:! type) {
  86. let X:! type;
  87. }
  88. class C(T:! type) {}
  89. impl forall [T:! type] C(T) as Z(C(())) where .X = () {}
  90. impl forall [T:! type] C(()) as Z(T) where .X = C(()) {}
  91. fn F() {
  92. // The specialization of `Z(C)` should match in preference to the blanket impl
  93. // of `Z(T)`. If the blanket impl is chosen, then `a` will have type `()`
  94. // which will fail to typecheck here.
  95. let a: C(()).(Z(C(())).X) = {} as C(());
  96. }
  97. // --- generic_class_with_fully_specified_impl.carbon
  98. library "[[@TEST_NAME]]";
  99. interface Z(T:! type) {
  100. let X:! type;
  101. }
  102. impl forall [T:! type] T as Z(T) where .X = {.a: ()} {}
  103. class C(T:! type) {}
  104. impl C(()) as Z(()) where .X = C(()) {}
  105. impl forall [T:! type] C(T) as Z(T) where .X = {.b: ()} {}
  106. fn F() {
  107. // The specialization of `Z(C)` should match in preference to the blanket
  108. // impls of `Z(T)`. If a blanket impl is chosen, then `a` will have a struct
  109. // type which will fail to typecheck here when constructed from a `C` value.
  110. let a: C(()).(Z(()).X) = {} as C(());
  111. }
  112. // --- generic_class_with_blanket_impl_first.carbon
  113. library "[[@TEST_NAME]]";
  114. interface Z(T:! type) {
  115. let X:! type;
  116. }
  117. class C(T:! type) {}
  118. impl forall [T:! type] C(T) as Z(T) where .X = C(()) {}
  119. impl forall [T:! type] T as Z(T) where .X = () {}
  120. fn F() {
  121. // The specialization of `Z(C)` should match in preference to the blanket
  122. // impls of `Z(T)`. If a blanket impl is chosen, then `a` will have type `()`
  123. // which will fail to typecheck here when constructed from a `C` value.
  124. let a: C(()).(Z(()).X) = {} as C(());
  125. }
  126. // --- generic_class_with_blanket_impl_second.carbon
  127. library "[[@TEST_NAME]]";
  128. interface Z(T:! type) {
  129. let X:! type;
  130. }
  131. impl forall [T:! type] T as Z(T) where .X = () {}
  132. class C(T:! type) {}
  133. impl forall [T:! type] C(T) as Z(T) where .X = C(()) {}
  134. fn F() {
  135. // The specialization of `Z(C)` should match in preference to the blanket
  136. // impls of `Z(T)`. If a blanket impl is chosen, then `a` will have type `()`
  137. // which will fail to typecheck here when constructed from a `C` value.
  138. let a: C(()).(Z(()).X) = {} as C(());
  139. }
  140. // --- specialized_class_with_facet_value_param.carbon
  141. library "[[@TEST_NAME]]";
  142. interface Z {
  143. let X:! type;
  144. }
  145. class D(T:! type) {}
  146. impl forall [T:! type] D(T) as Z where .X = D(()) {}
  147. class E {}
  148. impl E as Z where .X = () {}
  149. class C(T:! Z, U:! Z) {}
  150. // This places a FacetValue of type FacetType(Z) at the position of `D` in the
  151. // self type, because the class C requires a facet value satisfying Z. It tests
  152. // that we correctly determine that this FacetType is not symbolic, and look at
  153. // the parametes of D for symbolic references.
  154. impl forall [T:! Z] C(T, D(E)) as Z where .X = () {}
  155. impl forall [T:! Z] C(D(T), T) as Z where .X = () {}
  156. // This is the best match, `T` is in the last position compared to the others.
  157. impl forall [T:! Z] C(D(E), T) as Z where .X = C(E, E) {}
  158. impl forall [T:! Z] C(T, T) as Z where .X = () {}
  159. fn F() {
  160. let a: C(D(E), D(E)).(Z.X) = {} as C(E, E);
  161. }
  162. // --- fail_specialized_class_with_symbolic_facet_value_param.carbon
  163. interface Z {
  164. let X:! type;
  165. }
  166. impl forall [T:! type] T as Z where .X = T {}
  167. interface Y {}
  168. class C(T:! Y) {}
  169. class D {}
  170. impl D as Y {}
  171. // D can be either a concrete or symbolic FacetValue, depending on what the
  172. // caller has.
  173. impl forall [D:! Y] C(D) as Z where .X = () {}
  174. fn F[D:! Y](d: D) {
  175. // The FacetValue deduced for the param of `C` will be a symbolic FacetValue
  176. // because we are in a generic where `D` is an unknown type, which will cause
  177. // the query and impl self type to be C(FacetValue) for a symbolic FacetValue.
  178. //
  179. // CHECK:STDERR: fail_specialized_class_with_symbolic_facet_value_param.carbon:[[@LINE+7]]:23: error: cannot implicitly convert expression of type `()` to `C(D).(Z.X)` [ConversionFailure]
  180. // CHECK:STDERR: let a: C(D).(Z.X) = ();
  181. // CHECK:STDERR: ^~
  182. // CHECK:STDERR: fail_specialized_class_with_symbolic_facet_value_param.carbon:[[@LINE+4]]:23: note: type `()` does not implement interface `Core.ImplicitAs(C(D).(Z.X))` [MissingImplInMemberAccessNote]
  183. // CHECK:STDERR: let a: C(D).(Z.X) = ();
  184. // CHECK:STDERR: ^~
  185. // CHECK:STDERR:
  186. let a: C(D).(Z.X) = ();
  187. }
  188. // --- pointer_specialization_first.carbon
  189. library "[[@TEST_NAME]]";
  190. interface Z {
  191. let X:! type;
  192. }
  193. class C {}
  194. impl C* as Z where .X = C {}
  195. impl forall [T:! type] T* as Z where .X = () {}
  196. fn F() {
  197. // The specialization of `Z(C)` should match in preference to the blanket impl
  198. // of `Z(T)`. If the blanket impl is chosen, then `a` will have type `()`
  199. // which will fail to typecheck here.
  200. let a: (C*).(Z.X) = {} as C;
  201. }
  202. // --- pointer_specialization_second.carbon
  203. library "[[@TEST_NAME]]";
  204. interface Z {
  205. let X:! type;
  206. }
  207. impl forall [T:! type] T* as Z where .X = () {}
  208. class C {}
  209. impl C* as Z where .X = C {}
  210. fn F() {
  211. // The specialization of `Z(C)` should match in preference to the blanket impl
  212. // of `Z(T)`. If the blanket impl is chosen, then `a` will have type `()`
  213. // which will fail to typecheck here.
  214. let a: (C*).(Z.X) = {} as C;
  215. }
  216. // --- cycle_in_deduce_avoided_by_specialization.carbon
  217. library "[[@TEST_NAME]]";
  218. interface Z {
  219. let X:! type;
  220. }
  221. class C(T:! type) {}
  222. // This impl makes a cycle, but it's not considered at all for `C(())` since
  223. // there is another impl with a better type structure, so no diagnostic is
  224. // emitted.
  225. impl forall [T:! Z] T as Z where .X = () {}
  226. // Also a cycle, and also a worse match for `C(())`.
  227. impl forall [T:! Z] C(T) as Z where .X = () {}
  228. impl C(()) as Z where .X = C(()) {}
  229. fn F() {
  230. let a: C(()).(Z.X) = {} as C(());
  231. }
  232. // --- final_specialization_before_generic_use_of_type_constant.carbon
  233. library "[[@TEST_NAME]]";
  234. interface Z(T:! type) {
  235. let X:! type;
  236. }
  237. class C {}
  238. impl forall [T:! type, U:! type] T as Z(U) where .X = () {}
  239. final impl forall [T:! type] T as Z(C) where .X = C {}
  240. fn F[U:! type](T:! Z(C)) {
  241. // The value of `.X` can be known to be `C` here when the impl `T as Z(C)` is
  242. // final.
  243. let a: T.X = {} as C;
  244. }
  245. // --- fail_specialization_written_after_generic_use_of_type_constant.carbon
  246. library "[[@TEST_NAME]]";
  247. interface Z(T:! type) {
  248. let X:! type;
  249. }
  250. class C {}
  251. impl forall [T:! type, U:! type] T as Z(U) where .X = () {}
  252. fn F[U:! type](T:! Z(C)) {
  253. // The value of `.X` is symbolic, it can't be assigned a value of type `C`.
  254. // CHECK:STDERR: fail_specialization_written_after_generic_use_of_type_constant.carbon:[[@LINE+7]]:16: error: cannot implicitly convert expression of type `C` to `T.(Z(C).X)` [ConversionFailure]
  255. // CHECK:STDERR: let a: T.X = {} as C;
  256. // CHECK:STDERR: ^~~~~~~
  257. // CHECK:STDERR: fail_specialization_written_after_generic_use_of_type_constant.carbon:[[@LINE+4]]:16: note: type `C` does not implement interface `Core.ImplicitAs(T.(Z(C).X))` [MissingImplInMemberAccessNote]
  258. // CHECK:STDERR: let a: T.X = {} as C;
  259. // CHECK:STDERR: ^~~~~~~
  260. // CHECK:STDERR:
  261. let a: T.X = {} as C;
  262. }
  263. final impl forall [T:! type] T as Z(C) where .X = C {}
  264. // --- specialization_written_after_generic_use.carbon
  265. library "[[@TEST_NAME]]";
  266. interface Z {
  267. let V:! type;
  268. fn ZZ() -> V;
  269. }
  270. interface Y {}
  271. impl forall [T:! Y] T as Z where .V = () {
  272. fn ZZ() -> () { return (); }
  273. }
  274. fn H(W:! Z, X: W.(Z.V)) -> W.(Z.V) {
  275. return X;
  276. }
  277. fn G(U:! Y) -> U.(Z.V) {
  278. return H(U, U.(Z.ZZ)());
  279. }
  280. class C {
  281. impl as Y {}
  282. }
  283. impl C as Z where .V = {} {
  284. fn ZZ() -> {} { return {}; }
  285. }
  286. fn F() {
  287. let x: {} = G(C);
  288. }
  289. // --- specialization_written_after_generic_use_with_generic_interface.carbon
  290. library "[[@TEST_NAME]]";
  291. interface Z(T:! type) {
  292. let V:! type;
  293. fn ZZ() -> V;
  294. }
  295. interface Y {}
  296. impl forall [T:! Y] T as Z(T) where .V = () {
  297. fn ZZ() -> () { return (); }
  298. }
  299. fn H(U:! Y, W:! Y & Z(U), X: W.(Z(U).V)) -> W.(Z(U).V) {
  300. return X;
  301. }
  302. fn G(U:! Y) -> U.(Z(U).V) {
  303. return H(U, U, U.(Z(U).ZZ)());
  304. }
  305. class C {
  306. impl as Y {}
  307. }
  308. impl C as Z(C) where .V = {} {
  309. fn ZZ() -> {} { return {}; }
  310. }
  311. fn F() {
  312. let x: {} = G(C);
  313. }
  314. // --- type_structure_first_difference.carbon
  315. library "[[@TEST_NAME]]";
  316. interface Z(T:! type) {
  317. let X:! type;
  318. fn MakeX() -> X;
  319. }
  320. class C {}
  321. // Type structure: "?(?)"
  322. impl forall [T:! type] T as Z(T) where .X = {.less_good: ()} {
  323. fn MakeX() -> {.less_good: ()} { return {.less_good = ()}; }
  324. }
  325. // Type structure: "?(c)". Should outrank the previous impl.
  326. impl forall [T:! type] T as Z(C) where .X = () {
  327. fn MakeX() -> () { return (); }
  328. }
  329. fn F(T:! Z(C)) -> T.(Z(C).X) {
  330. return T.MakeX();
  331. }
  332. fn G() {
  333. // This won't typecheck if the first impl is selected.
  334. let a: () = F(C);
  335. }
  336. // --- extend_impl_as_specialization.carbon
  337. library "[[@TEST_NAME]]";
  338. interface Z(T:! type) {
  339. let X:! type;
  340. }
  341. impl forall [T:! type, S:! type] T as Z(S) where .X = {} {}
  342. class C(S:! type) {
  343. extend impl as Z(S) where .X = () {}
  344. fn CC(a: Self.(Z(S).X)) -> Self.(Z(S).X) { return a; }
  345. }
  346. fn F() {
  347. let a: () = C(()).CC(());
  348. let b: C(()).X = a;
  349. }
  350. // --- final_impl_as_specialization.carbon
  351. library "[[@TEST_NAME]]";
  352. interface Z(T:! type) {
  353. let X:! type;
  354. }
  355. impl forall [T:! type, S:! type] T as Z(S) where .X = {} {}
  356. class C(S:! type) {
  357. final impl as Z(S) where .X = () {}
  358. fn CC() -> Self.(Z(S).X) { return (); }
  359. }
  360. fn F() {
  361. let a: () = C(()).CC();
  362. }
  363. // --- final_extend_impl_as_specialization.carbon
  364. library "[[@TEST_NAME]]";
  365. interface Z(T:! type) {
  366. let X:! type;
  367. }
  368. impl forall [T:! type, S:! type] T as Z(S) where .X = {} {}
  369. class C(S:! type) {
  370. extend final impl as Z(S) where .X = () {}
  371. fn CC() -> Self.(Z(S).X) { return (); }
  372. }
  373. fn F() {
  374. let a: () = C(()).CC();
  375. let b: C(()).X = a;
  376. }