require_invalid.carbon 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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/facet/require_invalid.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/facet/require_invalid.carbon
  12. // --- fail_require_outside_scope.carbon
  13. library "[[@TEST_NAME]]";
  14. interface Y {}
  15. // CHECK:STDERR: fail_require_outside_scope.carbon:[[@LINE+4]]:1: error: `require` can only be used in an `interface` or `constraint` [RequireInWrongScope]
  16. // CHECK:STDERR: require impls Y;
  17. // CHECK:STDERR: ^~~~~~~
  18. // CHECK:STDERR:
  19. require impls Y;
  20. // --- fail_require_in_class.carbon
  21. library "[[@TEST_NAME]]";
  22. interface Y {}
  23. class C {
  24. // CHECK:STDERR: fail_require_in_class.carbon:[[@LINE+4]]:3: error: `require` can only be used in an `interface` or `constraint` [RequireInWrongScope]
  25. // CHECK:STDERR: require impls Y;
  26. // CHECK:STDERR: ^~~~~~~
  27. // CHECK:STDERR:
  28. require impls Y;
  29. }
  30. // --- fail_require_in_fn.carbon
  31. library "[[@TEST_NAME]]";
  32. interface Y {}
  33. fn F() {
  34. // require is not allowed outside of `interface` or `constraint`.
  35. //
  36. // CHECK:STDERR: fail_require_in_fn.carbon:[[@LINE+8]]:3: error: expected expression [ExpectedExpr]
  37. // CHECK:STDERR: require impls Y;
  38. // CHECK:STDERR: ^~~~~~~
  39. // CHECK:STDERR:
  40. // CHECK:STDERR: fail_require_in_fn.carbon:[[@LINE+4]]:3: error: semantics TODO: `handle invalid parse trees in `check`` [SemanticsTodo]
  41. // CHECK:STDERR: require impls Y;
  42. // CHECK:STDERR: ^~~~~~~
  43. // CHECK:STDERR:
  44. require impls Y;
  45. }
  46. // --- fail_require_in_nested_class.carbon
  47. library "[[@TEST_NAME]]";
  48. interface Y {}
  49. interface Z {
  50. // TODO: Add `default` modifier.
  51. fn F() {
  52. class C {
  53. // CHECK:STDERR: fail_require_in_nested_class.carbon:[[@LINE+4]]:7: error: `require` can only be used in an `interface` or `constraint` [RequireInWrongScope]
  54. // CHECK:STDERR: require impls Y;
  55. // CHECK:STDERR: ^~~~~~~
  56. // CHECK:STDERR:
  57. require impls Y;
  58. }
  59. }
  60. }
  61. // --- fail_require_in_nested_fn.carbon
  62. library "[[@TEST_NAME]]";
  63. interface Y {}
  64. interface Z {
  65. // TODO: Add `default` modifier.
  66. fn F() {
  67. // require is not allowed outside of `interface` or `constraint`.
  68. //
  69. // CHECK:STDERR: fail_require_in_nested_fn.carbon:[[@LINE+8]]:5: error: expected expression [ExpectedExpr]
  70. // CHECK:STDERR: require impls Y;
  71. // CHECK:STDERR: ^~~~~~~
  72. // CHECK:STDERR:
  73. // CHECK:STDERR: fail_require_in_nested_fn.carbon:[[@LINE+4]]:5: error: semantics TODO: `handle invalid parse trees in `check`` [SemanticsTodo]
  74. // CHECK:STDERR: require impls Y;
  75. // CHECK:STDERR: ^~~~~~~
  76. // CHECK:STDERR:
  77. require impls Y;
  78. }
  79. }
  80. // --- fail_errors_in_require_still_found.carbon
  81. library "[[@TEST_NAME]]";
  82. class C {
  83. // The `require` is diagnosed as being in the wrong place. But we can still
  84. // diagnose issues inside the decl too.
  85. //
  86. // CHECK:STDERR: fail_errors_in_require_still_found.carbon:[[@LINE+8]]:3: error: `require` can only be used in an `interface` or `constraint` [RequireInWrongScope]
  87. // CHECK:STDERR: require impls Y;
  88. // CHECK:STDERR: ^~~~~~~
  89. // CHECK:STDERR:
  90. // CHECK:STDERR: fail_errors_in_require_still_found.carbon:[[@LINE+4]]:17: error: name `Y` not found [NameNotFound]
  91. // CHECK:STDERR: require impls Y;
  92. // CHECK:STDERR: ^
  93. // CHECK:STDERR:
  94. require impls Y;
  95. }
  96. // --- fail_extend_require_type.carbon
  97. library "[[@TEST_NAME]]";
  98. interface Y {}
  99. interface Z {
  100. // CHECK:STDERR: fail_extend_require_type.carbon:[[@LINE+4]]:18: error: `extend require impls` with explicit type [RequireImplsExtendWithExplicitSelf]
  101. // CHECK:STDERR: extend require () impls Y;
  102. // CHECK:STDERR: ^~
  103. // CHECK:STDERR:
  104. extend require () impls Y;
  105. fn F() {
  106. // The erroneous self-type for an `extend` causes the error to be propagated
  107. // into the interface scope, which prevents errors if we fail to find a name
  108. // in that scope.
  109. Self.A;
  110. }
  111. }
  112. // --- fail_require_with_nonexistent_type.carbon
  113. library "[[@TEST_NAME]]";
  114. interface Y {}
  115. interface Z {
  116. // CHECK:STDERR: fail_require_with_nonexistent_type.carbon:[[@LINE+4]]:11: error: name `nonexistent` not found [NameNotFound]
  117. // CHECK:STDERR: require nonexistent impls Y;
  118. // CHECK:STDERR: ^~~~~~~~~~~
  119. // CHECK:STDERR:
  120. require nonexistent impls Y;
  121. fn F() {
  122. // The name lookup error still happens, since the `require` is not `extend`.
  123. // CHECK:STDERR: fail_require_with_nonexistent_type.carbon:[[@LINE+4]]:5: error: member name `A` not found in `Z` [MemberNameNotFoundInInstScope]
  124. // CHECK:STDERR: Self.A;
  125. // CHECK:STDERR: ^~~~~~
  126. // CHECK:STDERR:
  127. Self.A;
  128. }
  129. }
  130. // --- fail_extend_require_with_nonexistent_type.carbon
  131. library "[[@TEST_NAME]]";
  132. interface Y {}
  133. interface Z {
  134. // CHECK:STDERR: fail_extend_require_with_nonexistent_type.carbon:[[@LINE+4]]:18: error: name `nonexistent` not found [NameNotFound]
  135. // CHECK:STDERR: extend require nonexistent impls Y;
  136. // CHECK:STDERR: ^~~~~~~~~~~
  137. // CHECK:STDERR:
  138. extend require nonexistent impls Y;
  139. fn F() {
  140. // The erroneous self-type for an `extend` causes the error to be propagated
  141. // into the interface scope, which prevents errors if we fail to find a name
  142. // in that scope.
  143. Self.A;
  144. }
  145. }
  146. // --- fail_extend_require_with_nonexistent_type_pointer.carbon
  147. library "[[@TEST_NAME]]";
  148. interface Y {}
  149. interface Z {
  150. // CHECK:STDERR: fail_extend_require_with_nonexistent_type_pointer.carbon:[[@LINE+4]]:18: error: name `nonexistent` not found [NameNotFound]
  151. // CHECK:STDERR: extend require nonexistent* impls Y;
  152. // CHECK:STDERR: ^~~~~~~~~~~
  153. // CHECK:STDERR:
  154. extend require nonexistent* impls Y;
  155. fn F() {
  156. // The erroneous self-type for an `extend` causes the error to be propagated
  157. // into the interface scope, which prevents errors if we fail to find a name
  158. // in that scope.
  159. Self.A;
  160. }
  161. }