Repository: carbon-language/carbon-lang
File: toolchain/sem_ir/stringify.cpp (C++)

Code before:
    *out_ << "<type of ";
    step_stack_->Push(StepStack::QualifiedNameItem{interface.parent_scope_id,
                                                   interface.name_id},
                      ">");
  }

  // Determine the specific interface that an impl witness instruction provides
  // an implementation of.
  // TODO: Should we track this in the type?
  auto TryGetSpecificInterfaceForImplWitness(InstId impl_witness_id)
      -> std::optional<SpecificInterface> {
    if (auto lookup =
            sem_ir_->insts().TryGetAs<LookupImplWitness>(impl_witness_id)) {
      return sem_ir_->specific_interfaces().Get(
          lookup->query_specific_interface_id);
    }

    // TODO: Handle ImplWitness.
    return std::nullopt;
  }

  auto StringifyInst(InstId /*inst_id*/, ImplWitnessAccess inst) -> void {
    auto witness_inst_id =
        sem_ir_->constant_values().GetConstantInstId(inst.witness_id);
    auto specific_interface =
        TryGetSpecificInterfaceForImplWitness(witness_inst_id);
    const auto& interface =
        sem_ir_->interfaces().Get(specific_interface->interface_id);
    if (!interface.associated_entities_id.has_value()) {
      step_stack_->Push(".(TODO: element ", inst.index, " in incomplete ",
                        witness_inst_id, ")");
    } else {
      auto entities =
          sem_ir_->inst_blocks().Get(interface.associated_entities_id);
      size_t index = inst.index.index;
      CARBON_CHECK(index < entities.size(), "Access out of bounds.");
      auto entity_inst_id = entities[index];
      step_stack_->PushString(")");
      if (auto associated_const =
              sem_ir_->insts().TryGetAs<AssociatedConstantDecl>(
                  entity_inst_id)) {
        step_stack_->PushNameId(sem_ir_->associated_constants()
                                    .Get(associated_const->assoc_const_id)
                                    .name_id);
      } else if (auto function_decl =
                     sem_ir_->insts().TryGetAs<FunctionDecl>(entity_inst_id)) {
        const auto& function =
            sem_ir_->functions().Get(function_decl->function_id);
        step_stack_->PushNameId(function.name_id);
      } else {
        step_stack_->PushInstId(entity_inst_id);

Review comment (suggestion):
Cool, then we can get rid of the whole optional:

```suggestion
    auto lookup = sem_ir_->insts().GetAs<LookupImplWitness>(witness_inst_id);
    auto specific_interface = sem_ir_->specific_interfaces().Get(
          lookup.query_specific_interface_id);
```

And `TryGetSpecificInterfaceForImplWitness` can be removed.

Code after:
    *out_ << "<type of ";
    step_stack_->Push(StepStack::QualifiedNameItem{interface.parent_scope_id,
                                                   interface.name_id},
                      ">");
  }

  // Determine the specific interface that an impl witness instruction provides
  // an implementation of.
  // TODO: Should we track this in the type?
  auto TryGetSpecificInterfaceForImplWitness(InstId impl_witness_id)
      -> std::optional<SpecificInterface> {
    if (auto lookup =
            sem_ir_->insts().TryGetAs<LookupImplWitness>(impl_witness_id)) {
      return sem_ir_->specific_interfaces().Get(
          lookup->query_specific_interface_id);
    }

    // TODO: Handle ImplWitness.
    return std::nullopt;
  }

  auto StringifyInst(InstId /*inst_id*/, ImplWitnessAccess inst) -> void {
    auto witness_inst_id =
        sem_ir_->constant_values().GetConstantInstId(inst.witness_id);
    auto lookup = sem_ir_->insts().GetAs<LookupImplWitness>(witness_inst_id);
    auto specific_interface =
        sem_ir_->specific_interfaces().Get(lookup.query_specific_interface_id);
    const auto& interface =
        sem_ir_->interfaces().Get(specific_interface.interface_id);
    if (!interface.associated_entities_id.has_value()) {
      step_stack_->Push(".(TODO: element ", inst.index, " in incomplete ",
                        witness_inst_id, ")");
    } else {
      auto entities =
          sem_ir_->inst_blocks().Get(interface.associated_entities_id);
      size_t index = inst.index.index;
      CARBON_CHECK(index < entities.size(), "Access out of bounds.");
      auto entity_inst_id = entities[index];
      step_stack_->PushString(")");
      if (auto associated_const =
              sem_ir_->insts().TryGetAs<AssociatedConstantDecl>(
                  entity_inst_id)) {
        step_stack_->PushNameId(sem_ir_->associated_constants()
                                    .Get(associated_const->assoc_const_id)
                                    .name_id);
      } else if (auto function_decl =
                     sem_ir_->insts().TryGetAs<FunctionDecl>(entity_inst_id)) {
        const auto& function =
            sem_ir_->functions().Get(function_decl->function_id);
        step_stack_->PushNameId(function.name_id);
      } else {