error_recovery.carbon 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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/impl/error_recovery.carbon
  10. // TIP: To dump output, run:
  11. // TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/impl/error_recovery.carbon
  12. // --- fail_runtime_generic_param.carbon
  13. library "[[@TEST_NAME]]";
  14. class C {}
  15. interface I {}
  16. //@dump-sem-ir-begin
  17. // CHECK:STDERR: fail_runtime_generic_param.carbon:[[@LINE+4]]:14: error: parameters of generic types must be constant [GenericParamMustBeConstant]
  18. // CHECK:STDERR: impl forall [T: type] C as I {}
  19. // CHECK:STDERR: ^~~~~~~
  20. // CHECK:STDERR:
  21. impl forall [T: type] C as I {}
  22. //@dump-sem-ir-end
  23. // --- fail_nonfinal_lookup_impl_witness_error_in_import.carbon
  24. library "[[@TEST_NAME]]";
  25. interface Z {}
  26. // CHECK:STDERR: fail_nonfinal_lookup_impl_witness_error_in_import.carbon:[[@LINE+4]]:1: error: impl declared but not defined [ImplMissingDefinition]
  27. // CHECK:STDERR: impl forall [T:! type] T as Z;
  28. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  29. // CHECK:STDERR:
  30. impl forall [T:! type] T as Z;
  31. fn F(U:! Z) {}
  32. //@dump-sem-ir-begin
  33. fn G(T:! type) {
  34. // This makes a LookupImplWitness instruction, but future lookups (evaluation
  35. // of this instruction with a specific) will result in an error since the impl
  36. // is never defined and is left with an error as its witness at the end of the
  37. // file. The lookups should not fail entirely, just result in an error
  38. // witness.
  39. F(T);
  40. }
  41. //@dump-sem-ir-end
  42. // --- nonfinal_lookup_impl_witness_error_in_import.impl.carbon
  43. impl library "[[@TEST_NAME]]";
  44. fn H() {
  45. // The specific here contains errors, but does not fail entirely and crash
  46. // when resolving the LookupImplWitness.
  47. G(());
  48. }
  49. // --- fail_nonfinal_lookup_impl_witness_error.carbon
  50. library "[[@TEST_NAME]]";
  51. interface Z {}
  52. // CHECK:STDERR: fail_nonfinal_lookup_impl_witness_error.carbon:[[@LINE+4]]:1: error: impl declared but not defined [ImplMissingDefinition]
  53. // CHECK:STDERR: impl forall [T:! type] T as Z;
  54. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  55. // CHECK:STDERR:
  56. impl forall [T:! type] T as Z;
  57. fn F(U:! Z) {}
  58. //@dump-sem-ir-begin
  59. fn G(T:! type) {
  60. // This makes a LookupImplWitness instruction, but future lookups (evaluation
  61. // of this instruction with a specific) will fail with an error since the impl
  62. // is never defined and is left with an error as its witness at the end of the
  63. // file. The lookups should not fail entirely, just result in an error
  64. // witness.
  65. F(T);
  66. }
  67. //@dump-sem-ir-end
  68. fn H() {
  69. // The specific here contains errors, but does not fail entirely and crash
  70. // when resolving the LookupImplWitness.
  71. G(());
  72. }
  73. // --- fail_final_lookup_impl_witness_error.carbon
  74. library "[[@TEST_NAME]]";
  75. interface Z {}
  76. // CHECK:STDERR: fail_final_lookup_impl_witness_error.carbon:[[@LINE+4]]:1: error: impl declared but not defined [ImplMissingDefinition]
  77. // CHECK:STDERR: impl forall [T:! type] T as Z;
  78. // CHECK:STDERR: ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  79. // CHECK:STDERR:
  80. impl forall [T:! type] T as Z;
  81. fn F(U:! Z) {}
  82. fn G() {
  83. // This impl lookup resolves to a final witness, which poisons any future
  84. // queries. At the end of the file, the poisoned queries are replayed to make
  85. // sure they don't change. However, here it is changed by the impl being
  86. // diagnosed with an error. The poisoning check should handle that gracefully.
  87. //@dump-sem-ir-begin
  88. F(());
  89. //@dump-sem-ir-end
  90. }
  91. // CHECK:STDOUT: --- fail_runtime_generic_param.carbon
  92. // CHECK:STDOUT:
  93. // CHECK:STDOUT: constants {
  94. // CHECK:STDOUT: %C: type = class_type @C [concrete]
  95. // CHECK:STDOUT: %I.type: type = facet_type <@I> [concrete]
  96. // CHECK:STDOUT: %I.impl_witness: <witness> = impl_witness file.%I.impl_witness_table [concrete]
  97. // CHECK:STDOUT: }
  98. // CHECK:STDOUT:
  99. // CHECK:STDOUT: file {
  100. // CHECK:STDOUT: impl_decl @C.as.I.impl [concrete] {} {
  101. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C]
  102. // CHECK:STDOUT: %I.ref: type = name_ref I, file.%I.decl [concrete = constants.%I.type]
  103. // CHECK:STDOUT: }
  104. // CHECK:STDOUT: %I.impl_witness_table = impl_witness_table (), @C.as.I.impl [concrete]
  105. // CHECK:STDOUT: %I.impl_witness: <witness> = impl_witness %I.impl_witness_table [concrete = constants.%I.impl_witness]
  106. // CHECK:STDOUT: }
  107. // CHECK:STDOUT:
  108. // CHECK:STDOUT: impl @C.as.I.impl: %C.ref as %I.ref {
  109. // CHECK:STDOUT: !members:
  110. // CHECK:STDOUT: witness = file.%I.impl_witness
  111. // CHECK:STDOUT: }
  112. // CHECK:STDOUT:
  113. // CHECK:STDOUT: --- fail_nonfinal_lookup_impl_witness_error_in_import.carbon
  114. // CHECK:STDOUT:
  115. // CHECK:STDOUT: constants {
  116. // CHECK:STDOUT: %Z.type: type = facet_type <@Z> [concrete]
  117. // CHECK:STDOUT: %T: type = symbolic_binding T, 0 [symbolic]
  118. // CHECK:STDOUT: %pattern_type.98f: type = pattern_type type [concrete]
  119. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  120. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  121. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  122. // CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
  123. // CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
  124. // CHECK:STDOUT: %Z.lookup_impl_witness: <witness> = lookup_impl_witness %T, @Z [symbolic]
  125. // CHECK:STDOUT: %Z.facet: %Z.type = facet_value %T, (%Z.lookup_impl_witness) [symbolic]
  126. // CHECK:STDOUT: %F.specific_fn: <specific function> = specific_function %F, @F(%Z.facet) [symbolic]
  127. // CHECK:STDOUT: }
  128. // CHECK:STDOUT:
  129. // CHECK:STDOUT: file {
  130. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [concrete = constants.%G] {
  131. // CHECK:STDOUT: %T.patt: %pattern_type.98f = symbolic_binding_pattern T, 0 [concrete]
  132. // CHECK:STDOUT: } {
  133. // CHECK:STDOUT: <elided>
  134. // CHECK:STDOUT: %T.loc13_6.2: type = symbolic_binding T, 0 [symbolic = %T.loc13_6.1 (constants.%T)]
  135. // CHECK:STDOUT: }
  136. // CHECK:STDOUT: }
  137. // CHECK:STDOUT:
  138. // CHECK:STDOUT: generic fn @G(%T.loc13_6.2: type) {
  139. // CHECK:STDOUT: %T.loc13_6.1: type = symbolic_binding T, 0 [symbolic = %T.loc13_6.1 (constants.%T)]
  140. // CHECK:STDOUT:
  141. // CHECK:STDOUT: !definition:
  142. // CHECK:STDOUT: %Z.lookup_impl_witness: <witness> = lookup_impl_witness %T.loc13_6.1, @Z [symbolic = %Z.lookup_impl_witness (constants.%Z.lookup_impl_witness)]
  143. // CHECK:STDOUT: %Z.facet.loc19_6.2: %Z.type = facet_value %T.loc13_6.1, (%Z.lookup_impl_witness) [symbolic = %Z.facet.loc19_6.2 (constants.%Z.facet)]
  144. // CHECK:STDOUT: %F.specific_fn.loc19_3.2: <specific function> = specific_function constants.%F, @F(%Z.facet.loc19_6.2) [symbolic = %F.specific_fn.loc19_3.2 (constants.%F.specific_fn)]
  145. // CHECK:STDOUT:
  146. // CHECK:STDOUT: fn() {
  147. // CHECK:STDOUT: !entry:
  148. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
  149. // CHECK:STDOUT: %T.ref: type = name_ref T, %T.loc13_6.2 [symbolic = %T.loc13_6.1 (constants.%T)]
  150. // CHECK:STDOUT: %Z.facet.loc19_6.1: %Z.type = facet_value %T.ref, (constants.%Z.lookup_impl_witness) [symbolic = %Z.facet.loc19_6.2 (constants.%Z.facet)]
  151. // CHECK:STDOUT: %.loc19: %Z.type = converted %T.ref, %Z.facet.loc19_6.1 [symbolic = %Z.facet.loc19_6.2 (constants.%Z.facet)]
  152. // CHECK:STDOUT: %F.specific_fn.loc19_3.1: <specific function> = specific_function %F.ref, @F(constants.%Z.facet) [symbolic = %F.specific_fn.loc19_3.2 (constants.%F.specific_fn)]
  153. // CHECK:STDOUT: %F.call: init %empty_tuple.type = call %F.specific_fn.loc19_3.1()
  154. // CHECK:STDOUT: return
  155. // CHECK:STDOUT: }
  156. // CHECK:STDOUT: }
  157. // CHECK:STDOUT:
  158. // CHECK:STDOUT: specific @G(constants.%T) {
  159. // CHECK:STDOUT: %T.loc13_6.1 => constants.%T
  160. // CHECK:STDOUT: }
  161. // CHECK:STDOUT:
  162. // CHECK:STDOUT: --- fail_nonfinal_lookup_impl_witness_error.carbon
  163. // CHECK:STDOUT:
  164. // CHECK:STDOUT: constants {
  165. // CHECK:STDOUT: %Z.type: type = facet_type <@Z> [concrete]
  166. // CHECK:STDOUT: %T: type = symbolic_binding T, 0 [symbolic]
  167. // CHECK:STDOUT: %pattern_type.98f: type = pattern_type type [concrete]
  168. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  169. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  170. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  171. // CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
  172. // CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
  173. // CHECK:STDOUT: %Z.lookup_impl_witness: <witness> = lookup_impl_witness %T, @Z [symbolic]
  174. // CHECK:STDOUT: %Z.facet: %Z.type = facet_value %T, (%Z.lookup_impl_witness) [symbolic]
  175. // CHECK:STDOUT: %F.specific_fn: <specific function> = specific_function %F, @F(%Z.facet) [symbolic]
  176. // CHECK:STDOUT: }
  177. // CHECK:STDOUT:
  178. // CHECK:STDOUT: file {
  179. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [concrete = constants.%G] {
  180. // CHECK:STDOUT: %T.patt: %pattern_type.98f = symbolic_binding_pattern T, 0 [concrete]
  181. // CHECK:STDOUT: } {
  182. // CHECK:STDOUT: <elided>
  183. // CHECK:STDOUT: %T.loc13_6.2: type = symbolic_binding T, 0 [symbolic = %T.loc13_6.1 (constants.%T)]
  184. // CHECK:STDOUT: }
  185. // CHECK:STDOUT: }
  186. // CHECK:STDOUT:
  187. // CHECK:STDOUT: generic fn @G(%T.loc13_6.2: type) {
  188. // CHECK:STDOUT: %T.loc13_6.1: type = symbolic_binding T, 0 [symbolic = %T.loc13_6.1 (constants.%T)]
  189. // CHECK:STDOUT:
  190. // CHECK:STDOUT: !definition:
  191. // CHECK:STDOUT: %Z.lookup_impl_witness: <witness> = lookup_impl_witness %T.loc13_6.1, @Z [symbolic = %Z.lookup_impl_witness (constants.%Z.lookup_impl_witness)]
  192. // CHECK:STDOUT: %Z.facet.loc19_6.2: %Z.type = facet_value %T.loc13_6.1, (%Z.lookup_impl_witness) [symbolic = %Z.facet.loc19_6.2 (constants.%Z.facet)]
  193. // CHECK:STDOUT: %F.specific_fn.loc19_3.2: <specific function> = specific_function constants.%F, @F(%Z.facet.loc19_6.2) [symbolic = %F.specific_fn.loc19_3.2 (constants.%F.specific_fn)]
  194. // CHECK:STDOUT:
  195. // CHECK:STDOUT: fn() {
  196. // CHECK:STDOUT: !entry:
  197. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
  198. // CHECK:STDOUT: %T.ref: type = name_ref T, %T.loc13_6.2 [symbolic = %T.loc13_6.1 (constants.%T)]
  199. // CHECK:STDOUT: %Z.facet.loc19_6.1: %Z.type = facet_value %T.ref, (constants.%Z.lookup_impl_witness) [symbolic = %Z.facet.loc19_6.2 (constants.%Z.facet)]
  200. // CHECK:STDOUT: %.loc19: %Z.type = converted %T.ref, %Z.facet.loc19_6.1 [symbolic = %Z.facet.loc19_6.2 (constants.%Z.facet)]
  201. // CHECK:STDOUT: %F.specific_fn.loc19_3.1: <specific function> = specific_function %F.ref, @F(constants.%Z.facet) [symbolic = %F.specific_fn.loc19_3.2 (constants.%F.specific_fn)]
  202. // CHECK:STDOUT: %F.call: init %empty_tuple.type = call %F.specific_fn.loc19_3.1()
  203. // CHECK:STDOUT: return
  204. // CHECK:STDOUT: }
  205. // CHECK:STDOUT: }
  206. // CHECK:STDOUT:
  207. // CHECK:STDOUT: specific @G(constants.%T) {
  208. // CHECK:STDOUT: %T.loc13_6.1 => constants.%T
  209. // CHECK:STDOUT: }
  210. // CHECK:STDOUT:
  211. // CHECK:STDOUT: specific @G(constants.%empty_tuple.type) {
  212. // CHECK:STDOUT: %T.loc13_6.1 => constants.%empty_tuple.type
  213. // CHECK:STDOUT:
  214. // CHECK:STDOUT: !definition:
  215. // CHECK:STDOUT: %Z.lookup_impl_witness => <error>
  216. // CHECK:STDOUT: %Z.facet.loc19_6.2 => <error>
  217. // CHECK:STDOUT: %F.specific_fn.loc19_3.2 => <error>
  218. // CHECK:STDOUT: }
  219. // CHECK:STDOUT:
  220. // CHECK:STDOUT: --- fail_final_lookup_impl_witness_error.carbon
  221. // CHECK:STDOUT:
  222. // CHECK:STDOUT: constants {
  223. // CHECK:STDOUT: %Z.type: type = facet_type <@Z> [concrete]
  224. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  225. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  226. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  227. // CHECK:STDOUT: %Z.impl_witness.b07: <witness> = impl_witness file.%Z.impl_witness_table, @T.as.Z.impl(%empty_tuple.type) [concrete]
  228. // CHECK:STDOUT: %Z.facet: %Z.type = facet_value %empty_tuple.type, (%Z.impl_witness.b07) [concrete]
  229. // CHECK:STDOUT: %F.specific_fn: <specific function> = specific_function %F, @F(%Z.facet) [concrete]
  230. // CHECK:STDOUT: }
  231. // CHECK:STDOUT:
  232. // CHECK:STDOUT: fn @G() {
  233. // CHECK:STDOUT: !entry:
  234. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
  235. // CHECK:STDOUT: %.loc18_6: %empty_tuple.type = tuple_literal ()
  236. // CHECK:STDOUT: %Z.facet: %Z.type = facet_value constants.%empty_tuple.type, (constants.%Z.impl_witness.b07) [concrete = constants.%Z.facet]
  237. // CHECK:STDOUT: %.loc18_7: %Z.type = converted %.loc18_6, %Z.facet [concrete = constants.%Z.facet]
  238. // CHECK:STDOUT: %F.specific_fn: <specific function> = specific_function %F.ref, @F(constants.%Z.facet) [concrete = constants.%F.specific_fn]
  239. // CHECK:STDOUT: %F.call: init %empty_tuple.type = call %F.specific_fn()
  240. // CHECK:STDOUT: <elided>
  241. // CHECK:STDOUT: }
  242. // CHECK:STDOUT: