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

Code before:

  auto class_const_inst = resolver.local_insts().Get(
      resolver.local_constant_values().GetInstId(class_const_id));

  // TODO: Ensure the vtable is only imported once, in eg: if there's distinct
  // vtable constants (imported from multiple libraries using the vtable) that
  // refer to the same vtable, the vtable should still be singular.
  auto virtual_functions =
      resolver.import_inst_blocks().Get(import_vtable.virtual_functions_id);

  llvm::SmallVector<SemIR::InstId> lazy_virtual_functions;
  lazy_virtual_functions.reserve(virtual_functions.size());
  for (auto vtable_entry_id : virtual_functions) {
    auto local_attached_constant_id = GetLocalConstantId(
        resolver,
        resolver.import_constant_values().GetAttached(vtable_entry_id));
    lazy_virtual_functions.push_back(
        resolver.local_constant_values().GetInstIdIfValid(
            local_attached_constant_id));
  }

  if (resolver.HasNewWork()) {
    return ResolveResult::Retry();
  }

  for (auto p : llvm::zip(virtual_functions, lazy_virtual_functions)) {
    // Use LoadedImportRef for imported symbolic constant vtable entries so they
    // can carry attached constants necessary for applying specifics to these
    // constants when they are used.
    auto local_attached_constant_id =
        resolver.local_constant_values().Get(std::get<1>(p));
    if (local_attached_constant_id.is_symbolic()) {
      std::get<1>(p) = AddLoadedImportRef(
          resolver,
          GetSingletonType(resolver.local_context(),
                           SemIR::SpecificFunctionType::TypeInstId),
          std::get<0>(p), local_attached_constant_id);
    }
  }

  auto class_id = SemIR::ClassId::None;
  if (class_const_inst.Is<SemIR::ClassType>()) {
    class_id = class_const_inst.As<SemIR::ClassType>().class_id;
  } else {
    auto generic_class_type =
        resolver.local_types().GetAs<SemIR::GenericClassType>(
            class_const_inst.type_id());
    class_id = generic_class_type.class_id;
  }

  auto new_vtable_id = resolver.local_vtables().Add(

Review comment (suggestion):
Can you use a structured binding here instead of using `get<i>` below? I think that'd be more readable.

Code after:

  auto class_const_inst = resolver.local_insts().Get(
      resolver.local_constant_values().GetInstId(class_const_id));

  // TODO: Ensure the vtable is only imported once, in eg: if there's distinct
  // vtable constants (imported from multiple libraries using the vtable) that
  // refer to the same vtable, the vtable should still be singular.
  auto virtual_functions =
      resolver.import_inst_blocks().Get(import_vtable.virtual_functions_id);

  llvm::SmallVector<SemIR::InstId> lazy_virtual_functions;
  lazy_virtual_functions.reserve(virtual_functions.size());
  for (auto vtable_entry_id : virtual_functions) {
    auto local_attached_constant_id = GetLocalConstantId(
        resolver,
        resolver.import_constant_values().GetAttached(vtable_entry_id));
    lazy_virtual_functions.push_back(
        resolver.local_constant_values().GetInstIdIfValid(
            local_attached_constant_id));
  }

  if (resolver.HasNewWork()) {
    return ResolveResult::Retry();
  }

  for (auto [import_vtable_entry_inst_id, local_vtable_entry_inst_id] :
       llvm::zip(virtual_functions, lazy_virtual_functions)) {
    // Use LoadedImportRef for imported symbolic constant vtable entries so they
    // can carry attached constants necessary for applying specifics to these
    // constants when they are used.
    auto local_attached_constant_id =
        resolver.local_constant_values().GetAttached(
            local_vtable_entry_inst_id);
    if (local_attached_constant_id.is_symbolic()) {
      local_vtable_entry_inst_id = AddLoadedImportRef(
          resolver,
          GetSingletonType(resolver.local_context(),
                           SemIR::SpecificFunctionType::TypeInstId),
          import_vtable_entry_inst_id, local_attached_constant_id);
    }
  }

  auto class_id = SemIR::ClassId::None;
  if (class_const_inst.Is<SemIR::ClassType>()) {
    class_id = class_const_inst.As<SemIR::ClassType>().class_id;
  } else {
    auto generic_class_type =
        resolver.local_types().GetAs<SemIR::GenericClassType>(
            class_const_inst.type_id());
    class_id = generic_class_type.class_id;
  }