impl.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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.has_value()) {
  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::None,
  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. if (!CheckFunctionTypeMatches(
  118. context, context.functions().Get(impl_function_decl->function_id),
  119. context.functions().Get(interface_function_type.function_id),
  120. interface_function_specific_id,
  121. /*check_syntax=*/false)) {
  122. return SemIR::ErrorInst::SingletonInstId;
  123. }
  124. return impl_decl_id;
  125. }
  126. // Builds an initial empty witness.
  127. auto ImplWitnessForDeclaration(Context& context, const SemIR::Impl& impl)
  128. -> SemIR::InstId {
  129. CARBON_CHECK(!impl.has_definition_started());
  130. auto facet_type_id = context.GetTypeIdForTypeInst(impl.constraint_id);
  131. if (facet_type_id == SemIR::ErrorInst::SingletonTypeId) {
  132. return SemIR::ErrorInst::SingletonInstId;
  133. }
  134. auto facet_type = context.types().TryGetAs<SemIR::FacetType>(facet_type_id);
  135. if (!facet_type) {
  136. CARBON_DIAGNOSTIC(ImplAsNonFacetType, Error, "impl as non-facet type {0}",
  137. InstIdAsType);
  138. context.emitter().Emit(impl.latest_decl_id(), ImplAsNonFacetType,
  139. impl.constraint_id);
  140. return SemIR::ErrorInst::SingletonInstId;
  141. }
  142. const SemIR::FacetTypeInfo& facet_type_info =
  143. context.facet_types().Get(facet_type->facet_type_id);
  144. auto interface_type = facet_type_info.TryAsSingleInterface();
  145. if (!interface_type) {
  146. context.TODO(impl.latest_decl_id(), "impl as not 1 interface");
  147. return SemIR::ErrorInst::SingletonInstId;
  148. }
  149. const auto& interface =
  150. context.interfaces().Get(interface_type->interface_id);
  151. // TODO: This should be done as part of facet type resolution.
  152. if (!context.RequireDefinedType(
  153. facet_type_id, context.insts().GetLocId(impl.latest_decl_id()), [&] {
  154. CARBON_DIAGNOSTIC(ImplOfUndefinedInterface, Error,
  155. "implementation of undefined interface {0}",
  156. SemIR::NameId);
  157. return context.emitter().Build(impl.latest_decl_id(),
  158. ImplOfUndefinedInterface,
  159. interface.name_id);
  160. })) {
  161. return SemIR::ErrorInst::SingletonInstId;
  162. }
  163. auto assoc_entities =
  164. context.inst_blocks().Get(interface.associated_entities_id);
  165. for (auto decl_id : assoc_entities) {
  166. LoadImportRef(context, decl_id);
  167. }
  168. llvm::SmallVector<SemIR::InstId> table(assoc_entities.size(),
  169. SemIR::InstId::None);
  170. auto table_id = context.inst_blocks().Add(table);
  171. return context.AddInst<SemIR::ImplWitness>(
  172. context.insts().GetLocId(impl.latest_decl_id()),
  173. {.type_id = context.GetSingletonType(SemIR::WitnessType::SingletonInstId),
  174. .elements_id = table_id,
  175. .specific_id = context.generics().GetSelfSpecific(impl.generic_id)});
  176. }
  177. // Returns `true` if the `FacetAccessWitness` of `witness_id` matches
  178. // `interface`.
  179. static auto WitnessAccessMatchesInterface(
  180. Context& context, SemIR::InstId witness_id,
  181. SemIR::FacetTypeInfo::ImplsConstraint interface) -> bool {
  182. auto access = context.insts().GetAs<SemIR::FacetAccessWitness>(witness_id);
  183. auto type_id = context.insts().Get(access.facet_value_inst_id).type_id();
  184. auto facet_type = context.types().GetAs<SemIR::FacetType>(type_id);
  185. const auto& facet_info = context.facet_types().Get(facet_type.facet_type_id);
  186. if (auto impls = facet_info.TryAsSingleInterface()) {
  187. return *impls == interface;
  188. }
  189. return false;
  190. }
  191. auto AddConstantsToImplWitnessFromConstraint(Context& context,
  192. const SemIR::Impl& impl,
  193. SemIR::InstId witness_id,
  194. SemIR::ImplId prev_decl_id)
  195. -> void {
  196. CARBON_CHECK(!impl.has_definition_started());
  197. CARBON_CHECK(witness_id.has_value());
  198. if (witness_id == SemIR::ErrorInst::SingletonInstId) {
  199. return;
  200. }
  201. auto facet_type_id = context.GetTypeIdForTypeInst(impl.constraint_id);
  202. CARBON_CHECK(facet_type_id != SemIR::ErrorInst::SingletonTypeId);
  203. auto facet_type = context.types().GetAs<SemIR::FacetType>(facet_type_id);
  204. const SemIR::FacetTypeInfo& facet_type_info =
  205. context.facet_types().Get(facet_type.facet_type_id);
  206. auto interface_type = facet_type_info.TryAsSingleInterface();
  207. CARBON_CHECK(interface_type.has_value());
  208. const auto& interface =
  209. context.interfaces().Get(interface_type->interface_id);
  210. auto witness = context.insts().GetAs<SemIR::ImplWitness>(witness_id);
  211. auto witness_block = context.inst_blocks().GetMutable(witness.elements_id);
  212. auto assoc_entities =
  213. context.inst_blocks().Get(interface.associated_entities_id);
  214. CARBON_CHECK(witness_block.size() == assoc_entities.size());
  215. // Scan through rewrites, produce map from element index to constant value.
  216. // TODO: Perhaps move this into facet type resolution?
  217. llvm::SmallVector<SemIR::ConstantId> rewrite_values(assoc_entities.size(),
  218. SemIR::ConstantId::None);
  219. for (auto rewrite : facet_type_info.rewrite_constraints) {
  220. auto inst_id = context.constant_values().GetInstId(rewrite.lhs_const_id);
  221. auto access = context.insts().GetAs<SemIR::ImplWitnessAccess>(inst_id);
  222. if (!WitnessAccessMatchesInterface(context, access.witness_id,
  223. *interface_type)) {
  224. // Skip rewrite constraints that apply to associated constants of
  225. // a different interface than the one being implemented.
  226. continue;
  227. }
  228. CARBON_CHECK(access.index.index >= 0);
  229. CARBON_CHECK(access.index.index <
  230. static_cast<int32_t>(rewrite_values.size()));
  231. auto& rewrite_value = rewrite_values[access.index.index];
  232. if (rewrite_value.has_value() &&
  233. rewrite_value != SemIR::ErrorInst::SingletonConstantId) {
  234. if (rewrite_value != rewrite.rhs_const_id &&
  235. rewrite.rhs_const_id != SemIR::ErrorInst::SingletonConstantId) {
  236. // TODO: Do at least this checking as part of facet type resolution
  237. // instead.
  238. // TODO: Figure out how to print the two different values
  239. // `rewrite_value` & `rewrite.rhs_const_id` in the diagnostic message.
  240. CARBON_DIAGNOSTIC(AssociatedConstantWithDifferentValues, Error,
  241. "associated constant {0} given two different values",
  242. SemIR::NameId);
  243. auto decl_id = assoc_entities[access.index.index];
  244. decl_id = context.constant_values().GetInstId(
  245. SemIR::GetConstantValueInSpecific(
  246. context.sem_ir(), interface_type->specific_id, decl_id));
  247. CARBON_CHECK(decl_id.has_value(), "Non-constant associated entity");
  248. auto decl =
  249. context.insts().GetAs<SemIR::AssociatedConstantDecl>(decl_id);
  250. context.emitter().Emit(impl.constraint_id,
  251. AssociatedConstantWithDifferentValues,
  252. decl.name_id);
  253. }
  254. } else {
  255. rewrite_value = rewrite.rhs_const_id;
  256. }
  257. }
  258. // For each non-function associated constant, update witness entry.
  259. for (auto index : llvm::seq(assoc_entities.size())) {
  260. auto decl_id = assoc_entities[index];
  261. decl_id =
  262. context.constant_values().GetInstId(SemIR::GetConstantValueInSpecific(
  263. context.sem_ir(), interface_type->specific_id, decl_id));
  264. CARBON_CHECK(decl_id.has_value(), "Non-constant associated entity");
  265. if (auto decl =
  266. context.insts().TryGetAs<SemIR::AssociatedConstantDecl>(decl_id)) {
  267. auto& witness_value = witness_block[index];
  268. auto rewrite_value = rewrite_values[index];
  269. if (witness_value.has_value() &&
  270. witness_value != SemIR::ErrorInst::SingletonInstId) {
  271. // TODO: Support just using the witness values if the redeclaration uses
  272. // `where _`, per proposal #1084.
  273. if (!rewrite_value.has_value()) {
  274. CARBON_DIAGNOSTIC(AssociatedConstantMissingInRedecl, Error,
  275. "associated constant {0} given value in "
  276. "declaration but not redeclaration",
  277. SemIR::NameId);
  278. auto builder = context.emitter().Build(
  279. impl.latest_decl_id(), AssociatedConstantMissingInRedecl,
  280. decl->name_id);
  281. NotePreviousDecl(context, builder, prev_decl_id);
  282. builder.Emit();
  283. continue;
  284. }
  285. auto witness_const_id = context.constant_values().Get(witness_value);
  286. if (witness_const_id != rewrite_value &&
  287. rewrite_value != SemIR::ErrorInst::SingletonConstantId) {
  288. // TODO: Figure out how to print the two different values
  289. CARBON_DIAGNOSTIC(
  290. AssociatedConstantDifferentInRedecl, Error,
  291. "redeclaration with different value for associated constant {0}",
  292. SemIR::NameId);
  293. auto builder = context.emitter().Build(
  294. impl.latest_decl_id(), AssociatedConstantDifferentInRedecl,
  295. decl->name_id);
  296. NotePreviousDecl(context, builder, prev_decl_id);
  297. builder.Emit();
  298. continue;
  299. }
  300. } else if (rewrite_value.has_value()) {
  301. witness_value = context.constant_values().GetInstId(rewrite_value);
  302. }
  303. }
  304. }
  305. }
  306. auto ImplWitnessStartDefinition(Context& context, SemIR::Impl& impl) -> void {
  307. CARBON_CHECK(impl.is_being_defined());
  308. CARBON_CHECK(impl.witness_id.has_value());
  309. if (impl.witness_id == SemIR::ErrorInst::SingletonInstId) {
  310. return;
  311. }
  312. auto facet_type_id = context.GetTypeIdForTypeInst(impl.constraint_id);
  313. CARBON_CHECK(facet_type_id != SemIR::ErrorInst::SingletonTypeId);
  314. auto facet_type = context.types().GetAs<SemIR::FacetType>(facet_type_id);
  315. const SemIR::FacetTypeInfo& facet_type_info =
  316. context.facet_types().Get(facet_type.facet_type_id);
  317. auto interface_type = facet_type_info.TryAsSingleInterface();
  318. CARBON_CHECK(interface_type.has_value());
  319. const auto& interface =
  320. context.interfaces().Get(interface_type->interface_id);
  321. auto witness = context.insts().GetAs<SemIR::ImplWitness>(impl.witness_id);
  322. auto witness_block = context.inst_blocks().GetMutable(witness.elements_id);
  323. auto assoc_entities =
  324. context.inst_blocks().Get(interface.associated_entities_id);
  325. CARBON_CHECK(witness_block.size() == assoc_entities.size());
  326. // Check we have a value for all non-function associated constants in the
  327. // witness.
  328. for (auto index : llvm::seq(assoc_entities.size())) {
  329. auto decl_id = assoc_entities[index];
  330. decl_id =
  331. context.constant_values().GetInstId(SemIR::GetConstantValueInSpecific(
  332. context.sem_ir(), interface_type->specific_id, decl_id));
  333. CARBON_CHECK(decl_id.has_value(), "Non-constant associated entity");
  334. if (auto decl =
  335. context.insts().TryGetAs<SemIR::AssociatedConstantDecl>(decl_id)) {
  336. auto& witness_value = witness_block[index];
  337. if (!witness_value.has_value()) {
  338. CARBON_DIAGNOSTIC(ImplAssociatedConstantNeedsValue, Error,
  339. "associated constant {0} not given a value in impl "
  340. "of interface {1}",
  341. SemIR::NameId, SemIR::NameId);
  342. CARBON_DIAGNOSTIC(AssociatedConstantHere, Note,
  343. "associated constant {0} declared here",
  344. SemIR::NameId);
  345. context.emitter()
  346. .Build(impl.constraint_id, ImplAssociatedConstantNeedsValue,
  347. decl->name_id, interface.name_id)
  348. .Note(assoc_entities[index], AssociatedConstantHere, decl->name_id)
  349. .Emit();
  350. witness_value = SemIR::ErrorInst::SingletonInstId;
  351. }
  352. }
  353. }
  354. }
  355. // Adds functions to the witness that the specified impl implements the given
  356. // interface.
  357. auto FinishImplWitness(Context& context, SemIR::Impl& impl) -> void {
  358. CARBON_CHECK(impl.is_being_defined());
  359. CARBON_CHECK(impl.witness_id.has_value());
  360. if (impl.witness_id == SemIR::ErrorInst::SingletonInstId) {
  361. return;
  362. }
  363. auto facet_type_id = context.GetTypeIdForTypeInst(impl.constraint_id);
  364. CARBON_CHECK(facet_type_id != SemIR::ErrorInst::SingletonTypeId);
  365. auto facet_type = context.types().GetAs<SemIR::FacetType>(facet_type_id);
  366. const SemIR::FacetTypeInfo& facet_type_info =
  367. context.facet_types().Get(facet_type.facet_type_id);
  368. auto interface_type = facet_type_info.TryAsSingleInterface();
  369. CARBON_CHECK(interface_type.has_value());
  370. const auto& interface =
  371. context.interfaces().Get(interface_type->interface_id);
  372. auto witness = context.insts().GetAs<SemIR::ImplWitness>(impl.witness_id);
  373. auto witness_block = context.inst_blocks().GetMutable(witness.elements_id);
  374. auto& impl_scope = context.name_scopes().Get(impl.scope_id);
  375. auto self_type_id = context.GetTypeIdForTypeInst(impl.self_id);
  376. auto assoc_entities =
  377. context.inst_blocks().Get(interface.associated_entities_id);
  378. llvm::SmallVector<SemIR::InstId> used_decl_ids;
  379. for (auto index : llvm::seq(assoc_entities.size())) {
  380. auto decl_id = assoc_entities[index];
  381. decl_id =
  382. context.constant_values().GetInstId(SemIR::GetConstantValueInSpecific(
  383. context.sem_ir(), interface_type->specific_id, decl_id));
  384. CARBON_CHECK(decl_id.has_value(), "Non-constant associated entity");
  385. auto decl = context.insts().Get(decl_id);
  386. CARBON_KIND_SWITCH(decl) {
  387. case CARBON_KIND(SemIR::StructValue struct_value): {
  388. if (struct_value.type_id == SemIR::ErrorInst::SingletonTypeId) {
  389. witness_block[index] = SemIR::ErrorInst::SingletonInstId;
  390. break;
  391. }
  392. auto type_inst = context.types().GetAsInst(struct_value.type_id);
  393. auto fn_type = type_inst.TryAs<SemIR::FunctionType>();
  394. if (!fn_type) {
  395. CARBON_FATAL("Unexpected type: {0}", type_inst);
  396. }
  397. auto& fn = context.functions().Get(fn_type->function_id);
  398. auto [impl_decl_id, _, is_poisoned] = context.LookupNameInExactScope(
  399. decl_id, fn.name_id, impl.scope_id, impl_scope);
  400. if (impl_decl_id.has_value()) {
  401. used_decl_ids.push_back(impl_decl_id);
  402. witness_block[index] = CheckAssociatedFunctionImplementation(
  403. context, *fn_type, impl_decl_id, self_type_id, impl.witness_id);
  404. } else {
  405. CARBON_DIAGNOSTIC(
  406. ImplMissingFunction, Error,
  407. "missing implementation of {0} in impl of interface {1}",
  408. SemIR::NameId, SemIR::NameId);
  409. auto builder =
  410. context.emitter().Build(impl.definition_id, ImplMissingFunction,
  411. fn.name_id, interface.name_id);
  412. NoteAssociatedFunction(context, builder, fn_type->function_id);
  413. builder.Emit();
  414. witness_block[index] = SemIR::ErrorInst::SingletonInstId;
  415. }
  416. break;
  417. }
  418. case SemIR::AssociatedConstantDecl::Kind: {
  419. // These are set to their final values already.
  420. break;
  421. }
  422. default:
  423. CARBON_CHECK(decl_id == SemIR::ErrorInst::SingletonInstId,
  424. "Unexpected kind of associated entity {0}", decl);
  425. witness_block[index] = SemIR::ErrorInst::SingletonInstId;
  426. break;
  427. }
  428. }
  429. // TODO: Diagnose if any declarations in the impl are not in used_decl_ids.
  430. }
  431. auto FillImplWitnessWithErrors(Context& context, SemIR::Impl& impl) -> void {
  432. if (impl.witness_id.has_value() &&
  433. impl.witness_id != SemIR::ErrorInst::SingletonInstId) {
  434. auto witness = context.insts().GetAs<SemIR::ImplWitness>(impl.witness_id);
  435. auto witness_block = context.inst_blocks().GetMutable(witness.elements_id);
  436. for (auto& elem : witness_block) {
  437. if (!elem.has_value()) {
  438. elem = SemIR::ErrorInst::SingletonInstId;
  439. }
  440. }
  441. }
  442. }
  443. } // namespace Carbon::Check