name_poisoning.carbon 6.3 KB

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