Repository: carbon-language/carbon-lang
File: toolchain/check/handle_impl.cpp (C++)

Code before:
      // instructions which we will not use here. We should stop generating
      // those when deducing for impl lookup, but for now we discard them by
      // pushing an InstBlock on the stack and dropping it here.
      context.inst_block_stack().PopAndDiscard();
      if (!deduced_specific_id.has_value()) {
        CARBON_DIAGNOSTIC(ImplUnusedBinding, Error,
                          "`impl` with unused generic binding");
        // TODO: This location may be incorrect, the binding may be inherited
        // from an outer declaration. It would be nice to get the particular
        // binding that was undeducible back from DeduceImplArguments here and
        // use that.
        auto loc = name.implicit_params_loc_id.has_value()
                       ? name.implicit_params_loc_id
                       : node_id;
        context.emitter().Emit(loc, ImplUnusedBinding);
        // Don't try to match the impl at all, save us work and possible future
        // diagnostics.
        FillImplWitnessWithErrors(context, stored_impl_info);
        stored_impl_info.witness_id = SemIR::ErrorInst::InstId;
      }
    }
  } else {
    auto& stored_impl_info = context.impls().Get(impl_decl.impl_id);

    auto prev_decl_generic_id = stored_impl_info.generic_id;
    FinishGenericRedecl(context, prev_decl_generic_id);
  }

  // Write the impl ID into the ImplDecl.
  ReplaceInstBeforeConstantUse(context, impl_decl_id, impl_decl);

  // For an `extend impl` declaration, mark the impl as extending this `impl`.
  if (self_type_id != SemIR::ErrorInst::TypeId &&
      introducer.modifier_set.HasAnyOf(KeywordModifierSet::Extend)) {
    auto& stored_impl_info = context.impls().Get(impl_decl.impl_id);

    auto extend_node = introducer.modifier_node_id(ModifierOrder::Extend);
    if (stored_impl_info.generic_id.has_value()) {
      constraint_type_inst_id = AddTypeInst<SemIR::SpecificConstant>(
          context, context.insts().GetLocId(constraint_type_inst_id),
          {.type_id = SemIR::TypeType::TypeId,
           .inst_id = constraint_type_inst_id,
           .specific_id = context.generics().GetSelfSpecific(
               stored_impl_info.generic_id)});
    }
    if (!ExtendImpl(context, extend_node, node_id, impl_decl.impl_id,
                    self_type_node, self_type_id, name.implicit_params_loc_id,
                    constraint_type_inst_id, constraint_type_id)) {
      // Don't allow the invalid impl to be used.
      FillImplWitnessWithErrors(context, stored_impl_info);
      stored_impl_info.witness_id = SemIR::ErrorInst::InstId;

Review comment (suggestion):
Maybe use the `stored_impl_info` directly?
```suggestion
    FinishGenericRedecl(context, stored_impl_info.generic_id);
````

Code after:
          context.constant_values().Get(stored_impl_info.self_id),
          stored_impl_info.interface.specific_id);
      // TODO: Deduce has side effects in the semir by generating `Converted`
      // instructions which we will not use here. We should stop generating
      // those when deducing for impl lookup, but for now we discard them by
      // pushing an InstBlock on the stack and dropping it here.
      context.inst_block_stack().PopAndDiscard();
      if (!deduced_specific_id.has_value()) {
        CARBON_DIAGNOSTIC(ImplUnusedBinding, Error,
                          "`impl` with unused generic binding");
        // TODO: This location may be incorrect, the binding may be inherited
        // from an outer declaration. It would be nice to get the particular
        // binding that was undeducible back from DeduceImplArguments here and
        // use that.
        auto loc = name.implicit_params_loc_id.has_value()
                       ? name.implicit_params_loc_id
                       : node_id;
        context.emitter().Emit(loc, ImplUnusedBinding);
        // Don't try to match the impl at all, save us work and possible future
        // diagnostics.
        FillImplWitnessWithErrors(context, stored_impl_info);
      }
    }
  } else {
    auto& stored_impl_info = context.impls().Get(impl_decl.impl_id);
    FinishGenericRedecl(context, stored_impl_info.generic_id);
  }

  // Write the impl ID into the ImplDecl.
  ReplaceInstBeforeConstantUse(context, impl_decl_id, impl_decl);

  // For an `extend impl` declaration, mark the impl as extending this `impl`.
  if (self_type_id != SemIR::ErrorInst::TypeId &&
      introducer.modifier_set.HasAnyOf(KeywordModifierSet::Extend)) {
    auto& stored_impl_info = context.impls().Get(impl_decl.impl_id);

    auto extend_node = introducer.modifier_node_id(ModifierOrder::Extend);
    if (stored_impl_info.generic_id.has_value()) {
      constraint_type_inst_id = AddTypeInst<SemIR::SpecificConstant>(
          context, SemIR::LocId(constraint_type_inst_id),
          {.type_id = SemIR::TypeType::TypeId,
           .inst_id = constraint_type_inst_id,
           .specific_id = context.generics().GetSelfSpecific(
               stored_impl_info.generic_id)});
    }
    if (!ExtendImpl(context, extend_node, node_id, impl_decl.impl_id,
                    self_type_node, self_type_id, name.implicit_params_loc_id,
                    constraint_type_inst_id, constraint_type_id)) {
      // Don't allow the invalid impl to be used.
      FillImplWitnessWithErrors(context, stored_impl_info);
    }