specialization.carbon 12 KB

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