impl.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. #include "toolchain/check/impl.h"
  5. #include "toolchain/base/kind_switch.h"
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/check/eval.h"
  8. #include "toolchain/check/function.h"
  9. #include "toolchain/check/generic.h"
  10. #include "toolchain/check/import_ref.h"
  11. #include "toolchain/diagnostics/diagnostic_emitter.h"
  12. #include "toolchain/sem_ir/generic.h"
  13. #include "toolchain/sem_ir/ids.h"
  14. #include "toolchain/sem_ir/impl.h"
  15. #include "toolchain/sem_ir/inst.h"
  16. #include "toolchain/sem_ir/typed_insts.h"
  17. namespace Carbon::Check {
  18. // Adds the location of the associated function to a diagnostic.
  19. static auto NoteAssociatedFunction(Context& context,
  20. Context::DiagnosticBuilder& builder,
  21. SemIR::FunctionId function_id) -> void {
  22. CARBON_DIAGNOSTIC(AssociatedFunctionHere, Note,
  23. "associated function {0} declared here", SemIR::NameId);
  24. const auto& function = context.functions().Get(function_id);
  25. builder.Note(function.latest_decl_id(), AssociatedFunctionHere,
  26. function.name_id);
  27. }
  28. // Adds the location of the previous declaration to a diagnostic.
  29. static auto NotePreviousDecl(Context& context,
  30. Context::DiagnosticBuilder& builder,
  31. SemIR::ImplId impl_id) -> void {
  32. CARBON_DIAGNOSTIC(ImplPreviousDeclHere, Note,
  33. "impl previously declared here");
  34. const auto& impl = context.impls().Get(impl_id);
  35. builder.Note(impl.latest_decl_id(), ImplPreviousDeclHere);
  36. }
  37. // Gets the self specific of a generic declaration that is an interface member,
  38. // given a specific for an enclosing generic, plus a type to use as `Self`.
  39. static auto GetSelfSpecificForInterfaceMemberWithSelfType(
  40. Context& context, SemIRLoc loc, SemIR::SpecificId enclosing_specific_id,
  41. SemIR::GenericId generic_id, SemIR::TypeId self_type_id,
  42. SemIR::InstId witness_inst_id) -> SemIR::SpecificId {
  43. const auto& generic = context.generics().Get(generic_id);
  44. auto bindings = context.inst_blocks().Get(generic.bindings_id);
  45. llvm::SmallVector<SemIR::InstId> arg_ids;
  46. arg_ids.reserve(bindings.size());
  47. // Start with the enclosing arguments.
  48. if (enclosing_specific_id.is_valid()) {
  49. auto enclosing_specific_args_id =
  50. context.specifics().Get(enclosing_specific_id).args_id;
  51. auto enclosing_specific_args =
  52. context.inst_blocks().Get(enclosing_specific_args_id);
  53. arg_ids.assign(enclosing_specific_args.begin(),
  54. enclosing_specific_args.end());
  55. }
  56. // Add the `Self` argument. First find the `Self` binding.
  57. auto self_binding =
  58. context.insts().GetAs<SemIR::BindSymbolicName>(bindings[arg_ids.size()]);
  59. CARBON_CHECK(
  60. context.entity_names().Get(self_binding.entity_name_id).name_id ==
  61. SemIR::NameId::SelfType,
  62. "Expected a Self binding, found {0}", self_binding);
  63. // Create a facet value to be the value of `Self` in the interface.
  64. // This facet value consists of the type `self_type_id` and a witness that the
  65. // type implements `self_binding.type_id`. Note that the witness is incomplete
  66. // since we haven't finished defining the implementation here.
  67. auto type_inst_id = context.types().GetInstId(self_type_id);
  68. auto facet_value_const_id =
  69. TryEvalInst(context, SemIR::InstId::Invalid,
  70. SemIR::FacetValue{.type_id = self_binding.type_id,
  71. .type_inst_id = type_inst_id,
  72. .witness_inst_id = witness_inst_id});
  73. arg_ids.push_back(context.constant_values().GetInstId(facet_value_const_id));
  74. // Take any trailing argument values from the self specific.
  75. // TODO: If these refer to outer arguments, for example in their types, we may
  76. // need to perform extra substitutions here.
  77. auto self_specific_args = context.inst_blocks().Get(
  78. context.specifics().Get(generic.self_specific_id).args_id);
  79. for (auto arg_id : self_specific_args.drop_front(arg_ids.size())) {
  80. arg_ids.push_back(context.constant_values().GetConstantInstId(arg_id));
  81. }
  82. auto args_id = context.inst_blocks().AddCanonical(arg_ids);
  83. return MakeSpecific(context, loc, generic_id, args_id);
  84. }
  85. // Checks that `impl_function_id` is a valid implementation of the function
  86. // described in the interface as `interface_function_id`. Returns the value to
  87. // put into the corresponding slot in the witness table, which can be
  88. // `BuiltinErrorInst` if the function is not usable.
  89. static auto CheckAssociatedFunctionImplementation(
  90. Context& context, SemIR::FunctionType interface_function_type,
  91. SemIR::InstId impl_decl_id, SemIR::TypeId self_type_id,
  92. SemIR::InstId witness_inst_id) -> SemIR::InstId {
  93. auto impl_function_decl =
  94. context.insts().TryGetAs<SemIR::FunctionDecl>(impl_decl_id);
  95. if (!impl_function_decl) {
  96. CARBON_DIAGNOSTIC(ImplFunctionWithNonFunction, Error,
  97. "associated function {0} implemented by non-function",
  98. SemIR::NameId);
  99. auto builder = context.emitter().Build(
  100. impl_decl_id, ImplFunctionWithNonFunction,
  101. context.functions().Get(interface_function_type.function_id).name_id);
  102. NoteAssociatedFunction(context, builder,
  103. interface_function_type.function_id);
  104. builder.Emit();
  105. return SemIR::ErrorInst::SingletonInstId;
  106. }
  107. // Map from the specific for the function type to the specific for the
  108. // function signature. The function signature may have additional generic
  109. // parameters.
  110. auto interface_function_specific_id =
  111. GetSelfSpecificForInterfaceMemberWithSelfType(
  112. context, impl_decl_id, interface_function_type.specific_id,
  113. context.functions()
  114. .Get(interface_function_type.function_id)
  115. .generic_id,
  116. self_type_id, witness_inst_id);
  117. // TODO: This should be a semantic check rather than a syntactic one. The
  118. // functions should be allowed to have different signatures as long as we can
  119. // synthesize a suitable thunk.
  120. if (!CheckFunctionTypeMatches(
  121. context, context.functions().Get(impl_function_decl->function_id),
  122. context.functions().Get(interface_function_type.function_id),
  123. interface_function_specific_id,
  124. /*check_syntax=*/false)) {
  125. return SemIR::ErrorInst::SingletonInstId;
  126. }
  127. return impl_decl_id;
  128. }
  129. // Builds an initial empty witness.
  130. auto ImplWitnessForDeclaration(Context& context, const SemIR::Impl& impl)
  131. -> SemIR::InstId {
  132. CARBON_CHECK(!impl.has_definition_started());
  133. auto facet_type_id = context.GetTypeIdForTypeInst(impl.constraint_id);
  134. if (facet_type_id == SemIR::ErrorInst::SingletonTypeId) {
  135. return SemIR::ErrorInst::SingletonInstId;
  136. }
  137. auto facet_type = context.types().TryGetAs<SemIR::FacetType>(facet_type_id);
  138. if (!facet_type) {
  139. CARBON_DIAGNOSTIC(ImplAsNonFacetType, Error, "impl as non-facet type {0}",
  140. InstIdAsType);
  141. context.emitter().Emit(impl.latest_decl_id(), ImplAsNonFacetType,
  142. impl.constraint_id);
  143. return SemIR::ErrorInst::SingletonInstId;
  144. }
  145. const SemIR::FacetTypeInfo& facet_type_info =
  146. context.facet_types().Get(facet_type->facet_type_id);
  147. auto interface_type = facet_type_info.TryAsSingleInterface();
  148. if (!interface_type) {
  149. context.TODO(impl.latest_decl_id(), "impl as not 1 interface");
  150. return SemIR::ErrorInst::SingletonInstId;
  151. }
  152. const auto& interface =
  153. context.interfaces().Get(interface_type->interface_id);
  154. // TODO: This should be done as part of facet type resolution.
  155. if (!context.RequireDefinedType(
  156. facet_type_id, context.insts().GetLocId(impl.latest_decl_id()), [&] {
  157. CARBON_DIAGNOSTIC(ImplOfUndefinedInterface, Error,
  158. "implementation of undefined interface {0}",
  159. SemIR::NameId);
  160. return context.emitter().Build(impl.latest_decl_id(),
  161. ImplOfUndefinedInterface,
  162. interface.name_id);
  163. })) {
  164. return SemIR::ErrorInst::SingletonInstId;
  165. }
  166. auto assoc_entities =
  167. context.inst_blocks().Get(interface.associated_entities_id);
  168. for (auto decl_id : assoc_entities) {
  169. LoadImportRef(context, decl_id);
  170. }
  171. llvm::SmallVector<SemIR::InstId> table(assoc_entities.size(),
  172. SemIR::InstId::Invalid);
  173. auto table_id = context.inst_blocks().Add(table);
  174. return context.AddInst<SemIR::ImplWitness>(
  175. context.insts().GetLocId(impl.latest_decl_id()),
  176. {.type_id = context.GetSingletonType(SemIR::WitnessType::SingletonInstId),
  177. .elements_id = table_id,
  178. .specific_id = context.generics().GetSelfSpecific(impl.generic_id)});
  179. }
  180. // Returns `true` if the `FacetAccessWitness` of `witness_id` matches
  181. // `interface`.
  182. static auto WitnessAccessMatchesInterface(
  183. Context& context, SemIR::InstId witness_id,
  184. SemIR::FacetTypeInfo::ImplsConstraint interface) -> bool {
  185. auto access = context.insts().GetAs<SemIR::FacetAccessWitness>(witness_id);
  186. auto type_id = context.insts().Get(access.facet_value_inst_id).type_id();
  187. auto facet_type = context.types().GetAs<SemIR::FacetType>(type_id);
  188. const auto& facet_info = context.facet_types().Get(facet_type.facet_type_id);
  189. if (auto impls = facet_info.TryAsSingleInterface()) {
  190. return *impls == interface;
  191. }
  192. return false;
  193. }
  194. auto AddConstantsToImplWitnessFromConstraint(Context& context,
  195. const SemIR::Impl& impl,
  196. SemIR::InstId witness_id,
  197. SemIR::ImplId prev_decl_id)
  198. -> void {
  199. CARBON_CHECK(!impl.has_definition_started());
  200. CARBON_CHECK(witness_id.is_valid());
  201. if (witness_id == SemIR::ErrorInst::SingletonInstId) {
  202. return;
  203. }
  204. auto facet_type_id = context.GetTypeIdForTypeInst(impl.constraint_id);
  205. CARBON_CHECK(facet_type_id != SemIR::ErrorInst::SingletonTypeId);
  206. auto facet_type = context.types().GetAs<SemIR::FacetType>(facet_type_id);
  207. const SemIR::FacetTypeInfo& facet_type_info =
  208. context.facet_types().Get(facet_type.facet_type_id);
  209. auto interface_type = facet_type_info.TryAsSingleInterface();
  210. CARBON_CHECK(interface_type.has_value());
  211. const auto& interface =
  212. context.interfaces().Get(interface_type->interface_id);
  213. auto witness = context.insts().GetAs<SemIR::ImplWitness>(witness_id);
  214. auto witness_block = context.inst_blocks().GetMutable(witness.elements_id);
  215. auto assoc_entities =
  216. context.inst_blocks().Get(interface.associated_entities_id);
  217. CARBON_CHECK(witness_block.size() == assoc_entities.size());
  218. // Scan through rewrites, produce map from element index to constant value.
  219. // TODO: Perhaps move this into facet type resolution?
  220. llvm::SmallVector<SemIR::ConstantId> rewrite_values(
  221. assoc_entities.size(), SemIR::ConstantId::Invalid);
  222. for (auto rewrite : facet_type_info.rewrite_constraints) {
  223. auto inst_id = context.constant_values().GetInstId(rewrite.lhs_const_id);
  224. auto access = context.insts().GetAs<SemIR::ImplWitnessAccess>(inst_id);
  225. if (!WitnessAccessMatchesInterface(context, access.witness_id,
  226. *interface_type)) {
  227. // Skip rewrite constraints that apply to associated constants of
  228. // a different interface than the one being implemented.
  229. continue;
  230. }
  231. CARBON_CHECK(access.index.index >= 0);
  232. CARBON_CHECK(access.index.index <
  233. static_cast<int32_t>(rewrite_values.size()));
  234. auto& rewrite_value = rewrite_values[access.index.index];
  235. if (rewrite_value.is_valid() &&
  236. rewrite_value != SemIR::ErrorInst::SingletonConstantId) {
  237. if (rewrite_value != rewrite.rhs_const_id &&
  238. rewrite.rhs_const_id != SemIR::ErrorInst::SingletonConstantId) {
  239. // TODO: Do at least this checking as part of facet type resolution
  240. // instead.
  241. // TODO: Figure out how to print the two different values
  242. // `rewrite_value` & `rewrite.rhs_const_id` in the diagnostic message.
  243. CARBON_DIAGNOSTIC(AssociatedConstantWithDifferentValues, Error,
  244. "associated constant {0} given two different values",
  245. SemIR::NameId);
  246. auto decl_id = assoc_entities[access.index.index];
  247. decl_id = context.constant_values().GetInstId(
  248. SemIR::GetConstantValueInSpecific(
  249. context.sem_ir(), interface_type->specific_id, decl_id));
  250. CARBON_CHECK(decl_id.is_valid(), "Non-constant associated entity");
  251. auto decl =
  252. context.insts().GetAs<SemIR::AssociatedConstantDecl>(decl_id);
  253. context.emitter().Emit(impl.constraint_id,
  254. AssociatedConstantWithDifferentValues,
  255. decl.name_id);
  256. }
  257. } else {
  258. rewrite_value = rewrite.rhs_const_id;
  259. }
  260. }
  261. // For each non-function associated constant, update witness entry.
  262. for (auto index : llvm::seq(assoc_entities.size())) {
  263. auto decl_id = assoc_entities[index];
  264. decl_id =
  265. context.constant_values().GetInstId(SemIR::GetConstantValueInSpecific(
  266. context.sem_ir(), interface_type->specific_id, decl_id));
  267. CARBON_CHECK(decl_id.is_valid(), "Non-constant associated entity");
  268. if (auto decl =
  269. context.insts().TryGetAs<SemIR::AssociatedConstantDecl>(decl_id)) {
  270. auto& witness_value = witness_block[index];
  271. auto rewrite_value = rewrite_values[index];
  272. if (witness_value.is_valid() &&
  273. witness_value != SemIR::ErrorInst::SingletonInstId) {
  274. // TODO: Support just using the witness values if the redeclaration uses
  275. // `where _`, per proposal #1084.
  276. if (!rewrite_value.is_valid()) {
  277. CARBON_DIAGNOSTIC(AssociatedConstantMissingInRedecl, Error,
  278. "associated constant {0} given value in "
  279. "declaration but not redeclaration",
  280. SemIR::NameId);
  281. auto builder = context.emitter().Build(
  282. impl.latest_decl_id(), AssociatedConstantMissingInRedecl,
  283. decl->name_id);
  284. NotePreviousDecl(context, builder, prev_decl_id);
  285. builder.Emit();
  286. continue;
  287. }
  288. auto witness_const_id = context.constant_values().Get(witness_value);
  289. if (witness_const_id != rewrite_value &&
  290. rewrite_value != SemIR::ErrorInst::SingletonConstantId) {
  291. // TODO: Figure out how to print the two different values
  292. CARBON_DIAGNOSTIC(
  293. AssociatedConstantDifferentInRedecl, Error,
  294. "redeclaration with different value for associated constant {0}",
  295. SemIR::NameId);
  296. auto builder = context.emitter().Build(
  297. impl.latest_decl_id(), AssociatedConstantDifferentInRedecl,
  298. decl->name_id);
  299. NotePreviousDecl(context, builder, prev_decl_id);
  300. builder.Emit();
  301. continue;
  302. }
  303. } else if (rewrite_value.is_valid()) {
  304. witness_value = context.constant_values().GetInstId(rewrite_value);
  305. }
  306. }
  307. }
  308. }
  309. auto ImplWitnessStartDefinition(Context& context, SemIR::Impl& impl) -> void {
  310. CARBON_CHECK(impl.is_being_defined());
  311. CARBON_CHECK(impl.witness_id.is_valid());
  312. if (impl.witness_id == SemIR::ErrorInst::SingletonInstId) {
  313. return;
  314. }
  315. auto facet_type_id = context.GetTypeIdForTypeInst(impl.constraint_id);
  316. CARBON_CHECK(facet_type_id != SemIR::ErrorInst::SingletonTypeId);
  317. auto facet_type = context.types().GetAs<SemIR::FacetType>(facet_type_id);
  318. const SemIR::FacetTypeInfo& facet_type_info =
  319. context.facet_types().Get(facet_type.facet_type_id);
  320. auto interface_type = facet_type_info.TryAsSingleInterface();
  321. CARBON_CHECK(interface_type.has_value());
  322. const auto& interface =
  323. context.interfaces().Get(interface_type->interface_id);
  324. auto witness = context.insts().GetAs<SemIR::ImplWitness>(impl.witness_id);
  325. auto witness_block = context.inst_blocks().GetMutable(witness.elements_id);
  326. auto assoc_entities =
  327. context.inst_blocks().Get(interface.associated_entities_id);
  328. CARBON_CHECK(witness_block.size() == assoc_entities.size());
  329. // Check we have a value for all non-function associated constants in the
  330. // witness.
  331. for (auto index : llvm::seq(assoc_entities.size())) {
  332. auto decl_id = assoc_entities[index];
  333. decl_id =
  334. context.constant_values().GetInstId(SemIR::GetConstantValueInSpecific(
  335. context.sem_ir(), interface_type->specific_id, decl_id));
  336. CARBON_CHECK(decl_id.is_valid(), "Non-constant associated entity");
  337. if (auto decl =
  338. context.insts().TryGetAs<SemIR::AssociatedConstantDecl>(decl_id)) {
  339. auto& witness_value = witness_block[index];
  340. if (!witness_value.is_valid()) {
  341. CARBON_DIAGNOSTIC(ImplAssociatedConstantNeedsValue, Error,
  342. "associated constant {0} not given a value in impl "
  343. "of interface {1}",
  344. SemIR::NameId, SemIR::NameId);
  345. CARBON_DIAGNOSTIC(AssociatedConstantHere, Note,
  346. "associated constant {0} declared here",
  347. SemIR::NameId);
  348. context.emitter()
  349. .Build(impl.constraint_id, ImplAssociatedConstantNeedsValue,
  350. decl->name_id, interface.name_id)
  351. .Note(assoc_entities[index], AssociatedConstantHere, decl->name_id)
  352. .Emit();
  353. witness_value = SemIR::ErrorInst::SingletonInstId;
  354. }
  355. }
  356. }
  357. }
  358. // Adds functions to the witness that the specified impl implements the given
  359. // interface.
  360. auto FinishImplWitness(Context& context, SemIR::Impl& impl) -> void {
  361. CARBON_CHECK(impl.is_being_defined());
  362. CARBON_CHECK(impl.witness_id.is_valid());
  363. if (impl.witness_id == SemIR::ErrorInst::SingletonInstId) {
  364. return;
  365. }
  366. auto facet_type_id = context.GetTypeIdForTypeInst(impl.constraint_id);
  367. CARBON_CHECK(facet_type_id != SemIR::ErrorInst::SingletonTypeId);
  368. auto facet_type = context.types().GetAs<SemIR::FacetType>(facet_type_id);
  369. const SemIR::FacetTypeInfo& facet_type_info =
  370. context.facet_types().Get(facet_type.facet_type_id);
  371. auto interface_type = facet_type_info.TryAsSingleInterface();
  372. CARBON_CHECK(interface_type.has_value());
  373. const auto& interface =
  374. context.interfaces().Get(interface_type->interface_id);
  375. auto witness = context.insts().GetAs<SemIR::ImplWitness>(impl.witness_id);
  376. auto witness_block = context.inst_blocks().GetMutable(witness.elements_id);
  377. auto& impl_scope = context.name_scopes().Get(impl.scope_id);
  378. auto self_type_id = context.GetTypeIdForTypeInst(impl.self_id);
  379. auto assoc_entities =
  380. context.inst_blocks().Get(interface.associated_entities_id);
  381. llvm::SmallVector<SemIR::InstId> used_decl_ids;
  382. for (auto index : llvm::seq(assoc_entities.size())) {
  383. auto decl_id = assoc_entities[index];
  384. decl_id =
  385. context.constant_values().GetInstId(SemIR::GetConstantValueInSpecific(
  386. context.sem_ir(), interface_type->specific_id, decl_id));
  387. CARBON_CHECK(decl_id.is_valid(), "Non-constant associated entity");
  388. auto decl = context.insts().Get(decl_id);
  389. CARBON_KIND_SWITCH(decl) {
  390. case CARBON_KIND(SemIR::StructValue struct_value): {
  391. if (struct_value.type_id == SemIR::ErrorInst::SingletonTypeId) {
  392. witness_block[index] = SemIR::ErrorInst::SingletonInstId;
  393. break;
  394. }
  395. auto type_inst = context.types().GetAsInst(struct_value.type_id);
  396. auto fn_type = type_inst.TryAs<SemIR::FunctionType>();
  397. if (!fn_type) {
  398. CARBON_FATAL("Unexpected type: {0}", type_inst);
  399. }
  400. auto& fn = context.functions().Get(fn_type->function_id);
  401. auto [impl_decl_id, _, is_poisoned] = context.LookupNameInExactScope(
  402. decl_id, fn.name_id, impl.scope_id, impl_scope);
  403. if (impl_decl_id.is_valid()) {
  404. used_decl_ids.push_back(impl_decl_id);
  405. witness_block[index] = CheckAssociatedFunctionImplementation(
  406. context, *fn_type, impl_decl_id, self_type_id, impl.witness_id);
  407. } else {
  408. CARBON_DIAGNOSTIC(
  409. ImplMissingFunction, Error,
  410. "missing implementation of {0} in impl of interface {1}",
  411. SemIR::NameId, SemIR::NameId);
  412. auto builder =
  413. context.emitter().Build(impl.definition_id, ImplMissingFunction,
  414. fn.name_id, interface.name_id);
  415. NoteAssociatedFunction(context, builder, fn_type->function_id);
  416. builder.Emit();
  417. witness_block[index] = SemIR::ErrorInst::SingletonInstId;
  418. }
  419. break;
  420. }
  421. case SemIR::AssociatedConstantDecl::Kind: {
  422. // These are set to their final values already.
  423. break;
  424. }
  425. default:
  426. CARBON_CHECK(decl_id == SemIR::ErrorInst::SingletonInstId,
  427. "Unexpected kind of associated entity {0}", decl);
  428. witness_block[index] = SemIR::ErrorInst::SingletonInstId;
  429. break;
  430. }
  431. }
  432. // TODO: Diagnose if any declarations in the impl are not in used_decl_ids.
  433. }
  434. auto FillImplWitnessWithErrors(Context& context, SemIR::Impl& impl) -> void {
  435. if (impl.witness_id.is_valid() &&
  436. impl.witness_id != SemIR::ErrorInst::SingletonInstId) {
  437. auto witness = context.insts().GetAs<SemIR::ImplWitness>(impl.witness_id);
  438. auto witness_block = context.inst_blocks().GetMutable(witness.elements_id);
  439. for (auto& elem : witness_block) {
  440. if (!elem.is_valid()) {
  441. elem = SemIR::ErrorInst::SingletonInstId;
  442. }
  443. }
  444. }
  445. }
  446. } // namespace Carbon::Check