unused_generic_binding.carbon 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/unused_generic_binding.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/lookup/unused_generic_binding.carbon
  12. // --- fail_no_binding_used.carbon
  13. library "[[@TEST_NAME]]";
  14. interface I {
  15. let T:! type;
  16. }
  17. impl forall [U:! type] U as I where .T = () {}
  18. class C {}
  19. // This impl can never deduce its implicit generic parameter `V`, which
  20. // should be diagnosed.
  21. //
  22. // CHECK:STDERR: fail_no_binding_used.carbon:[[@LINE+4]]:13: error: `impl` with unused generic binding [ImplUnusedBinding]
  23. // CHECK:STDERR: impl forall [V:! type] C as I where .T = {.unmatched: ()} {}
  24. // CHECK:STDERR: ^~~~~~~~~~
  25. // CHECK:STDERR:
  26. impl forall [V:! type] C as I where .T = {.unmatched: ()} {}
  27. fn F() {
  28. // This shows that the first impl declaration was matched even though the
  29. // second is more specific. But since it has an unused generic parameter, it
  30. // will never match. If the second was matched, this line would not type
  31. // check.
  32. let x: C.(I.T) = ();
  33. }
  34. // --- fail_one_binding_unused.carbon
  35. library "[[@TEST_NAME]]";
  36. interface I {
  37. let T:! type;
  38. }
  39. impl forall [U:! type] U as I where .T = () {}
  40. class C {}
  41. // This impl can never deduce its implicit generic parameter `W`, which
  42. // should be diagnosed.
  43. // https://discord.com/channels/655572317891461132/941071822756143115/1354563497731686550
  44. //
  45. // CHECK:STDERR: fail_one_binding_unused.carbon:[[@LINE+4]]:13: error: `impl` with unused generic binding [ImplUnusedBinding]
  46. // CHECK:STDERR: impl forall [V:! type, W:! type] V as I where .T = {.unmatched: ()} {}
  47. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~
  48. // CHECK:STDERR:
  49. impl forall [V:! type, W:! type] V as I where .T = {.unmatched: ()} {}
  50. fn F() {
  51. // This shows that the first impl declaration was matched even though the
  52. // second is more specific. If the second was matched, it would not type
  53. // check.
  54. let x: C.(I.T) = ();
  55. }
  56. // --- fail_inherited_binding_unused.carbon
  57. library "[[@TEST_NAME]]";
  58. interface I {
  59. let T:! type;
  60. }
  61. impl forall [U:! type] U as I where .T = () {}
  62. class C(U:! type) {
  63. // This impl can never deduce its inherited generic binding `U`, which
  64. // should be diagnosed.
  65. //
  66. // TODO: We should point the diagnostic at the binding `U` in C.
  67. //
  68. // CHECK:STDERR: fail_inherited_binding_unused.carbon:[[@LINE+4]]:3: error: `impl` with unused generic binding [ImplUnusedBinding]
  69. // CHECK:STDERR: impl C(()) as I where .T = {.unmatched: ()} {}
  70. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  71. // CHECK:STDERR:
  72. impl C(()) as I where .T = {.unmatched: ()} {}
  73. }
  74. fn F() {
  75. let x: C(()).(I.T) = ();
  76. }
  77. // --- fail_inherited_binding_in_forall_unused.carbon
  78. library "[[@TEST_NAME]]";
  79. interface I {
  80. let T:! type;
  81. }
  82. impl forall [U:! type] U as I where .T = () {}
  83. class C(U:! type) {
  84. // This impl can never deduce its inherited generic binding `U`, which
  85. // should be diagnosed.
  86. //
  87. // TODO: We should point the diagnostic at the binding `U` in C.
  88. //
  89. // CHECK:STDERR: fail_inherited_binding_in_forall_unused.carbon:[[@LINE+4]]:15: error: `impl` with unused generic binding [ImplUnusedBinding]
  90. // CHECK:STDERR: impl forall [V:! type] C(V) as I where .T = {.unmatched: ()} {}
  91. // CHECK:STDERR: ^~~~~~~~~~
  92. // CHECK:STDERR:
  93. impl forall [V:! type] C(V) as I where .T = {.unmatched: ()} {}
  94. }
  95. fn F() {
  96. let x: C(()).(I.T) = ();
  97. }
  98. // --- inherited_binding_used.carbon
  99. library "[[@TEST_NAME]]";
  100. interface I {
  101. let T:! type;
  102. }
  103. impl forall [U:! type] U as I where .T = {.unmatched: ()} {}
  104. class C(U:! type) {
  105. // `U` can be deduced here, so no diagnostic issued.
  106. impl C(U) as I where .T = () {}
  107. }
  108. fn F() {
  109. // The `impl` inside C is matched here.
  110. let x: C(()).(I.T) = ();
  111. }
  112. // --- inherited_binding_in_forall_used.carbon
  113. library "[[@TEST_NAME]]";
  114. interface I(W:! type) {
  115. let T:! type;
  116. }
  117. impl forall [U:! type] U as I(U) where .T = {.unmatched: ()} {}
  118. class C(U:! type) {
  119. // `U` can be deduced here, so no diagnostic issued.
  120. impl forall [V:! type] C(U) as I(V) where .T = () {}
  121. }
  122. fn F() {
  123. // The `impl` inside C is matched here.
  124. let x: C(()).(I({}).T) = ();
  125. }