error_recovery.carbon 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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(unused 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(unused 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(unused 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. // --- fail_invalid_fn_syntax_in_impl.carbon
  92. library "[[@TEST_NAME]]";
  93. interface I {
  94. fn Op[self: Self]();
  95. }
  96. class C {};
  97. //@dump-sem-ir-begin
  98. impl C as I {
  99. // This leaves the impl with a placeholder instruction in the witness table.
  100. // CHECK:STDERR: fail_invalid_fn_syntax_in_impl.carbon:[[@LINE+12]]:9: error: name `x` not found [NameNotFound]
  101. // CHECK:STDERR: fn Op[x self: C]() {}
  102. // CHECK:STDERR: ^
  103. // CHECK:STDERR:
  104. // CHECK:STDERR: fail_invalid_fn_syntax_in_impl.carbon:[[@LINE+8]]:11: error: expected `,` or `]` [UnexpectedTokenAfterListElement]
  105. // CHECK:STDERR: fn Op[x self: C]() {}
  106. // CHECK:STDERR: ^~~~
  107. // CHECK:STDERR:
  108. // CHECK:STDERR: fail_invalid_fn_syntax_in_impl.carbon:[[@LINE+4]]:8: error: semantics TODO: `handle invalid parse trees in `check`` [SemanticsTodo]
  109. // CHECK:STDERR: fn Op[x self: C]() {}
  110. // CHECK:STDERR: ^~~~~~~~~~~
  111. // CHECK:STDERR:
  112. fn Op[x self: C]() {}
  113. }
  114. //@dump-sem-ir-end
  115. // CHECK:STDOUT: --- fail_runtime_generic_param.carbon
  116. // CHECK:STDOUT:
  117. // CHECK:STDOUT: constants {
  118. // CHECK:STDOUT: %C: type = class_type @C [concrete]
  119. // CHECK:STDOUT: %I.type: type = facet_type <@I> [concrete]
  120. // CHECK:STDOUT: %I.impl_witness: <witness> = impl_witness @C.as.I.impl.%I.impl_witness_table [concrete]
  121. // CHECK:STDOUT: }
  122. // CHECK:STDOUT:
  123. // CHECK:STDOUT: file {
  124. // CHECK:STDOUT: impl_decl @C.as.I.impl [concrete] {} {
  125. // CHECK:STDOUT: %C.ref: type = name_ref C, file.%C.decl [concrete = constants.%C]
  126. // CHECK:STDOUT: %I.ref: type = name_ref I, file.%I.decl [concrete = constants.%I.type]
  127. // CHECK:STDOUT: }
  128. // CHECK:STDOUT: }
  129. // CHECK:STDOUT:
  130. // CHECK:STDOUT: impl @C.as.I.impl: %C.ref as %I.ref {
  131. // CHECK:STDOUT: %I.impl_witness_table = impl_witness_table (), @C.as.I.impl [concrete]
  132. // CHECK:STDOUT: %I.impl_witness: <witness> = impl_witness %I.impl_witness_table [concrete = constants.%I.impl_witness]
  133. // CHECK:STDOUT:
  134. // CHECK:STDOUT: !members:
  135. // CHECK:STDOUT: witness = %I.impl_witness
  136. // CHECK:STDOUT: }
  137. // CHECK:STDOUT:
  138. // CHECK:STDOUT: --- fail_nonfinal_lookup_impl_witness_error_in_import.carbon
  139. // CHECK:STDOUT:
  140. // CHECK:STDOUT: constants {
  141. // CHECK:STDOUT: %Z.type: type = facet_type <@Z> [concrete]
  142. // CHECK:STDOUT: %pattern_type.98f: type = pattern_type type [concrete]
  143. // CHECK:STDOUT: %T: type = symbolic_binding T, 0 [symbolic]
  144. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  145. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  146. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  147. // CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
  148. // CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
  149. // CHECK:STDOUT: %Z.lookup_impl_witness: <witness> = lookup_impl_witness %T, @Z [symbolic]
  150. // CHECK:STDOUT: %.242: require_specific_def_type = require_specific_def @T.as.Z.impl(%T) [symbolic]
  151. // CHECK:STDOUT: %Z.facet: %Z.type = facet_value %T, (%Z.lookup_impl_witness) [symbolic]
  152. // CHECK:STDOUT: %F.specific_fn: <specific function> = specific_function %F, @F(%Z.facet) [symbolic]
  153. // CHECK:STDOUT: }
  154. // CHECK:STDOUT:
  155. // CHECK:STDOUT: file {
  156. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [concrete = constants.%G] {
  157. // CHECK:STDOUT: %T.patt: %pattern_type.98f = symbolic_binding_pattern T, 0 [concrete]
  158. // CHECK:STDOUT: } {
  159. // CHECK:STDOUT: %.loc13_10.1: type = splice_block %.loc13_10.2 [concrete = type] {
  160. // CHECK:STDOUT: <elided>
  161. // CHECK:STDOUT: %.loc13_10.2: type = type_literal type [concrete = type]
  162. // CHECK:STDOUT: }
  163. // CHECK:STDOUT: %T.loc13_7.2: type = symbolic_binding T, 0 [symbolic = %T.loc13_7.1 (constants.%T)]
  164. // CHECK:STDOUT: }
  165. // CHECK:STDOUT: }
  166. // CHECK:STDOUT:
  167. // CHECK:STDOUT: generic fn @G(%T.loc13_7.2: type) {
  168. // CHECK:STDOUT: %T.loc13_7.1: type = symbolic_binding T, 0 [symbolic = %T.loc13_7.1 (constants.%T)]
  169. // CHECK:STDOUT:
  170. // CHECK:STDOUT: !definition:
  171. // CHECK:STDOUT: %.loc19_6.2: require_specific_def_type = require_specific_def @T.as.Z.impl(%T.loc13_7.1) [symbolic = %.loc19_6.2 (constants.%.242)]
  172. // CHECK:STDOUT: %Z.lookup_impl_witness: <witness> = lookup_impl_witness %T.loc13_7.1, @Z [symbolic = %Z.lookup_impl_witness (constants.%Z.lookup_impl_witness)]
  173. // CHECK:STDOUT: %Z.facet.loc19_6.2: %Z.type = facet_value %T.loc13_7.1, (%Z.lookup_impl_witness) [symbolic = %Z.facet.loc19_6.2 (constants.%Z.facet)]
  174. // 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)]
  175. // CHECK:STDOUT:
  176. // CHECK:STDOUT: fn() {
  177. // CHECK:STDOUT: !entry:
  178. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
  179. // CHECK:STDOUT: %T.ref: type = name_ref T, %T.loc13_7.2 [symbolic = %T.loc13_7.1 (constants.%T)]
  180. // 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)]
  181. // CHECK:STDOUT: %.loc19_6.1: %Z.type = converted %T.ref, %Z.facet.loc19_6.1 [symbolic = %Z.facet.loc19_6.2 (constants.%Z.facet)]
  182. // 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)]
  183. // CHECK:STDOUT: %F.call: init %empty_tuple.type = call %F.specific_fn.loc19_3.1()
  184. // CHECK:STDOUT: return
  185. // CHECK:STDOUT: }
  186. // CHECK:STDOUT: }
  187. // CHECK:STDOUT:
  188. // CHECK:STDOUT: specific @G(constants.%T) {
  189. // CHECK:STDOUT: %T.loc13_7.1 => constants.%T
  190. // CHECK:STDOUT: }
  191. // CHECK:STDOUT:
  192. // CHECK:STDOUT: --- fail_nonfinal_lookup_impl_witness_error.carbon
  193. // CHECK:STDOUT:
  194. // CHECK:STDOUT: constants {
  195. // CHECK:STDOUT: %Z.type: type = facet_type <@Z> [concrete]
  196. // CHECK:STDOUT: %pattern_type.98f: type = pattern_type type [concrete]
  197. // CHECK:STDOUT: %T: type = symbolic_binding T, 0 [symbolic]
  198. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  199. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  200. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  201. // CHECK:STDOUT: %G.type: type = fn_type @G [concrete]
  202. // CHECK:STDOUT: %G: %G.type = struct_value () [concrete]
  203. // CHECK:STDOUT: %Z.lookup_impl_witness: <witness> = lookup_impl_witness %T, @Z [symbolic]
  204. // CHECK:STDOUT: %.242: require_specific_def_type = require_specific_def @T.as.Z.impl(%T) [symbolic]
  205. // CHECK:STDOUT: %Z.facet: %Z.type = facet_value %T, (%Z.lookup_impl_witness) [symbolic]
  206. // CHECK:STDOUT: %F.specific_fn: <specific function> = specific_function %F, @F(%Z.facet) [symbolic]
  207. // CHECK:STDOUT: %.194: require_specific_def_type = require_specific_def @T.as.Z.impl(%empty_tuple.type) [concrete]
  208. // CHECK:STDOUT: }
  209. // CHECK:STDOUT:
  210. // CHECK:STDOUT: file {
  211. // CHECK:STDOUT: %G.decl: %G.type = fn_decl @G [concrete = constants.%G] {
  212. // CHECK:STDOUT: %T.patt: %pattern_type.98f = symbolic_binding_pattern T, 0 [concrete]
  213. // CHECK:STDOUT: } {
  214. // CHECK:STDOUT: %.loc13_10.1: type = splice_block %.loc13_10.2 [concrete = type] {
  215. // CHECK:STDOUT: <elided>
  216. // CHECK:STDOUT: %.loc13_10.2: type = type_literal type [concrete = type]
  217. // CHECK:STDOUT: }
  218. // CHECK:STDOUT: %T.loc13_7.2: type = symbolic_binding T, 0 [symbolic = %T.loc13_7.1 (constants.%T)]
  219. // CHECK:STDOUT: }
  220. // CHECK:STDOUT: }
  221. // CHECK:STDOUT:
  222. // CHECK:STDOUT: generic fn @G(%T.loc13_7.2: type) {
  223. // CHECK:STDOUT: %T.loc13_7.1: type = symbolic_binding T, 0 [symbolic = %T.loc13_7.1 (constants.%T)]
  224. // CHECK:STDOUT:
  225. // CHECK:STDOUT: !definition:
  226. // CHECK:STDOUT: %.loc19_6.2: require_specific_def_type = require_specific_def @T.as.Z.impl(%T.loc13_7.1) [symbolic = %.loc19_6.2 (constants.%.242)]
  227. // CHECK:STDOUT: %Z.lookup_impl_witness: <witness> = lookup_impl_witness %T.loc13_7.1, @Z [symbolic = %Z.lookup_impl_witness (constants.%Z.lookup_impl_witness)]
  228. // CHECK:STDOUT: %Z.facet.loc19_6.2: %Z.type = facet_value %T.loc13_7.1, (%Z.lookup_impl_witness) [symbolic = %Z.facet.loc19_6.2 (constants.%Z.facet)]
  229. // 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)]
  230. // CHECK:STDOUT:
  231. // CHECK:STDOUT: fn() {
  232. // CHECK:STDOUT: !entry:
  233. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
  234. // CHECK:STDOUT: %T.ref: type = name_ref T, %T.loc13_7.2 [symbolic = %T.loc13_7.1 (constants.%T)]
  235. // 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)]
  236. // CHECK:STDOUT: %.loc19_6.1: %Z.type = converted %T.ref, %Z.facet.loc19_6.1 [symbolic = %Z.facet.loc19_6.2 (constants.%Z.facet)]
  237. // 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)]
  238. // CHECK:STDOUT: %F.call: init %empty_tuple.type = call %F.specific_fn.loc19_3.1()
  239. // CHECK:STDOUT: return
  240. // CHECK:STDOUT: }
  241. // CHECK:STDOUT: }
  242. // CHECK:STDOUT:
  243. // CHECK:STDOUT: specific @G(constants.%T) {
  244. // CHECK:STDOUT: %T.loc13_7.1 => constants.%T
  245. // CHECK:STDOUT: }
  246. // CHECK:STDOUT:
  247. // CHECK:STDOUT: specific @G(constants.%empty_tuple.type) {
  248. // CHECK:STDOUT: %T.loc13_7.1 => constants.%empty_tuple.type
  249. // CHECK:STDOUT:
  250. // CHECK:STDOUT: !definition:
  251. // CHECK:STDOUT: %.loc19_6.2 => constants.%.194
  252. // CHECK:STDOUT: %Z.lookup_impl_witness => <error>
  253. // CHECK:STDOUT: %Z.facet.loc19_6.2 => <error>
  254. // CHECK:STDOUT: %F.specific_fn.loc19_3.2 => <error>
  255. // CHECK:STDOUT: }
  256. // CHECK:STDOUT:
  257. // CHECK:STDOUT: --- fail_final_lookup_impl_witness_error.carbon
  258. // CHECK:STDOUT:
  259. // CHECK:STDOUT: constants {
  260. // CHECK:STDOUT: %Z.type: type = facet_type <@Z> [concrete]
  261. // CHECK:STDOUT: %F.type: type = fn_type @F [concrete]
  262. // CHECK:STDOUT: %empty_tuple.type: type = tuple_type () [concrete]
  263. // CHECK:STDOUT: %F: %F.type = struct_value () [concrete]
  264. // CHECK:STDOUT: %empty_tuple: %empty_tuple.type = tuple_value () [concrete]
  265. // CHECK:STDOUT: %Z.impl_witness.db1: <witness> = impl_witness @T.as.Z.impl.%Z.impl_witness_table, @T.as.Z.impl(%empty_tuple.type) [concrete]
  266. // CHECK:STDOUT: %Z.facet: %Z.type = facet_value %empty_tuple.type, (%Z.impl_witness.db1) [concrete]
  267. // CHECK:STDOUT: %F.specific_fn: <specific function> = specific_function %F, @F(%Z.facet) [concrete]
  268. // CHECK:STDOUT: }
  269. // CHECK:STDOUT:
  270. // CHECK:STDOUT: fn @G() {
  271. // CHECK:STDOUT: !entry:
  272. // CHECK:STDOUT: %F.ref: %F.type = name_ref F, file.%F.decl [concrete = constants.%F]
  273. // CHECK:STDOUT: %.loc18_6: %empty_tuple.type = tuple_literal () [concrete = constants.%empty_tuple]
  274. // CHECK:STDOUT: %Z.facet: %Z.type = facet_value constants.%empty_tuple.type, (constants.%Z.impl_witness.db1) [concrete = constants.%Z.facet]
  275. // CHECK:STDOUT: %.loc18_7: %Z.type = converted %.loc18_6, %Z.facet [concrete = constants.%Z.facet]
  276. // CHECK:STDOUT: %F.specific_fn: <specific function> = specific_function %F.ref, @F(constants.%Z.facet) [concrete = constants.%F.specific_fn]
  277. // CHECK:STDOUT: %F.call: init %empty_tuple.type = call %F.specific_fn()
  278. // CHECK:STDOUT: <elided>
  279. // CHECK:STDOUT: }
  280. // CHECK:STDOUT:
  281. // CHECK:STDOUT: --- fail_invalid_fn_syntax_in_impl.carbon
  282. // CHECK:STDOUT:
  283. // CHECK:STDOUT: impl @<null name>: <unexpected>.inst{{[0-9A-F]+}}.loc9_6 as <unexpected>.inst{{[0-9A-F]+}}.loc9_11;
  284. // CHECK:STDOUT: