find_in_final.carbon 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/find_in_final.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/lookup/find_in_final.carbon
  12. // --- final_impl_precedence_over_facet.carbon
  13. library "[[@TEST_NAME]]";
  14. interface I {
  15. let T:! type;
  16. }
  17. final impl forall [U:! type] U as I where .T = () {}
  18. fn F(V:! I) -> V.T {
  19. // Even though we have a witness that `V impls I` from the constraint on `I`,
  20. // we should do an impl lookup to see if any effectively final impl applies
  21. // when we find an unknown value in that witness. In this case, that lookup
  22. // would find an impl with more specific values for associated constants that
  23. // we should merge.
  24. return ();
  25. }
  26. // --- final_impl_precedence_over_facet_with_where.carbon
  27. library "[[@TEST_NAME]]";
  28. interface Z {
  29. let X:! type;
  30. let Y:! type;
  31. }
  32. final impl forall [T:! type] T as Z where .X = () and .Y = () {}
  33. fn F(ZZ:! Z where .X = ()) {
  34. // Z.Y is unspecified on `ZZ` so it's found on the final impl where it's known
  35. // to be the concrete type (), which can then be used in this generic
  36. // function.
  37. let a: ZZ.Y = ();
  38. }
  39. // --- final_impl_precedence_over_facet_access_type_with_where.carbon
  40. library "[[@TEST_NAME]]";
  41. interface Z {
  42. let X:! type;
  43. let Y:! type;
  44. }
  45. final impl forall [T:! type] T as Z where .X = () and .Y = () {}
  46. fn F[T:! Z where .X = ()](z: T) {
  47. // z.Y is unspecified on `ZZ` so it's found on the final impl where it's known
  48. // to be the concrete type (), which can then be used in this generic
  49. // function.
  50. let a: z.Y = ();
  51. }
  52. // --- final_impl_makes_compatible_facet_values.carbon
  53. library "[[@TEST_NAME]]";
  54. interface I { let X:! type; }
  55. interface J {}
  56. final impl forall [T:! J] T as I where .X = () {}
  57. class C(T:! I) {
  58. var b: T.X;
  59. }
  60. class D(T:! J) {
  61. var c: C(T)*;
  62. }
  63. fn F(T:! I & J) -> () {
  64. // The witness for `I` found here directly, and inside `D` from a FacetValue
  65. // come from the same facet, the `T` binding in the params of F, so they
  66. // should be compatible.
  67. var x: C(T);
  68. var y: D(T);
  69. y.c = &x;
  70. return (*y.c).b;
  71. }
  72. // --- non_final_impl_makes_compatible_facet_values.carbon
  73. library "[[@TEST_NAME]]";
  74. interface I { let X:! type; }
  75. interface J {}
  76. impl forall [T:! J] T as I where .X = () {}
  77. class C(T:! I) {
  78. var b: T.X;
  79. }
  80. class D(T:! J) {
  81. var c: C(T)*;
  82. }
  83. fn F(T:! I & J) -> T.(I.X)* {
  84. var x: C(T);
  85. var y: D(T);
  86. // The witness for `I` found here directly, and inside `D` from a FacetValue
  87. // come from the same facet, the `T` binding in the params of F, so they
  88. // should be compatible.
  89. y.c = &x;
  90. return &(*y.c).b;
  91. }
  92. // --- todo_fail_facet_value_rewrite_incompatible_with_final_impl.carbon
  93. library "[[@TEST_NAME]]";
  94. interface Z {
  95. let X:! type;
  96. }
  97. final impl forall [T:! type] T as Z where .X = () {}
  98. // TODO: This should be diagnosed as there is a final impl defining `.X = ()`,
  99. // which makes the LHS of this rewrite constraint concrete. And since the RHS is
  100. // not the same (or convertible from), the rewrite is impossible.
  101. fn F(ZZ:! Z where .X = {.r: ()}) {}