handle_impl.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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/context.h"
  5. #include "toolchain/check/convert.h"
  6. #include "toolchain/check/decl_name_stack.h"
  7. #include "toolchain/check/generic.h"
  8. #include "toolchain/check/handle.h"
  9. #include "toolchain/check/impl.h"
  10. #include "toolchain/check/inst.h"
  11. #include "toolchain/check/merge.h"
  12. #include "toolchain/check/modifiers.h"
  13. #include "toolchain/check/name_lookup.h"
  14. #include "toolchain/check/pattern_match.h"
  15. #include "toolchain/check/type.h"
  16. #include "toolchain/check/type_completion.h"
  17. #include "toolchain/parse/typed_nodes.h"
  18. #include "toolchain/sem_ir/generic.h"
  19. #include "toolchain/sem_ir/ids.h"
  20. #include "toolchain/sem_ir/typed_insts.h"
  21. namespace Carbon::Check {
  22. auto HandleParseNode(Context& context, Parse::ImplIntroducerId node_id)
  23. -> bool {
  24. // Create an instruction block to hold the instructions created for the type
  25. // and interface.
  26. context.inst_block_stack().Push();
  27. // Push the bracketing node.
  28. context.node_stack().Push(node_id);
  29. // Optional modifiers follow.
  30. context.decl_introducer_state_stack().Push<Lex::TokenKind::Impl>();
  31. // An impl doesn't have a name per se, but it makes the processing more
  32. // consistent to imagine that it does. This also gives us a scope for implicit
  33. // parameters.
  34. context.decl_name_stack().PushScopeAndStartName();
  35. // This might be a generic impl.
  36. StartGenericDecl(context);
  37. return true;
  38. }
  39. auto HandleParseNode(Context& context, Parse::ForallId /*node_id*/) -> bool {
  40. // Push a pattern block for the signature of the `forall`.
  41. context.pattern_block_stack().Push();
  42. context.full_pattern_stack().PushFullPattern(
  43. FullPatternStack::Kind::ImplicitParamList);
  44. return true;
  45. }
  46. auto HandleParseNode(Context& context, Parse::TypeImplAsId node_id) -> bool {
  47. auto [self_node, self_id] = context.node_stack().PopExprWithNodeId();
  48. self_id = ExprAsType(context, self_node, self_id).inst_id;
  49. context.node_stack().Push(node_id, self_id);
  50. // Introduce `Self`. Note that we add this name lexically rather than adding
  51. // to the `NameScopeId` of the `impl`, because this happens before we enter
  52. // the `impl` scope or even identify which `impl` we're declaring.
  53. // TODO: Revisit this once #3714 is resolved.
  54. AddNameToLookup(context, SemIR::NameId::SelfType, self_id);
  55. return true;
  56. }
  57. // If the specified name scope corresponds to a class, returns the corresponding
  58. // class declaration.
  59. // TODO: Should this be somewhere more central?
  60. static auto TryAsClassScope(Context& context, SemIR::NameScopeId scope_id)
  61. -> std::optional<SemIR::ClassDecl> {
  62. if (!scope_id.has_value()) {
  63. return std::nullopt;
  64. }
  65. auto& scope = context.name_scopes().Get(scope_id);
  66. if (!scope.inst_id().has_value()) {
  67. return std::nullopt;
  68. }
  69. return context.insts().TryGetAs<SemIR::ClassDecl>(scope.inst_id());
  70. }
  71. static auto GetDefaultSelfType(Context& context) -> SemIR::TypeId {
  72. auto parent_scope_id = context.decl_name_stack().PeekParentScopeId();
  73. if (auto class_decl = TryAsClassScope(context, parent_scope_id)) {
  74. return context.classes().Get(class_decl->class_id).self_type_id;
  75. }
  76. // TODO: This is also valid in a mixin.
  77. return SemIR::TypeId::None;
  78. }
  79. auto HandleParseNode(Context& context, Parse::DefaultSelfImplAsId node_id)
  80. -> bool {
  81. auto self_type_id = GetDefaultSelfType(context);
  82. if (!self_type_id.has_value()) {
  83. CARBON_DIAGNOSTIC(ImplAsOutsideClass, Error,
  84. "`impl as` can only be used in a class");
  85. context.emitter().Emit(node_id, ImplAsOutsideClass);
  86. self_type_id = SemIR::ErrorInst::SingletonTypeId;
  87. }
  88. // Build the implicit access to the enclosing `Self`.
  89. // TODO: Consider calling `HandleNameAsExpr` to build this implicit `Self`
  90. // expression. We've already done the work to check that the enclosing context
  91. // is a class and found its `Self`, so additionally performing an unqualified
  92. // name lookup would be redundant work, but would avoid duplicating the
  93. // handling of the `Self` expression.
  94. auto self_inst_id = AddInst(
  95. context, node_id,
  96. SemIR::NameRef{.type_id = SemIR::TypeType::SingletonTypeId,
  97. .name_id = SemIR::NameId::SelfType,
  98. .value_id = context.types().GetInstId(self_type_id)});
  99. // There's no need to push `Self` into scope here, because we can find it in
  100. // the parent class scope.
  101. context.node_stack().Push(node_id, self_inst_id);
  102. return true;
  103. }
  104. static auto DiagnoseExtendImplOutsideClass(Context& context,
  105. Parse::AnyImplDeclId node_id)
  106. -> void {
  107. CARBON_DIAGNOSTIC(ExtendImplOutsideClass, Error,
  108. "`extend impl` can only be used in a class");
  109. context.emitter().Emit(node_id, ExtendImplOutsideClass);
  110. }
  111. // Process an `extend impl` declaration by extending the impl scope with the
  112. // `impl`'s scope.
  113. static auto ExtendImpl(Context& context, Parse::NodeId extend_node,
  114. Parse::AnyImplDeclId node_id, SemIR::ImplId impl_id,
  115. Parse::NodeId self_type_node, SemIR::TypeId self_type_id,
  116. Parse::NodeId params_node,
  117. SemIR::InstId constraint_inst_id,
  118. SemIR::TypeId constraint_id) -> bool {
  119. auto parent_scope_id = context.decl_name_stack().PeekParentScopeId();
  120. if (!parent_scope_id.has_value()) {
  121. DiagnoseExtendImplOutsideClass(context, node_id);
  122. return false;
  123. }
  124. // TODO: This is also valid in a mixin.
  125. if (!TryAsClassScope(context, parent_scope_id)) {
  126. DiagnoseExtendImplOutsideClass(context, node_id);
  127. return false;
  128. }
  129. auto& parent_scope = context.name_scopes().Get(parent_scope_id);
  130. if (params_node.has_value()) {
  131. CARBON_DIAGNOSTIC(ExtendImplForall, Error,
  132. "cannot `extend` a parameterized `impl`");
  133. context.emitter().Emit(extend_node, ExtendImplForall);
  134. parent_scope.set_has_error();
  135. return false;
  136. }
  137. if (context.parse_tree().node_kind(self_type_node) ==
  138. Parse::NodeKind::TypeImplAs) {
  139. CARBON_DIAGNOSTIC(ExtendImplSelfAs, Error,
  140. "cannot `extend` an `impl` with an explicit self type");
  141. auto diag = context.emitter().Build(extend_node, ExtendImplSelfAs);
  142. // If the explicit self type is not the default, just bail out.
  143. if (self_type_id != GetDefaultSelfType(context)) {
  144. diag.Emit();
  145. parent_scope.set_has_error();
  146. return false;
  147. }
  148. // The explicit self type is the same as the default self type, so suggest
  149. // removing it and recover as if it were not present.
  150. if (auto self_as =
  151. context.parse_tree_and_subtrees().ExtractAs<Parse::TypeImplAs>(
  152. self_type_node)) {
  153. CARBON_DIAGNOSTIC(ExtendImplSelfAsDefault, Note,
  154. "remove the explicit `Self` type here");
  155. diag.Note(self_as->type_expr, ExtendImplSelfAsDefault);
  156. }
  157. diag.Emit();
  158. }
  159. if (!context.types().Is<SemIR::FacetType>(constraint_id)) {
  160. context.TODO(node_id, "extending non-facet-type constraint");
  161. parent_scope.set_has_error();
  162. return false;
  163. }
  164. const auto& impl = context.impls().Get(impl_id);
  165. if (impl.witness_id == SemIR::ErrorInst::SingletonInstId) {
  166. parent_scope.set_has_error();
  167. }
  168. parent_scope.AddExtendedScope(constraint_inst_id);
  169. return true;
  170. }
  171. // Pops the parameters of an `impl`, forming a `NameComponent` with no
  172. // associated name that describes them.
  173. static auto PopImplIntroducerAndParamsAsNameComponent(
  174. Context& context, Parse::AnyImplDeclId end_of_decl_node_id)
  175. -> NameComponent {
  176. auto [implicit_params_loc_id, implicit_param_patterns_id] =
  177. context.node_stack()
  178. .PopWithNodeIdIf<Parse::NodeKind::ImplicitParamList>();
  179. if (implicit_param_patterns_id) {
  180. context.node_stack()
  181. .PopAndDiscardSoloNodeId<Parse::NodeKind::ImplicitParamListStart>();
  182. // Emit the `forall` match. This shouldn't produce any valid `Call` params,
  183. // because `impl`s are never actually called at runtime.
  184. auto call_params_id =
  185. CalleePatternMatch(context, *implicit_param_patterns_id,
  186. SemIR::InstBlockId::None, SemIR::InstId::None);
  187. CARBON_CHECK(call_params_id == SemIR::InstBlockId::Empty ||
  188. llvm::all_of(context.inst_blocks().Get(call_params_id),
  189. [](SemIR::InstId inst_id) {
  190. return inst_id ==
  191. SemIR::ErrorInst::SingletonInstId;
  192. }));
  193. }
  194. Parse::NodeId first_param_node_id =
  195. context.node_stack().PopForSoloNodeId<Parse::NodeKind::ImplIntroducer>();
  196. // Subtracting 1 since we don't want to include the final `{` or `;` of the
  197. // declaration when performing syntactic match.
  198. Parse::Tree::PostorderIterator last_param_iter(end_of_decl_node_id);
  199. --last_param_iter;
  200. auto pattern_block_id = SemIR::InstBlockId::None;
  201. if (implicit_param_patterns_id) {
  202. pattern_block_id = context.pattern_block_stack().Pop();
  203. context.full_pattern_stack().PopFullPattern();
  204. }
  205. return {.name_loc_id = Parse::NodeId::None,
  206. .name_id = SemIR::NameId::None,
  207. .first_param_node_id = first_param_node_id,
  208. .last_param_node_id = *last_param_iter,
  209. .implicit_params_loc_id = implicit_params_loc_id,
  210. .implicit_param_patterns_id =
  211. implicit_param_patterns_id.value_or(SemIR::InstBlockId::None),
  212. .params_loc_id = Parse::NodeId::None,
  213. .param_patterns_id = SemIR::InstBlockId::None,
  214. .call_params_id = SemIR::InstBlockId::None,
  215. .return_slot_pattern_id = SemIR::InstId::None,
  216. .pattern_block_id = pattern_block_id};
  217. }
  218. static auto MergeImplRedecl(Context& context, SemIR::Impl& new_impl,
  219. SemIR::ImplId prev_impl_id) -> bool {
  220. auto& prev_impl = context.impls().Get(prev_impl_id);
  221. // If the parameters aren't the same, then this is not a redeclaration of this
  222. // `impl`. Keep looking for a prior declaration without issuing a diagnostic.
  223. if (!CheckRedeclParamsMatch(context, DeclParams(new_impl),
  224. DeclParams(prev_impl), SemIR::SpecificId::None,
  225. /*diagnose=*/false, /*check_syntax=*/true,
  226. /*check_self=*/true)) {
  227. // NOLINTNEXTLINE(readability-simplify-boolean-expr)
  228. return false;
  229. }
  230. return true;
  231. }
  232. static auto IsValidImplRedecl(Context& context, SemIR::Impl& new_impl,
  233. SemIR::ImplId prev_impl_id) -> bool {
  234. auto& prev_impl = context.impls().Get(prev_impl_id);
  235. // TODO: Following #3763, disallow redeclarations in different scopes.
  236. // Following #4672, disallowing defining non-extern declarations in another
  237. // file.
  238. if (auto import_ref =
  239. context.insts().TryGetAs<SemIR::AnyImportRef>(prev_impl.self_id)) {
  240. // TODO: Handle extern.
  241. CARBON_DIAGNOSTIC(RedeclImportedImpl, Error,
  242. "redeclaration of imported impl");
  243. // TODO: Note imported declaration
  244. context.emitter().Emit(new_impl.latest_decl_id(), RedeclImportedImpl);
  245. return false;
  246. }
  247. if (prev_impl.has_definition_started()) {
  248. // Impls aren't merged in order to avoid generic region lookup into a
  249. // mismatching table.
  250. CARBON_DIAGNOSTIC(ImplRedefinition, Error,
  251. "redefinition of `impl {0} as {1}`", InstIdAsRawType,
  252. InstIdAsRawType);
  253. CARBON_DIAGNOSTIC(ImplPreviousDefinition, Note,
  254. "previous definition was here");
  255. context.emitter()
  256. .Build(new_impl.latest_decl_id(), ImplRedefinition, new_impl.self_id,
  257. new_impl.constraint_id)
  258. .Note(prev_impl.definition_id, ImplPreviousDefinition)
  259. .Emit();
  260. return false;
  261. }
  262. // TODO: Only allow redeclaration in a match_first/impl_priority block.
  263. return true;
  264. }
  265. // Checks that the constraint specified for the impl is valid and complete.
  266. // Returns a pointer to the interface that the impl implements. On error,
  267. // issues a diagnostic and returns nullptr.
  268. static auto CheckConstraintIsInterface(Context& context,
  269. const SemIR::Impl& impl)
  270. -> const SemIR::CompleteFacetType::RequiredInterface* {
  271. auto facet_type_id =
  272. context.types().GetTypeIdForTypeInstId(impl.constraint_id);
  273. if (facet_type_id == SemIR::ErrorInst::SingletonTypeId) {
  274. return nullptr;
  275. }
  276. auto facet_type = context.types().TryGetAs<SemIR::FacetType>(facet_type_id);
  277. if (!facet_type) {
  278. CARBON_DIAGNOSTIC(ImplAsNonFacetType, Error, "impl as non-facet type {0}",
  279. InstIdAsType);
  280. context.emitter().Emit(impl.latest_decl_id(), ImplAsNonFacetType,
  281. impl.constraint_id);
  282. return nullptr;
  283. }
  284. auto complete_id = RequireCompleteFacetType(
  285. context, facet_type_id, context.insts().GetLocId(impl.constraint_id),
  286. *facet_type, [&] {
  287. CARBON_DIAGNOSTIC(ImplAsIncompleteFacetType, Error,
  288. "impl as incomplete facet type {0}", InstIdAsType);
  289. return context.emitter().Build(impl.latest_decl_id(),
  290. ImplAsIncompleteFacetType,
  291. impl.constraint_id);
  292. });
  293. if (!complete_id.has_value()) {
  294. return nullptr;
  295. }
  296. const auto& complete = context.complete_facet_types().Get(complete_id);
  297. if (complete.num_to_impl != 1) {
  298. CARBON_DIAGNOSTIC(ImplOfNotOneInterface, Error,
  299. "impl as {0} interfaces, expected 1", int);
  300. context.emitter().Emit(impl.latest_decl_id(), ImplOfNotOneInterface,
  301. complete.num_to_impl);
  302. return nullptr;
  303. }
  304. return &complete.required_interfaces.front();
  305. }
  306. // Build an ImplDecl describing the signature of an impl. This handles the
  307. // common logic shared by impl forward declarations and impl definitions.
  308. static auto BuildImplDecl(Context& context, Parse::AnyImplDeclId node_id,
  309. bool is_definition)
  310. -> std::pair<SemIR::ImplId, SemIR::InstId> {
  311. auto [constraint_node, constraint_id] =
  312. context.node_stack().PopExprWithNodeId();
  313. auto [self_type_node, self_inst_id] =
  314. context.node_stack().PopWithNodeId<Parse::NodeCategory::ImplAs>();
  315. auto self_type_id = context.types().GetTypeIdForTypeInstId(self_inst_id);
  316. // Pop the `impl` introducer and any `forall` parameters as a "name".
  317. auto name = PopImplIntroducerAndParamsAsNameComponent(context, node_id);
  318. auto decl_block_id = context.inst_block_stack().Pop();
  319. // Convert the constraint expression to a type.
  320. auto [constraint_inst_id, constraint_type_id] =
  321. ExprAsType(context, constraint_node, constraint_id);
  322. // Process modifiers.
  323. // TODO: Should we somehow permit access specifiers on `impl`s?
  324. // TODO: Handle `final` modifier.
  325. auto introducer =
  326. context.decl_introducer_state_stack().Pop<Lex::TokenKind::Impl>();
  327. LimitModifiersOnDecl(context, introducer, KeywordModifierSet::ImplDecl);
  328. // Finish processing the name, which should be empty, but might have
  329. // parameters.
  330. auto name_context = context.decl_name_stack().FinishImplName();
  331. CARBON_CHECK(name_context.state == DeclNameStack::NameContext::State::Empty);
  332. // TODO: Check for an orphan `impl`.
  333. // Add the impl declaration.
  334. SemIR::ImplDecl impl_decl = {.impl_id = SemIR::ImplId::None,
  335. .decl_block_id = decl_block_id};
  336. auto impl_decl_id =
  337. AddPlaceholderInst(context, SemIR::LocIdAndInst(node_id, impl_decl));
  338. SemIR::Impl impl_info = {name_context.MakeEntityWithParamsBase(
  339. name, impl_decl_id,
  340. /*is_extern=*/false, SemIR::LibraryNameId::None),
  341. {.self_id = self_inst_id,
  342. .constraint_id = constraint_inst_id,
  343. .interface = SemIR::SpecificInterface::None}};
  344. const SemIR::CompleteFacetType::RequiredInterface* required_interface =
  345. CheckConstraintIsInterface(context, impl_info);
  346. if (required_interface) {
  347. impl_info.interface = *required_interface;
  348. }
  349. // Add the impl declaration.
  350. bool invalid_redeclaration = false;
  351. auto lookup_bucket_ref = context.impls().GetOrAddLookupBucket(impl_info);
  352. // TODO: Detect two impl declarations with the same self type and interface,
  353. // and issue an error if they don't match.
  354. for (auto prev_impl_id : lookup_bucket_ref) {
  355. if (MergeImplRedecl(context, impl_info, prev_impl_id)) {
  356. if (IsValidImplRedecl(context, impl_info, prev_impl_id)) {
  357. impl_decl.impl_id = prev_impl_id;
  358. } else {
  359. // IsValidImplRedecl() has issued a diagnostic, avoid generating more
  360. // diagnostics for this declaration.
  361. invalid_redeclaration = true;
  362. }
  363. break;
  364. }
  365. }
  366. // Create a new impl if this isn't a valid redeclaration.
  367. if (!impl_decl.impl_id.has_value()) {
  368. impl_info.generic_id = BuildGeneric(context, impl_decl_id);
  369. if (required_interface) {
  370. impl_info.witness_id = ImplWitnessForDeclaration(context, impl_info);
  371. } else {
  372. impl_info.witness_id = SemIR::ErrorInst::SingletonInstId;
  373. // TODO: We might also want to mark that the name scope for the impl has
  374. // an error -- at least once we start making name lookups within the impl
  375. // also look into the facet (eg, so you can name associated constants from
  376. // within the impl).
  377. }
  378. FinishGenericDecl(context, impl_decl_id, impl_info.generic_id);
  379. impl_decl.impl_id = context.impls().Add(impl_info);
  380. lookup_bucket_ref.push_back(impl_decl.impl_id);
  381. } else {
  382. const auto& first_impl = context.impls().Get(impl_decl.impl_id);
  383. FinishGenericRedecl(context, impl_decl_id, first_impl.generic_id);
  384. }
  385. // Write the impl ID into the ImplDecl.
  386. ReplaceInstBeforeConstantUse(context, impl_decl_id, impl_decl);
  387. // For an `extend impl` declaration, mark the impl as extending this `impl`.
  388. if (self_type_id != SemIR::ErrorInst::SingletonTypeId &&
  389. introducer.modifier_set.HasAnyOf(KeywordModifierSet::Extend)) {
  390. auto extend_node = introducer.modifier_node_id(ModifierOrder::Decl);
  391. if (impl_info.generic_id.has_value()) {
  392. SemIR::TypeId type_id = context.insts().Get(constraint_inst_id).type_id();
  393. constraint_inst_id = AddInst<SemIR::SpecificConstant>(
  394. context, context.insts().GetLocId(constraint_inst_id),
  395. {.type_id = type_id,
  396. .inst_id = constraint_inst_id,
  397. .specific_id =
  398. context.generics().GetSelfSpecific(impl_info.generic_id)});
  399. }
  400. if (!ExtendImpl(context, extend_node, node_id, impl_decl.impl_id,
  401. self_type_node, self_type_id, name.implicit_params_loc_id,
  402. constraint_inst_id, constraint_type_id)) {
  403. // Don't allow the invalid impl to be used.
  404. context.impls().Get(impl_decl.impl_id).witness_id =
  405. SemIR::ErrorInst::SingletonInstId;
  406. }
  407. }
  408. // Impl definitions are required in the same file as the declaration. We skip
  409. // this requirement if we've already issued an invalid redeclaration error.
  410. if (!is_definition && !invalid_redeclaration) {
  411. context.definitions_required().push_back(impl_decl_id);
  412. }
  413. return {impl_decl.impl_id, impl_decl_id};
  414. }
  415. auto HandleParseNode(Context& context, Parse::ImplDeclId node_id) -> bool {
  416. BuildImplDecl(context, node_id, /*is_definition=*/false);
  417. context.decl_name_stack().PopScope();
  418. return true;
  419. }
  420. auto HandleParseNode(Context& context, Parse::ImplDefinitionStartId node_id)
  421. -> bool {
  422. auto [impl_id, impl_decl_id] =
  423. BuildImplDecl(context, node_id, /*is_definition=*/true);
  424. auto& impl_info = context.impls().Get(impl_id);
  425. CARBON_CHECK(!impl_info.has_definition_started());
  426. impl_info.definition_id = impl_decl_id;
  427. impl_info.scope_id =
  428. context.name_scopes().Add(impl_decl_id, SemIR::NameId::None,
  429. context.decl_name_stack().PeekParentScopeId());
  430. context.scope_stack().Push(
  431. impl_decl_id, impl_info.scope_id,
  432. context.generics().GetSelfSpecific(impl_info.generic_id));
  433. StartGenericDefinition(context);
  434. ImplWitnessStartDefinition(context, impl_info);
  435. context.inst_block_stack().Push();
  436. context.node_stack().Push(node_id, impl_id);
  437. // TODO: Handle the case where there's control flow in the impl body. For
  438. // example:
  439. //
  440. // impl C as I {
  441. // fn F() -> if true then i32 else f64;
  442. // }
  443. //
  444. // We may need to track a list of instruction blocks here, as we do for a
  445. // function.
  446. impl_info.body_block_id = context.inst_block_stack().PeekOrAdd();
  447. return true;
  448. }
  449. auto HandleParseNode(Context& context, Parse::ImplDefinitionId /*node_id*/)
  450. -> bool {
  451. auto impl_id =
  452. context.node_stack().Pop<Parse::NodeKind::ImplDefinitionStart>();
  453. auto& impl_info = context.impls().Get(impl_id);
  454. CARBON_CHECK(!impl_info.is_defined());
  455. FinishImplWitness(context, impl_info);
  456. impl_info.defined = true;
  457. FinishGenericDefinition(context, impl_info.generic_id);
  458. context.inst_block_stack().Pop();
  459. // The decl_name_stack and scopes are popped by `ProcessNodeIds`.
  460. return true;
  461. }
  462. } // namespace Carbon::Check