specialization_poison.carbon 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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_poison.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_poison.carbon
  12. // --- fail_final_poisoned_concrete_query_in_specific.carbon
  13. library "[[@TEST_NAME]]";
  14. interface I {
  15. let T:! type;
  16. fn F[self: Self]() -> T;
  17. }
  18. impl forall [U:! type] U as I where .T = () {
  19. fn F[self: Self]() -> () { return (); }
  20. }
  21. // CHECK:STDERR: fail_final_poisoned_concrete_query_in_specific.carbon:[[@LINE+3]]:25: error: found `impl` that would change the result of an earlier use of `C* as I` [PoisonedImplLookupConcreteResult]
  22. // CHECK:STDERR: fn H[W:! type](v: W) -> W.(I.T) {
  23. // CHECK:STDERR: ^~~~~~~
  24. fn H[W:! type](v: W) -> W.(I.T) {
  25. return v.(I.F)();
  26. }
  27. class C { adapt (); }
  28. fn G(p: C*) -> () {
  29. // This concrete impl lookup query poisons any further specializations.
  30. return H(p);
  31. }
  32. // CHECK:STDERR: fail_final_poisoned_concrete_query_in_specific.carbon:[[@LINE+7]]:1: note: the use would select the `impl` here but it was not found yet [PoisonedImplLookupConcreteResultNoteBadImpl]
  33. // CHECK:STDERR: impl C* as I where .T = C {
  34. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~
  35. // CHECK:STDERR: fail_final_poisoned_concrete_query_in_specific.carbon:[[@LINE-21]]:1: note: the use had selected the `impl` here [PoisonedImplLookupConcreteResultNotePreviousImpl]
  36. // CHECK:STDERR: impl forall [U:! type] U as I where .T = () {
  37. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  38. // CHECK:STDERR:
  39. impl C* as I where .T = C {
  40. fn F[self: Self]() -> C { return *self; }
  41. }
  42. // --- fail_final_poisoned_concrete_query_in_generic.carbon
  43. library "[[@TEST_NAME]]";
  44. interface I {
  45. let T:! type;
  46. }
  47. impl forall [U:! type] U as I where .T = () {}
  48. class C {}
  49. fn H[W:! type](v: W) {
  50. // This concrete impl lookup query poisons any further specializations.
  51. // CHECK:STDERR: fail_final_poisoned_concrete_query_in_generic.carbon:[[@LINE+3]]:10: error: found `impl` that would change the result of an earlier use of `C as I` [PoisonedImplLookupConcreteResult]
  52. // CHECK:STDERR: let a: C.(I.T) = ();
  53. // CHECK:STDERR: ^~~~~~~
  54. let a: C.(I.T) = ();
  55. }
  56. // CHECK:STDERR: fail_final_poisoned_concrete_query_in_generic.carbon:[[@LINE+7]]:1: note: the use would select the `impl` here but it was not found yet [PoisonedImplLookupConcreteResultNoteBadImpl]
  57. // CHECK:STDERR: impl C as I where .T = C {}
  58. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~
  59. // CHECK:STDERR: fail_final_poisoned_concrete_query_in_generic.carbon:[[@LINE-15]]:1: note: the use had selected the `impl` here [PoisonedImplLookupConcreteResultNotePreviousImpl]
  60. // CHECK:STDERR: impl forall [U:! type] U as I where .T = () {}
  61. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  62. // CHECK:STDERR:
  63. impl C as I where .T = C {}
  64. // --- fail_final_poisoned_concrete_query_nested_type_in_self.carbon
  65. library "[[@TEST_NAME]]";
  66. interface I {
  67. let T:! type;
  68. fn F[self: Self]() -> T;
  69. }
  70. class C(U:! type) {}
  71. impl forall [U:! type] C(U) as I where .T = () {
  72. fn F[self: Self]() -> () { return (); }
  73. }
  74. fn G(c: C(())) -> () {
  75. // This concrete impl lookup query poisons any further specializations.
  76. // CHECK:STDERR: fail_final_poisoned_concrete_query_nested_type_in_self.carbon:[[@LINE+3]]:10: error: found `impl` that would change the result of an earlier use of `C(()) as I` [PoisonedImplLookupConcreteResult]
  77. // CHECK:STDERR: return c.(I.F)();
  78. // CHECK:STDERR: ^~~~~~~
  79. return c.(I.F)();
  80. }
  81. // CHECK:STDERR: fail_final_poisoned_concrete_query_nested_type_in_self.carbon:[[@LINE+7]]:1: note: the use would select the `impl` here but it was not found yet [PoisonedImplLookupConcreteResultNoteBadImpl]
  82. // CHECK:STDERR: impl C(()) as I where .T = {} {
  83. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  84. // CHECK:STDERR: fail_final_poisoned_concrete_query_nested_type_in_self.carbon:[[@LINE-15]]:1: note: the use had selected the `impl` here [PoisonedImplLookupConcreteResultNotePreviousImpl]
  85. // CHECK:STDERR: impl forall [U:! type] C(U) as I where .T = () {
  86. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  87. // CHECK:STDERR:
  88. impl C(()) as I where .T = {} {
  89. fn F[self: Self]() -> {} { return {}; }
  90. }
  91. // --- fail_final_poisoned_concrete_query_nested_type_in_interface.carbon
  92. library "[[@TEST_NAME]]";
  93. interface I(U:! type) {
  94. let T:! type;
  95. fn F[self: Self]() -> T;
  96. }
  97. impl forall [U:! type] U as I(U) where .T = () {
  98. fn F[self: Self]() -> () { return (); }
  99. }
  100. class C {}
  101. fn G(c: C) -> () {
  102. // This concrete impl lookup query poisons any further specializations.
  103. // CHECK:STDERR: fail_final_poisoned_concrete_query_nested_type_in_interface.carbon:[[@LINE+3]]:10: error: found `impl` that would change the result of an earlier use of `C as I(C)` [PoisonedImplLookupConcreteResult]
  104. // CHECK:STDERR: return c.(I(C).F)();
  105. // CHECK:STDERR: ^~~~~~~~~~
  106. return c.(I(C).F)();
  107. }
  108. // CHECK:STDERR: fail_final_poisoned_concrete_query_nested_type_in_interface.carbon:[[@LINE+7]]:1: note: the use would select the `impl` here but it was not found yet [PoisonedImplLookupConcreteResultNoteBadImpl]
  109. // CHECK:STDERR: impl C as I(C) where .T = {} {
  110. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  111. // CHECK:STDERR: fail_final_poisoned_concrete_query_nested_type_in_interface.carbon:[[@LINE-17]]:1: note: the use had selected the `impl` here [PoisonedImplLookupConcreteResultNotePreviousImpl]
  112. // CHECK:STDERR: impl forall [U:! type] U as I(U) where .T = () {
  113. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  114. // CHECK:STDERR:
  115. impl C as I(C) where .T = {} {
  116. fn F[self: Self]() -> {} { return {}; }
  117. }
  118. // --- todo_fail_final_poisoned_by_generic_query.carbon
  119. library "[[@TEST_NAME]]";
  120. interface I {
  121. let T:! type;
  122. fn F[self: Self]() -> T;
  123. }
  124. impl forall [U:! type] U as I where .T = () {
  125. fn F[self: Self]() -> () { return (); }
  126. }
  127. fn H[W:! type](v: W) -> W.(I.T) {
  128. return v.(I.F)();
  129. }
  130. // This function could return a concrete `X` if it saw the `final` impl below.
  131. fn G[X:! type](p: X*) -> (X*).(I.T) {
  132. return H(p);
  133. }
  134. // TODO: This should be diagnosed as poisoned, as H() uses an associated
  135. // constant from the `impl` which was treated as a symbolic, but this would
  136. // change it to be a concrete type.
  137. final impl forall [V:! Core.Copy] V* as I where .T = V {
  138. fn F[self: Self]() -> V { return *self; }
  139. }