name_poisoning.carbon 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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/none.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/interface/name_poisoning.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interface/name_poisoning.carbon
  12. // --- no_poison.carbon
  13. library "[[@TEST_NAME]]";
  14. interface I;
  15. // `N.F` uses `N.I` and not `package.I`.
  16. namespace N;
  17. interface N.I {}
  18. fn N.F(x:! I) {}
  19. fn TestCall(x:! N.I) {
  20. // `N.F` accepts an `N.I` not a `package.I`.
  21. N.F(x);
  22. }
  23. // --- poison.carbon
  24. library "[[@TEST_NAME]]";
  25. interface I;
  26. namespace N;
  27. // Use `package.I` and poison `N.I`.
  28. fn N.F(x:! I);
  29. // --- fail_declare_after_poison.carbon
  30. library "[[@TEST_NAME]]";
  31. interface I;
  32. namespace N;
  33. // Use `package.I` and poison `N.I`.
  34. // CHECK:STDERR: fail_declare_after_poison.carbon:[[@LINE+3]]:12: error: name `I` used before it was declared [NameUseBeforeDecl]
  35. // CHECK:STDERR: fn N.F(x:! I);
  36. // CHECK:STDERR: ^
  37. fn N.F(x:! I);
  38. // Failure: N.I declared after it was poisoned.
  39. // CHECK:STDERR: fail_declare_after_poison.carbon:[[@LINE+4]]:13: note: declared here [NameUseBeforeDeclNote]
  40. // CHECK:STDERR: interface N.I {}
  41. // CHECK:STDERR: ^
  42. // CHECK:STDERR:
  43. interface N.I {}
  44. // --- fail_use_poison.carbon
  45. library "[[@TEST_NAME]]";
  46. interface I;
  47. namespace N;
  48. // Use `package.I` and poison `N.I`.
  49. fn N.F1(x:! I);
  50. // CHECK:STDERR: fail_use_poison.carbon:[[@LINE+4]]:13: error: member name `I` not found in `N` [MemberNameNotFoundInInstScope]
  51. // CHECK:STDERR: fn N.F2(x:! N.I);
  52. // CHECK:STDERR: ^~~
  53. // CHECK:STDERR:
  54. fn N.F2(x:! N.I);
  55. // --- fail_use_declaration_after_poison.carbon
  56. library "[[@TEST_NAME]]";
  57. interface I;
  58. namespace N;
  59. // Use `package.I` and poison `N.I`.
  60. // CHECK:STDERR: fail_use_declaration_after_poison.carbon:[[@LINE+3]]:13: error: name `I` used before it was declared [NameUseBeforeDecl]
  61. // CHECK:STDERR: fn N.F1(x:! I);
  62. // CHECK:STDERR: ^
  63. fn N.F1(x:! I);
  64. // CHECK:STDERR: fail_use_declaration_after_poison.carbon:[[@LINE+4]]:13: note: declared here [NameUseBeforeDeclNote]
  65. // CHECK:STDERR: interface N.I;
  66. // CHECK:STDERR: ^
  67. // CHECK:STDERR:
  68. interface N.I;
  69. // CHECK:STDERR: fail_use_declaration_after_poison.carbon:[[@LINE+4]]:13: error: member name `I` not found in `N` [MemberNameNotFoundInInstScope]
  70. // CHECK:STDERR: fn N.F2(x:! N.I);
  71. // CHECK:STDERR: ^~~
  72. // CHECK:STDERR:
  73. fn N.F2(x:! N.I);
  74. // --- fail_alias.carbon
  75. library "[[@TEST_NAME]]";
  76. interface I;
  77. namespace N;
  78. // CHECK:STDERR: fail_alias.carbon:[[@LINE+7]]:13: error: name `I` used before it was declared [NameUseBeforeDecl]
  79. // CHECK:STDERR: alias N.I = I;
  80. // CHECK:STDERR: ^
  81. // CHECK:STDERR: fail_alias.carbon:[[@LINE+4]]:9: note: declared here [NameUseBeforeDeclNote]
  82. // CHECK:STDERR: alias N.I = I;
  83. // CHECK:STDERR: ^
  84. // CHECK:STDERR:
  85. alias N.I = I;
  86. // --- fail_poison_multiple_scopes.carbon
  87. library "[[@TEST_NAME]]";
  88. interface I1;
  89. interface I2 {
  90. interface I3 {
  91. interface I4 {
  92. // Use `package.I1` and poison:
  93. // * `I2.I1`
  94. // * `I2.I3.I1`
  95. // * `I2.I3.I4.I1`
  96. // CHECK:STDERR: fail_poison_multiple_scopes.carbon:[[@LINE+3]]:16: error: name `I1` used before it was declared [NameUseBeforeDecl]
  97. // CHECK:STDERR: fn F(x:! I1);
  98. // CHECK:STDERR: ^~
  99. fn F(x:! I1);
  100. // CHECK:STDERR: fail_poison_multiple_scopes.carbon:[[@LINE+7]]:17: note: declared here [NameUseBeforeDeclNote]
  101. // CHECK:STDERR: interface I1;
  102. // CHECK:STDERR: ^~
  103. // CHECK:STDERR:
  104. // CHECK:STDERR: fail_poison_multiple_scopes.carbon:[[@LINE-6]]:16: error: name `I1` used before it was declared [NameUseBeforeDecl]
  105. // CHECK:STDERR: fn F(x:! I1);
  106. // CHECK:STDERR: ^~
  107. interface I1;
  108. }
  109. // CHECK:STDERR: fail_poison_multiple_scopes.carbon:[[@LINE+7]]:15: note: declared here [NameUseBeforeDeclNote]
  110. // CHECK:STDERR: interface I1;
  111. // CHECK:STDERR: ^~
  112. // CHECK:STDERR:
  113. // CHECK:STDERR: fail_poison_multiple_scopes.carbon:[[@LINE-15]]:16: error: name `I1` used before it was declared [NameUseBeforeDecl]
  114. // CHECK:STDERR: fn F(x:! I1);
  115. // CHECK:STDERR: ^~
  116. interface I1;
  117. }
  118. // CHECK:STDERR: fail_poison_multiple_scopes.carbon:[[@LINE+4]]:13: note: declared here [NameUseBeforeDeclNote]
  119. // CHECK:STDERR: interface I1;
  120. // CHECK:STDERR: ^~
  121. // CHECK:STDERR:
  122. interface I1;
  123. }
  124. // --- ignored_poison_in_import.carbon
  125. library "[[@TEST_NAME]]";
  126. import library "poison";
  127. // This doesn't fail.
  128. interface N.I;
  129. // --- poison.impl.carbon
  130. impl library "[[@TEST_NAME]]";
  131. // TODO: #4622 This should fail since `N.I` was poisoned in the api.
  132. interface N.I;
  133. // --- fail_poison_when_lookup_fails.carbon
  134. library "[[@TEST_NAME]]";
  135. namespace N;
  136. // `package.I` and `N.I` poisoned when not found.
  137. // CHECK:STDERR: fail_poison_when_lookup_fails.carbon:[[@LINE+7]]:12: error: name `I` not found [NameNotFound]
  138. // CHECK:STDERR: fn N.F(x:! I);
  139. // CHECK:STDERR: ^
  140. // CHECK:STDERR:
  141. // CHECK:STDERR: fail_poison_when_lookup_fails.carbon:[[@LINE+3]]:12: error: name `I` used before it was declared [NameUseBeforeDecl]
  142. // CHECK:STDERR: fn N.F(x:! I);
  143. // CHECK:STDERR: ^
  144. fn N.F(x:! I);
  145. // TODO: We should ideally only produce one diagnostic here.
  146. // CHECK:STDERR: fail_poison_when_lookup_fails.carbon:[[@LINE+7]]:11: note: declared here [NameUseBeforeDeclNote]
  147. // CHECK:STDERR: interface I;
  148. // CHECK:STDERR: ^
  149. // CHECK:STDERR:
  150. // CHECK:STDERR: fail_poison_when_lookup_fails.carbon:[[@LINE-7]]:12: error: name `I` used before it was declared [NameUseBeforeDecl]
  151. // CHECK:STDERR: fn N.F(x:! I);
  152. // CHECK:STDERR: ^
  153. interface I;
  154. // CHECK:STDERR: fail_poison_when_lookup_fails.carbon:[[@LINE+4]]:13: note: declared here [NameUseBeforeDeclNote]
  155. // CHECK:STDERR: interface N.I;
  156. // CHECK:STDERR: ^
  157. // CHECK:STDERR:
  158. interface N.I;
  159. // --- fail_poison_with_lexical_result.carbon
  160. library "[[@TEST_NAME]]";
  161. fn F() {
  162. interface I1 {}
  163. class C {
  164. // CHECK:STDERR: fail_poison_with_lexical_result.carbon:[[@LINE+3]]:12: error: name `I1` used before it was declared [NameUseBeforeDecl]
  165. // CHECK:STDERR: var v: I1;
  166. // CHECK:STDERR: ^~
  167. var v: I1;
  168. // CHECK:STDERR: fail_poison_with_lexical_result.carbon:[[@LINE+4]]:15: note: declared here [NameUseBeforeDeclNote]
  169. // CHECK:STDERR: interface I1;
  170. // CHECK:STDERR: ^~
  171. // CHECK:STDERR:
  172. interface I1;
  173. }
  174. }